CoolProp 7.0.0
An open-source fluid property and humid air property database
CoolPropTools.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
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/*
28// see http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
29#ifndef thread_local
30# if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
31# define thread_local _Thread_local
32# elif defined _WIN32 && (defined _MSC_VER || defined __ICL || defined __DMC__ || defined __BORLANDC__)
33# define thread_local __declspec(thread)
34# elif defined(__ISAPPLE__) && (defined(__llvm__) || defined(__clang__)) && !__has_feature(cxx_thread_local)
35# define thread_local
36// note that ICC (linux) and Clang are covered by __GNUC__
37# elif defined __GNUC__ || defined __SUNPRO_C || defined __xlC__
38# define thread_local __thread
39# else
40# error "Cannot define thread_local"
41// #define thread_local
42# endif
43#endif
44*/
45
46#define COOLPROPDBL_MAPS_TO_DOUBLE
47#ifdef COOLPROPDBL_MAPS_TO_DOUBLE
48typedef double CoolPropDbl;
49#else
50typedef 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
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
75 public:
77 bool is_empty(void) const {
78 return numbers.empty() && strings.empty() && double_vectors.empty() && string_vectors.empty();
79 }
80 void add_string(const std::string& s1, const std::string& s2) {
81 strings.insert(std::pair<std::string, std::string>(s1, s2));
82 }
83 void add_number(const std::string& s1, double d) {
84 numbers.erase(s1);
85 numbers.insert(std::pair<std::string, double>(s1, d));
86 }
87 bool has_number(const std::string& s1) {
88 return numbers.find(s1) != numbers.end();
89 }
90 void add_double_vector(const std::string& s1, const std::vector<double>& d) {
91 double_vectors.insert(std::pair<std::string, std::vector<double>>(s1, d));
92 }
93 void add_string_vector(const std::string& s1, const std::vector<std::string>& d) {
94 string_vectors.insert(std::pair<std::string, std::vector<std::string>>(s1, d));
95 }
96 std::string get_string(const std::string& s) const {
97 strings_map::const_iterator i = strings.find(s);
98 if (i != strings.end()) {
99 return i->second;
100 } else {
101 throw CoolProp::ValueError(format("%s could not be matched in get_string", s.c_str()));
102 }
103 };
104 double get_double(const std::string& s) const {
105 numbers_map::const_iterator i = numbers.find(s);
106 if (i != numbers.end()) {
107 return i->second;
108 } else {
109 throw CoolProp::ValueError(format("%s could not be matched in get_number", s.c_str()));
110 }
111 };
113 double get_double(const std::string& s, const double default_value) const {
114 numbers_map::const_iterator i = numbers.find(s);
115 if (i != numbers.end()) {
116 return i->second;
117 } else {
118 return default_value;
119 }
120 };
121 double get_number(const std::string& s) const {
122 return get_double(s);
123 };
124 const std::vector<double>& get_double_vector(const std::string& s) const {
125 double_vectors_map::const_iterator i = double_vectors.find(s);
126 if (i != double_vectors.end()) {
127 return i->second;
128 } else {
129 throw CoolProp::ValueError(format("%s could not be matched in get_double_vector", s.c_str()));
130 }
131 };
132 const std::vector<std::string>& get_string_vector(const std::string& s) const {
133 string_vectors_map::const_iterator i = string_vectors.find(s);
134 if (i != string_vectors.end()) {
135 return i->second;
136 } else {
137 throw CoolProp::ValueError(format("%s could not be matched in get_string_vector", s.c_str()));
138 }
139 };
140};
142//http://stackoverflow.com/questions/569110/why-is-memory-still-accessible-after-stdmapclear-is-called
143template <typename M>
144void freeClear(M& amap) {
145 for (typename M::iterator it = amap.begin(); it != amap.end(); ++it) {
146 delete it->second;
147 }
148 amap.clear();
149}
150
151#define CATCH_ALL_ERRORS_RETURN_HUGE(x) \
152 try { \
153 x \
154 } catch (const std::exception& e) { \
155 return _HUGE; \
156 } catch (...) { \
157 return _HUGE; \
158 }
159
161{
165void miniz(const std::string& inFile, const std::string& outFile, miniz_mode mode);
166#endif