WvStreams
include/uniconf.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Defines a hierarchical registry abstraction.
6 */
7#ifndef __UNICONF_H
8#define __UNICONF_H
9
10#include <vector>
11
12#include "uniconfgen.h"
13#include "uniconfkey.h"
14#include "wvtr1.h"
15
16class WvStream;
17class UniConf;
18class UniConfRoot;
19
27typedef wv::function<void(const UniConf&, const UniConfKey&)> UniConfCallback;
28
50class UniConf
51{
52 friend class UniConfRoot;
53
54protected:
55 UniConfRoot *xroot;
56 UniConfKey xfullkey;
57
65
66public:
69
71 UniConf(const UniConf &other);
72
74 virtual ~UniConf();
75
76
77 /***** Handle Manipulation API *****/
78
80 UniConf root() const
81 { return UniConf(xroot, UniConfKey::EMPTY); }
82
85 { return UniConf(xroot, xfullkey.removelast()); }
86
92 { return xroot; }
93
95 bool isnull() const
96 { return xroot == NULL; }
97
100 { return xfullkey; }
101
105
107 UniConfKey fullkey(const UniConf &cfg) const
108 { return fullkey(cfg.fullkey()); }
109
112 { return xfullkey.last(); }
113
119 const UniConf operator[] (const UniConfKey &key) const
120 { return UniConf(xroot, UniConfKey(xfullkey, key)); }
121
126 const UniConf u(const UniConfKey &key) const
127 { return (*this)[key]; }
128
131 {
132 xroot = other.xroot;
133 xfullkey = other.xfullkey;
134 return *this;
135 }
136
137
138 /***** Key Retrieval API *****/
139
141 void prefetch(bool recursive) const;
142
147 WvString getme(WvStringParm defvalue = WvString::null) const;
148
151 { return getme(); }
152
155 { return getme(); }
156
159 WvStringParm defvalue = WvString::null) const
160 { return (*this)[key].getme(defvalue); }
161
169 int getmeint(int defvalue = 0) const;
170
172 int xgetint(WvStringParm key, int defvalue = 0) const
173 { return (*this)[key].getmeint(defvalue); }
174
181 bool exists() const;
182
183
184 /***** Key Storage API *****/
185
190 void setme(WvStringParm value) const;
191
195 void setme(WVSTRING_FORMAT_DECL) const
196 { return setme(WvString(WVSTRING_FORMAT_CALL)); }
197
200 { (*this)[key].setme(value); }
201
205 void setmeint(int value) const;
206
208 void xsetint(WvStringParm key, int value) const
209 { (*this)[key].setmeint(value); }
210
211
212 /***** Key Handling API *****/
213
227 void move(const UniConf &dst) const;
228
232 void remove() const
233 { setme(WvString::null); }
234
244 void copy(const UniConf &dst, bool force) const;
245
246
247
248 /***** Key Persistence API *****/
249
255 bool refresh() const;
256
260 void commit() const;
261
262
263 /***** Generator Mounting API *****/
264
273 IUniConfGen *mount(WvStringParm moniker, bool refresh = true) const;
274
285 IUniConfGen *mountgen(IUniConfGen *gen, bool refresh = true) const;
286
288 void unmount(IUniConfGen *gen, bool commit) const;
289
291 bool ismountpoint() const;
292
294 bool isok() const;
295
306 IUniConfGen *whichmount(UniConfKey *mountpoint = NULL) const;
307
308
309 /***** Notification API *****/
310
319 void add_callback(void *cookie, const UniConfCallback &callback,
320 bool recurse = true) const;
321
325 void del_callback(void *cookie, bool recurse = true) const;
326
331 void add_setbool(bool *flag, bool recurse = true) const;
332
336 void del_setbool(bool *flag, bool recurse = true) const;
337
347
357
363
369
370
371 /***** Key Enumeration API *****/
372
377 void dump(WvStream &stream, bool everything = false) const;
378
385 bool haschildren() const;
386
387 /*** Iterators (see comments in class declaration) ***/
388
389 // internal base class for all of the key iterators
390 class IterBase;
391 // iterates over direct children
392 class Iter;
393 // iterates over all descendents in preorder traversal
394 class RecursiveIter;
395 // iterates over children matching a wildcard
396 class XIter;
397
398 // internal base class for sorted key iterators
399 class SortedIterBase;
400 // sorted variant of Iter
401 class SortedIter;
402 // sorted variant of RecursiveIter
404 // sorted variant of XIter
405 class SortedXIter;
406
407 // lists of iterators
408 DeclareWvList(Iter);
409};
410
411
416{
417protected:
418 UniConf top;
419 UniConf current;
420
421 IterBase(const UniConf &_top)
422 : top(_top)
423 { }
424
425public:
426 const UniConf *ptr() const
427 { return &current; }
428 WvIterStuff(const UniConf);
429};
430
431
435class UniConf::Iter : public UniConf::IterBase
436{
438
439public:
441 Iter(const UniConf &_top);
442
443 ~Iter()
444 { delete it; }
445
446 void rewind()
447 { it->rewind(); }
448 bool next()
449 {
450 if (!it->next())
451 return false;
452 current = top[it->key()];
453 return true;
454 }
455
456 // FIXME: this is a speed optimization only. Don't use this unless
457 // you're apenwarr. It will change.
458 WvString _value() const
459 { return it->value(); }
460};
461
462
467{
469
470public:
472 RecursiveIter(const UniConf &_top);
473
475 { delete it; }
476
477 void rewind()
478 { it->rewind(); }
479 bool next()
480 {
481 if (!it->next())
482 return false;
483 current = top[it->key()];
484 return true;
485 }
486
487 // FIXME: this is a speed optimization only. Don't use this unless
488 // you're apenwarr. It will change.
489 WvString _value() const
490 { return it->value(); }
491};
492
493
512{
513 UniConfKey pathead;
514 UniConfKey pattail;
515 UniConf::XIter *subit;
516 UniConf::Iter *it;
518 bool ready;
520public:
522 XIter(const UniConf &_top, const UniConfKey &pattern);
523 ~XIter();
524
525 void rewind();
526 bool next();
527
528private:
529 void cleanup();
530 bool qnext();
531 void enter(const UniConf &child);
532};
533
534
544{
545public:
546 typedef int (*Comparator)(const UniConf &a, const UniConf &b);
547
549 static int defcomparator(const UniConf &a, const UniConf &b);
550
551 SortedIterBase(const UniConf &_top, Comparator comparator = defcomparator);
553
554 bool next();
555
556private:
557 Comparator xcomparator;
558 int index;
559 int count;
560
561 void _purge();
562 void _rewind();
563
564protected:
565 std::vector<UniConf> xkeys;
566
567 template<class Iter>
568 void populate(Iter &i)
569 {
570 _purge();
571 for (i.rewind(); i.next(); )
572 xkeys.push_back(*i);
573 _rewind();
574 }
575};
576
577
582{
584
585public:
586 SortedIter(const UniConf &_top, Comparator comparator = defcomparator)
587 : SortedIterBase(_top, comparator), i(_top)
588 { }
589
590 void rewind()
591 { populate(i); }
592};
593
594
599{
601
602public:
603 SortedRecursiveIter(const UniConf &_top,
604 Comparator comparator = defcomparator)
605 : SortedIterBase(_top, comparator), i(_top)
606 { }
607
608 void rewind()
609 { populate(i); }
610};
611
612
617{
619
620public:
621 SortedXIter(const UniConf &_top, const UniConfKey &pattern,
622 Comparator comparator = defcomparator)
623 : SortedIterBase(_top, comparator), i(_top, pattern)
624 { }
625
626 void rewind()
627 { populate(i); }
628};
629
630#endif // __UNICONF_H
An abstract data container that backs a UniConf tree.
An abstract iterator over keys and values in a generator.
virtual bool next()=0
Seeks to the next element in the sequence.
virtual WvString value() const =0
Returns the value of the current key.
virtual void rewind()=0
Rewinds the iterator.
virtual UniConfKey key() const =0
Returns the current key.
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
UniConfKey removelast(int n=1) const
Returns the path formed by removing the last n segments of this path.
UniConfKey last(int n=1) const
Returns the path formed by the n last segments of this path.
Represents the root of a hierarhical registry consisting of pairs of UniConfKeys and associated strin...
An implementation base class for key iterators.
This iterator walks through all immediate children of a UniConf node.
Iter(const UniConf &_top)
Creates an iterator over the direct children of a branch.
This iterator performs depth-first traversal of a subtree.
RecursiveIter(const UniConf &_top)
Creates a recursive iterator over a branch.
An implementation base class for sorted key iterators.
static int defcomparator(const UniConf &a, const UniConf &b)
Default comparator.
Definition uniconf.cc:424
static int defcomparator(const UniConf &a, const UniConf &b)
Default comparator.
This iterator walks over all children that match a wildcard pattern.
XIter(const UniConf &_top, const UniConfKey &pattern)
Creates a wildcard iterator.
UniConf instances function as handles to subtrees of a UniConf tree and expose a high-level interface...
UniConf(UniConfRoot *root, const UniConfKey &fullkey=UniConfKey::EMPTY)
Creates a handle to the specified subtree of the given root.
UniConfKey key() const
Returns the path of this node relative to its parent.
void commit() const
Commits information about this key recursively.
void add_callback(void *cookie, const UniConfCallback &callback, bool recurse=true) const
Requests notification when any of the keys covered by the recursive depth specification change by inv...
void remove() const
Removes this key and all of its children from the registry.
void prefetch(bool recursive) const
See UniConfGen::prefetch().
UniConf root() const
Returns a handle to the root of the tree.
WvStringStar operator->() const
A different way to say cfg.getme().num(): use cfg->num() instead.
virtual ~UniConf()
Destroys the UniConf handle.
UniConfKey fullkey(const UniConfKey &k) const
Returns the full path of this node, starting at the given key.
void xset(WvStringParm key, WvStringParm value) const
A different way to say cfg[x].setme(y).
void clear_delta()
Clears the list of pending notifications without sending them.
const UniConf u(const UniConfKey &key) const
Return a subtree handle (see operator[]).
void move(const UniConf &dst) const
Equivalent to "mv" in a standard unix filesystem.
WvString operator*() const
A different way to say cfg.getme(): use *cfg instead.
WvString xget(WvStringParm key, WvStringParm defvalue=WvString::null) const
A different way to say cfg[x].getme(y).
int getmeint(int defvalue=0) const
Fetches the integer value for this key from the registry.
bool haschildren() const
Returns true if this key has children.
UniConf parent() const
Returns a handle to the parent of this node.
UniConf(const UniConf &other)
Copies a UniConf handle.
void setme(WvStringParm value) const
Stores a string value for this key into the registry.
bool exists() const
Without fetching its value, returns true if this key exists.
void unmount(IUniConfGen *gen, bool commit) const
Unmounts the generator providing this key and destroys it.
bool isnull() const
Returns true if the handle is invalid (NULL).
UniConfKey fullkey(const UniConf &cfg) const
Returns the full path of this node, starting at the given handle.
UniConf()
Creates a NULL UniConf handle, useful for reporting errors.
void add_setbool(bool *flag, bool recurse=true) const
Requests notification when any of the keys covered by the recursive depth specification change by set...
void del_setbool(bool *flag, bool recurse=true) const
Cancels notification requested using add_setbool().
const UniConf operator[](const UniConfKey &key) const
Returns a handle for a subtree below this key.
bool refresh() const
Refreshes information about this key recursively.
IUniConfGen * mount(WvStringParm moniker, bool refresh=true) const
Mounts a generator at this key using a moniker.
int xgetint(WvStringParm key, int defvalue=0) const
A different way to say cfg[x].getmeint(y).
IUniConfGen * whichmount(UniConfKey *mountpoint=NULL) const
Finds the generator that owns this key.
UniConfKey fullkey() const
Returns the full path of this node, starting at the root.
void flush_delta()
Flushes the list of pending notifications by sending them.
void del_callback(void *cookie, bool recurse=true) const
Cancels notification requested using add_callback().
UniConfRoot * rootobj() const
Returns a pointer to the UniConfRoot that manages this node.
void copy(const UniConf &dst, bool force) const
Equivalent to "cp -r" in a standard unix filesystem.
bool isok() const
Returns true if the generator at this key isok().
void setme(WVSTRING_FORMAT_DECL) const
Stores a string value for this key into the registry.
void dump(WvStream &stream, bool everything=false) const
Prints the entire contents of this subtree to a stream.
void unhold_delta()
Resumes notifications when each hold_delta() has been matched.
void setmeint(int value) const
Stores an integer value for this key into the registry.
void hold_delta()
Pauses notifications until matched with a call to unhold_delta().
bool ismountpoint() const
Determines if any generators are mounted at this key.
WvString getme(WvStringParm defvalue=WvString::null) const
Fetches the string value for this key from the registry.
IUniConfGen * mountgen(IUniConfGen *gen, bool refresh=true) const
Mounts a generator at this key.
void xsetint(WvStringParm key, int value) const
A different way to say cfg[x].setme(y).
UniConf & operator=(const UniConf &other)
Reassigns the target of this handle to match a different one.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
A ridiculous class needed because UniConf::operator->() needs to return a pointer,...
WvString is an implementation of a simple and efficient printable-string class.