CoolProp 8.0.0
An open-source fluid property and humid air property database
tools.h
Go to the documentation of this file.
1#ifndef COOLPROPTOOLS_H
2#define COOLPROPTOOLS_H
3
4#ifndef _CRT_SECURE_NO_WARNINGS
5# define _CRT_SECURE_NO_WARNINGS
6#endif
7
10#include <string>
11#include <vector>
12#include <map>
13
17
18#ifndef __has_feature // Optional of course.
19# define __has_feature(x) 0 // Compatibility with non-clang compilers.
20#endif
21
22#ifdef __EMSCRIPTEN__
23# define thread_local
24#endif
25
26/*
27// see http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
28#ifndef thread_local
29# if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
30# define thread_local _Thread_local
31# elif defined _WIN32 && (defined _MSC_VER || defined __ICL || defined __DMC__ || defined __BORLANDC__)
32# define thread_local __declspec(thread)
33# elif defined(__ISAPPLE__) && (defined(__llvm__) || defined(__clang__)) && !__has_feature(cxx_thread_local)
34# define thread_local
35// note that ICC (linux) and Clang are covered by __GNUC__
36# elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
37# define thread_local __thread
38# else
39# error "Cannot define thread_local"
40// #define thread_local
41# endif
42#endif
43*/
44
45#define COOLPROPDBL_MAPS_TO_DOUBLE
46#ifdef COOLPROPDBL_MAPS_TO_DOUBLE
47using CoolPropDbl = double;
48#else
49typedef long double CoolPropDbl;
50#endif
51
52// DEPRECATED macro removed; use [[deprecated("message")]] directly instead.
53
55{
56 private:
57 using numbers_map = std::map<std::string, double>;
58 numbers_map numbers;
59 using strings_map = std::map<std::string, std::string>;
60 strings_map strings;
61 using double_vectors_map = std::map<std::string, std::vector<double>>;
62 double_vectors_map double_vectors;
63 using string_vectors_map = std::map<std::string, std::vector<std::string>>;
64 string_vectors_map string_vectors;
65
66 public:
67 Dictionary() = default;
68 bool is_empty() const {
69 return numbers.empty() && strings.empty() && double_vectors.empty() && string_vectors.empty();
70 }
71 void add_string(const std::string& s1, const std::string& s2) {
72 strings.insert(std::pair<std::string, std::string>(s1, s2));
73 }
74 void add_number(const std::string& s1, double d) {
75 numbers.erase(s1);
76 numbers.insert(std::pair<std::string, double>(s1, d));
77 }
78 [[nodiscard]] bool has_number(const std::string& s1) {
79 return numbers.find(s1) != numbers.end();
80 }
81 void add_double_vector(const std::string& s1, const std::vector<double>& d) {
82 double_vectors.insert(std::pair<std::string, std::vector<double>>(s1, d));
83 }
84 void add_string_vector(const std::string& s1, const std::vector<std::string>& d) {
85 string_vectors.insert(std::pair<std::string, std::vector<std::string>>(s1, d));
86 }
87 std::string get_string(const std::string& s) const {
88 auto i = strings.find(s);
89 if (i != strings.end()) {
90 return i->second;
91 } else {
92 throw CoolProp::ValueError(format("%s could not be matched in get_string", s.c_str()));
93 }
94 };
95 double get_double(const std::string& s) const {
96 auto i = numbers.find(s);
97 if (i != numbers.end()) {
98 return i->second;
99 } else {
100 throw CoolProp::ValueError(format("%s could not be matched in get_number", s.c_str()));
101 }
102 };
104 double get_double(const std::string& s, const double default_value) const {
105 auto i = numbers.find(s);
106 if (i != numbers.end()) {
107 return i->second;
108 } else {
109 return default_value;
110 }
111 };
112 double get_number(const std::string& s) const {
113 return get_double(s);
114 };
115 const std::vector<double>& get_double_vector(const std::string& s) const {
116 auto i = double_vectors.find(s);
117 if (i != double_vectors.end()) {
118 return i->second;
119 } else {
120 throw CoolProp::ValueError(format("%s could not be matched in get_double_vector", s.c_str()));
121 }
122 };
123 const std::vector<std::string>& get_string_vector(const std::string& s) const {
124 auto i = string_vectors.find(s);
125 if (i != string_vectors.end()) {
126 return i->second;
127 } else {
128 throw CoolProp::ValueError(format("%s could not be matched in get_string_vector", s.c_str()));
129 }
130 };
131};
133//http://stackoverflow.com/questions/569110/why-is-memory-still-accessible-after-stdmapclear-is-called
134template <typename M>
135void freeClear(M& amap) {
136 for (typename M::iterator it = amap.begin(); it != amap.end(); ++it) {
137 delete it->second;
138 }
139 amap.clear();
140}
141
142#define CATCH_ALL_ERRORS_RETURN_HUGE(x) \
143 try { \
144 x \
145 } catch (const std::exception& e) { \
146 return _HUGE; \
147 } catch (...) { \
148 return _HUGE; \
149 }
150
152{
156void miniz(const std::string& inFile, const std::string& outFile, miniz_mode mode);
157#endif