Fix several warnings that appear in gcc 4.3.2.
[wvstreams.git] / uniconf / unifastregetgen.cc
blob2eeab92ebcca8a4b9186a5f1c1bbaac01371515b
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>
9 #include "unifastregetgen.h"
10 #include "uniconftree.h"
11 #include "wvmoniker.h"
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)
17 return new UniFastRegetGen(wvcreate<IUniConfGen>(s, _obj));
20 static WvMoniker<IUniConfGen> reg("fast-reget", creator);
23 UniFastRegetGen::UniFastRegetGen(IUniConfGen *_inner) :
24 UniFilterGen(_inner),
25 tree(NULL)
27 tree = new UniConfValueTree(NULL, "/", UniFilterGen::get("/"));
31 UniFastRegetGen::~UniFastRegetGen()
33 if (tree)
35 delete tree;
36 tree = NULL;
41 void UniFastRegetGen::gencallback(const UniConfKey &key, WvStringParm value)
43 if (tree == NULL)
44 return; // initialising
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);
53 WvString UniFastRegetGen::get(const UniConfKey &key)
55 if (!tree)
57 wvassert(tree, "key: '%s'", key);
58 abort();
61 // Keys with trailing slashes can't have values set on them
62 if (key.hastrailingslash())
63 return WvString::null;
65 UniConfValueTree *t = tree->find(key);
66 if (!t)
68 UniConfKey parentkey(key.removelast());
69 get(parentkey); // guaranteed to create parent node
70 t = tree->find(parentkey);
71 assert(t);
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;
79 else
80 return t->value();
84 bool UniFastRegetGen::exists(const UniConfKey &key)
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();
92 bool UniFastRegetGen::haschildren(const UniConfKey &key)
94 if (!tree)
96 wvassert(tree, "key: '%s'", key);
97 abort();
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);