~ubuntu-branches/debian/sid/gsmartcontrol/sid

« back to all changes in this revision

Viewing changes to src/rconfig/rcmain.h

  • Committer: Package Import Robot
  • Author(s): Giuseppe Iuculano
  • Date: 2013-05-31 11:41:52 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130531114152-5ljhkuswwpt4kdwo
Tags: 0.8.7-1
* [314881d] Updated debian/watch
* [18ebada] Imported Upstream version 0.8.7
* [c2a1f1b] debian/rules: Provide build-arch and build-indep
* [d3036a4] Enabled Hardening Options
* [2edfb87] Refreshed patches and removed patches apllied upstream
* [ac3b953] Bump to standard versions 3.9.4
* [292c276] Remove quilt from depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**************************************************************************
2
2
 Copyright:
3
 
      (C) 2008 - 2011  Alexander Shaduri <ashaduri 'at' gmail.com>
 
3
      (C) 2008 - 2012  Alexander Shaduri <ashaduri 'at' gmail.com>
4
4
 License: See LICENSE_zlib.txt file
5
5
***************************************************************************/
 
6
/// \file
 
7
/// \author Alexander Shaduri
 
8
/// \ingroup rconfig
 
9
/// \weakgroup rconfig
 
10
/// @{
6
11
 
7
12
#ifndef RCONFIG_RCMAIN_H
8
13
#define RCONFIG_RCMAIN_H
31
36
 
32
37
 
33
38
 
 
39
/// Rconfig node type
34
40
typedef rmn::resource_node< rmn::ResourceDataAny<rmn::ResourceSyncPolicyNone> > node_t;
 
41
 
 
42
/// Rconfig strong reference-holding node pointer
35
43
typedef node_t::node_ptr node_ptr;
36
44
 
 
45
/// Locking policy for rconfig (thread-safe)
37
46
typedef hz::SyncPolicyMtDefault ConfigLockPolicy;
38
47
 
39
48
 
40
 
 
 
49
/// Config branch for serializable values ("/config")
41
50
static const char* const s_config_name = "config";
 
51
 
 
52
/// Config branch for default config values ("/default")
42
53
static const char* const s_default_name = "default";
43
54
 
44
55
 
50
61
// This gives us opportunity to get rid of the cpp file.
51
62
 
52
63
 
53
 
// specify the same type to get the same set of variables.
 
64
/// Static variable holder
54
65
template<typename Dummy>
55
66
struct NodeStaticHolder {
56
 
        static node_ptr root_node;  // "/"
57
 
        static node_ptr config_node;  // "/config"
58
 
        static node_ptr default_node;  // "/default"
59
 
        static ConfigLockPolicy::Mutex mutex;  // mutex for static variables above
 
67
        static node_ptr root_node;  ///< Node for "/"
 
68
        static node_ptr config_node;  ///< Node for "/config"
 
69
        static node_ptr default_node;  ///< Node for "/default"
 
70
        static ConfigLockPolicy::Mutex mutex;  ///< Mutex for static variables
60
71
};
61
72
 
62
73
// definitions
66
77
template<typename Dummy> ConfigLockPolicy::Mutex NodeStaticHolder<Dummy>::mutex;
67
78
 
68
79
 
69
 
typedef NodeStaticHolder<void> RootHolder;  // one (and only) instantiation.
70
 
 
71
 
 
72
 
 
73
 
// This is called automatically. This function is thread-safe.
 
80
// Specify the same template parameter to get the same set of variables.
 
81
typedef NodeStaticHolder<void> RootHolder;  ///< Holder for static variables (one (and only) instantiation).
 
82
 
 
83
 
 
84
 
 
85
/// Initialize the root node. This is called automatically.
 
86
/// This function is thread-safe.
74
87
inline bool init_root(bool do_lock = true)
75
88
{
76
89
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex, do_lock);
108
121
// To use them in a thread-safe environment, you have to lock their access manually.
109
122
 
