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 }
UniUnwrapGen::Iter
Definition: uniunwrapgen.cc:124
UniUnwrapGen::RecursiveIter::value
virtual WvString value() const
Returns the value of the current key.
Definition: uniunwrapgen.cc:158
UniConf::refresh
bool refresh() const
Refreshes information about this key recursively.
Definition: uniconf.cc:119
UniConf::commit
void commit() const
Commits information about this key recursively.
Definition: uniconf.cc:125
UniUnwrapGen::haschildren
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
Definition: uniunwrapgen.cc:111
UniUnwrapGen::isok
virtual bool isok()
Determines if the generator is usable and working properly.
Definition: uniunwrapgen.cc:117
UniUnwrapGen::Iter::value
virtual WvString value() const
Returns the value of the current key.
Definition: uniunwrapgen.cc:139
UniUnwrapGen::Iter::key
virtual UniConfKey key() const
Returns the current key.
Definition: uniunwrapgen.cc:138
UniUnwrapGen::Iter::~Iter
virtual ~Iter()
Destroys the iterator.
Definition: uniunwrapgen.cc:132
UniUnwrapGen::inner
const UniConf & inner() const
Returns the inner generator.
Definition: uniunwrapgen.h:45
UniUnwrapGen::RecursiveIter
Definition: uniunwrapgen.cc:143
UniConf
UniConf instances function as handles to subtrees of a UniConf tree and expose a high-level interface...
Definition: uniconf.h:50
UniConf::prefetch
void prefetch(bool recursive) const
See UniConfGen::prefetch().
Definition: uniconf.cc:62
UniConf::getme
WvString getme(WvStringParm defvalue=WvString::null) const
Fetches the string value for this key from the registry.
Definition: uniconf.cc:68
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
IUniConfGen
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:39
UniUnwrapGen::RecursiveIter::rewind
virtual void rewind()
Rewinds the iterator.
Definition: uniunwrapgen.cc:155
UniConf::rootobj
UniConfRoot * rootobj() const
Returns a pointer to the UniConfRoot that manages this node.
Definition: uniconf.h:91
UniUnwrapGen::Iter::next
virtual bool next()
Seeks to the next element in the sequence.
Definition: uniunwrapgen.cc:137
UniUnwrapGen::RecursiveIter::next
virtual bool next()
Seeks to the next element in the sequence.
Definition: uniunwrapgen.cc:156
UniUnwrapGen::setv
virtual void setv(const UniConfPairList &pairs)
Stores multiple key-value pairs into the registry.
Definition: uniunwrapgen.cc:97
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
UniUnwrapGen::exists
virtual bool exists(const UniConfKey &key)
Without fetching its value, returns true if a key exists.
Definition: uniunwrapgen.cc:105
UniUnwrapGen::iterator
virtual Iter * iterator(const UniConfKey &key)
Returns an iterator over the children of the specified key.
Definition: uniunwrapgen.cc:162
UniConfKey::suborsame
bool suborsame(const UniConfKey &key) const
Returns true if 'key' is a the same, or a subkey, of this UniConfKey.
Definition: uniconfkey.cc:294
UniConfKey::isempty
bool isempty() const
Returns true if this path has zero segments (also known as root).
Definition: uniconfkey.h:264
UniConfGen::delta
void delta(const UniConfKey &key, WvStringParm value)
Call this when a key's value or children have possibly changed.
Definition: uniconfgen.cc:77
UniUnwrapGen::RecursiveIter::key
virtual UniConfKey key() const
Returns the current key.
Definition: uniunwrapgen.cc:157
UniUnwrapGen::commit
virtual void commit()
Commits any changes.
Definition: uniunwrapgen.cc:55
UniUnwrapGen::refresh
virtual bool refresh()
Refreshes information about a key recursively.
Definition: uniunwrapgen.cc:66
UniConf::setme
void setme(WvStringParm value) const
Stores a string value for this key into the registry.
Definition: uniconf.cc:83
UniUnwrapGen
Deprecated: a UniConfGen that delegates all requests to an inner UniConf.
Definition: uniunwrapgen.h:33
UniUnwrapGen::prefetch
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
IUniConfGen::isok
virtual bool isok()=0
Determines if the generator is usable and working properly.
UniUnwrapGen::Iter::rewind
virtual void rewind()
Rewinds the iterator.
Definition: uniunwrapgen.cc:136
UniConf::fullkey
UniConfKey fullkey() const
Returns the full path of this node, starting at the root.
Definition: uniconf.h:99
UniUnwrapGen::recursiveiterator
virtual Iter * recursiveiterator(const UniConfKey &key)
Like iterator(), but the returned iterator is recursive, that is, it will return children of the imme...
Definition: uniunwrapgen.cc:168
UniConf::haschildren
bool haschildren() const
Returns true if this key has children.
Definition: uniconf.cc:56
UniConfRoot
Represents the root of a hierarhical registry consisting of pairs of UniConfKeys and associated strin...
Definition: uniconfroot.h:73
UniUnwrapGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: uniunwrapgen.cc:85
UniConf::Iter
This iterator walks through all immediate children of a UniConf node.
Definition: uniconf.h:435
UniConf::exists
bool exists() const
Without fetching its value, returns true if this key exists.
Definition: uniconf.cc:50
UniUnwrapGen::set
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: uniunwrapgen.cc:91
UniConf::RecursiveIter
This iterator performs depth-first traversal of a subtree.
Definition: uniconf.h:466
UniConf::whichmount
IUniConfGen * whichmount(UniConfKey *mountpoint=NULL) const
Finds the generator that owns this key.
Definition: uniconf.cc:155
UniConfGen::Iter
An abstract iterator over keys and values in a generator.
Definition: uniconfgen.h:323