26 #include "yaml_node.h"
28 #include <core/exceptions/software.h>
29 #include <core/threading/mutex.h>
30 #include <utils/misc/string_split.h>
31 #include <yaml-cpp/exceptions.h>
43 root_ = std::make_shared<YamlConfigurationNode>();
68 std::shared_ptr<YamlConfigurationNode> n = root_->find(path);
69 return !n->has_children();
78 std::shared_ptr<YamlConfigurationNode> n = root_->find(path);
79 if (n->has_children()) {
83 return YamlConfigurationNode::Type::to_string(n->get_type());
101 get_value_as(std::shared_ptr<YamlConfigurationNode> root,
const char *path)
103 std::shared_ptr<YamlConfigurationNode> n = root->find(path);
104 if (n->has_children()) {
107 return n->get_value<T>();
117 template <
typename T>
118 static inline std::vector<T>
119 get_list(std::shared_ptr<YamlConfigurationNode> root,
const char *path)
121 std::shared_ptr<YamlConfigurationNode> n = root->find(path);
122 if (n->has_children()) {
125 return n->get_list<T>();
131 return get_value_as<float>(root_, path);
137 return get_value_as<unsigned int>(root_, path);
143 return get_value_as<int>(root_, path);
149 return get_value_as<bool>(root_, path);
155 return get_value_as<std::string>(root_, path);
161 return get_list<float>(root_, path);
164 std::vector<unsigned int>
167 return get_list<unsigned int>(root_, path);
173 return get_list<int>(root_, path);
179 return get_list<bool>(root_, path);
182 std::vector<std::string>
185 return get_list<std::string>(root_, path);
193 template <
typename T>
195 is_type(std::shared_ptr<YamlConfigurationNode> root,
const char *path)
197 std::shared_ptr<YamlConfigurationNode> n = root->find(path);
198 if (n->has_children()) {
201 return n->is_type<T>();
207 return is_type<float>(root_, path);
213 return is_type<unsigned int>(root_, path);
219 return is_type<int>(root_, path);
225 return is_type<bool>(root_, path);
231 return is_type<std::string>(root_, path);
237 std::shared_ptr<YamlConfigurationNode> n = root_->find(path);
238 if (n->has_children()) {
241 return (n->get_type() == YamlConfigurationNode::Type::SEQUENCE);
254 std::shared_ptr<YamlConfigurationNode> n = root_->find(path);
255 if (n->has_children()) {
258 return n->is_default();
270 std::shared_ptr<YamlConfigurationNode> n = root_->find(path);
271 if (n->has_children()) {
274 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes;
285 root_->set_value(path, f);
286 root_->set_default(path,
false);
292 root_->set_value(path, uint);
293 root_->set_default(path,
false);
299 root_->set_value(path, i);
300 root_->set_default(path,
false);
306 root_->set_value(path, b);
307 root_->set_default(path,
false);
313 root_->set_value(path, std::string(s));
314 root_->set_default(path,
false);
326 root_->set_list(path, f);
327 root_->set_default(path,
false);
333 root_->set_list(path, u);
334 root_->set_default(path,
false);
340 root_->set_list(path, i);
341 root_->set_default(path,
false);
347 root_->set_list(path, b);
348 root_->set_default(path,
false);
354 root_->set_list(path, s);
355 root_->set_default(path,
false);
361 root_->set_list(path, s);
362 root_->set_default(path,
false);
384 root_->set_value(path, f);
385 root_->set_default(path,
true);
391 root_->set_value(path, uint);
392 root_->set_default(path,
true);
398 root_->set_value(path, i);
399 root_->set_default(path,
true);
405 root_->set_value(path, b);
406 root_->set_default(path,
true);
412 root_->set_value(path, s);
413 root_->set_default(path,
true);
475 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes;
476 root_->enum_leafs(nodes);
486 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes;
487 root_->enum_leafs(nodes);
488 std::queue<std::string> delnodes;
489 std::map<std::string, std::shared_ptr<YamlConfigurationNode>>
::iterator n;
490 for (n = nodes.begin(); n != nodes.end(); ++n) {
491 if (!n->second->is_default()) {
492 delnodes.push(n->first);
495 while (!delnodes.empty()) {
496 nodes.erase(delnodes.front());
508 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes;
509 root_->enum_leafs(nodes);
510 std::queue<std::string> delnodes;
511 std::map<std::string, std::shared_ptr<YamlConfigurationNode>>
::iterator n;
512 for (n = nodes.begin(); n != nodes.end(); ++n) {
513 if (n->second->is_default()) {
514 delnodes.push(n->first);
517 while (!delnodes.empty()) {
518 nodes.erase(delnodes.front());
527 std::string tmp_path = path;
528 std::string::size_type tl = tmp_path.length();
529 if ((tl > 0) && (tmp_path[tl - 1] ==
'/')) {
530 tmp_path.resize(tl - 1);
533 std::shared_ptr<YamlConfigurationNode> n = root_->find(tmp_path.c_str());
534 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes;
535 n->enum_leafs(nodes, tmp_path);
547 std::shared_ptr<YamlConfigurationNode>
548 MemoryConfiguration::query(
const char *path)
const
551 return root_->find(pel_q);
Thrown if a config entry could not be found.
Iterator interface to iterate over config values.
Interface for configuration handling.
Base class for exceptions in Fawkes.
ValueIterator * search(const char *path)
Iterator with search results.
virtual void try_dump()
Try to dump configuration.
virtual bool is_list(const char *path)
Check if a value is a list.
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
virtual void copy(Configuration *copyconf)
Copies all values from the given configuration.
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
virtual bool exists(const char *path)
Check if a given value exists.
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
ValueIterator * iterator_hostspecific()
Get iterator over host-specific values.
virtual std::vector< unsigned int > get_uints(const char *path)
Get list of values from configuration which is of type unsigned int.
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
virtual void erase(const char *path)
Erase the given value from the configuration.
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
virtual void load(const char *file_path)
Load configuration.
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
ValueIterator * iterator()
Iterator for all values.
virtual bool is_string(const char *path)
Check if a value is of type string.
MemoryConfiguration()
Constructor.
ValueIterator * iterator_default()
Get iterator over default values.
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
virtual bool is_bool(const char *path)
Check if a value is of type bool.
virtual bool is_int(const char *path)
Check if a value is of type int.
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
virtual ~MemoryConfiguration()
Destructor.
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
void unlock()
Unlock the config.
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
bool try_lock()
Try to lock the config.
void lock()
Lock the config.
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
virtual bool is_default(const char *path)
Check if a value was read from the default config.
virtual std::string get_type(const char *path)
Get type of value at given path.
virtual bool is_float(const char *path)
Check if a value is of type float.
virtual float get_float(const char *path)
Get value from configuration which is of type float.
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
virtual std::string get_comment(const char *path)
Get comment of value at given path.
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
virtual int get_int(const char *path)
Get value from configuration which is of type int.
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
Mutex mutual exclusion lock.
bool try_lock()
Tries to lock the mutex.
void lock()
Lock this mutex.
void unlock()
Unlock the mutex.
Called method has not been implemented.
Iterator for YAML config trees.
Fawkes library namespace.
static std::queue< std::string > str_split_to_queue(const std::string &s, char delim='/')
Split string by delimiter.
static T get_value_as(std::shared_ptr< YamlConfigurationNode > root, const char *path)
Retrieve value casted to given type T.
static bool is_type(std::shared_ptr< YamlConfigurationNode > root, const char *path)
Check if value is of given type T.
static std::vector< T > get_list(std::shared_ptr< YamlConfigurationNode > root, const char *path)
Retrieve value casted to given type T.