uniconftree.h

00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * UniConf low-level tree storage abstraction.
00006  */
00007 #ifndef __UNICONFTREE_H
00008 #define __UNICONFTREE_H
00009 
00010 #include "uniconfkey.h"
00011 #include "wvcallback.h"
00012 #include "unihashtree.h"
00013 
00022 template<class Sub, class Base = UniHashTreeBase>
00023 class UniConfTree : public Base
00024 {
00025    
00026 public:
00027     typedef WvCallback<void, const Sub *, void *> Visitor;
00028     typedef WvCallback<bool, const Sub *, const Sub *, void *> Comparator;
00029 
00031     UniConfTree(Sub *parent, const UniConfKey &key) :
00032         Base(parent, key)
00033         { }
00034 
00036     ~UniConfTree()
00037         { zap(); }
00038 
00040     Sub *parent() const
00041         { return static_cast<Sub*>(this->xparent); }
00042 
00044     void setparent(Sub *parent)
00045         { Base::_setparent(parent); }
00046     
00048     Sub *root() const
00049         { return static_cast<Sub*>(Base::_root()); }
00050     
00055     UniConfKey fullkey(const Sub *ancestor = NULL) const
00056         { return Base::_fullkey(ancestor); }
00057 
00062     Sub *find(const UniConfKey &key) const
00063         { return static_cast<Sub*>(Base::_find(key)); }
00064     
00071     Sub *findchild(const UniConfKey &key) const
00072         { return static_cast<Sub*>(Base::_findchild(key)); }
00073 
00080     void remove(const UniConfKey &key)
00081         { delete find(key); }
00082     
00084     void zap()
00085     {
00086         if (!(this->xchildren))
00087             return;
00088         // set xchildren to NULL first so that the zap() will happen faster
00089         // otherwise, each child will attempt to unlink itself uselessly
00090 
00091         typename Base::Container *oldchildren = this->xchildren;
00092         this->xchildren = NULL;
00093 
00094         // delete all children
00095         typename Base::Container::Iter i(*oldchildren);
00096         for (i.rewind(); i.next();)
00097             delete static_cast<Sub*>(i.ptr());
00098 
00099         delete oldchildren;
00100     }
00101 
00108     void visit(const Visitor &visitor, void *userdata,
00109         bool preorder = true, bool postorder = false) const
00110     {
00111         _recursive_unsorted_visit(this, reinterpret_cast<
00112             const typename Base::BaseVisitor&>(visitor), userdata,
00113             preorder, postorder);
00114     }
00115 
00124     bool compare(const Sub *other, const Comparator &comparator,
00125         void *userdata)
00126     {
00127         return _recursivecompare(this, other, reinterpret_cast<
00128             const typename Base::BaseComparator&>(comparator), userdata);
00129     }
00130 
00135     class Iter : public Base::Iter
00136     {
00137     public:
00138         typedef typename Base::Iter MyBase;
00139 
00141         Iter(Sub &tree) : Base::Iter(tree)
00142             { }
00143 
00145         Sub *ptr() const
00146             { return static_cast<Sub*>(MyBase::ptr()); }
00147         WvIterStuff(Sub);
00148     };
00149 };
00150 
00151 
00153 class UniConfValueTree : public UniConfTree<UniConfValueTree>
00154 {
00155     WvString xvalue;  
00157 public:
00158     UniConfValueTree(UniConfValueTree *parent,
00159                      const UniConfKey &key, WvStringParm value)
00160         : UniConfTree<UniConfValueTree>(parent, key), xvalue(value)
00161         { }
00162     
00164     const WvString &value() const
00165         { return xvalue; }
00166 
00168     void setvalue(WvStringParm value)
00169         { xvalue = value; }
00170 };
00171 
00172 
00173 #endif // __UNICONFTREE_H

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