Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

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 
00016 #ifdef  __cplusplus
00017 
00018 #include "uniconfkey.h"
00019 #include "uniconfgen.h"
00020 #include "wvcallback.h"
00021 #include "wvvector.h"
00022 
00023 class WvStream;
00024 class UniConf;
00025 class UniConfRoot;
00026 
00034 typedef WvCallback<void, const UniConf &, const UniConfKey &> UniConfCallback;
00035 
00036 #ifdef SWIG_NO_OVERLOAD
00037 // FIXME: This directive doesn't work.  Why not?
00038 %ignore UniConf::u(const UniConfKey &key);
00039 #endif
00040 
00062 class UniConf
00063 {
00064     friend class UniConfRoot;
00065     
00066 protected:
00067     UniConfRoot *xroot;
00068     UniConfKey xfullkey;
00069 
00076     UniConf(UniConfRoot *root, const UniConfKey &fullkey = UniConfKey::EMPTY);
00077     
00078 public:
00080     UniConf();
00081     
00083     UniConf(const UniConf &other);
00084     
00086     virtual ~UniConf();
00087 
00088     
00089     /***** Handle Manipulation API *****/
00090 
00092     UniConf root() const
00093         { return UniConf(xroot, UniConfKey::EMPTY); }
00094 
00096     UniConf parent() const
00097         { return UniConf(xroot, xfullkey.removelast()); }
00098     
00103     UniConfRoot *rootobj() const
00104         { return xroot; }
00105 
00107     bool isnull() const
00108         { return xroot == NULL; }
00109 
00111     UniConfKey fullkey() const
00112         { return xfullkey; }
00113     
00116 #ifdef SWIG_NO_OVERLOAD
00117     %name(fullkey_from_key)
00118 #endif
00119     UniConfKey fullkey(const UniConfKey &k) const;
00120     
00122 #ifdef SWIG_NO_OVERLOAD
00123     %name(fullkey_from_handle)
00124 #endif
00125 
00126     UniConfKey fullkey(const UniConf &cfg) const
00127         { return fullkey(cfg.fullkey()); }
00128 
00130     UniConfKey key() const
00131         { return xfullkey.last(); }
00132 
00133 #ifndef SWIG
00134 
00139     const UniConf operator[] (const UniConfKey &key) const
00140         { return UniConf(xroot, UniConfKey(xfullkey, key)); }
00141 #endif  // SWIG
00142 
00147 #ifdef SWIG_NO_OVERLOAD
00148     %name(u_should_be_ignored)
00149 #endif
00150     const UniConf u(const UniConfKey &key) const
00151         { return (*this)[key]; }
00152 
00154     UniConf &operator= (const UniConf &other)
00155     {
00156         xroot = other.xroot;
00157         xfullkey = other.xfullkey;
00158         return *this;
00159     }
00160 
00161     
00162     /***** Key Retrieval API *****/
00163     
00165     void prefetch(bool recursive) const;
00166     
00171     WvString getme(WvStringParm defvalue = WvString::null) const;
00172 
00174     WvString operator* () const
00175         { return getme(); }
00176 
00178     WvStringStar operator -> () const
00179         { return getme(); }
00180     
00182     WvString xget(WvStringParm key,
00183                   WvStringParm defvalue = WvString::null) const
00184         { return (*this)[key].getme(defvalue); }
00185 
00193     int getmeint(int defvalue = 0) const;
00194 
00196     int xgetint(WvStringParm key, int defvalue = 0) const
00197         { return (*this)[key].getmeint(defvalue); }
00198 
00205     bool exists() const;
00206 
00207 
00208     /***** Key Storage API *****/
00209 
00215     void setme(WvStringParm value) const;
00216 
00221     void setme(WVSTRING_FORMAT_DECL) const
00222         { return setme(WvString(WVSTRING_FORMAT_CALL)); }
00223 
00225     void xset(WvStringParm key, WvStringParm value) const
00226         { (*this)[key].setme(value); }
00227 
00232     void setmeint(int value) const;
00233 
00235     void xsetint(WvStringParm key, int value) const
00236         { (*this)[key].setmeint(value); }
00237 
00238 
00239     /***** Key Handling API *****/
00240 
00254     void move(const UniConf &dst) const;
00255 
00260     void remove() const
00261         { setme(WvString::null); }
00262 
00272     void copy(const UniConf &dst, bool force) const;
00273 
00274 
00275     
00276     /***** Key Persistence API *****/
00277 
00283     bool refresh() const;
00284     
00288     void commit() const;
00289 
00290     
00291     /***** Generator Mounting API *****/
00292     
00301     IUniConfGen *mount(WvStringParm moniker, bool refresh = true) const;
00302     
00313     IUniConfGen *mountgen(IUniConfGen *gen, bool refresh = true) const;
00314     
00316     void unmount(IUniConfGen *gen, bool commit) const;
00317     
00319     bool ismountpoint() const;
00320     
00331     IUniConfGen *whichmount(UniConfKey *mountpoint = NULL) const;
00332 
00333     
00334     /***** Notification API *****/
00335 
00340     void add_callback(void *cookie, const UniConfCallback &callback,
00341                       bool recurse = true) const;
00342     
00346     void del_callback(void *cookie, bool recurse = true) const;
00347 
00352     void add_setbool(bool *flag, bool recurse = true) const;
00353 
00357     void del_setbool(bool *flag, bool recurse = true) const;
00358     
00367     void hold_delta();
00368 
00377     void unhold_delta();
00378     
00383     void clear_delta();
00384 
00389     void flush_delta();
00390     
00391     
00392     /***** Key Enumeration API *****/
00393     
00398     void dump(WvStream &stream, bool everything = false) const;
00399     
00406     bool haschildren() const;
00407     
00408     /*** Iterators (see comments in class declaration) ***/
00409 
00410     // internal base class for all of the key iterators
00411     class IterBase;
00412     // iterates over direct children
00413     class Iter;
00414     // iterates over all descendents in preorder traversal
00415     class RecursiveIter;
00416     // iterates over children matching a wildcard
00417     class XIter;
00418 
00419     // internal base class for sorted key iterators
00420     class SortedIterBase;
00421     // sorted variant of Iter
00422     class SortedIter;
00423     // sorted variant of RecursiveIter
00424     class SortedRecursiveIter;
00425     // sorted variant of XIter
00426     class SortedXIter;
00427 
00428 #ifndef SWIG
00429     // lists of iterators
00430     DeclareWvList(Iter);
00431 #endif  // SWIG
00432 };
00433 
00434 
00435 #ifndef SWIG
00436 
00440 class UniConf::IterBase
00441 {
00442 protected:
00443     UniConf top;
00444     UniConf current;
00445 
00446     IterBase(const UniConf &_top)
00447         : top(_top)
00448         { }
00449 
00450 public:
00451     const UniConf *ptr() const
00452         { return &current; }
00453     WvIterStuff(const UniConf);
00454 };
00455 
00456 
00460 class UniConf::Iter : public UniConf::IterBase
00461 {
00462     UniConfGen::Iter *it;
00463     
00464 public:
00466     Iter(const UniConf &_top);
00467 
00468     ~Iter()
00469         { delete it; }
00470 
00471     void rewind()
00472         { it->rewind(); }
00473     bool next()
00474     {
00475         if (!it->next())
00476             return false;
00477         current = top[it->key()];
00478         return true;
00479     }
00480     
00481     // FIXME: this is a speed optimization only.  Don't use this unless
00482     // you're apenwarr.  It will change.
00483     WvString _value() const
00484         { return it->value(); }
00485 };
00486 
00487 
00491 class UniConf::RecursiveIter : public UniConf::IterBase
00492 {
00493     UniConfGen::Iter *it;
00494 
00495 public:
00497     RecursiveIter(const UniConf &_top);
00498 
00499     ~RecursiveIter()
00500         { delete it; }
00501 
00502     void rewind()
00503         { it->rewind(); }
00504     bool next()
00505     {
00506         if (!it->next())
00507             return false;
00508         current = top[it->key()];
00509         return true;
00510     }   
00511 
00512     // FIXME: this is a speed optimization only.  Don't use this unless
00513     // you're apenwarr.  It will change.
00514     WvString _value() const
00515         { return it->value(); }
00516 };
00517 
00518 
00536 class UniConf::XIter : public UniConf::IterBase
00537 {
00538     UniConfKey pathead;
00539     UniConfKey pattail;
00540     UniConf::XIter *subit;
00541     UniConf::Iter *it; 
00542     UniConf::RecursiveIter *recit; 
00543     bool ready; 
00545 public:
00547     XIter(const UniConf &_top, const UniConfKey &pattern);
00548     ~XIter();
00549 
00550     void rewind();
00551     bool next();
00552     
00553 private:
00554     void cleanup();
00555     bool qnext();
00556     void enter(const UniConf &child);
00557 };
00558 
00559 
00568 class UniConf::SortedIterBase : public UniConf::IterBase
00569 {
00570 public:
00571     typedef int (*Comparator)(const UniConf &a, const UniConf &b);
00572 
00574     static int defcomparator(const UniConf &a, const UniConf &b);
00575 
00576     SortedIterBase(const UniConf &_top, Comparator comparator = defcomparator);
00577     ~SortedIterBase();
00578 
00579     bool next();
00580 
00581 private:
00582     Comparator xcomparator;
00583     int index;
00584     int count;
00585     
00586     void _purge();
00587     void _rewind();
00588     
00589     static int wrapcomparator(const UniConf **a, const UniConf **b);
00590     static Comparator innercomparator;
00591 
00592 protected:
00593     typedef WvVector<UniConf> Vector;
00594     Vector xkeys;
00595     
00596     template<class Iter>
00597     void populate(Iter &i)
00598     {
00599         _purge();
00600         for (i.rewind(); i.next(); )
00601             xkeys.append(new UniConf(*i));
00602         _rewind();
00603     }
00604 };
00605 
00606 
00610 class UniConf::SortedIter : public UniConf::SortedIterBase
00611 {
00612     UniConf::Iter i;
00613 
00614 public:
00615     SortedIter(const UniConf &_top, Comparator comparator = defcomparator)
00616         : SortedIterBase(_top, comparator), i(_top)
00617         { }
00618 
00619     void rewind()
00620         { populate(i); }
00621 };
00622 
00623 
00627 class UniConf::SortedRecursiveIter : public UniConf::SortedIterBase
00628 {
00629     UniConf::RecursiveIter i;
00630 
00631 public:
00632     SortedRecursiveIter(const UniConf &_top,
00633                         Comparator comparator = defcomparator)
00634         : SortedIterBase(_top, comparator), i(_top)
00635         { }
00636 
00637     void rewind()
00638         { populate(i); }
00639 };
00640 
00641 
00645 class UniConf::SortedXIter : public UniConf::SortedIterBase
00646 {
00647     UniConf::XIter i;
00648 
00649 public:
00650     SortedXIter(const UniConf &_top, const UniConfKey &pattern,
00651                 Comparator comparator = defcomparator) 
00652         : SortedIterBase(_top, comparator), i(_top, pattern) 
00653         { }
00654 
00655     void rewind()
00656         { populate(i); }
00657 };
00658 
00659 #endif  // SWIG
00660 
00661 #endif /* __cplusplus */
00662 
00663 
00669 #ifdef __cplusplus
00670 extern "C" {
00671 #endif  // __cplusplus
00672 
00673 
00675 typedef void *uniconf_t;
00676 
00677 
00678 /* Initialize and destroy UniConf. */
00679 uniconf_t uniconf_init(const char* _moniker);
00680 
00681 
00682 void uniconf_free(uniconf_t _uniconf);
00683 
00684 
00685 /* The string returned has been allocated with malloc() and should
00686  * thus be free()d. */
00687 const char* uniconf_get(uniconf_t _uniconf, const char* _key);
00688 
00689 
00690 void uniconf_set(uniconf_t _uniconf,
00691                  const char* _key, const char* _value);
00692 
00693 
00694 #ifdef  __cplusplus
00695 }
00696 #endif  // __cplusplus
00697 
00698 
00699 #endif // __UNICONF_H

Generated on Sun Jul 10 17:30:56 2005 for WvStreams by  doxygen 1.4.0