WvStreams
Main Page
Modules
Classes
Files
File List
File Members
uniconf
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
53
WvString
UniFastRegetGen::get
(
const
UniConfKey
&key)
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
84
bool
UniFastRegetGen::exists
(
const
UniConfKey
&key)
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
92
bool
UniFastRegetGen::haschildren
(
const
UniConfKey
&key)
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
}
Generated on Wed Aug 28 2019 23:57:17 for WvStreams by
1.8.3.1