WvStreams
unicachegen.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2002 Net Integration Technologies, Inc.
4  *
5  * A UniConf generator that stores keys in memory.
6  */
7 #include "uniconf.h"
8 #include "unicachegen.h"
9 #include "wvmoniker.h"
10 #include "wvlinkerhack.h"
11 
12 WV_LINK(UniCacheGen);
13 
14 
15 // if 'obj' is non-NULL and is a UniConfGen, wrap that; otherwise wrap the
16 // given moniker.
17 static IUniConfGen *creator(WvStringParm s, IObject *_obj)
18 {
19  return new UniCacheGen(wvcreate<IUniConfGen>(s, _obj));
20 }
21 
22 static WvMoniker<IUniConfGen> reg("cache", creator);
23 
24 
25 /***** UniCacheGen *****/
26 
27 UniCacheGen::UniCacheGen(IUniConfGen *_inner)
28  : log("UniCache", WvLog::Debug1), inner(_inner)
29 {
30  if (inner)
31  inner->add_callback(this, wv::bind(&UniCacheGen::deltacallback, this,
32  _1, _2));
33  refreshed_once = false;
34 }
35 
36 
37 UniCacheGen::~UniCacheGen()
38 {
39  inner->del_callback(this);
40  WVRELEASE(inner);
41 }
42 
43 
45 {
46  return inner->isok();
47 }
48 
49 
51 {
52  if (!refreshed_once)
53  {
54  bool ret = inner->refresh();
55  loadtree();
56  refreshed_once = true;
57  return ret;
58  }
59  else
60  return false;
61 }
62 
63 
65 {
66  inner->commit();
67 }
68 
69 
70 void UniCacheGen::loadtree(const UniConfKey &key)
71 {
72  UniConfGen::Iter *i = inner->recursiveiterator(key);
73  if (!i) return;
74 
75  //assert(false);
76  for (i->rewind(); i->next(); )
77  {
78  WvString value(i->value());
79 
80  //fprintf(stderr, "Key: '%s'\n", i->key().cstr());
81  //fprintf(stderr, " Val: '%s'\n", value.cstr());
82 
83  if (!!value)
84  UniTempGen::set(i->key(), value);
85  }
86 
87  delete i;
88 }
89 
90 
91 void UniCacheGen::deltacallback(const UniConfKey &key, WvStringParm value)
92 {
93  UniTempGen::set(key, value);
94 }
95 
96 void UniCacheGen::set(const UniConfKey &key, WvStringParm value)
97 {
98  inner->set(key, value);
99 }
100 
102 {
103  //inner->get(key);
104  inner->flush_buffers(); // update all pending notifications
105  return UniTempGen::get(key);
106 }