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
22template<class Sub>
24{
25
26public:
27 typedef wv::function<void(const Sub*, void*)> Visitor;
28 typedef wv::function<bool(const Sub*, const Sub*)> Comparator;
29
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
152class UniConfValueTree : public UniConfTree<UniConfValueTree>
153{
154 WvString xvalue;
156public:
160 { }
161
163 const WvString &value() const
164 { return xvalue; }
165
168 { xvalue = value; }
169};
170
171
172#endif // __UNICONFTREE_H
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:39
An iterator that walks over all elements on one level of a UniConfTree.
Definition: uniconftree.h:135
Sub * ptr() const
Returns a pointer to the current node.
Definition: uniconftree.h:144
Iter(Sub &tree)
Creates an iterator over the specified tree.
Definition: uniconftree.h:140
A recursively composed dictionary for tree-structured data indexed by UniConfKey.
Definition: uniconftree.h:24
UniConfKey fullkey(const Sub *ancestor=NULL) const
Returns full path of this node relative to an ancestor.
Definition: uniconftree.h:55
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
Sub * parent() const
Returns a pointer to the parent node, or NULL if there is none.
Definition: uniconftree.h:40
Sub * find(const UniConfKey &key) const
Finds the sub-node with the specified key.
Definition: uniconftree.h:62
Sub * root() const
Returns a pointer to the root node of the tree.
Definition: uniconftree.h:48
void setparent(Sub *parent)
Reparents this node.
Definition: uniconftree.h:44
~UniConfTree()
Destroy this node's contents and children.
Definition: uniconftree.h:36
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
void zap()
Removes and deletes all children of this node.
Definition: uniconftree.h:84
Sub * findchild(const UniConfKey &key) const
Finds the direct child node with the specified key.
Definition: uniconftree.h:71
UniConfTree(Sub *parent, const UniConfKey &key)
Creates a node and links it to a subtree, if parent is non-NULL.
Definition: uniconftree.h:31
bool compare(const Sub *other, const Comparator &comparator)
Compares this tree with another using the specified comparator function.
Definition: uniconftree.h:124
A plain UniConfTree that holds keys and values.
Definition: uniconftree.h:153
void setvalue(WvStringParm value)
Sets the value field.
Definition: uniconftree.h:167
const WvString & value() const
Returns the value field.
Definition: uniconftree.h:163
Container * xchildren
Definition: unihashtree.h:63
const UniConfKey & key() const
Returns the key field.
Definition: unihashtree.h:40
UniHashTreeBase * xparent
Definition: unihashtree.h:62
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition: wvstring.h:94
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:330