gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
yaml_node_utility.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
17 
18 
19 #ifndef __yaml_node_utility_hpp__
20 #define __yaml_node_utility_hpp__
21 
22 #include <string>
23 #include <iostream>
24 #include <limits>
25 #include <cmath>
26 #include <cstring>
27 
28 #include "yaml-cpp/yaml.h"
29 
31 
32 namespace Gambit
33 {
34 
35  namespace NodeUtility
36  {
37 
41  template<class TYPE>
42  TYPE safeIntegerTypeCast(const std::string& s)
43  {
44  try
45  {
46  const double d = std::stod(s);
47  if (d != std::floor(d))
48  {
49  std::ostringstream os;
50  os << "Provided value " << d << " as option in the yaml file does not represent an integer.";
51  utils_error().raise(LOCAL_INFO, os.str());
52  }
53  if (std::numeric_limits<TYPE>::max() < d or std::numeric_limits<TYPE>::min() > d)
54  {
55  std::ostringstream os;
56  os << "Provided value " << d << " as option in the yaml file does not fit into the implemented integer type.";
57  utils_error().raise(LOCAL_INFO, os.str());
58  }
59  return static_cast<TYPE>(d);
60  }
61  catch (const std::out_of_range& e)
62  {
63  std::ostringstream os;
64  os << "Out of range error: " << e.what() << "\n";
65  os << "Provided value " << s << " as option in the yaml file does not fit into double.";
66  utils_error().raise(LOCAL_INFO, os.str());
67  }
68  catch (const std::invalid_argument& e)
69  {
70  std::ostringstream os;
71  os << "Invalid argument: " << e.what() << "\n";
72  os << "Provided value " << s << " as option in the yaml file can not be interpreted as double.";
73  utils_error().raise(LOCAL_INFO, os.str());
74  }
75  throw std::runtime_error("Reached end of function safeIntegerTypeCast. This should not happen.");
76  }
77 
79  void autoExpandEnvironmentVariables(std::string & text);
80 
82  void removeCharsFromString(std::string& text, const char* charsToRemove);
83 
86  std::string expandEnvironmentVariables(const std::string& input);
87 
91  template<class TYPE>
92  TYPE getNode(const YAML::Node node) { return node.as<TYPE>(); }
93 
97  template<>
98  inline int getNode<int>(const YAML::Node node)
99  {
100  try { return node.as<int>(); }
101  catch (...) { return safeIntegerTypeCast<int>(node.as<std::string>()); }
102  }
103 
105  template<>
106  inline unsigned int getNode<unsigned int>(const YAML::Node node)
107  {
108  try { return node.as<unsigned int>(); }
109  catch (...) { return safeIntegerTypeCast<unsigned int>(node.as<std::string>()); }
110  }
111 
113  template<>
114  inline long getNode<long>(const YAML::Node node)
115  {
116  try { return node.as<long>(); }
117  catch (...) { return safeIntegerTypeCast<long>(node.as<std::string>()); }
118  }
119 
121  template<>
122  inline unsigned long getNode<unsigned long>(const YAML::Node node)
123  {
124  try { return node.as<unsigned long>(); }
125  catch (...) { return safeIntegerTypeCast<unsigned long>(node.as<std::string>()); }
126  }
127 
129  template<>
130  inline long long getNode<long long>(const YAML::Node node)
131  {
132  try { return node.as<long long>(); }
133  catch (...) { return safeIntegerTypeCast<long long>(node.as<std::string>()); }
134  }
135 
137  template<>
138  inline unsigned long long getNode<unsigned long long>(const YAML::Node node)
139  {
140  try { return node.as<unsigned long long>(); }
141  catch (...) { return safeIntegerTypeCast<unsigned long long>(node.as<std::string>()); }
142  }
143 
147  template<>
148  inline std::string getNode<std::string>(const YAML::Node node)
149  {
150  return NodeUtility::expandEnvironmentVariables(node.as<std::string>());
151  }
152 
153  }
154 
155 }
156 
157 #endif //__yaml_node_utility_hpp__
void removeCharsFromString(std::string &text, const char *charsToRemove)
Remove characters in the given string.
EXPORT_SYMBOLS error & utils_error()
Utility errors.
unsigned long getNode< unsigned long >(const YAML::Node node)
See int specialization.
TYPE getNode(const YAML::Node node)
Wrapper for reading the node for a given type.
#define LOCAL_INFO
Definition: local_info.hpp:34
int getNode< int >(const YAML::Node node)
Allows to read scientific notation integer numbers.
unsigned int getNode< unsigned int >(const YAML::Node node)
See int specialization.
std::string expandEnvironmentVariables(const std::string &input)
Leave input alone and return new string, which has environment variables substituted and escpae chara...
long getNode< long >(const YAML::Node node)
See int specialization.
long long getNode< long long >(const YAML::Node node)
See int specialization.
TYPE safeIntegerTypeCast(const std::string &s)
Wrapper for integer type casts from a double in string representation.
unsigned long long getNode< unsigned long long >(const YAML::Node node)
See int specialization.
Exception objects required for standalone compilation.
void autoExpandEnvironmentVariables(std::string &text)
Expand environment variables in the given string.
TODO: see if we can use this one:
Definition: Analysis.hpp:33