WvStreams
unifastregetgen.cc
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2002-2005 Net Integration Technologies, Inc.
4  *
5  * A lightweight but slightly dangerous version of UniCacheGen.
6  */
7 #include <wvassert.h>
8 
9 #include "unifastregetgen.h"
10 #include "uniconftree.h"
11 #include "wvmoniker.h"
12 
13 // if 'obj' is non-NULL and is a UniConfGen, wrap that; otherwise wrap the
14 // given moniker.
15 static IUniConfGen *creator(WvStringParm s, IObject *_obj)
16 {
17  return new UniFastRegetGen(wvcreate<IUniConfGen>(s, _obj));
18 }
19 
20 static WvMoniker<IUniConfGen> reg("fast-reget", creator);
21 
22 
23 UniFastRegetGen::UniFastRegetGen(IUniConfGen *_inner) :
24  UniFilterGen(_inner),
25  tree(NULL)
26 {
27  tree = new UniConfValueTree(NULL, "/", UniFilterGen::get("/"));
28 }
29 
30 
31 UniFastRegetGen::~UniFastRegetGen()
32 {
33  if (tree)
34  {
35  delete tree;
36  tree = NULL;
37  }
38 }
39 
40 
41 void UniFastRegetGen::gencallback(const UniConfKey &key, WvStringParm value)
42 {
43  if (tree == NULL)
44  return; // initialising
45 
46  UniConfValueTree *t = tree->find(key);
47  if (t) // never previously retrieved; don't cache it
48  t->setvalue(value);
49  UniFilterGen::gencallback(key, value);
50 }
51 
52 
54 {
55  if (!tree)
56  {
57  wvassert(tree, "key: '%s'", key);
58  abort();
59  }
60 
61  // Keys with trailing slashes can't have values set on them
62  if (key.hastrailingslash())
63  return WvString::null;
64 
65  UniConfValueTree *t = tree->find(key);
66  if (!t)
67  {
68  UniConfKey parentkey(key.removelast());
69  get(parentkey); // guaranteed to create parent node
70  t = tree->find(parentkey);
71  assert(t);
72 
73  WvString value;
74  if (!t->value().isnull()) // if parent is null, child guaranteed null
75  value = UniFilterGen::get(key);
76  new UniConfValueTree(t, key.last(), value);
77  return value;
78  }
79  else
80  return t->value();
81 }
82 
83 
85 {
86  // even if inner generator has a more efficient version of exists(),
87  // do it this way so we can cache the result.
88  return !get(key).isnull();
89 }
90 
91 
93 {
94  if (!tree)
95  {
96  wvassert(tree, "key: '%s'", key);
97  abort();
98  }
99 
100  // if we already know the node is null, we can short circuit this one
101  UniConfValueTree *t = tree->find(key);
102  if (t && t->value().isnull())
103  return false; // definitely no children
104  return UniFilterGen::haschildren(key);
105 }
UniFastRegetGen
A lightwight but slightly dangerous variant of UniCacheGen.
Definition: unifastregetgen.h:33
UniConfKey::removelast
UniConfKey removelast(int n=1) const
Returns the path formed by removing the last n segments of this path.
Definition: uniconfkey.h:346
UniFilterGen::gencallback
virtual void gencallback(const UniConfKey &key, WvStringParm value)
Called by inner generator when a key changes.
Definition: unifiltergen.cc:157
UniFastRegetGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unifastregetgen.cc:53
UniFilterGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unifiltergen.cc:76
UniFastRegetGen::haschildren
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
Definition: unifastregetgen.cc:92
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
UniConfValueTree::value
const WvString & value() const
Returns the value field.
Definition: uniconftree.h:163
WvFastString::isnull
bool isnull() const
returns true if this string is null
Definition: wvstring.h:290
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
UniConfValueTree::setvalue
void setvalue(WvStringParm value)
Sets the value field.
Definition: uniconftree.h:167
WvMoniker
A type-safe version of WvMonikerBase that lets you provide create functions for object types other th...
Definition: wvmoniker.h:61
UniFastRegetGen::exists
virtual bool exists(const UniConfKey &key)
Without fetching its value, returns true if a key exists.
Definition: unifastregetgen.cc:84
UniFastRegetGen::gencallback
virtual void gencallback(const UniConfKey &key, WvStringParm value)
Called by inner generator when a key changes.
Definition: unifastregetgen.cc:41
IObject
Definition: IObject.h:65
UniConfValueTree
A plain UniConfTree that holds keys and values.
Definition: uniconftree.h:152
UniConfKey::hastrailingslash
bool hastrailingslash() const
Returns true if the key has a trailing slash.
Definition: uniconfkey.h:273
UniConfTree::find
Sub * find(const UniConfKey &key) const
Finds the sub-node with the specified key.
Definition: uniconftree.h:62
UniConfKey::last
UniConfKey last(int n=1) const
Returns the path formed by the n last segments of this path.
Definition: uniconfkey.h:324
UniFilterGen::haschildren
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
Definition: unifiltergen.cc:118
UniFilterGen
A UniConfGen that delegates all requests to an inner generator.
Definition: unifiltergen.h:17