uniconf.h

00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * Defines a hierarchical registry abstraction.
00006  */
00007 #ifndef __UNICONF_H
00008 #define __UNICONF_H
00009 
00010 #include "uniconfkey.h"
00011 #include "uniconfgen.h"
00012 #include "wvcallback.h"
00013 #include "wvvector.h"
00014 
00015 class WvStream;
00016 class UniConf;
00017 class UniConfRoot;
00018 
00026 typedef WvCallback<void, const UniConf &, const UniConfKey &> UniConfCallback;
00027 
00049 class UniConf
00050 {
00051     friend class UniConfRoot;
00052     
00053 protected:
00054     UniConfRoot *xroot;
00055     UniConfKey xfullkey;
00056 
00063     UniConf(UniConfRoot *root, const UniConfKey &fullkey = UniConfKey::EMPTY);
00064     
00065 public:
00067     UniConf();
00068     
00070     UniConf(const UniConf &other);
00071     
00073     virtual ~UniConf();
00074 
00075     
00076     /***** Handle Manipulation API *****/
00077 
00079     UniConf root() const
00080         { return UniConf(xroot, UniConfKey::EMPTY); }
00081 
00083     UniConf parent() const
00084         { return UniConf(xroot, xfullkey.removelast()); }
00085     
00090     UniConfRoot *rootobj() const
00091         { return xroot; }
00092 
00094     bool isnull() const
00095         { return xroot == NULL; }
00096 
00098     UniConfKey fullkey() const
00099         { return xfullkey; }
00100     
00103     UniConfKey fullkey(const UniConfKey &k) const;
00104     
00106     UniConfKey fullkey(const UniConf &cfg) const
00107         { return fullkey(cfg.fullkey()); }
00108 
00110     UniConfKey key() const
00111         { return xfullkey.last(); }
00112 
00118     const UniConf operator[] (const UniConfKey &key) const
00119         { return UniConf(xroot, UniConfKey(xfullkey, key)); }
00120 
00125     const UniConf u(const UniConfKey &key) const
00126         { return (*this)[key]; }
00127 
00129     UniConf &operator= (const UniConf &other)
00130     {
00131         xroot = other.xroot;
00132         xfullkey = other.xfullkey;
00133         return *this;
00134     }
00135 
00136     
00137     /***** Key Retrieval API *****/
00138     
00140     void prefetch(bool recursive) const;
00141     
00146     WvString getme(WvStringParm defvalue = WvString::null) const;
00147 
00149     WvString operator* () const
00150         { return getme(); }
00151 
00153     WvStringStar operator -> () const
00154         { return getme(); }
00155     
00157     WvString xget(WvStringParm key,
00158                   WvStringParm defvalue = WvString::null) const
00159         { return (*this)[key].getme(defvalue); }
00160 
00168     int getmeint(int defvalue = 0) const;
00169 
00171     int xgetint(WvStringParm key, int defvalue = 0) const
00172         { return (*this)[key].getmeint(defvalue); }
00173 
00180     bool exists() const;
00181 
00182 
00183     /***** Key Storage API *****/
00184 
00189     void setme(WvStringParm value) const;
00190 
00194     void setme(WVSTRING_FORMAT_DECL) const
00195         { return setme(WvString(WVSTRING_FORMAT_CALL)); }
00196 
00198     void xset(WvStringParm key, WvStringParm value) const
00199         { (*this)[key].setme(value); }
00200 
00204     void setmeint(int value) const;
00205 
00207     void xsetint(WvStringParm key, int value) const
00208         { (*this)[key].setmeint(value); }
00209 
00210 
00211     /***** Key Handling API *****/
00212 
00226     void move(const UniConf &dst) const;
00227 
00231     void remove() const
00232         { setme(WvString::null); }
00233 
00243     void copy(const UniConf &dst, bool force) const;
00244 
00245 
00246     
00247     /***** Key Persistence API *****/
00248 
00254     bool refresh() const;
00255     
00259     void commit() const;
00260 
00261     
00262     /***** Generator Mounting API *****/
00263     
00272     IUniConfGen *mount(WvStringParm moniker, bool refresh = true) const;
00273     
00284     IUniConfGen *mountgen(IUniConfGen *gen, bool refresh = true) const;
00285     
00287     void unmount(IUniConfGen *gen, bool commit) const;
00288     
00290     bool ismountpoint() const;
00291     
00293     bool isok() const;
00294     
00305     IUniConfGen *whichmount(UniConfKey *mountpoint = NULL) const;
00306 
00307     
00308     /***** Notification API *****/
00309 
00318     void add_callback(void *cookie, const UniConfCallback &callback,
00319                       bool recurse = true) const;
00320     
00324     void del_callback(void *cookie, bool recurse = true) const;
00325 
00330     void add_setbool(bool *flag, bool recurse = true) const;
00331 
00335     void del_setbool(bool *flag, bool recurse = true) const;
00336     
00345     void hold_delta();
00346 
00355     void unhold_delta();
00356     
00361     void clear_delta();
00362 
00367     void flush_delta();
00368     
00369     
00370     /***** Key Enumeration API *****/
00371     
00376     void dump(WvStream &stream, bool everything = false) const;
00377     
00384     bool haschildren() const;
00385     
00386     /*** Iterators (see comments in class declaration) ***/
00387 
00388     // internal base class for all of the key iterators
00389     class IterBase;
00390     // iterates over direct children
00391     class Iter;
00392     // iterates over all descendents in preorder traversal
00393     class RecursiveIter;
00394     // iterates over children matching a wildcard
00395     class XIter;
00396 
00397     // internal base class for sorted key iterators
00398     class SortedIterBase;
00399     // sorted variant of Iter
00400     class SortedIter;
00401     // sorted variant of RecursiveIter
00402     class SortedRecursiveIter;
00403     // sorted variant of XIter
00404     class SortedXIter;
00405 
00406     // lists of iterators
00407     DeclareWvList(Iter);
00408 };
00409 
00410 
00414 class UniConf::IterBase
00415 {
00416 protected:
00417     UniConf top;
00418     UniConf current;
00419 
00420     IterBase(const UniConf &_top)
00421         : top(_top)
00422         { }
00423 
00424 public:
00425     const UniConf *ptr() const
00426         { return &current; }
00427     WvIterStuff(const UniConf);
00428 };
00429 
00430 
00434 class UniConf::Iter : public UniConf::IterBase
00435 {
00436     UniConfGen::Iter *it;
00437     
00438 public:
00440     Iter(const UniConf &_top);
00441 
00442     ~Iter()
00443         { delete it; }
00444 
00445     void rewind()
00446         { it->rewind(); }
00447     bool next()
00448     {
00449         if (!it->next())
00450             return false;
00451         current = top[it->key()];
00452         return true;
00453     }
00454     
00455     // FIXME: this is a speed optimization only.  Don't use this unless
00456     // you're apenwarr.  It will change.
00457     WvString _value() const
00458         { return it->value(); }
00459 };
00460 
00461 
00465 class UniConf::RecursiveIter : public UniConf::IterBase
00466 {
00467     UniConfGen::Iter *it;
00468 
00469 public:
00471     RecursiveIter(const UniConf &_top);
00472 
00473     ~RecursiveIter()
00474         { delete it; }
00475 
00476     void rewind()
00477         { it->rewind(); }
00478     bool next()
00479     {
00480         if (!it->next())
00481             return false;
00482         current = top[it->key()];
00483         return true;
00484     }   
00485 
00486     // FIXME: this is a speed optimization only.  Don't use this unless
00487     // you're apenwarr.  It will change.
00488     WvString _value() const
00489         { return it->value(); }
00490 };
00491 
00492 
00510 class UniConf::XIter : public UniConf::IterBase
00511 {
00512     UniConfKey pathead;
00513     UniConfKey pattail;
00514     UniConf::XIter *subit;
00515     UniConf::Iter *it; 
00516     UniConf::RecursiveIter *recit; 
00517     bool ready; 
00519 public:
00521     XIter(const UniConf &_top, const UniConfKey &pattern);
00522     ~XIter();
00523 
00524     void rewind();
00525     bool next();
00526     
00527 private:
00528     void cleanup();
00529     bool qnext();
00530     void enter(const UniConf &child);
00531 };
00532 
00533 
00542 class UniConf::SortedIterBase : public UniConf::IterBase
00543 {
00544 public:
00545     typedef int (*Comparator)(const UniConf &a, const UniConf &b);
00546 
00548     static int defcomparator(const UniConf &a, const UniConf &b);
00549 
00550     SortedIterBase(const UniConf &_top, Comparator comparator = defcomparator);
00551     ~SortedIterBase();
00552 
00553     bool next();
00554 
00555 private:
00556     Comparator xcomparator;
00557     int index;
00558     int count;
00559     
00560     void _purge();
00561     void _rewind();
00562     
00563     static int wrapcomparator(const UniConf *a, const UniConf *b);
00564     static Comparator innercomparator;
00565 
00566 protected:
00567     typedef WvVector<UniConf> Vector;
00568     Vector xkeys;
00569     
00570     template<class Iter>
00571     void populate(Iter &i)
00572     {
00573         _purge();
00574         for (i.rewind(); i.next(); )
00575             xkeys.append(new UniConf(*i), true);
00576         _rewind();
00577     }
00578 };
00579 
00580 
00584 class UniConf::SortedIter : public UniConf::SortedIterBase
00585 {
00586     UniConf::Iter i;
00587 
00588 public:
00589     SortedIter(const UniConf &_top, Comparator comparator = defcomparator)
00590         : SortedIterBase(_top, comparator), i(_top)
00591         { }
00592 
00593     void rewind()
00594         { populate(i); }
00595 };
00596 
00597 
00601 class UniConf::SortedRecursiveIter : public UniConf::SortedIterBase
00602 {
00603     UniConf::RecursiveIter i;
00604 
00605 public:
00606     SortedRecursiveIter(const UniConf &_top,
00607                         Comparator comparator = defcomparator)
00608         : SortedIterBase(_top, comparator), i(_top)
00609         { }
00610 
00611     void rewind()
00612         { populate(i); }
00613 };
00614 
00615 
00619 class UniConf::SortedXIter : public UniConf::SortedIterBase
00620 {
00621     UniConf::XIter i;
00622 
00623 public:
00624     SortedXIter(const UniConf &_top, const UniConfKey &pattern,
00625                 Comparator comparator = defcomparator) 
00626         : SortedIterBase(_top, comparator), i(_top, pattern) 
00627         { }
00628 
00629     void rewind()
00630         { populate(i); }
00631 };
00632 
00633 #endif // __UNICONF_H

Generated on Thu Jan 24 16:50:55 2008 for WvStreams by  doxygen 1.5.4