2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * UniConf low-level tree storage abstraction.
7 #ifndef __UNICONFTREE_H
8 #define __UNICONFTREE_H
10 #include "uniconfkey.h"
11 #include "unihashtree.h"
15 * A recursively composed dictionary for tree-structured
16 * data indexed by UniConfKey.
18 * Someday this could be further abstracted into a generic WvTreeDict.
20 * "Sub" is the name of the concrete subclass of UniConfTree.
23 class UniConfTree
: public UniHashTreeBase
27 typedef wv::function
<void(const Sub
*, void*)> Visitor
;
28 typedef wv::function
<bool(const Sub
*, const Sub
*)> Comparator
;
30 /** Creates a node and links it to a subtree, if parent is non-NULL */
31 UniConfTree(Sub
*parent
, const UniConfKey
&key
) :
32 UniHashTreeBase(parent
, key
)
35 /** Destroy this node's contents and children. */
39 /** Returns a pointer to the parent node, or NULL if there is none. */
41 { return static_cast<Sub
*>(this->xparent
); }
43 /** Reparents this node. */
44 void setparent(Sub
*parent
)
45 { UniHashTreeBase::_setparent(parent
); }
47 /** Returns a pointer to the root node of the tree. */
49 { return static_cast<Sub
*>(UniHashTreeBase::_root()); }
52 * Returns full path of this node relative to an ancestor.
53 * If ancestor is NULL, returns the root.
55 UniConfKey
fullkey(const Sub
*ancestor
= NULL
) const
56 { return UniHashTreeBase::_fullkey(ancestor
); }
59 * Finds the sub-node with the specified key.
60 * If key.isempty(), returns this node.
62 Sub
*find(const UniConfKey
&key
) const
63 { return static_cast<Sub
*>(UniHashTreeBase::_find(key
)); }
66 * Finds the direct child node with the specified key.
68 * If key.numsegments() == 1, then performs the same task
69 * as find(key), but a little faster. Otherwise returns NULL.
71 Sub
*findchild(const UniConfKey
&key
) const
72 { return static_cast<Sub
*>(UniHashTreeBase::_findchild(key
)); }
75 * Removes the node for the specified key from the tree
76 * and deletes it along with any of its children.
78 * If the key is UniConfKey::EMPTY, deletes this object.
80 void remove(const UniConfKey
&key
)
83 /** Removes and deletes all children of this node. */
86 if (!(this->xchildren
))
88 // set xchildren to NULL first so that the zap() will happen faster
89 // otherwise, each child will attempt to unlink itself uselessly
91 typename
UniHashTreeBase::Container
*oldchildren
= this->xchildren
;
92 this->xchildren
= NULL
;
94 // delete all children
95 typename
UniHashTreeBase::Container::Iter
i(*oldchildren
);
96 for (i
.rewind(); i
.next();)
97 delete static_cast<Sub
*>(i
.ptr());
103 * Performs a traversal on this tree using the specified
104 * visitor function and traversal type(s).
105 * "visitor" is the tree visitor function
106 * "userdata" is userdata for the tree visitor function
108 void visit(const Visitor
&visitor
, void *userdata
,
109 bool preorder
= true, bool postorder
= false) const
111 _recursive_unsorted_visit(this, reinterpret_cast<
112 const typename
UniHashTreeBase::BaseVisitor
&>(visitor
), userdata
,
113 preorder
, postorder
);
117 * Compares this tree with another using the specified comparator
119 * Comparison of a subtree ends when the comparator returns false.
120 * "comparator" is the value compare function
121 * "userdata" is userdata for the compare function
122 * Returns: true if the comparison function returned true each time
124 bool compare(const Sub
*other
, const Comparator
&comparator
)
126 return _recursivecompare(this, other
, reinterpret_cast<
127 const typename
UniHashTreeBase::BaseComparator
&>(comparator
));
131 * An iterator that walks over all elements on one level of a
134 class Iter
: public UniHashTreeBase::Iter
137 typedef typename
UniHashTreeBase::Iter MyBase
;
139 /** Creates an iterator over the specified tree. */
140 Iter(Sub
&tree
) : UniHashTreeBase::Iter(tree
)
143 /** Returns a pointer to the current node. */
145 { return static_cast<Sub
*>(MyBase::ptr()); }
151 /** A plain UniConfTree that holds keys and values. */
152 class UniConfValueTree
: public UniConfTree
<UniConfValueTree
>
154 WvString xvalue
; /*!< the value of this entry */
157 UniConfValueTree(UniConfValueTree
*parent
,
158 const UniConfKey
&key
, WvStringParm value
)
159 : UniConfTree
<UniConfValueTree
>(parent
, key
), xvalue(value
)
162 /** Returns the value field. */
163 const WvString
&value() const
166 /** Sets the value field. */
167 void setvalue(WvStringParm value
)
172 #endif // __UNICONFTREE_H