1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/**
* @file llregistry.h
* @brief template classes for registering name, value pairs in nested scopes, statically, etc.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#ifndef LL_LLREGISTRY_H
#define LL_LLREGISTRY_H
#include <list>
#include <boost/type_traits.hpp>
#include "llsingleton.h"
template <typename T>
class LLRegistryDefaultComparator
{
bool operator()(const T& lhs, const T& rhs) { return lhs < rhs; }
};
template <typename KEY, typename VALUE, typename COMPARATOR = LLRegistryDefaultComparator<KEY> >
class LLRegistry
{
public:
typedef LLRegistry<KEY, VALUE, COMPARATOR> registry_t;
typedef typename boost::add_reference<typename boost::add_const<KEY>::type>::type ref_const_key_t;
typedef typename boost::add_reference<typename boost::add_const<VALUE>::type>::type ref_const_value_t;
typedef typename boost::add_reference<VALUE>::type ref_value_t;
typedef typename boost::add_pointer<typename boost::add_const<VALUE>::type>::type ptr_const_value_t;
typedef typename boost::add_pointer<VALUE>::type ptr_value_t;
class Registrar
{
friend class LLRegistry<KEY, VALUE, COMPARATOR>;
public:
typedef typename std::map<KEY, VALUE> registry_map_t;
bool add(ref_const_key_t key, ref_const_value_t value)
{
if (mMap.insert(std::make_pair(key, value)).second == false)
{
llwarns << "Tried to register " << key << " but it was already registered!" << llendl;
return false;
}
return true;
}
void remove(ref_const_key_t key)
{
mMap.erase(key);
}
void replace(ref_const_key_t key, ref_const_value_t value)
{
mMap[key] = value;
}
typename registry_map_t::const_iterator beginItems() const
{
return mMap.begin();
}
typename registry_map_t::const_iterator endItems() const
{
return mMap.end();
}
protected:
ptr_value_t getValue(ref_const_key_t key)
{
typename registry_map_t::iterator found_it = mMap.find(key);
if (found_it != mMap.end())
{
return &(found_it->second);
}
return NULL;
}
ptr_const_value_t getValue(ref_const_key_t key) const
{
typename registry_map_t::const_iterator found_it = mMap.find(key);
if (found_it != mMap.end())
{
return &(found_it->second);
}
return NULL;
}
// if the registry is used to store pointers, and null values are valid entries
// then use this function to check the existence of an entry
bool exists(ref_const_key_t key) const
{
return mMap.find(key) != mMap.end();
}
bool empty() const
{
return mMap.empty();
}
protected:
// use currentRegistrar() or defaultRegistrar()
Registrar() {}
~Registrar() {}
private:
registry_map_t mMap;
};
typedef typename std::list<Registrar*> scope_list_t;
typedef typename std::list<Registrar*>::iterator scope_list_iterator_t;
typedef typename std::list<Registrar*>::const_iterator scope_list_const_iterator_t;
LLRegistry()
{}
~LLRegistry() {}
ptr_value_t getValue(ref_const_key_t key)
{
for(scope_list_iterator_t it = mActiveScopes.begin();
it != mActiveScopes.end();
++it)
{
ptr_value_t valuep = (*it)->getValue(key);
if (valuep != NULL) return valuep;
}
return mDefaultRegistrar.getValue(key);
}
ptr_const_value_t getValue(ref_const_key_t key) const
{
for(scope_list_const_iterator_t it = mActiveScopes.begin();
it != mActiveScopes.end();
++it)
{
ptr_value_t valuep = (*it)->getValue(key);
if (valuep != NULL) return valuep;
}
return mDefaultRegistrar.getValue(key);
}
bool exists(ref_const_key_t key) const
{
for(scope_list_const_iterator_t it = mActiveScopes.begin();
it != mActiveScopes.end();
++it)
{
if ((*it)->exists(key)) return true;
}
return mDefaultRegistrar.exists(key);
}
bool empty() const
{
for(scope_list_const_iterator_t it = mActiveScopes.begin();
it != mActiveScopes.end();
++it)
{
if (!(*it)->empty()) return false;
}
return mDefaultRegistrar.empty();
}
Registrar& defaultRegistrar()
{
return mDefaultRegistrar;
}
const Registrar& defaultRegistrar() const
{
return mDefaultRegistrar;
}
Registrar& currentRegistrar()
{
if (!mActiveScopes.empty())
{
return *mActiveScopes.front();
}
return mDefaultRegistrar;
}
const Registrar& currentRegistrar() const
{
if (!mActiveScopes.empty())
{
return *mActiveScopes.front();
}
return mDefaultRegistrar;
}
protected:
void addScope(Registrar* scope)
{
// newer scopes go up front
mActiveScopes.insert(mActiveScopes.begin(), scope);
}
void removeScope(Registrar* scope)
{
// O(N) but should be near the beggining and N should be small and this is safer than storing iterators
scope_list_iterator_t iter = std::find(mActiveScopes.begin(), mActiveScopes.end(), scope);
if (iter != mActiveScopes.end())
{
mActiveScopes.erase(iter);
}
}
private:
scope_list_t mActiveScopes;
Registrar mDefaultRegistrar;
};
template <typename KEY, typename VALUE, typename DERIVED_TYPE, typename COMPARATOR = LLRegistryDefaultComparator<KEY> >
class LLRegistrySingleton
: public LLRegistry<KEY, VALUE, COMPARATOR>,
public LLSingleton<DERIVED_TYPE>
{
friend class LLSingleton<DERIVED_TYPE>;
public:
typedef LLRegistry<KEY, VALUE, COMPARATOR> registry_t;
typedef const KEY& ref_const_key_t;
typedef const VALUE& ref_const_value_t;
typedef VALUE* ptr_value_t;
typedef const VALUE* ptr_const_value_t;
typedef LLSingleton<DERIVED_TYPE> singleton_t;
class ScopedRegistrar : public registry_t::Registrar
{
public:
ScopedRegistrar(bool push_scope = true)
{
if (push_scope)
{
pushScope();
}
}
~ScopedRegistrar()
{
if (!singleton_t::destroyed())
{
popScope();
}
}
void pushScope()
{
singleton_t::instance().addScope(this);
}
void popScope()
{
singleton_t::instance().removeScope(this);
}
ptr_value_t getValueFromScope(ref_const_key_t key)
{
return getValue(key);
}
ptr_const_value_t getValueFromScope(ref_const_key_t key) const
{
return getValue(key);
}
private:
typename std::list<typename registry_t::Registrar*>::iterator mListIt;
};
class StaticRegistrar : public registry_t::Registrar
{
public:
virtual ~StaticRegistrar() {}
StaticRegistrar(ref_const_key_t key, ref_const_value_t value)
{
singleton_t::instance().mStaticScope->add(key, value);
}
};
// convenience functions
typedef typename LLRegistry<KEY, VALUE, COMPARATOR>::Registrar& ref_registrar_t;
static ref_registrar_t currentRegistrar()
{
return singleton_t::instance().registry_t::currentRegistrar();
}
static ref_registrar_t defaultRegistrar()
{
return singleton_t::instance().registry_t::defaultRegistrar();
}
static ptr_value_t getValue(ref_const_key_t key)
{
return singleton_t::instance().registry_t::getValue(key);
}
protected:
// DERIVED_TYPE needs to derive from LLRegistrySingleton
LLRegistrySingleton()
: mStaticScope(NULL)
{}
virtual void initSingleton()
{
mStaticScope = new ScopedRegistrar();
}
virtual ~LLRegistrySingleton()
{
delete mStaticScope;
}
private:
ScopedRegistrar* mStaticScope;
};
// helper macro for doing static registration
#define GLUED_TOKEN(x, y) x ## y
#define GLUE_TOKENS(x, y) GLUED_TOKEN(x, y)
#define LLREGISTER_STATIC(REGISTRY, KEY, VALUE) static REGISTRY::StaticRegistrar GLUE_TOKENS(reg, __COUNTER__)(KEY, VALUE);
#endif
|