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.
15static IUniConfGen *creator(WvStringParm s, IObject *_obj)
16{
17 return new UniFastRegetGen(wvcreate<IUniConfGen>(s, _obj));
18}
19
20static WvMoniker<IUniConfGen> reg("fast-reget", creator);
21
22
23UniFastRegetGen::UniFastRegetGen(IUniConfGen *_inner) :
24 UniFilterGen(_inner),
25 tree(NULL)
26{
27 tree = new UniConfValueTree(NULL, "/", UniFilterGen::get("/"));
28}
29
30
31UniFastRegetGen::~UniFastRegetGen()
32{
33 if (tree)
34 {
35 delete tree;
36 tree = NULL;
37 }
38}
39
40
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}
The basic interface which is included by all other XPLC interfaces and objects.
Definition IObject.h:65
An abstract data container that backs a UniConf tree.
Definition uniconfgen.h:40
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition uniconfkey.h:39
UniConfKey removelast(int n=1) const
Returns the path formed by removing the last n segments of this path.
Definition uniconfkey.h:346
bool hastrailingslash() const
Returns true if the key has a trailing slash.
Definition uniconfkey.h:273
UniConfKey last(int n=1) const
Returns the path formed by the n last segments of this path.
Definition uniconfkey.h:324
Sub * find(const UniConfKey &key) const
Finds the sub-node with the specified key.
Definition uniconftree.h:62
A plain UniConfTree that holds keys and values.
void setvalue(WvStringParm value)
Sets the value field.
const WvString & value() const
Returns the value field.
A lightwight but slightly dangerous variant of UniCacheGen.
virtual void gencallback(const UniConfKey &key, WvStringParm value)
Called by inner generator when a key changes.
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
virtual bool exists(const UniConfKey &key)
Without fetching its value, returns true if a key exists.
A UniConfGen that delegates all requests to an inner generator.
virtual void gencallback(const UniConfKey &key, WvStringParm value)
Called by inner generator when a key changes.
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
virtual bool haschildren(const UniConfKey &key)
Returns true if a key has children.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition wvstring.h:94
bool isnull() const
returns true if this string is null
Definition wvstring.h:290
A type-safe version of WvMonikerBase that lets you provide create functions for object types other th...
Definition wvmoniker.h:62
WvString is an implementation of a simple and efficient printable-string class.
Definition wvstring.h:330