WvStreams
uniconftree.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * UniConf low-level tree storage abstraction.
6  */
7 #ifndef __UNICONFTREE_H
8 #define __UNICONFTREE_H
9 
10 #include "uniconfkey.h"
11 #include "unihashtree.h"
12 #include "wvtr1.h"
13 
22 template<class Sub>
24 {
25 
26 public:
27  typedef wv::function<void(const Sub*, void*)> Visitor;
28  typedef wv::function<bool(const Sub*, const Sub*)> Comparator;
29 
31  UniConfTree(Sub *parent, const UniConfKey &key) :
32  UniHashTreeBase(parent, key)
33  { }
34 
37  { zap(); }
38 
40  Sub *parent() const
41  { return static_cast<Sub*>(this->xparent); }
42 
44  void setparent(Sub *parent)
45  { UniHashTreeBase::_setparent(parent); }
46 
48  Sub *root() const
49  { return static_cast<Sub*>(UniHashTreeBase::_root()); }
50 
55  UniConfKey fullkey(const Sub *ancestor = NULL) const
56  { return UniHashTreeBase::_fullkey(ancestor); }
57 
62  Sub *find(const UniConfKey &key) const
63  { return static_cast<Sub*>(UniHashTreeBase::_find(key)); }
64 
71  Sub *findchild(const UniConfKey &key) const
72  { return static_cast<Sub*>(UniHashTreeBase::_findchild(key)); }
73 
80  void remove(const UniConfKey &key)
81  { delete find(key); }
82 
84  void zap()
85  {
86  if (!(this->xchildren))
87  return;
88  // set xchildren to NULL first so that the zap() will happen faster
89  // otherwise, each child will attempt to unlink itself uselessly
90 
91  typename UniHashTreeBase::Container *oldchildren = this->xchildren;
92  this->xchildren = NULL;
93 
94  // delete all children
95  typename UniHashTreeBase::Container::Iter i(*oldchildren);
96  for (i.rewind(); i.next();)
97  delete static_cast<Sub*>(i.ptr());
98 
99  delete oldchildren;
100  }
101 
108  void visit(const Visitor &visitor, void *userdata,
109  bool preorder = true, bool postorder = false) const
110  {
111  _recursive_unsorted_visit(this, reinterpret_cast<
112  const typename UniHashTreeBase::BaseVisitor&>(visitor), userdata,
113  preorder, postorder);
114  }
115 
124  bool compare(const Sub *other, const Comparator &comparator)
125  {
126  return _recursivecompare(this, other, reinterpret_cast<
127  const typename UniHashTreeBase::BaseComparator&>(comparator));
128  }
129 
135  {
136  public:
137  typedef typename UniHashTreeBase::Iter MyBase;
138 
140  Iter(Sub &tree) : UniHashTreeBase::Iter(tree)
141  { }
142 
144  Sub *ptr() const
145  { return static_cast<Sub*>(MyBase::ptr()); }
146  WvIterStuff(Sub);
147  };
148 };
149 
150 
152 class UniConfValueTree : public UniConfTree<UniConfValueTree>
153 {
154  WvString xvalue;
156 public:
159  : UniConfTree<UniConfValueTree>(parent, key), xvalue(value)
160  { }
161 
163  const WvString &value() const
164  { return xvalue; }
165 
167  void setvalue(WvStringParm value)
168  { xvalue = value; }
169 };
170 
171 
172 #endif // __UNICONFTREE_H