WvStreams
uniunwrapgen.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2002 Net Integration Technologies, Inc.
4  *
5  * A totally evil UniConfGen that "unwraps" a UniConf object by turning it
6  * back into a UniConfGen. See uniunwrapgen.h.
7  */
8 #include "uniconfroot.h"
9 #include "uniunwrapgen.h"
10 #include "wvlinkerhack.h"
11 
12 WV_LINK(UniUnwrapGen);
13 
14 
15 UniUnwrapGen::UniUnwrapGen(const UniConf &inner)
16 {
17  refreshing = committing = false;
18  setinner(inner);
19 }
20 
21 
22 UniUnwrapGen::~UniUnwrapGen()
23 {
24  UniConfRoot *root = xinner.rootobj();
25  if (root)
26  root->mounts.del_callback(this);
27 }
28 
29 
30 void UniUnwrapGen::setinner(const UniConf &inner)
31 {
32  UniConfRoot *root = xinner.rootobj();
33  if (root)
34  root->mounts.del_callback(this);
35 
36  xinner = inner;
37  xfullkey = xinner.fullkey();
38 
39  root = xinner.rootobj();
40  if (root)
41  root->mounts.add_callback(this, wv::bind(&UniUnwrapGen::gencallback,
42  this, _1, _2));
43 }
44 
45 
46 UniConf UniUnwrapGen::_sub(const UniConfKey &key)
47 {
48  if (key.isempty())
49  return xinner;
50  else
51  return xinner[key];
52 }
53 
54 
56 {
57  if (!committing)
58  {
59  committing = true;
60  xinner.commit();
61  committing = false;
62  }
63 }
64 
65 
67 {
68  if (!refreshing)
69  {
70  refreshing = true;
71  bool ret = xinner.refresh();
72  refreshing = false;
73  return ret;
74  }
75  return true;
76 }
77 
78 
79 void UniUnwrapGen::prefetch(const UniConfKey &key, bool recursive)
80 {
81  _sub(key).prefetch(recursive);
82 }
83 
84 
86 {
87  return _sub(key).getme();
88 }
89 
90 
91 void UniUnwrapGen::set(const UniConfKey &key, WvStringParm value)
92 {
93  _sub(key).setme(value);
94 }
95 
96 
97 void UniUnwrapGen::setv(const UniConfPairList &pairs)
98 {
99  // Extremely evil. This pokes directly into UniMountGen, because we
100  // don't want to expose setv to users.
101  xinner.rootobj()->mounts.setv(pairs);
102 }
103 
104 
106 {
107  return _sub(key).exists();
108 }
109 
110 
112 {
113  return _sub(key).haschildren();
114 }
115 
116 
118 {
119  IUniConfGen *gen = xinner.whichmount();
120  return gen ? gen->isok() : false;
121 }
122 
123 
125 {
126  UniConf::Iter i;
127 
128 public:
129  Iter(const UniConf &cfg)
130  : i(cfg)
131  { }
132  virtual ~Iter()
133  { }
134 
135  /***** Overridden members *****/
136  virtual void rewind() { i.rewind(); }
137  virtual bool next() { return i.next(); }
138  virtual UniConfKey key() const { return i->key(); }
139  virtual WvString value() const { return i->getme(); }
140 };
141 
142 
144 {
146 
147 public:
148  RecursiveIter(const UniConf &cfg)
149  : i(cfg)
150  { }
151  virtual ~RecursiveIter()
152  { }
153 
154  /***** Overridden members *****/
155  virtual void rewind() { i.rewind(); }
156  virtual bool next() { return i.next(); }
157  virtual UniConfKey key() const { return i->key(); }
158  virtual WvString value() const { return i->getme(); }
159 };
160 
161 
163 {
164  return new Iter(_sub(key));
165 }
166 
167 
169 {
170  return new RecursiveIter(_sub(key));
171 }
172 
173 
174 void UniUnwrapGen::gencallback(const UniConfKey &key, WvStringParm value)
175 {
176  UniConfKey subkey;
177  if (xfullkey.suborsame(key, subkey))
178  delta(subkey, value);
179 }
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:40
virtual bool isok()=0
Determines if the generator is usable and working properly.
An abstract iterator over keys and values in a generator.
Definition: uniconfgen.h:324
void delta(const UniConfKey &key, WvStringParm value)
Call this when a key's value or children have possibly changed.
Definition: uniconfgen.cc:77
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:39
bool isempty() const
Returns true if this path has zero segments (also known as root).
Definition: uniconfkey.h:264
bool suborsame(const UniConfKey &key) const
Returns true if 'key' is a the same, or a subkey, of this UniConfKey.
Definition: uniconfkey.cc:294
Represents the root of a hierarhical registry consisting of pairs of UniConfKeys and associated strin...
Definition: uniconfroot.h:74
This iterator walks through all immediate children of a UniConf node.
Definition: uniconf.h:436
This iterator performs depth-first traversal of a subtree.
Definition: uniconf.h:467
UniConf instances function as handles to subtrees of a UniConf tree and expose a high-level interface...
Definition: uniconf.h:51
void commit() const
Commits information about this key recursively.
Definition: uniconf.cc:125
void prefetch(bool recursive) const
See UniConfGen::prefetch().
Definition: uniconf.cc:62
bool haschildren() const
Returns true if this key has children.
Definition: uniconf.cc:56
void setme(WvStringParm value) const
Stores a string value for this key into the registry.
Definition: uniconf.cc:83
bool exists() const
Without fetching its value, returns true if this key exists.
Definition: uniconf.cc:50
bool refresh() const
Refreshes information about this key recursively.
Definition: uniconf.cc:119
UniConfRoot * rootobj() const
Returns a pointer to the UniConfRoot that manages this node.
Definition: uniconf.h:91
IUniConfGen * whichmount(UniConfKey *mountpoint=NULL) const
Finds the generator that owns this key.
Definition: uniconf.cc:155
UniConfKey fullkey() const
Returns the full path of this node, starting at the root.
Definition: uniconf.h:99
WvString getme(WvStringParm defvalue=WvString::null) const
Fetches the string value for this key from the registry.
Definition: uniconf.cc:68
virtual ~Iter()
Destroys the iterator.
virtual UniConfKey key() const
Returns the current key.
virtual void rewind()
Rewinds the iterator.
virtual WvString value() const
Returns the value of the current key.
virtual bool next()
Seeks to the next element in the sequence.
virtual void rewind()
Rewinds the iterator.
virtual bool next()
Seeks to the next element in the sequence.
virtual WvString value() const
Returns the value of the current key.
virtual UniConfKey key() const
Returns the current key.
Deprecated: a UniConfGen that delegates all requests to an inner UniConf.
Definition: uniunwrapgen.h:34
virtual Iter * recursiveiterator(const UniConfKey &key)
Like iterator(), but the returned iterator is recursive, that is, it will return children of the imme...
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: uniunwrapgen.cc:91
virtual void setv(const UniConfPairList &pairs)
Stores multiple key-value pairs into the registry.
Definition: uniunwrapgen.cc:97
const UniConf & inner() const
Returns the inner generator.
Definition: uniunwrapgen.h:45
virtual void commit()
Commits any changes.
Definition: uniunwrapgen.cc:55
virtual bool refresh()
Refreshes information about a key recursively.
Definition: uniunwrapgen.cc:66
virtual bool exists(const UniConfKey &key)
Without fetching its value, returns true if a key exists.
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: uniunwrapgen.cc:85
virtual Iter * iterator(const UniConfKey &key)
Returns an iterator over the children of the specified key.
virtual bool isok()
Determines if the generator is usable and working properly.
virtual void prefetch(const UniConfKey &key, bool recursive)
Indicate that we will eventually be interested in doing get(), haschildren(), or other "get-like" ope...
Definition: uniunwrapgen.cc:79
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
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