WvStreams
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 "unihashtree.h"
00012 #include "wvtr1.h"
00013 
00022 template<class Sub>
00023 class UniConfTree : public UniHashTreeBase
00024 {
00025    
00026 public:
00027     typedef wv::function<void(const Sub*, void*)> Visitor;
00028     typedef wv::function<bool(const Sub*, const Sub*)> Comparator;
00029 
00031     UniConfTree(Sub *parent, const UniConfKey &key) :
00032         UniHashTreeBase(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         { UniHashTreeBase::_setparent(parent); }
00046     
00048     Sub *root() const
00049         { return static_cast<Sub*>(UniHashTreeBase::_root()); }
00050     
00055     UniConfKey fullkey(const Sub *ancestor = NULL) const
00056         { return UniHashTreeBase::_fullkey(ancestor); }
00057 
00062     Sub *find(const UniConfKey &key) const
00063         { return static_cast<Sub*>(UniHashTreeBase::_find(key)); }
00064     
00071     Sub *findchild(const UniConfKey &key) const
00072         { return static_cast<Sub*>(UniHashTreeBase::_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 UniHashTreeBase::Container *oldchildren = this->xchildren;
00092         this->xchildren = NULL;
00093 
00094         // delete all children
00095         typename UniHashTreeBase::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 UniHashTreeBase::BaseVisitor&>(visitor), userdata,
00113             preorder, postorder);
00114     }
00115 
00124     bool compare(const Sub *other, const Comparator &comparator)
00125     {
00126         return _recursivecompare(this, other, reinterpret_cast<
00127             const typename UniHashTreeBase::BaseComparator&>(comparator));
00128     }
00129 
00134     class Iter : public UniHashTreeBase::Iter
00135     {
00136     public:
00137         typedef typename UniHashTreeBase::Iter MyBase;
00138 
00140         Iter(Sub &tree) : UniHashTreeBase::Iter(tree)
00141             { }
00142 
00144         Sub *ptr() const
00145             { return static_cast<Sub*>(MyBase::ptr()); }
00146         WvIterStuff(Sub);
00147     };
00148 };
00149 
00150 
00152 class UniConfValueTree : public UniConfTree<UniConfValueTree>
00153 {
00154     WvString xvalue;  
00156 public:
00157     UniConfValueTree(UniConfValueTree *parent,
00158                      const UniConfKey &key, WvStringParm value)
00159         : UniConfTree<UniConfValueTree>(parent, key), xvalue(value)
00160         { }
00161     
00163     const WvString &value() const
00164         { return xvalue; }
00165 
00167     void setvalue(WvStringParm value)
00168         { xvalue = value; }
00169 };
00170 
00171 
00172 #endif // __UNICONFTREE_H