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()) {
80 throw ConfigEntryNotFoundException(path);
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()) {
123 throw ConfigEntryNotFoundException(path);
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();
259 }
catch (ConfigEntryNotFoundException &e) {
266 Configuration::ValueIterator *
270 std::shared_ptr<YamlConfigurationNode> n = root_->find(path);
271 if (n->has_children()) {
274 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes;
277 }
catch (ConfigEntryNotFoundException &e) {
278 return new YamlConfiguration::YamlValueIterator();
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());
499 return new YamlConfiguration::YamlValueIterator(nodes);
505 Configuration::ValueIterator *
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());
521 return new YamlConfiguration::YamlValueIterator(nodes);
524 Configuration::ValueIterator *
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);
537 }
catch (Exception &e) {
538 return new YamlConfiguration::YamlValueIterator();
547 std::shared_ptr<YamlConfigurationNode>
548 MemoryConfiguration::query(
const char *path)
const
551 return root_->find(pel_q);