48 #ifndef _MIRA_TOSTRING_H_ 49 #define _MIRA_TOSTRING_H_ 51 #include <boost/algorithm/string/predicate.hpp> 73 inline std::string genericToString(
const T& value,
int precision)
80 MIRA_THROW(XIO,
"Failed to convert value into a string");
86 inline T genericFromString(
const std::string& str)
89 std::stringstream ss(str);
92 MIRA_THROW(XIO,
"Failed to convert value from string '" << str <<
"'");
99 inline std::string fpToString(
const T& value,
int precision)
101 if(boost::math::isnan(value))
103 else if(boost::math::isinf(value)) {
110 return genericToString<T>(value,
precision);
114 template <
typename T>
115 inline T fpFromString(
const std::string& str)
118 return genericFromString<T>(str);
121 return std::numeric_limits<T>::quiet_NaN();
123 return std::numeric_limits<T>::infinity();
125 return -std::numeric_limits<T>::infinity();
132 template <
typename T>
133 inline std::string charToString(
const T& value)
135 return genericToString<int>((int)value, 0);
139 template <
typename T>
140 inline T charFromString(
const std::string& str)
142 int val = genericFromString<int>(str);
144 if(val>std::numeric_limits<T>::max())
145 MIRA_THROW(XIO,
"Overflow while converting value from string '" <<
146 str <<
"', max: " << (
int)std::numeric_limits<T>::max());
147 if(val<std::numeric_limits<T>::min())
148 MIRA_THROW(XIO,
"Underflow while converting value from string '" <<
149 str <<
"', min: " << (
int)std::numeric_limits<T>::min());
151 return static_cast<T
>(val);
168 template <
typename T>
172 operator T()
const {
return value; }
175 iStream >> std::hex >> oValue.
value;
188 template <
typename T>
193 operator T()
const {
return value; }
194 friend std::ostream& operator<<(std::ostream& oStream, const ToHex<T>& iValue)
196 oStream << std::hex << iValue.value;
209 template <
typename T>
213 operator T()
const {
return value; }
216 iStream >> std::oct >> oValue.
value;
229 template <
typename T>
234 operator T()
const {
return value; }
235 friend std::ostream& operator<<(std::ostream& oStream, const ToOct<T>& iValue)
237 oStream << std::oct << iValue.value;
251 template <
typename T>
254 return Private::genericToString<T>(value,
precision);
264 template <
typename T>
267 return Private::genericFromString<T>(str);
275 return Private::charToString<char>(value);
282 return Private::charFromString<char>(str);
290 return Private::charToString<signed char>(value);
297 return Private::charFromString<signed char>(str);
305 return Private::charToString<unsigned char>(value);
312 return Private::charFromString<unsigned char>(str);
318 inline std::string toString<std::string>(
const std::string& value,
326 inline std::string fromString<std::string>(
const std::string& str)
335 return Private::fpToString<float>(value,
precision);
342 return Private::fpFromString<float>(str);
349 return Private::fpToString<double>(value,
precision);
356 return Private::fpFromString<double>(str);
364 return Private::fpToString<long double>(value,
precision);
371 return Private::fpFromString<long double>(str);
389 if (boost::iequals(str,
"true"))
392 if (!boost::iequals(str,
"false"))
393 MIRA_THROW(XIO,
"Failed to convert bool value from string '" << str <<
"'");
Can be used with fromString to convert hex strings into numbers.
Definition: ToString.h:169
signed char fromString< signed char >(const std::string &str)
Specialization for signed char.
Definition: ToString.h:295
std::string toString< bool >(const bool &value, int precision)
Specialization for bool.
Definition: ToString.h:376
T value
Definition: ToString.h:179
T value
Definition: ToString.h:200
Includes often needed math headers and methods and provides additional constants. ...
T fromString(const std::string &str)
Converts a string to any data type that supports the stream >> operator.
Definition: ToString.h:265
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
friend std::istream & operator>>(std::istream &iStream, FromOct< T > &oValue)
Definition: ToString.h:214
Can be used with toString to convert values to their string hex representation.
Definition: ToString.h:189
float fromString< float >(const std::string &str)
Specialization for float.
Definition: ToString.h:340
Can be used with fromString to convert oct strings into numbers.
Definition: ToString.h:210
std::string toString< long double >(const long double &value, int precision)
Specialization for long double.
Definition: ToString.h:361
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:252
ToOct(T v)
Definition: ToString.h:233
static constexpr auto MIRA_TOSTRING_POSITIVE_INF
Definition: ToString.h:63
std::string toString< unsigned char >(const unsigned char &value, int precision)
Specialization for unsigned char.
Definition: ToString.h:302
Commonly used exception classes.
char fromString< char >(const std::string &str)
Specialization for char.
Definition: ToString.h:280
std::string toString< signed char >(const signed char &value, int precision)
Specialization for signed char.
Definition: ToString.h:287
T value
Definition: ToString.h:220
static constexpr auto MIRA_TOSTRING_NEGATIVE_INF
Definition: ToString.h:64
ToHex(T v)
Definition: ToString.h:192
unsigned char fromString< unsigned char >(const std::string &str)
Specialization for unsigned char.
Definition: ToString.h:310
std::string toString< float >(const float &value, int precision)
Specialization for float.
Definition: ToString.h:333
Can be used with toString to convert values to their string oct representation.
Definition: ToString.h:230
std::string toString< char >(const char &value, int precision)
Specialization for char.
Definition: ToString.h:273
double fromString< double >(const std::string &str)
Specialization for double.
Definition: ToString.h:354
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:285
friend std::istream & operator>>(std::istream &iStream, FromHex< T > &oValue)
Definition: ToString.h:173
static constexpr auto MIRA_TOSTRING_NAN
Definition: ToString.h:62
std::string toString< double >(const double &value, int precision)
Specialization for double.
Definition: ToString.h:347
T value
Definition: ToString.h:241
bool fromString< bool >(const std::string &str)
Specialization for bool.
Definition: ToString.h:386
long double fromString< long double >(const std::string &str)
Specialization for long double.
Definition: ToString.h:369