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) :
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:
158  const UniConfKey &key, WvStringParm 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
UniConfTree::setparent
void setparent(Sub *parent)
Reparents this node.
Definition: uniconftree.h:44
UniHashTreeBase::xchildren
Container * xchildren
Definition: unihashtree.h:63
UniConfTree::compare
bool compare(const Sub *other, const Comparator &comparator)
Compares this tree with another using the specified comparator function.
Definition: uniconftree.h:124
UniConfTree
A recursively composed dictionary for tree-structured data indexed by UniConfKey.
Definition: uniconftree.h:23
UniConfTree::remove
void remove(const UniConfKey &key)
Removes the node for the specified key from the tree and deletes it along with any of its children.
Definition: uniconftree.h:80
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
UniConfTree::findchild
Sub * findchild(const UniConfKey &key) const
Finds the direct child node with the specified key.
Definition: uniconftree.h:71
WvScatterHash::Iter
Definition: wvscatterhash.h:180
UniHashTreeBase::key
const UniConfKey & key() const
Returns the key field.
Definition: unihashtree.h:40
UniConfValueTree::value
const WvString & value() const
Returns the value field.
Definition: uniconftree.h:163
UniConfTree::visit
void visit(const Visitor &visitor, void *userdata, bool preorder=true, bool postorder=false) const
Performs a traversal on this tree using the specified visitor function and traversal type(s).
Definition: uniconftree.h:108
UniConfTree::Iter::ptr
Sub * ptr() const
Returns a pointer to the current node.
Definition: uniconftree.h:144
UniConfTree::Iter::Iter
Iter(Sub &tree)
Creates an iterator over the specified tree.
Definition: uniconftree.h:140
UniConfTree::root
Sub * root() const
Returns a pointer to the root node of the tree.
Definition: uniconftree.h:48
UniHashTreeBase::Iter
Definition: unihashtree.h:78
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
UniConfValueTree::setvalue
void setvalue(WvStringParm value)
Sets the value field.
Definition: uniconftree.h:167
UniConfTree::~UniConfTree
~UniConfTree()
Destroy this node's contents and children.
Definition: uniconftree.h:36
UniHashTreeBase::xparent
UniHashTreeBase * xparent
Definition: unihashtree.h:62
UniConfTree::parent
Sub * parent() const
Returns a pointer to the parent node, or NULL if there is none.
Definition: uniconftree.h:40
UniConfTree::zap
void zap()
Removes and deletes all children of this node.
Definition: uniconftree.h:84
UniConfTree::fullkey
UniConfKey fullkey(const Sub *ancestor=NULL) const
Returns full path of this node relative to an ancestor.
Definition: uniconftree.h:55
WvScatterHash
Definition: wvscatterhash.h:125
UniHashTreeBase
Definition: unihashtree.h:23
UniConfValueTree
A plain UniConfTree that holds keys and values.
Definition: uniconftree.h:152
UniConfTree::Iter
An iterator that walks over all elements on one level of a UniConfTree.
Definition: uniconftree.h:134
UniConfTree::find
Sub * find(const UniConfKey &key) const
Finds the sub-node with the specified key.
Definition: uniconftree.h:62
UniConfTree::UniConfTree
UniConfTree(Sub *parent, const UniConfKey &key)
Creates a node and links it to a subtree, if parent is non-NULL.
Definition: uniconftree.h:31