CoolProp 8.0.0
An open-source fluid property and humid air property database
Configuration.cpp
Go to the documentation of this file.
4
5namespace {
6
7void item_to_json(const CoolProp::ConfigurationItem& item, nlohmann::json& obj) {
8 const std::string name = CoolProp::config_key_to_string(item.get_key());
9 switch (item.get_type()) {
11 obj[name] = static_cast<bool>(item);
12 break;
14 obj[name] = static_cast<int>(item);
15 break;
17 obj[name] = static_cast<double>(item);
18 break;
20 obj[name] = static_cast<std::string>(item);
21 break;
25 }
26}
27
28void item_from_json(CoolProp::ConfigurationItem& item, const nlohmann::json& val) {
29 switch (item.get_type()) {
31 if (!val.is_boolean()) {
32 throw CoolProp::ValueError(format("Input is not boolean"));
33 }
34 item.set_bool(val.get<bool>());
35 break;
37 if (!val.is_number_integer()) {
38 throw CoolProp::ValueError(format("Input is not integer"));
39 }
40 item.set_integer(val.get<int>());
41 break;
43 if (!val.is_number()) {
44 throw CoolProp::ValueError(format("Input [%s] is not double (or something that can be cast to double)", val.dump().c_str()));
45 }
46 item.set_double(val.get<double>());
47 break;
49 if (!val.is_string()) {
50 throw CoolProp::ValueError(format("Input is not string"));
51 }
52 item.set_string(val.get<std::string>());
53 break;
57 }
58}
59
60} // namespace
61
62namespace CoolProp {
63
65 switch (keys) {
66 /* ***MAGIC WARNING**!!
67 * See http://stackoverflow.com/a/148610
68 * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
69 */
70#define X(Enum, String, Default, Desc) \
71 case Enum: \
72 return String; \
73 break;
75#undef X
76 }
77 return ""; // will never get here, just to make compiler happy
78};
79
81 switch (keys) {
82/* ***MAGIC WARNING**!!
83 * See http://stackoverflow.com/a/148610
84 * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
85 */
86#define X(Enum, String, Default, Desc) \
87 case Enum: \
88 return Desc; \
89 break;
91#undef X
92 }
93 return ""; // will never get here, just to make compiler happy
94};
95
96std::string config_key_description(const std::string& key) {
97/* ***MAGIC WARNING**!!
98 * See http://stackoverflow.com/a/148610
99 * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
100 */
101#define X(Enum, String, Default, Desc) \
102 if (key == (String)) { \
103 return (Desc); \
104 }
106#undef X
107 return "INVALID KEY";
108};
109
112/* See http://stackoverflow.com/a/148610
113 * See http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c#202511
114 */
115#define X(Enum, String, Default, Desc) \
116 if (s == (String)) { \
117 return Enum; \
118 }
120#undef X
121
122 // Nothing else has fired
123 throw ValueError();
124};
125
126std::unique_ptr<Configuration> pconfig;
129 if (!pconfig) {
130 pconfig = std::make_unique<Configuration>();
131 }
132 return pconfig.get();
133}
134
136 _get_config()->get_item(key).set_bool(val);
137}
139 _get_config()->get_item(key).set_integer(val);
140}
142 _get_config()->get_item(key).set_double(val);
143}
144void set_config_string(configuration_keys key, const std::string& val) {
145 _get_config()->get_item(key).set_string(val);
146 if (key == ALTERNATIVE_REFPROP_PATH || key == ALTERNATIVE_REFPROP_HMX_BNC_PATH || key == ALTERNATIVE_REFPROP_LIBRARY_PATH) {
148 }
149}
150
152 return static_cast<bool>(_get_config()->get_item(key));
153}
155 return static_cast<int>(_get_config()->get_item(key));
156}
158 return static_cast<double>(_get_config()->get_item(key));
159}
161 return static_cast<std::string>(_get_config()->get_item(key));
162}
164 nlohmann::json doc = nlohmann::json::object();
165 for (auto& kv : _get_config()->get_items()) {
166 item_to_json(kv.second, doc);
167 }
168 return doc.dump();
169}
170void set_config_as_json_string(const std::string& s) {
171 nlohmann::json doc = cpjson::parse(s);
172
173 // First pass: validate all keys
174 for (auto& [name, value] : doc.items()) {
175 try {
177 } catch (std::exception& e) {
178 throw ValueError(format("Unable to parse json file with error: %s", e.what()));
179 }
180 }
181
182 // Second pass: set the values
183 for (auto& [name, value] : doc.items()) {
185 try {
186 item_from_json(item, value);
187 } catch (std::exception& e) {
188 throw ValueError(format("Unable to parse json file with error: %s", e.what()));
189 }
190 }
191}
192
193} // namespace CoolProp