110
123
 
 
124
/// Get the root node.
111
125
inline node_ptr get_root()
112
126
{
113
127
        if (!RootHolder::root_node)
116
130
}
117
131
 
118
132
 
 
133
/// Get the config branch node
119
134
inline node_ptr get_config_branch()
120
135
{
121
136
        if (!RootHolder::root_node)
124
139
}
125
140
 
126
141
 
 
142
/// Get the default branch node
127
143
inline node_ptr get_default_branch()
128
144
{
129
145
        if (!RootHolder::root_node)
140
156
// To use them in a thread-safe environment, you have to lock their access manually.
141
157
 
142
158
 
143
 
// Looks in /config, then /default. Useful for searching values (for reading).
144
 
// Note: if path is absolute (starts with "/"), then it's looked up in root ("/").
 
159
/// Get a node by path (relative or absolute). If relative, look in /config, then /default.
 
160
/// Useful for searching values (for reading).
145
161
inline node_ptr get_node(const std::string& path)
146
162
{
147
163
        if (node_t::is_abs_path(path)) {  // absolute path, start from root
166
182
 
167
183
 
168
184
 
169
 
// same as above, but only for "/config". additionally, you may create it.
170
 
// Note: if path is absolute (starts with "/"), then it's looked up in root ("/").
 
185
/// Get a node by path (relative or absolute). If relative, look in /config.
 
186
/// If the path doesn't exist, it can be created.
171
187
inline node_ptr get_config_node(std::string path, bool create_if_not_exists = false)
172
188
{
173
189
        if (node_t::is_abs_path(path)) {  // absolute path, start from root
199
215
 
200
216
 
201
217
 
202
 
// same as above, but only for "/default". additionally, you may create it.
203
 
// Note: if path is absolute (starts with "/"), then it's looked up in root ("/").
 
218
/// Get a node by path (relative or absolute). If relative, look in /default.
 
219
/// If the path doesn't exist, it can be created.
204
220
inline node_ptr get_default_node(std::string path, bool create_if_not_exists = false)
205
221
{
206
222
        if (node_t::is_abs_path(path)) {  // absolute path, start from root
236
252
// --------------------------------- (Thread-safe) Root node manipulation
237
253
 
238
254
 
239
 
// Note: This function will clear everything, including /config and /default.
 
255
/// Clear everything, including /config and /default.
240
256
inline void clear_root_all()
241
257
{
242
258
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex);
244
260
}
245
261
 
246
262
 
 
263
/// Clear /config
247
264
inline void clear_config_all()
248
265
{
249
266
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex);
252
269
}
253
270
 
254
271
 
 
272
/// Clear /default
255
273
inline void clear_default_all()
256
274
{
257
275
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex);
268
286
 
269
287
 
270
288
 
271
 
// clear data in "/config".
 
289
/// Clear the data in path (the node becomes empty), or "/config" if the path is relative.
272
290
inline void clear_data(const std::string& path)
273
291
{
274
292
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex);
279
297
}
280
298
 
281
299
 
282
 
// clear data in "/default".
 
300
/// Clear the data in path (the node becomes empty), or "/default" if the path is relative.
283
301
inline void clear_default_data(const std::string& path)
284
302
{
285
303
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex);
291
309
 
292
310
 
293
311
 
294
 
// Set data in "/config".
295
 
// Note: This will convert "const char*" to std::string!
 
312
/// Set the data in path, or "/config" if the path is relative.
 
313
/// Note: This will convert "const char*" data to std::string.
296
314
template<typename T> inline
297
315
bool set_data(const std::string& path, T data)
298
316
{
320
338
}
321
339
 
322
340
 
323
 
// Set data in "/default".
324
 
// Note: This will convert "const char*" to std::string!
 
341
 
 
342
/// Set the data in path, or "/default" if the path is relative.
 
343
/// Note: This will convert "const char*" data to std::string.
325
344
template<typename T> inline
326
345
bool set_default_data(const std::string& path, T data)
327
346
{
342
361
// Note: if path is absolute (starts with "/"), then it's looked up in root ("/").
343
362
 
344
363
 
345
 
// returns false if cast failed or no such node.
 
364
/// Get the data in path, or "/config" if the path is relative.
 
365
/// \return false if cast failed or no such node.
346
366
template<typename T> inline
347
367
bool get_config_data(const std::string& path, T& put_it_here)
348
368
{
356
376
 
357
377
 
358
378
 
359
 
// returns false if cast failed or no such node.
 
379
/// Get the data in path, or "/default" if the path is relative.
 
380
/// \return false if cast failed or no such node.
360
381
template<typename T> inline
361
382
bool get_default_data(const std::string& path, T& put_it_here)
362
383
{
378
399
 
379
400
 
380
401
 
 
402
/// Check if data at path is empty. If the path is relative, look in /config, then /default.
381
403
inline bool data_is_empty(const std::string& path)
382
404
{
383
405
        ConfigLockPolicy::ScopedLock locker(RootHolder::mutex);
389
411
}
390
412
 
391
413
 
392
 
// this function works only if either RTTI or type tracking is enabled
393
414
#if !(defined DISABLE_RTTI && DISABLE_RTTI) \
394
415
                || (defined RMN_TYPE_TRACKING && RMN_TYPE_TRACKING)
 
416
 
 
417
/// Check if data at path is of type \c T. If the path is relative, look in /config, then /default.
 
418
/// This function works only if either RTTI or type tracking is enabled
395
419
template<typename T> inline
396
420
bool data_is_type(const std::string& path)
397
421
{
402
426
                return false;  // no such node
403
427
        return node->data_is_type<T>();
404
428
}
 
429
 
405
430
#endif
406
431
 
407
432
 
408
 
// returns false if cast failed or no such node.
 
433
 
 
434
/// Get data at path. If the path is relative, look in /config, then /default.
 
435
/// \return false if cast failed or no such node.
409
436
template<typename T> inline
410
437
bool get_data(const std::string& path, T& put_it_here)
411
438
{
418
445
}
419
446
 
420
447
 
421
 
// This function throws if:
422
 
//      * no such node (rmn::no_such_node);
423
 
//      * data empty (rmn::empty_data_retrieval);
424
 
//      * type mismatch (rmn::type_mismatch).
 
448
 
 
449
/// Get data at path. If the path is relative, look in /config, then /default.
 
450
/// \throw rmn::no_such_node No such node
 
451
/// \throw rmn::empty_data_retrieval Data is empty
 
452
/// \throw rmn::type_mismatch Type mismatch
425
453
template<typename T> inline
426
454
T get_data(const std::string& path)
427
455
{
434
462
}
435
463
 
436
464
 
437
 
// more loose conversion - can convert between C++ built-in types and std::string.
 
465
 
 
466
/// Similar to get_data(), but with looser conversion - can convert between
 
467
/// C++ built-in types and std::string. Uses hz::any_convert<>.
 
468
/// \return false if casting failed, or empty or invalid type.
438
469
template<typename T> inline
439
470
bool convert_data(const std::string& path, T& put_it_here)  // returns false if cast failed
440
471
{
447
478
}
448
479
 
449
480
 
450
 
// This function throws if:
451
 
//      * no such node (rmn::no_such_node);
452
 
//      * data empty (rmn::empty_data_retrieval);
453
 
//      * type conversion error (rmn::type_convert_error).
 
481
/// Similar to get_data(), but with looser conversion - can convert between
 
482
/// C++ built-in types and std::string. Uses hz::any_convert<>.
 
483
/// \throw rmn::no_such_node No such node
 
484
/// \throw rmn::empty_data_retrieval Data is empty
 
485
/// \throw rmn::type_mismatch Type mismatch
454
486
template<typename T> inline
455
487
T convert_data(const std::string& path)
456
488
{
472
504
 
473
505
 
474
506
#endif
 
507
 
 
508
/// @}