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> 47 root_ =
new YamlConfigurationNode();
77 YamlConfigurationNode *n = root_->find(path);
78 return ! n->has_children();
88 YamlConfigurationNode *n = root_->find(path);
89 if (n->has_children()) {
93 return YamlConfigurationNode::Type::to_string(n->get_type());
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>();
143 return get_value_as<float>(root_, path);
149 return get_value_as<unsigned int>(root_, path);
155 return get_value_as<int>(root_, path);
161 return get_value_as<bool>(root_, path);
167 return get_value_as<std::string>(root_, path);
174 return get_list<float>(root_, path);
178 std::vector<unsigned int>
181 return get_list<unsigned int>(root_, path);
188 return get_list<int>(root_, path);
194 return get_list<bool>(root_, path);
197 std::vector<std::string>
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>();
224 return is_type<float>(root_, path);
230 return is_type<unsigned int>(root_, path);
236 return is_type<int>(root_, path);
242 return is_type<bool>(root_, path);
248 return is_type<std::string>(root_, path);
255 YamlConfigurationNode *n = root_->find(path);
256 if (n->has_children()) {
259 return (n->get_type() == YamlConfigurationNode::Type::SEQUENCE);
273 YamlConfigurationNode *n = root_->find(path);
274 if (n->has_children()) {
277 return n->is_default();
290 YamlConfigurationNode *n = root_->find(path);
291 if (n->has_children()) {
294 std::map<std::string, YamlConfigurationNode *> nodes;
306 root_->set_value(path, f);
307 root_->set_default(path,
false);
313 root_->set_value(path, uint);
314 root_->set_default(path,
false);
320 root_->set_value(path, i);
321 root_->set_default(path,
false);
327 root_->set_value(path, b);
328 root_->set_default(path,
false);
334 root_->set_value(path, std::string(s));
335 root_->set_default(path,
false);
348 root_->set_list(path, f);
349 root_->set_default(path,
false);
355 root_->set_list(path, u);
356 root_->set_default(path,
false);
362 root_->set_list(path, i);
363 root_->set_default(path,
false);
369 root_->set_list(path, b);
370 root_->set_default(path,
false);
376 root_->set_list(path, s);
377 root_->set_default(path,
false);
383 root_->set_list(path, s);
384 root_->set_default(path,
false);
406 root_->set_value(path, f);
407 root_->set_default(path,
true);
413 root_->set_value(path, uint);
414 root_->set_default(path,
true);
420 root_->set_value(path, i);
421 root_->set_default(path,
true);
427 root_->set_value(path, b);
428 root_->set_default(path,
true);
435 root_->set_value(path, s);
436 root_->set_default(path,
true);
502 std::map<std::string, YamlConfigurationNode *> nodes;
503 root_->enum_leafs(nodes);
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());
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());
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 581 return root_->find(pel_q);
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
ValueIterator * iterator()
Iterator for all values.
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
virtual void copy(Configuration *copyconf)
Copies all values from the given configuration.
virtual bool is_float(const char *path)
Check if a value is of type float.
void lock()
Lock the config.
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
virtual void try_dump()
Try to dump configuration.
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
Fawkes library namespace.
void unlock()
Unlock the mutex.
virtual bool is_string(const char *path)
Check if a value is of type string.
Called method has not been implemented.
Thrown if a config entry could not be found.
bool try_lock()
Try to lock the config.
virtual bool is_bool(const char *path)
Check if a value is of type bool.
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
virtual std::vector< unsigned int > get_uints(const char *path)
Get list of values from configuration which is of type unsigned int.
static std::queue< std::string > str_split_to_queue(const std::string &s, char delim='/')
Split string by delimiter.
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Iterator for YAML config trees.
MemoryConfiguration()
Constructor.
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
virtual int get_int(const char *path)
Get value from configuration which is of type int.
virtual float get_float(const char *path)
Get value from configuration which is of type float.
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
static std::vector< T > get_list(YamlConfigurationNode *root, const char *path)
Retrieve value casted to given type T.
virtual bool is_int(const char *path)
Check if a value is of type int.
virtual bool is_default(const char *path)
Check if a value was read from the default config.
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
virtual bool exists(const char *path)
Check if a given value exists.
Base class for exceptions in Fawkes.
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
virtual std::string get_comment(const char *path)
Get comment of value at given path.
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
bool try_lock()
Tries to lock the mutex.
virtual std::string get_type(const char *path)
Get type of value at given path.
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
ValueIterator * search(const char *path)
Iterator with search results.
void unlock()
Unlock the config.
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
static T get_value_as(YamlConfigurationNode *root, const char *path)
Retrieve value casted to given type T.
Iterator interface to iterate over config values.
ValueIterator * iterator_hostspecific()
Get iterator over host-specific values.
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
void lock()
Lock this mutex.
virtual void load(const char *file_path)
Load configuration.
virtual ~MemoryConfiguration()
Destructor.
Mutex mutual exclusion lock.
ValueIterator * iterator_default()
Get iterator over default values.
static bool is_type(YamlConfigurationNode *root, const char *path)
Check if value is of given type T.
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
virtual bool is_list(const char *path)
Check if a value is a list.
Interface for configuration handling.
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
virtual void erase(const char *path)
Erase the given value from the configuration.
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.