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 }