#ifndef ATOOLS_Org_Yaml_Reader_H #define ATOOLS_Org_Yaml_Reader_H #include "ATOOLS/Org/Exception.H" #include "ATOOLS/Org/Message.H" #include "ATOOLS/YAML/yaml-cpp/yaml.h" #include namespace ATOOLS { class Settings_Keys; class Yaml_Reader { public: // constructors Yaml_Reader(); Yaml_Reader(std::istream&); Yaml_Reader(const std::string& path, const std::string& filename); std::vector GetKeys(const Settings_Keys& scopekeys); bool IsList(const Settings_Keys& scopekeys); bool IsMap(const Settings_Keys& scopekeys); size_t GetItemsCount(const Settings_Keys& scopekeys); bool IsParameterCustomised(const Settings_Keys& scopekeys); template T GetScalar(const Settings_Keys& keys) { T value; const auto node = NodeForKeys(keys); if (!node.IsNull()) value = node.as(); return value; } template std::vector GetVector(const Settings_Keys& keys) { std::vector values; const auto node = NodeForKeys(keys); if (node.IsNull()) return values; // auto-wrap scalars in a vector if (node.Type() == SHERPA_YAML::NodeType::Scalar) { values.push_back(node.as()); } else { values = node.as>(); } return values; } template std::vector > GetMatrix(const Settings_Keys& keys) { std::vector > values; const auto node = NodeForKeys(keys); if (node.IsNull()) return values; if (node.Type() == SHERPA_YAML::NodeType::Scalar) { // auto-wrap scalar const auto value = node.as(); values.push_back(std::vector{value}); } else if (node.Type() == SHERPA_YAML::NodeType::Sequence) { auto ismatrix = true; auto isvector = true; for (const auto& subnode : node) { if (!subnode.IsSequence()) ismatrix = false; if (subnode.Type() != SHERPA_YAML::NodeType::Scalar) isvector = false; if (!isvector && !ismatrix) break; } if (ismatrix) { for (const auto& subnode : node) values.push_back(subnode.as>()); } else if (isvector) { // auto-wrap vector auto valuesvec = node.as>(); values.push_back(valuesvec); } } return values; } protected: void Parse(std::istream&); private: SHERPA_YAML::Node m_node; SHERPA_YAML::Node NodeForKeys(const Settings_Keys& scopekeys); }; } #endif