WARPXM v1.10.0
Loading...
Searching...
No Matches
wmudgfunctions.h
Go to the documentation of this file.
1#ifndef WMUDGFUNCTIONS_H
2#define WMUDGFUNCTIONS_H
3
4// Wm includes
5#include "warpxm/warpxm_config.h"
6#include "wxexcept.h"
7
8// STL includes
9#include <algorithm>
10#include <fstream>
11#include <iomanip>
12#include <iostream>
13#include <sstream>
14#include <typeinfo>
15
16// This is just a namespace to hold some useful functions I keep using for DG
17
18#define SAFE_DELETE(ptr) \
19 if (ptr) \
20 { \
21 delete ptr; \
22 ptr = NULL; \
23 }
24#define SAFE_DELETE_ARRAY(ptr) \
25 if (ptr) \
26 { \
27 delete[] ptr; \
28 ptr = NULL; \
29 }
30#define SAFE_DELETE_POINTER_VECTOR(vec) \
31 for (int i = 0; i < vec.size(); i++) \
32 { \
33 SAFE_DELETE(vec[i]); \
34 } \
35 vec.clear();
36
38{
39inline size_t getTypeSize(const std::type_info& typeinfo)
40{
41
42 size_t typeSize = 0;
43
44 if (typeinfo == typeid(float))
45 {
46 typeSize = sizeof(float);
47 }
48 else if (typeinfo == typeid(double))
49 {
50 typeSize = sizeof(double);
51 }
52 else if (typeinfo == typeid(int))
53 {
54 typeSize = sizeof(int);
55 }
56 else if (typeinfo == typeid(unsigned))
57 {
58 typeSize = sizeof(unsigned);
59 }
60 else
61 {
62 WxExcept wxe("WmPatchedProcessingObject::getTypeSize : ");
63 wxe << "Not yet setup for type '" << typeinfo.name() << "'" << std::endl;
64 throw wxe;
65 }
66
67 return typeSize;
68}
69
70template<class T> struct typeString;
71
72template<> struct typeString<int>
73{
74 static constexpr char const* value = "int";
75};
76
77template<> struct typeString<unsigned>
78{
79 static constexpr char const* value = "unsigned";
80};
81
82template<> struct typeString<float>
83{
84 static constexpr char const* value = "float";
85};
86
87template<> struct typeString<double>
88{
89 static constexpr char const* value = "double";
90};
91
92inline bool isaspace(unsigned char const c)
93{
94 return std::isspace(c);
95}
96
97inline bool isjustaspace(unsigned char const c)
98{
99 return c == ' ';
100}
101
102inline bool isacomma(unsigned char const c)
103{
104 return c == ',';
105}
106
107template<typename T> inline T stringToValue(const std::string& text)
108{
109 std::stringstream ss(text);
110 T result;
111 return ss >> result ? result : 0;
112}
113
114template<typename T> inline std::vector<T> stringToValueVec(const std::string& text)
115{
116 std::vector<T> vec;
117 std::string::const_iterator start = text.begin(), end;
118 const std::string delimiter = ",";
119 while (true)
120 {
121 end = std::find_first_of(start, text.end(), delimiter.begin(), delimiter.end());
122 const std::string substr(start, end);
123 // std::cout << "Line '" << text << "' subline '" << substr << "'" << std::endl;
124 vec.push_back(WmUDGFunctions::stringToValue<T>(substr));
125 if (end == text.end())
126 {
127 break;
128 }
129 start = end + 1;
130 }
131 return vec;
132}
133
134template<typename T> inline std::string valueToString(T value)
135{
136 std::stringstream ss;
137 ss << value;
138 return ss.str();
139}
140
141template<typename T> inline void arrayPrinter(const T* array, const int n)
142{
143 std::cout << "[";
144 for (int i = 0; i < n; i++)
145 {
146 std::cout << array[i];
147 if (i != n - 1)
148 {
149 std::cout << ", ";
150 }
151 }
152 std::cout << "]" << std::endl;
153}
154
155template<typename T> inline void arrayPrinter(const std::vector<T>& array)
156{
157 arrayPrinter(&(array[0]), array.size());
158}
159
160template<typename T> inline std::string defineOption(const std::string& name, T value)
161{
162 std::stringstream ss;
163 ss << " -D" << name << "=(" << value << ") ";
164 return ss.str();
165}
166
167template<typename T> inline std::string define(const std::string& name, T value)
168{
169 std::stringstream ss;
170 ss << "#define " << name << " " << value << " \n";
171 return ss.str();
172}
173
174inline std::string readFile(const std::string& filename)
175{
176 std::ifstream fileString;
177 fileString.open(filename.c_str());
178 if (!fileString.good())
179 {
180 WxExcept wxe("WmUDGFunctions::readFile : ");
181 wxe << "File '" << filename << "' does not exist\n";
182 throw wxe;
183 }
184 std::stringstream ss;
185 ss << fileString.rdbuf();
186 std::string srccode(ss.str());
187 // std::string srccode((std::istreambuf_iterator<char>(fileString)),
188 // std::istreambuf_iterator<char>());
189 fileString.close();
190 return srccode;
191}
192
193inline void writeFile(const std::string& filename, const std::string& text)
194{
195 std::ofstream out(filename.c_str());
196 out << text;
197 out.close();
198}
199
200template<typename T>
201T parseStringForValue(const std::string& string, const std::string& key)
202{
203 size_t start = string.find(key);
204 if (start == string.npos)
205 {
206 WxExcept wxe("WmUDGFunctions::parseString : ");
207 wxe << "Key '" << key << "' not found\n";
208 throw wxe;
209 }
210 size_t valStart = string.find('=', start) + 1;
211 size_t valLength = string.find('\n', start) - valStart;
212 std::string valString = string.substr(valStart, valLength);
213 valString.erase(
214 std::remove_if(valString.begin(), valString.end(), WmUDGFunctions::isaspace),
215 valString.end());
216 // std::cout << "looking for '" << key << "'', found '"<<string.substr(start,
217 // string.find('\n',start)-start)<<"' for ("<<start<<" ("<<valStart<<"),
218 // "<<string.find('\n',start)<<") giving value string '"<<valString<<"'" << std::endl;
219 return WmUDGFunctions::stringToValue<T>(valString);
220}
221
222template<typename T>
223std::vector<T> parseStringForVector(const std::string& string,
224 const std::string& key,
225 const int expectedLength = -1)
226{
227 size_t start = string.find(key);
228 if (start == string.npos)
229 {
230 WxExcept wxe("WmUDGFunctions::parseString : ");
231 wxe << "Key '" << key << "' not found\n";
232 throw wxe;
233 }
234 size_t valStart = string.find('=', start) + 1;
235 size_t valLength = string.find('\n', start) - valStart;
236 std::string valString = string.substr(valStart, valLength);
237 valString.erase(
238 std::remove_if(valString.begin(), valString.end(), WmUDGFunctions::isaspace),
239 valString.end());
240 // std::cout << "looking for '" << key << "'', found '"<<string.substr(start,
241 // string.find('\n',start)-start)<<"' for ("<<start<<" ("<<valStart<<"),
242 // "<<string.find('\n',start)<<") giving value string '"<<valString<<"'" << std::endl;
243 std::vector<T> vals = WmUDGFunctions::stringToValueVec<T>(valString);
244 if (expectedLength >= 0)
245 {
246 if (vals.size() != expectedLength)
247 {
248 WxExcept wxe("WmUDGFunctions::parseString : ");
249 wxe << "Key '" << key << "' array size '" << vals.size()
250 << "' was not the expected '" << expectedLength << "'\n";
251 throw wxe;
252 }
253 }
254 return vals;
255}
256
257template<typename T> inline std::string toString(const T& val)
258{
259 std::stringstream ss;
260 if ((typeid(T) == typeid(double)) || (typeid(T) == typeid(float)))
261 {
262 ss << std::setprecision(12) << std::scientific;
263 }
264 ss << val;
265 return ss.str();
266}
267
268template<typename T> inline bool hasDuplicates(const std::vector<T>& array)
269{
270 std::vector<T> vec = array;
271 std::sort(vec.begin(), vec.end());
272 // There is a way to do this with std::unique, but it wasn't intuitive...
273 bool exists = false;
274 for (int i = 1; i < vec.size(); i++)
275 {
276 if (vec[i] == vec[i - 1])
277 {
278 exists = true;
279 }
280 }
281 return exists;
282}
283} // namespace WmUDGFunctions
284
285#endif // WMUDGFUNCTIONS_H
wxm::lib::Except is the class to use for creating and throwing exceptions.
Definition: wxexcept.h:31
Definition: wmudgfunctions.h:38
std::vector< T > stringToValueVec(const std::string &text)
Definition: wmudgfunctions.h:114
void arrayPrinter(const T *array, const int n)
Definition: wmudgfunctions.h:141
T parseStringForValue(const std::string &string, const std::string &key)
Definition: wmudgfunctions.h:201
bool hasDuplicates(const std::vector< T > &array)
Definition: wmudgfunctions.h:268
std::string readFile(const std::string &filename)
Definition: wmudgfunctions.h:174
std::vector< T > parseStringForVector(const std::string &string, const std::string &key, const int expectedLength=-1)
Definition: wmudgfunctions.h:223
std::string valueToString(T value)
Definition: wmudgfunctions.h:134
void writeFile(const std::string &filename, const std::string &text)
Definition: wmudgfunctions.h:193
std::string defineOption(const std::string &name, T value)
Definition: wmudgfunctions.h:160
bool isjustaspace(unsigned char const c)
Definition: wmudgfunctions.h:97
std::string define(const std::string &name, T value)
Definition: wmudgfunctions.h:167
std::string toString(const T &val)
Definition: wmudgfunctions.h:257
size_t getTypeSize(const std::type_info &typeinfo)
Definition: wmudgfunctions.h:39
bool isacomma(unsigned char const c)
Definition: wmudgfunctions.h:102
T stringToValue(const std::string &text)
Definition: wmudgfunctions.h:107
bool isaspace(unsigned char const c)
Definition: wmudgfunctions.h:92
Definition: wmudgfunctions.h:70