1#ifndef WMUDGFUNCTIONS_H
2#define WMUDGFUNCTIONS_H
5#include "warpxm/warpxm_config.h"
18#define SAFE_DELETE(ptr) \
24#define SAFE_DELETE_ARRAY(ptr) \
30#define SAFE_DELETE_POINTER_VECTOR(vec) \
31 for (int i = 0; i < vec.size(); i++) \
33 SAFE_DELETE(vec[i]); \
44 if (typeinfo ==
typeid(
float))
46 typeSize =
sizeof(float);
48 else if (typeinfo ==
typeid(
double))
50 typeSize =
sizeof(double);
52 else if (typeinfo ==
typeid(
int))
54 typeSize =
sizeof(int);
56 else if (typeinfo ==
typeid(
unsigned))
58 typeSize =
sizeof(unsigned);
62 WxExcept wxe(
"WmPatchedProcessingObject::getTypeSize : ");
63 wxe <<
"Not yet setup for type '" << typeinfo.name() <<
"'" << std::endl;
74 static constexpr char const* value =
"int";
79 static constexpr char const* value =
"unsigned";
84 static constexpr char const* value =
"float";
89 static constexpr char const* value =
"double";
94 return std::isspace(c);
109 std::stringstream ss(text);
111 return ss >> result ? result : 0;
117 std::string::const_iterator start = text.begin(), end;
118 const std::string delimiter =
",";
121 end = std::find_first_of(start, text.end(), delimiter.begin(), delimiter.end());
122 const std::string substr(start, end);
124 vec.push_back(WmUDGFunctions::stringToValue<T>(substr));
125 if (end == text.end())
136 std::stringstream ss;
141template<
typename T>
inline void arrayPrinter(
const T* array,
const int n)
144 for (
int i = 0; i < n; i++)
146 std::cout << array[i];
152 std::cout <<
"]" << std::endl;
155template<
typename T>
inline void arrayPrinter(
const std::vector<T>& array)
160template<
typename T>
inline std::string
defineOption(
const std::string& name, T value)
162 std::stringstream ss;
163 ss <<
" -D" << name <<
"=(" << value <<
") ";
167template<
typename T>
inline std::string
define(
const std::string& name, T value)
169 std::stringstream ss;
170 ss <<
"#define " << name <<
" " << value <<
" \n";
174inline std::string
readFile(
const std::string& filename)
176 std::ifstream fileString;
177 fileString.open(filename.c_str());
178 if (!fileString.good())
180 WxExcept wxe(
"WmUDGFunctions::readFile : ");
181 wxe <<
"File '" << filename <<
"' does not exist\n";
184 std::stringstream ss;
185 ss << fileString.rdbuf();
186 std::string srccode(ss.str());
193inline void writeFile(
const std::string& filename,
const std::string& text)
195 std::ofstream out(filename.c_str());
203 size_t start =
string.find(key);
204 if (start ==
string.npos)
206 WxExcept wxe(
"WmUDGFunctions::parseString : ");
207 wxe <<
"Key '" << key <<
"' not found\n";
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);
219 return WmUDGFunctions::stringToValue<T>(valString);
224 const std::string& key,
225 const int expectedLength = -1)
227 size_t start =
string.find(key);
228 if (start ==
string.npos)
230 WxExcept wxe(
"WmUDGFunctions::parseString : ");
231 wxe <<
"Key '" << key <<
"' not found\n";
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);
243 std::vector<T> vals = WmUDGFunctions::stringToValueVec<T>(valString);
244 if (expectedLength >= 0)
246 if (vals.size() != expectedLength)
248 WxExcept wxe(
"WmUDGFunctions::parseString : ");
249 wxe <<
"Key '" << key <<
"' array size '" << vals.size()
250 <<
"' was not the expected '" << expectedLength <<
"'\n";
257template<
typename T>
inline std::string
toString(
const T& val)
259 std::stringstream ss;
260 if ((
typeid(T) ==
typeid(
double)) || (
typeid(T) ==
typeid(
float)))
262 ss << std::setprecision(12) << std::scientific;
270 std::vector<T> vec = array;
271 std::sort(vec.begin(), vec.end());
274 for (
int i = 1; i < vec.size(); i++)
276 if (vec[i] == vec[i - 1])
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