2 * Worldvisions Weaver Software:
3 * Copyright (C) 2002-2005 Net Integration Technologies, Inc.
5 * A lightweight but slightly dangerous version of UniCacheGen.
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
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
) :
27 tree
= new UniConfValueTree(NULL
, "/", UniFilterGen::get("/"));
31 UniFastRegetGen::~UniFastRegetGen()
41 void UniFastRegetGen::gencallback(const UniConfKey
&key
, WvStringParm value
)
44 return; // initialising
46 UniConfValueTree
*t
= tree
->find(key
);
47 if (t
) // never previously retrieved; don't cache it
49 UniFilterGen::gencallback(key
, value
);
53 WvString
UniFastRegetGen::get(const UniConfKey
&key
)
57 wvassert(tree
, "key: '%s'", key
);
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
);
68 UniConfKey
parentkey(key
.removelast());
69 get(parentkey
); // guaranteed to create parent node
70 t
= tree
->find(parentkey
);
74 if (!t
->value().isnull()) // if parent is null, child guaranteed null
75 value
= UniFilterGen::get(key
);
76 new UniConfValueTree(t
, key
.last(), 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
)
96 wvassert(tree
, "key: '%s'", key
);
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
);