CoolProp 7.0.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
4namespace 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
38std::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
68std::unique_ptr<Configuration> pconfig;
71 if (!pconfig){
72 pconfig = std::make_unique<Configuration>();
73 }
74 return pconfig.get();
75}
76
78 _get_config()->get_item(key).set_bool(val);
79}
81 _get_config()->get_item(key).set_integer(val);
82}
84 _get_config()->get_item(key).set_double(val);
85}
86void set_config_string(configuration_keys key, const std::string& val) {
87 _get_config()->get_item(key).set_string(val);
88 if (key == ALTERNATIVE_REFPROP_PATH || key == ALTERNATIVE_REFPROP_HMX_BNC_PATH || key == ALTERNATIVE_REFPROP_LIBRARY_PATH) {
90 }
91}
92
94 return static_cast<bool>(_get_config()->get_item(key));
95}
97 return static_cast<int>(_get_config()->get_item(key));
98}
100 return static_cast<double>(_get_config()->get_item(key));
101}
103 return static_cast<std::string>(_get_config()->get_item(key));
104}
105void get_config_as_json(rapidjson::Document& doc) {
106 // Get the items
107 std::unordered_map<configuration_keys, ConfigurationItem> items = _get_config()->get_items();
108 for (std::unordered_map<configuration_keys, ConfigurationItem>::const_iterator it = items.begin(); it != items.end(); ++it) {
109 it->second.add_to_json(doc, doc);
110 }
111}
113 rapidjson::Document doc;
114 doc.SetObject();
116 return cpjson::to_string(doc);
117}
118void set_config_as_json(rapidjson::Value& val) {
119
120 // First check that all keys are valid
121 for (rapidjson::Value::MemberIterator it = val.MemberBegin(); it != val.MemberEnd(); ++it) {
122 try {
123 // Try to get the key for the string
124 std::string s = std::string(it->name.GetString());
126 // Try to retrieve the item from the config for this key
127 _get_config()->get_item(key);
128 } catch (std::exception& e) {
129 throw ValueError(format("Unable to parse json file with error: %s", e.what()));
130 }
131 }
132
133 // Now we actually set the values
134 for (rapidjson::Value::MemberIterator it = val.MemberBegin(); it != val.MemberEnd(); ++it) {
135 // Try to get the key for the string
136 std::string s = std::string(it->name.GetString());
138 // Try to retrieve the item from the config for this key
139 ConfigurationItem& item = _get_config()->get_item(key);
140 try {
141 // Set the value from what is stored in the json value
142 item.set_from_json(it->value);
143 } catch (std::exception& e) {
144 throw ValueError(format("Unable to parse json file with error: %s", e.what()));
145 }
146 }
147}
148void set_config_as_json_string(const std::string& s) {
149 // Init the rapidjson doc
150 rapidjson::Document doc;
151 doc.Parse<0>(s.c_str());
153}
154
155} // namespace CoolProp