gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
yaml_options.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
32 
33 #ifndef __yaml_options_hpp__
34 #define __yaml_options_hpp__
35 
36 #include <vector>
37 #include <sstream>
38 
43 
44 namespace Gambit
45 {
46 
50  class Options
51  {
52 
53  public:
54 
56  Options() {}
57 
59  Options(const YAML::Node &options) : options(options) {}
60 
62  Options(YAML::Node &&options) : options(std::move(options)) {}
63 
66  template <typename... args>
67  bool hasKey(const args&... keys) const
68  {
69  return getVariadicNode(options, keys...);
70  }
71 
72  template<typename TYPE, typename... args>
73  TYPE getValue(const args&... keys) const
74  {
75  const YAML::Node node = getVariadicNode(options, keys...);
76  TYPE result;
77  if (not node)
78  {
79  std::ostringstream os;
80  os << "No options entry for [" << stringifyVariadic(keys...) << "]\n Node contents: " << options;
81  utils_error().raise(LOCAL_INFO,os.str());
82  result = TYPE();
83  }
84  else
85  {
86  try
87  {
88  result = NodeUtility::getNode<TYPE>(node);
89  }
90  catch(YAML::Exception& e)
91  {
92  std::string nodestr;
93  try
94  {
95  nodestr = node.as<std::string>();
96  }
97  catch(YAML::Exception& e)
98  {
99  nodestr = "<Couldn't even convert to string!>";
100  }
101  std::ostringstream os;
102  os << "Error retrieving options entry for [" << stringifyVariadic(keys...)
103  << "] as type " << typeid(TYPE).name() << " (template parameter: see below). String form of node value was: "
104  << nodestr << std::endl
105  << "YAML message follows: " << std::endl
106  << e.what();
107  utils_error().raise(LOCAL_INFO,os.str());
108  result = TYPE();
109  }
110  }
111  return result;
112  }
113 
114  template<typename TYPE, typename... args>
115  TYPE getValueOrDef(TYPE def, const args&... keys) const
116  {
117  const YAML::Node node = getVariadicNode(options, keys...);
118  TYPE result;
119  if (not node)
120  {
121  result = def;
122  }
123  else
124  {
125  result = getValue<TYPE>(keys...);
126  }
127  return result;
128  }
130 
131 
134  template<typename KEYTYPE, typename VALTYPE>
135  void setValue(const KEYTYPE &key, const VALTYPE &val)
136  {
137  options[key] = val;
138  return;
139  }
141 
148  template<typename TYPE>
149  std::vector<TYPE> getVector(std::string key) const
150  {
151  if (getNode(key).IsScalar())
152  {
153  return {getValue<TYPE>(key)};
154  }
155  else
156  {
157  return getValue<std::vector<TYPE>>(key);
158  }
159  }
160 
164  template<typename... args>
165  const std::vector<str> getNames(const args&... keys) const
166  {
167  std::vector<str> result;
168  const YAML::Node node = getVariadicNode(options, keys...);
169 
170  if (node.IsMap())
171  {
172  for (auto it = node.begin(), end = node.end(); it != end; ++it)
173  {
174  result.push_back( it->first.as<str>() );
175  }
176  }
177 
178  return result;
179  }
180 
183  const std::vector<str> getNames() const
184  {
185  std::vector<str> result;
186 
187  for (auto it = options.begin(), end = options.end(); it != end; ++it)
188  {
189  result.push_back( it->first.as<str>() );
190  }
191 
192  return result;
193  }
194 
196  template<typename... args>
197  const Options getOptions(const args&... keys) const
198  {
199  const YAML::Node node = getVariadicNode(options, keys...);
200  if (node["options"])
201  {
202  return Options(node["options"]);
203  }
204  else
205  {
206  return Options(node);
207  }
208  }
209 
211  template<typename... args>
212  YAML::Node getNode(const args&... keys) const
213  {
214  const YAML::Node node = getVariadicNode(options, keys...);
215  if (not node)
216  {
217  std::ostringstream os;
218  os << "No options entry for [" << stringifyVariadic(keys...) << "]\n Node contents: " << options;
219  utils_error().raise(LOCAL_INFO,os.str());
220  }
221  return node;
222  }
223 
225  template<typename... args>
226  YAML::Node loadFromFile(const args&... keys) const
227  {
228  const YAML::Node node = getVariadicNode(options, keys...);
229  if (not node)
230  {
231  std::ostringstream os;
232  os << "No options entry for [" << stringifyVariadic(keys...) << "]\n Node contents: " << options;
233  utils_error().raise(LOCAL_INFO,os.str());
234  }
235  return YAML::LoadFile(node.as<std::string>().c_str());
236  }
237 
239  YAML::const_iterator begin() const { return options.begin(); }
240  YAML::const_iterator end() const { return options.end(); }
241 
242  private:
243 
244  YAML::Node options;
245  };
246 
247 
248 }
249 
250 #endif //#ifndef __yaml_options_hpp__
EXPORT_SYMBOLS error & utils_error()
Utility errors.
Options(YAML::Node &&options)
Move constructor.
const std::vector< str > getNames(const args &... keys) const
Retrieve values from key-value pairs in options node.
#define LOCAL_INFO
Definition: local_info.hpp:34
STL namespace.
General small utility classes, typedefs, etc.
Variadic utilty functions which work with YAML objects.
const std::string stringifyVariadic()
const Options getOptions(const args &... keys) const
Recursive options retrieval.
Options()
Default constructor.
bool hasKey(const args &... keys) const
Getters for key/value pairs (which is all the options node should contain)
TYPE getValue(const args &... keys) const
const YAML::Node getVariadicNode(const YAML::Node &node)
YAML::Node options
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
std::vector< TYPE > getVector(std::string key) const
Get a std::vector of a particular type.
TYPE getValueOrDef(TYPE def, const args &... keys) const
YAML::const_iterator end() const
Exception objects required for standalone compilation.
Options(const YAML::Node &options)
Copy constructor.
const std::vector< str > getNames() const
Retrieve values from all key-value pairs in options node.
YAML::const_iterator begin() const
Return begin and end of options.
void setValue(const KEYTYPE &key, const VALTYPE &val)
Basic setter, for adding extra options.
Wrapper functionality to get yaml nodes with some extras.
TODO: see if we can use this one:
Definition: Analysis.hpp:33
YAML::Node loadFromFile(const args &... keys) const
Get YAML node from file.
A small wrapper object for &#39;options&#39; nodes.
YAML::Node getNode(const args &... keys) const
Retrieve raw YAML node.