CoolProp  6.6.0
An open-source fluid property and humid air property database
Configuration.cpp
Go to the documentation of this file.
1 #include "Configuration.h"
3 
4 namespace CoolProp {
5 
7  switch (keys) {
8  /* ***MAGIC WARNING**!!
9  * See http://stackoverflow.com/a/148610
10  * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
11  */
12 #define X(Enum, String, Default, Desc) \
13  case Enum: \
14  return String; \
15  break;
17 #undef X
18  }
19  return ""; // will never get here, just to make compiler happy
20 };
21 
23  switch (keys) {
24 /* ***MAGIC WARNING**!!
25  * See http://stackoverflow.com/a/148610
26  * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
27  */
28 #define X(Enum, String, Default, Desc) \
29  case Enum: \
30  return Desc; \
31  break;
33 #undef X
34  }
35  return ""; // will never get here, just to make compiler happy
36 };
37 
38 std::string config_key_description(const std::string& key) {
39 /* ***MAGIC WARNING**!!
40  * See http://stackoverflow.com/a/148610
41  * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
42  */
43 #define X(Enum, String, Default, Desc) \
44  if (key == String) { \
45  return Desc; \
46  }
48 #undef X
49  return "INVALID KEY";
50 };
51 
54 /* See http://stackoverflow.com/a/148610
55  * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
56  */
57 #define X(Enum, String, Default, Desc) \
58  if (s == String) { \
59  return Enum; \
60  }
62 #undef X
63 
64  // Nothing else has fired
65  throw ValueError();
66 };
67 
68 static Configuration config;
69 
70 void set_config_bool(configuration_keys key, bool val) {
71  config.get_item(key).set_bool(val);
72 }
73 void set_config_int(configuration_keys key, int val) {
74  config.get_item(key).set_integer(val);
75 }
76 void set_config_double(configuration_keys key, double val) {
77  config.get_item(key).set_double(val);
78 }
79 void set_config_string(configuration_keys key, const std::string& val) {
80  config.get_item(key).set_string(val);
81  if (key == ALTERNATIVE_REFPROP_PATH || key == ALTERNATIVE_REFPROP_HMX_BNC_PATH || key == ALTERNATIVE_REFPROP_LIBRARY_PATH) {
83  }
84 }
85 
87  return static_cast<bool>(config.get_item(key));
88 }
90  return static_cast<int>(config.get_item(key));
91 }
93  return static_cast<double>(config.get_item(key));
94 }
96  return static_cast<std::string>(config.get_item(key));
97 }
98 void get_config_as_json(rapidjson::Document& doc) {
99  // Get the items
100  std::map<configuration_keys, ConfigurationItem> items = config.get_items();
101  for (std::map<configuration_keys, ConfigurationItem>::const_iterator it = items.begin(); it != items.end(); ++it) {
102  it->second.add_to_json(doc, doc);
103  }
104 }
106  rapidjson::Document doc;
107  doc.SetObject();
108  get_config_as_json(doc);
109  return cpjson::to_string(doc);
110 }
111 void set_config_as_json(rapidjson::Value& val) {
112 
113  // First check that all keys are valid
114  for (rapidjson::Value::MemberIterator it = val.MemberBegin(); it != val.MemberEnd(); ++it) {
115  try {
116  // Try to get the key for the string
117  std::string s = std::string(it->name.GetString());
119  // Try to retrieve the item from the config for this key
120  config.get_item(key);
121  } catch (std::exception& e) {
122  throw ValueError(format("Unable to parse json file with error: %s", e.what()));
123  }
124  }
125 
126  // Now we actually set the values
127  for (rapidjson::Value::MemberIterator it = val.MemberBegin(); it != val.MemberEnd(); ++it) {
128  // Try to get the key for the string
129  std::string s = std::string(it->name.GetString());
131  // Try to retrieve the item from the config for this key
132  ConfigurationItem& item = config.get_item(key);
133  try {
134  // Set the value from what is stored in the json value
135  item.set_from_json(it->value);
136  } catch (std::exception& e) {
137  throw ValueError(format("Unable to parse json file with error: %s", e.what()));
138  }
139  }
140 }
141 void set_config_as_json_string(const std::string& s) {
142  // Init the rapidjson doc
143  rapidjson::Document doc;
144  doc.Parse<0>(s.c_str());
145  set_config_as_json(doc);
146 }
147 
148 } // namespace CoolProp