File indexing completed on 2025-04-19 09:09:54
0001 #ifndef ATOOLS_Org_Yaml_Reader_H
0002 #define ATOOLS_Org_Yaml_Reader_H
0003
0004 #include "ATOOLS/Org/Exception.H"
0005 #include "ATOOLS/Org/Message.H"
0006 #include "ATOOLS/Org/Settings_Keys.H"
0007 #include "ATOOLS/YAML/yaml-cpp/yaml.h"
0008 #include <iostream>
0009
0010 namespace ATOOLS {
0011
0012 class Settings_Keys;
0013
0014 class Yaml_Reader {
0015
0016 public:
0017
0018
0019 Yaml_Reader(const std::string& name="");
0020 Yaml_Reader(std::istream&);
0021 Yaml_Reader(const std::string& path, const std::string& filename);
0022
0023 std::string Name() const;
0024
0025 std::vector<Settings_Keys> AllSettingsKeys();
0026
0027 std::vector<std::string> GetKeys(const Settings_Keys& scopekeys);
0028 bool IsScalar(const Settings_Keys& scopekeys);
0029 bool IsList(const Settings_Keys& scopekeys);
0030 bool IsMap(const Settings_Keys& scopekeys);
0031 size_t GetItemsCount(const Settings_Keys& scopekeys);
0032
0033 bool IsParameterCustomised(const Settings_Keys& scopekeys);
0034
0035 std::vector<std::string> GetFlattenedStringVectorWithDelimiters(
0036 const Settings_Keys&,
0037 const std::string& open_delimiter="{{",
0038 const std::string& close_delimiter="}}");
0039
0040 template <typename T>
0041 T GetScalar(const Settings_Keys& keys)
0042 {
0043 T value;
0044 const auto node = NodeForKeys(keys);
0045 if (!node.IsNull())
0046 value = node.as<T>();
0047 return value;
0048 }
0049
0050 template <typename T>
0051 std::vector<T> GetVector(const Settings_Keys& keys)
0052 {
0053 std::vector<T> values;
0054 const auto node = NodeForKeys(keys);
0055 if (node.IsNull())
0056 return values;
0057
0058
0059 if (node.Type() == SHERPA_YAML::NodeType::Scalar) {
0060 values.push_back(node.as<T>());
0061 } else {
0062 values = node.as<std::vector<T>>();
0063 }
0064
0065 return values;
0066 }
0067
0068 template <typename T>
0069 std::vector<std::vector<T> > GetMatrix(const Settings_Keys& keys)
0070 {
0071 std::vector<std::vector<T> > values;
0072 const auto node = NodeForKeys(keys);
0073 if (node.IsNull())
0074 return values;
0075
0076 if (node.Type() == SHERPA_YAML::NodeType::Scalar) {
0077
0078 const auto value = node.as<T>();
0079 values.push_back(std::vector<T>{value});
0080 } else if (node.Type() == SHERPA_YAML::NodeType::Sequence) {
0081 auto ismatrix = true;
0082 auto isvector = true;
0083 for (const auto& subnode : node) {
0084 if (subnode.IsSequence()) {
0085 for (const auto& subsubnode : subnode) {
0086 if (!subsubnode.IsScalar())
0087 THROW(invalid_input,
0088 "Attempting to read a more than 2-dimensional setting: "
0089 + keys.Name());
0090 }
0091 } else {
0092 ismatrix = false;
0093 }
0094 if (!subnode.IsScalar())
0095 isvector = false;
0096 if (!isvector && !ismatrix)
0097 break;
0098 }
0099 if (ismatrix) {
0100 for (const auto& subnode : node)
0101 values.push_back(subnode.as<std::vector<T>>());
0102 } else if (isvector) {
0103
0104 auto valuesvec = node.as<std::vector<T>>();
0105 values.push_back(valuesvec);
0106 }
0107 }
0108 return values;
0109 }
0110
0111 protected:
0112
0113 void Parse(std::istream&);
0114
0115 std::string m_name;
0116
0117 private:
0118
0119 std::vector<SHERPA_YAML::Node> m_nodes;
0120
0121 SHERPA_YAML::Node NodeForKeys(const Settings_Keys& scopekeys);
0122 SHERPA_YAML::Node NodeForKeysInNode(const Settings_Keys& scopekeys,
0123 const SHERPA_YAML::Node&);
0124
0125 void AddSettingsKeys(
0126 std::vector<Settings_Keys>&,
0127 Settings_Keys&,
0128 const SHERPA_YAML::Node&);
0129
0130 };
0131
0132 }
0133
0134 #endif