CoolProp
CoolPropTools.h
1 #ifndef COOLPROPTOOLS_H
2 #define COOLPROPTOOLS_H
3 
4 #ifndef _CRT_SECURE_NO_WARNINGS
5 # define _CRT_SECURE_NO_WARNINGS
6 #endif
7 
8 #include "PlatformDetermination.h"
9 #include "Exceptions.h"
10 #include <string>
11 #include <vector>
12 #include <cctype>
13 #include <map>
14 
15 #include "CPstrings.h"
16 #include "CPnumerics.h"
17 #include "CPfilepaths.h"
18 
19 #ifndef __has_feature // Optional of course.
20 # define __has_feature(x) 0 // Compatibility with non-clang compilers.
21 #endif
22 
23 #ifdef __EMSCRIPTEN__
24 # define thread_local
25 #endif
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 #define COOLPROPDBL_MAPS_TO_DOUBLE
45 #ifdef COOLPROPDBL_MAPS_TO_DOUBLE
46 typedef double CoolPropDbl;
47 #else
48 typedef long double CoolPropDbl;
49 #endif
50 
52 #ifdef __GNUC__
53 # define DEPRECATED(func) func __attribute__((deprecated))
54 #elif defined(_MSC_VER)
55 # define DEPRECATED(func) __declspec(deprecated) func
56 #else
57 # pragma message("WARNING: You need to implement DEPRECATED for this compiler")
58 # define DEPRECATED(func) func
59 #endif
60 
62 {
63  private:
64  typedef std::map<std::string, double> numbers_map;
65  numbers_map numbers;
66  typedef std::map<std::string, std::string> strings_map;
67  strings_map strings;
68  typedef std::map<std::string, std::vector<double>> double_vectors_map;
69  double_vectors_map double_vectors;
70  typedef std::map<std::string, std::vector<std::string>> string_vectors_map;
71  string_vectors_map string_vectors;
72 
73  public:
74  Dictionary(){};
75  bool is_empty(void) const {
76  return numbers.empty() && strings.empty() && double_vectors.empty() && string_vectors.empty();
77  }
78  void add_string(const std::string& s1, const std::string& s2) {
79  strings.insert(std::pair<std::string, std::string>(s1, s2));
80  }
81  void add_number(const std::string& s1, double d) {
82  numbers.erase(s1);
83  numbers.insert(std::pair<std::string, double>(s1, d));
84  }
85  bool has_number(const std::string& s1) {
86  return numbers.find(s1) != numbers.end();
87  }
88  void add_double_vector(const std::string& s1, const std::vector<double>& d) {
89  double_vectors.insert(std::pair<std::string, std::vector<double>>(s1, d));
90  }
91  void add_string_vector(const std::string& s1, const std::vector<std::string>& d) {
92  string_vectors.insert(std::pair<std::string, std::vector<std::string>>(s1, d));
93  }
94  std::string get_string(const std::string& s) const {
95  strings_map::const_iterator i = strings.find(s);
96  if (i != strings.end()) {
97  return i->second;
98  } else {
99  throw CoolProp::ValueError(format("%s could not be matched in get_string", s.c_str()));
100  }
101  };
102  double get_double(const std::string& s) const {
103  numbers_map::const_iterator i = numbers.find(s);
104  if (i != numbers.end()) {
105  return i->second;
106  } else {
107  throw CoolProp::ValueError(format("%s could not be matched in get_number", s.c_str()));
108  }
109  };
111  double get_double(const std::string& s, const double default_value) const {
112  numbers_map::const_iterator i = numbers.find(s);
113  if (i != numbers.end()) {
114  return i->second;
115  } else {
116  return default_value;
117  }
118  };
119  double get_number(const std::string& s) const {
120  return get_double(s);
121  };
122  const std::vector<double>& get_double_vector(const std::string& s) const {
123  double_vectors_map::const_iterator i = double_vectors.find(s);
124  if (i != double_vectors.end()) {
125  return i->second;
126  } else {
127  throw CoolProp::ValueError(format("%s could not be matched in get_double_vector", s.c_str()));
128  }
129  };
130  const std::vector<std::string>& get_string_vector(const std::string& s) const {
131  string_vectors_map::const_iterator i = string_vectors.find(s);
132  if (i != string_vectors.end()) {
133  return i->second;
134  } else {
135  throw CoolProp::ValueError(format("%s could not be matched in get_string_vector", s.c_str()));
136  }
137  };
138 };
140 //http://stackoverflow.com/questions/569110/why-is-memory-still-accessible-after-stdmapclear-is-called
141 template <typename M>
142 void freeClear(M& amap) {
143  for (typename M::iterator it = amap.begin(); it != amap.end(); ++it) {
144  delete it->second;
145  }
146  amap.clear();
147 }
148 
149 #define CATCH_ALL_ERRORS_RETURN_HUGE(x) \
150  try { \
151  x \
152  } catch (const std::exception& e) { \
153  return _HUGE; \
154  } catch (...) { \
155  return _HUGE; \
156  }
157 
158 enum miniz_mode
159 {
160  MINIZ_COMPRESS,
161  MINIZ_DECOMPRESS
162 };
163 void miniz(const std::string& inFile, const std::string& outFile, miniz_mode mode);
164 #endif
Definition: CoolPropTools.h:61
Definition: Exceptions.h:45
double get_double(const std::string &s, const double default_value) const
Get a double, or return the default value if not found.
Definition: CoolPropTools.h:111