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 }
UniTempGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unitempgen.cc:38
IUniConfGen::set
virtual void set(const UniConfKey &key, WvStringParm value)=0
Stores a string value for a key into the registry.
UniCacheGen::set
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: unicachegen.cc:96
UniConfGen::Iter::value
virtual WvString value() const =0
Returns the value of the current key.
UniCacheGen::commit
virtual void commit()
Commits any changes.
Definition: unicachegen.cc:64
UniCacheGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unicachegen.cc:101
UniCacheGen
A UniConf generator that adds a cache layer on top of another generator.
Definition: unicachegen.h:26
IUniConfGen::flush_buffers
virtual void flush_buffers()=0
Flushes any commitment/notification buffers .
UniConfGen::Iter::key
virtual UniConfKey key() const =0
Returns the current key.
UniCacheGen::refresh
virtual bool refresh()
Refreshes information about a key recursively.
Definition: unicachegen.cc:50
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
WvLog
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition: wvlog.h:56
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
WvMoniker
A type-safe version of WvMonikerBase that lets you provide create functions for object types other th...
Definition: wvmoniker.h:61
UniFilterGen::inner
IUniConfGen * inner() const
Returns the inner generator.
Definition: unifiltergen.h:33
UniTempGen::set
virtual void set(const UniConfKey &key, WvStringParm value)
Stores a string value for a key into the registry.
Definition: unitempgen.cc:57
IObject
Definition: IObject.h:65
IUniConfGen::commit
virtual void commit()=0
Commits any changes.
UniConfGen::Iter::rewind
virtual void rewind()=0
Rewinds the iterator.
IUniConfGen::del_callback
virtual void del_callback(void *cookie)=0
Removes a callback for change notification.
IUniConfGen::isok
virtual bool isok()=0
Determines if the generator is usable and working properly.
IUniConfGen::refresh
virtual bool refresh()=0
Refreshes information about a key recursively.
UniConfGen::Iter::next
virtual bool next()=0
Seeks to the next element in the sequence.
IUniConfGen::add_callback
virtual void add_callback(void *cookie, const UniConfGenCallback &callback)=0
Adds a callback for change notification.
IUniConfGen::recursiveiterator
virtual Iter * recursiveiterator(const UniConfKey &key)=0
Like iterator(), but the returned iterator is recursive, that is, it will return children of the imme...
UniCacheGen::isok
virtual bool isok()
Determines if the generator is usable and working properly.
Definition: unicachegen.cc:44
UniConfGen::Iter
An abstract iterator over keys and values in a generator.
Definition: uniconfgen.h:323