24 #include <config/memory.h> 26 #include "yaml_node.h" 28 #include <core/threading/mutex.h> 29 #include <core/exceptions/software.h> 30 #include <utils/misc/string_split.h> 32 #include <yaml-cpp/exceptions.h> 45 MemoryConfiguration::MemoryConfiguration()
47 root_ =
new YamlConfigurationNode();
52 MemoryConfiguration::~MemoryConfiguration()
62 MemoryConfiguration::load(
const char *file_path)
74 MemoryConfiguration::exists(
const char *path)
77 YamlConfigurationNode *n = root_->find(path);
78 return ! n->has_children();
86 MemoryConfiguration::get_type(
const char *path)
88 YamlConfigurationNode *n = root_->find(path);
89 if (n->has_children()) {
93 return YamlConfigurationNode::Type::to_string(n->get_type());
97 MemoryConfiguration::get_comment(
const char *path)
114 YamlConfigurationNode *n = root->find(path);
115 if (n->has_children()) {
118 return n->get_value<T>();
129 static inline std::vector<T>
130 get_list(YamlConfigurationNode *root,
const char *path)
132 YamlConfigurationNode *n = root->find(path);
133 if (n->has_children()) {
136 return n->get_list<T>();
141 MemoryConfiguration::get_float(
const char *path)
143 return get_value_as<float>(root_, path);
147 MemoryConfiguration::get_uint(
const char *path)
149 return get_value_as<unsigned int>(root_, path);
153 MemoryConfiguration::get_int(
const char *path)
155 return get_value_as<int>(root_, path);
159 MemoryConfiguration::get_bool(
const char *path)
161 return get_value_as<bool>(root_, path);
165 MemoryConfiguration::get_string(
const char *path)
167 return get_value_as<std::string>(root_, path);
172 MemoryConfiguration::get_floats(
const char *path)
174 return get_list<float>(root_, path);
178 std::vector<unsigned int>
179 MemoryConfiguration::get_uints(
const char *path)
181 return get_list<unsigned int>(root_, path);
186 MemoryConfiguration::get_ints(
const char *path)
188 return get_list<int>(root_, path);
192 MemoryConfiguration::get_bools(
const char *path)
194 return get_list<bool>(root_, path);
197 std::vector<std::string>
198 MemoryConfiguration::get_strings(
const char *path)
200 return get_list<std::string>(root_, path);
211 is_type(YamlConfigurationNode *root,
const char *path)
213 YamlConfigurationNode *n = root->find(path);
214 if (n->has_children()) {
217 return n->is_type<T>();
222 MemoryConfiguration::is_float(
const char *path)
224 return is_type<float>(root_, path);
228 MemoryConfiguration::is_uint(
const char *path)
230 return is_type<unsigned int>(root_, path);
234 MemoryConfiguration::is_int(
const char *path)
236 return is_type<int>(root_, path);
240 MemoryConfiguration::is_bool(
const char *path)
242 return is_type<bool>(root_, path);
246 MemoryConfiguration::is_string(
const char *path)
248 return is_type<std::string>(root_, path);
253 MemoryConfiguration::is_list(
const char *path)
255 YamlConfigurationNode *n = root_->find(path);
256 if (n->has_children()) {
259 return (n->get_type() == YamlConfigurationNode::Type::SEQUENCE);
264 MemoryConfiguration::get_default_comment(
const char *path)
270 MemoryConfiguration::is_default(
const char *path)
273 YamlConfigurationNode *n = root_->find(path);
274 if (n->has_children()) {
277 return n->is_default();
287 MemoryConfiguration::get_value(
const char *path)
290 YamlConfigurationNode *n = root_->find(path);
291 if (n->has_children()) {
294 std::map<std::string, YamlConfigurationNode *> nodes;
304 MemoryConfiguration::set_float(
const char *path,
float f)
306 root_->set_value(path, f);
307 root_->set_default(path,
false);
311 MemoryConfiguration::set_uint(
const char *path,
unsigned int uint)
313 root_->set_value(path, uint);
314 root_->set_default(path,
false);
318 MemoryConfiguration::set_int(
const char *path,
int i)
320 root_->set_value(path, i);
321 root_->set_default(path,
false);
325 MemoryConfiguration::set_bool(
const char *path,
bool b)
327 root_->set_value(path, b);
328 root_->set_default(path,
false);
332 MemoryConfiguration::set_string(
const char *path,
const char *s)
334 root_->set_value(path, std::string(s));
335 root_->set_default(path,
false);
340 MemoryConfiguration::set_string(
const char *path, std::string &s)
342 set_string(path, s.c_str());
346 MemoryConfiguration::set_floats(
const char *path, std::vector<float> &f)
348 root_->set_list(path, f);
349 root_->set_default(path,
false);
353 MemoryConfiguration::set_uints(
const char *path, std::vector<unsigned int> &u)
355 root_->set_list(path, u);
356 root_->set_default(path,
false);
360 MemoryConfiguration::set_ints(
const char *path, std::vector<int> &i)
362 root_->set_list(path, i);
363 root_->set_default(path,
false);
367 MemoryConfiguration::set_bools(
const char *path, std::vector<bool> &b)
369 root_->set_list(path, b);
370 root_->set_default(path,
false);
374 MemoryConfiguration::set_strings(
const char *path, std::vector<std::string> &s)
376 root_->set_list(path, s);
377 root_->set_default(path,
false);
381 MemoryConfiguration::set_strings(
const char *path, std::vector<const char *> &s)
383 root_->set_list(path, s);
384 root_->set_default(path,
false);
388 MemoryConfiguration::set_comment(
const char *path,
const char *comment)
393 MemoryConfiguration::set_comment(
const char *path, std::string &comment)
398 MemoryConfiguration::erase(
const char *path)
404 MemoryConfiguration::set_default_float(
const char *path,
float f)
406 root_->set_value(path, f);
407 root_->set_default(path,
true);
411 MemoryConfiguration::set_default_uint(
const char *path,
unsigned int uint)
413 root_->set_value(path, uint);
414 root_->set_default(path,
true);
418 MemoryConfiguration::set_default_int(
const char *path,
int i)
420 root_->set_value(path, i);
421 root_->set_default(path,
true);
425 MemoryConfiguration::set_default_bool(
const char *path,
bool b)
427 root_->set_value(path, b);
428 root_->set_default(path,
true);
432 MemoryConfiguration::set_default_string(
const char *path,
435 root_->set_value(path, s);
436 root_->set_default(path,
true);
440 MemoryConfiguration::set_default_string(
const char *path, std::string &s)
442 set_default_string(path, s.c_str());
446 MemoryConfiguration::set_default_comment(
const char *path,
const char *comment)
451 MemoryConfiguration::set_default_comment(
const char *path, std::string &comment)
457 MemoryConfiguration::erase_default(
const char *path)
467 MemoryConfiguration::lock()
478 MemoryConfiguration::try_lock()
480 return mutex_->try_lock();
487 MemoryConfiguration::unlock()
494 MemoryConfiguration::try_dump()
500 MemoryConfiguration::iterator()
502 std::map<std::string, YamlConfigurationNode *> nodes;
503 root_->enum_leafs(nodes);
512 MemoryConfiguration::iterator_default()
514 std::map<std::string, YamlConfigurationNode *> nodes;
515 root_->enum_leafs(nodes);
516 std::queue<std::string> delnodes;
517 std::map<std::string, YamlConfigurationNode *>::iterator n;
518 for (n = nodes.begin(); n != nodes.end(); ++n) {
519 if (! n->second->is_default()) {
520 delnodes.push(n->first);
523 while (!delnodes.empty()) {
524 nodes.erase(delnodes.front());
534 MemoryConfiguration::iterator_hostspecific()
536 std::map<std::string, YamlConfigurationNode *> nodes;
537 root_->enum_leafs(nodes);
538 std::queue<std::string> delnodes;
539 std::map<std::string, YamlConfigurationNode *>::iterator n;
540 for (n = nodes.begin(); n != nodes.end(); ++n) {
541 if (n->second->is_default()) {
542 delnodes.push(n->first);
545 while (!delnodes.empty()) {
546 nodes.erase(delnodes.front());
555 MemoryConfiguration::search(
const char *path)
557 std::string tmp_path = path;
558 std::string::size_type tl = tmp_path.length();
559 if ((tl > 0) && (tmp_path[tl - 1] ==
'/')) {
560 tmp_path.resize(tl - 1);
563 YamlConfigurationNode *n = root_->find(tmp_path.c_str());
564 std::map<std::string, YamlConfigurationNode *> nodes;
565 n->enum_leafs(nodes, tmp_path);
577 YamlConfigurationNode *
578 MemoryConfiguration::query(
const char *path)
const 580 std::queue<std::string> pel_q = str_split_to_queue(path);
581 return root_->find(pel_q);
Fawkes library namespace.
Called method has not been implemented.
Thrown if a config entry could not be found.
Iterator for YAML config trees.
static std::vector< T > get_list(YamlConfigurationNode *root, const char *path)
Retrieve value casted to given type T.
Base class for exceptions in Fawkes.
static T get_value_as(YamlConfigurationNode *root, const char *path)
Retrieve value casted to given type T.
Iterator interface to iterate over config values.
Mutex mutual exclusion lock.
static bool is_type(YamlConfigurationNode *root, const char *path)
Check if value is of given type T.
Interface for configuration handling.