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  // see http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
24  #ifndef thread_local
25  #if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
26  # define thread_local _Thread_local
27  #elif defined _WIN32 && ( \
28  defined _MSC_VER || \
29  defined __ICL || \
30  defined __DMC__ || \
31  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__ || \
37  defined __SUNPRO_C || \
38  defined __xlC__
39  #define thread_local __thread
40  #else
41  #error "Cannot define thread_local"
42 // #define thread_local
43  #endif
44  #endif
45 
46  #define COOLPROPDBL_MAPS_TO_DOUBLE
47  #ifdef COOLPROPDBL_MAPS_TO_DOUBLE
48  typedef double CoolPropDbl;
49  #else
50  typedef long double CoolPropDbl;
51  #endif
52 
54  #ifdef __GNUC__
55  #define DEPRECATED(func) func __attribute__ ((deprecated))
56  #elif defined(_MSC_VER)
57  #define DEPRECATED(func) __declspec(deprecated) func
58  #else
59  #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
60  #define DEPRECATED(func) func
61  #endif
62 
63  class Dictionary
64  {
65  private:
66  typedef std::map<std::string, double> numbers_map;
67  numbers_map numbers;
68  typedef std::map<std::string, std::string> strings_map;
69  strings_map strings;
70  typedef std::map<std::string, std::vector<double> > double_vectors_map;
71  double_vectors_map double_vectors;
72  typedef std::map<std::string, std::vector<std::string> > string_vectors_map;
73  string_vectors_map string_vectors;
74  public:
75  Dictionary(){};
76  bool is_empty(void) const {return numbers.empty() && strings.empty() && double_vectors.empty() && string_vectors.empty();}
77  void add_string(const std::string &s1, const std::string &s2){ strings.insert(std::pair<std::string, std::string>(s1, s2));}
78  void add_number(const std::string &s1, double d){ numbers.erase(s1); numbers.insert(std::pair<std::string, double>(s1, d));}
79  bool has_number(const std::string &s1){ return numbers.find(s1) != numbers.end(); }
80  void add_double_vector(const std::string &s1, const std::vector<double> &d){ double_vectors.insert(std::pair<std::string, std::vector<double> >(s1, d));}
81  void add_string_vector(const std::string &s1, const std::vector<std::string> &d){ string_vectors.insert(std::pair<std::string, std::vector<std::string> >(s1, d));}
82  std::string get_string(const std::string &s) const
83  {
84  strings_map::const_iterator i = strings.find(s);
85  if (i != strings.end()){
86  return i->second;
87  }
88  else{
89  throw CoolProp::ValueError(format("%s could not be matched in get_string",s.c_str()));
90  }
91  };
92  double get_double(const std::string &s) const
93  {
94  numbers_map::const_iterator i = numbers.find(s);
95  if (i != numbers.end()){
96  return i->second;
97  }
98  else{
99  throw CoolProp::ValueError(format("%s could not be matched in get_number",s.c_str()));
100  }
101  };
103  double get_double(const std::string &s, const double default_value) const
104  {
105  numbers_map::const_iterator i = numbers.find(s);
106  if (i != numbers.end()){
107  return i->second;
108  }
109  else{
110  return default_value;
111  }
112  };
113  double get_number(const std::string &s) const
114  {
115  return get_double(s);
116  };
117  const std::vector<double>& get_double_vector(const std::string &s) const
118  {
119  double_vectors_map::const_iterator i = double_vectors.find(s);
120  if (i != double_vectors.end()){
121  return i->second;
122  }
123  else{
124  throw CoolProp::ValueError(format("%s could not be matched in get_double_vector",s.c_str()));
125  }
126  };
127  const std::vector<std::string>& get_string_vector(const std::string &s) const
128  {
129  string_vectors_map::const_iterator i = string_vectors.find(s);
130  if (i != string_vectors.end()){
131  return i->second;
132  }
133  else{
134  throw CoolProp::ValueError(format("%s could not be matched in get_string_vector",s.c_str()));
135  }
136  };
137  };
139  //http://stackoverflow.com/questions/569110/why-is-memory-still-accessible-after-stdmapclear-is-called
140  template <typename M> void freeClear( M & amap ) {
141  for ( typename M::iterator it = amap.begin(); it != amap.end(); ++it ) {
142  delete it->second;
143  }
144  amap.clear();
145  }
146 
147 #define CATCH_ALL_ERRORS_RETURN_HUGE(x) try{ \
148  x \
149  } \
150  catch(const std::exception& e){ \
151  return _HUGE; \
152  } \
153  catch(...){ \
154  return _HUGE; \
155  }
156 
157  enum miniz_mode{MINIZ_COMPRESS, MINIZ_DECOMPRESS};
158  void miniz(const std::string &inFile, const std::string &outFile, miniz_mode mode);
159 #endif
Definition: CoolPropTools.h:63
Definition: Exceptions.h:26
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:103