48 #ifndef _MIRA_TOSTRING_H_ 49 #define _MIRA_TOSTRING_H_ 54 #include <boost/numeric/conversion/cast.hpp> 55 #include <boost/lexical_cast.hpp> 64 #define MIRA_TOSTRING_NAN "nan" 65 #define MIRA_TOSTRING_POSITIVE_INF "inf" 66 #define MIRA_TOSTRING_NEGATIVE_INF "-inf" 77 inline std::string genericToString(
const T& value,
int precision)
84 MIRA_THROW(XIO,
"Failed to convert value into a string");
90 inline T genericFromString(
const std::string& str)
93 std::stringstream ss(str);
96 MIRA_THROW(XIO,
"Failed to convert value from string '" << str <<
"'");
102 template <
typename T>
103 inline std::string fpToString(
const T& value,
int precision)
105 if(boost::math::isnan(value))
107 else if(boost::math::isinf(value)) {
114 return genericToString<T>(value,
precision);
118 template <
typename T>
119 inline T fpFromString(
const std::string& str)
122 return genericFromString<T>(str);
125 return std::numeric_limits<T>::quiet_NaN();
127 return std::numeric_limits<T>::infinity();
129 return -std::numeric_limits<T>::infinity();
136 template <
typename T>
137 inline std::string charToString(
const T& value)
139 return genericToString<int>((int)value, 0);
143 template <
typename T>
144 inline T charFromString(
const std::string& str)
146 int val = genericFromString<int>(str);
148 if(val>std::numeric_limits<T>::max())
149 MIRA_THROW(XIO,
"Overflow while converting value from string '" <<
150 str <<
"', max: " << (
int)std::numeric_limits<T>::max());
151 if(val<std::numeric_limits<T>::min())
152 MIRA_THROW(XIO,
"Underflow while converting value from string '" <<
153 str <<
"', min: " << (
int)std::numeric_limits<T>::min());
155 return static_cast<T
>(val);
172 template <
typename T>
176 operator T()
const {
return value; }
179 iStream >> std::hex >> oValue.
value;
192 template <
typename T>
197 operator T()
const {
return value; }
198 friend std::ostream& operator<<(std::ostream& oStream, const ToHex<T>& iValue)
200 oStream << std::hex << iValue.value;
213 template <
typename T>
217 operator T()
const {
return value; }
220 iStream >> std::oct >> oValue.
value;
233 template <
typename T>
238 operator T()
const {
return value; }
239 friend std::ostream& operator<<(std::ostream& oStream, const ToOct<T>& iValue)
241 oStream << std::oct << iValue.value;
255 template <
typename T>
258 return Private::genericToString<T>(value,
precision);
268 template <
typename T>
271 return Private::genericFromString<T>(str);
279 return Private::charToString<char>(value);
286 return Private::charFromString<char>(str);
294 return Private::charToString<signed char>(value);
301 return Private::charFromString<signed char>(str);
309 return Private::charToString<unsigned char>(value);
316 return Private::charFromString<unsigned char>(str);
322 inline std::string toString<std::string>(
const std::string& value,
330 inline std::string fromString<std::string>(
const std::string& str)
339 return Private::fpToString<float>(value,
precision);
346 return Private::fpFromString<float>(str);
353 return Private::fpToString<double>(value,
precision);
360 return Private::fpFromString<double>(str);
368 return Private::fpToString<long double>(value,
precision);
375 return Private::fpFromString<long double>(str);
392 return str ==
"true";
Can be used with fromString to convert hex strings into numbers.
Definition: ToString.h:173
signed char fromString< signed char >(const std::string &str)
Specialization for signed char.
Definition: ToString.h:299
std::string toString< bool >(const bool &value, int precision)
Specialization for bool.
Definition: ToString.h:380
T value
Definition: ToString.h:183
T value
Definition: ToString.h:204
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:269
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
#define MIRA_TOSTRING_NEGATIVE_INF
Definition: ToString.h:66
friend std::istream & operator>>(std::istream &iStream, FromOct< T > &oValue)
Definition: ToString.h:218
Can be used with toString to convert values to their string hex representation.
Definition: ToString.h:193
float fromString< float >(const std::string &str)
Specialization for float.
Definition: ToString.h:344
#define MIRA_TOSTRING_POSITIVE_INF
Definition: ToString.h:65
Can be used with fromString to convert oct strings into numbers.
Definition: ToString.h:214
std::string toString< long double >(const long double &value, int precision)
Specialization for long double.
Definition: ToString.h:365
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:81
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:256
ToOct(T v)
Definition: ToString.h:237
std::string toString< unsigned char >(const unsigned char &value, int precision)
Specialization for unsigned char.
Definition: ToString.h:306
Commonly used exception classes.
char fromString< char >(const std::string &str)
Specialization for char.
Definition: ToString.h:284
std::string toString< signed char >(const signed char &value, int precision)
Specialization for signed char.
Definition: ToString.h:291
T value
Definition: ToString.h:224
#define MIRA_TOSTRING_NAN
Definition: ToString.h:64
ToHex(T v)
Definition: ToString.h:196
unsigned char fromString< unsigned char >(const std::string &str)
Specialization for unsigned char.
Definition: ToString.h:314
std::string toString< float >(const float &value, int precision)
Specialization for float.
Definition: ToString.h:337
Can be used with toString to convert values to their string oct representation.
Definition: ToString.h:234
std::string toString< char >(const char &value, int precision)
Specialization for char.
Definition: ToString.h:277
double fromString< double >(const std::string &str)
Specialization for double.
Definition: ToString.h:358
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:177
std::string toString< double >(const double &value, int precision)
Specialization for double.
Definition: ToString.h:351
T value
Definition: ToString.h:245
bool fromString< bool >(const std::string &str)
Specialization for bool.
Definition: ToString.h:390
long double fromString< long double >(const std::string &str)
Specialization for long double.
Definition: ToString.h:373