MIRA
ToString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_TOSTRING_H_
49 #define _MIRA_TOSTRING_H_
50 
51 #include <iomanip>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/numeric/conversion/cast.hpp>
55 #include <boost/lexical_cast.hpp>
56 #endif
57 
58 #include <error/Exceptions.h>
59 
60 #include <math/Math.h>
61 
63 
64 #define MIRA_TOSTRING_NAN "nan"
65 #define MIRA_TOSTRING_POSITIVE_INF "inf"
66 #define MIRA_TOSTRING_NEGATIVE_INF "-inf"
67 
68 namespace mira {
69 
71 
73 namespace Private {
74 
76 template <typename T>
77 inline std::string genericToString(const T& value, int precision)
78 {
79  std::stringstream ss;
80  if(precision>=0)
81  ss << std::setprecision(precision);
82  ss << value;
83  if(ss.fail())
84  MIRA_THROW(XIO, "Failed to convert value into a string");
85  return ss.str();
86 }
87 
89 template <typename T>
90 inline T genericFromString(const std::string& str)
91 {
92  T value;
93  std::stringstream ss(str);
94  ss >> value;
95  if(ss.fail()) // make sure that we have no error flag set
96  MIRA_THROW(XIO, "Failed to convert value from string '" << str << "'");
97 
98  return value;
99 }
100 
102 template <typename T>
103 inline std::string fpToString(const T& value, int precision)
104 {
105  if(boost::math::isnan(value))
106  return MIRA_TOSTRING_NAN;
107  else if(boost::math::isinf(value)) {
108  if(value>0)
110  else
112  }
113  else
114  return genericToString<T>(value, precision);
115 }
116 
118 template <typename T>
119 inline T fpFromString(const std::string& str)
120 {
121  try {
122  return genericFromString<T>(str);
123  } catch(XIO&) {
124  if(str==MIRA_TOSTRING_NAN)
125  return std::numeric_limits<T>::quiet_NaN();
126  else if(str==MIRA_TOSTRING_POSITIVE_INF)
127  return std::numeric_limits<T>::infinity();
128  else if(str==MIRA_TOSTRING_NEGATIVE_INF)
129  return -std::numeric_limits<T>::infinity();
130  else
131  throw; // we could not handle the exception, so rethrow it
132  }
133 }
134 
136 template <typename T>
137 inline std::string charToString(const T& value)
138 {
139  return genericToString<int>((int)value, 0);
140 }
141 
143 template <typename T>
144 inline T charFromString(const std::string& str)
145 {
146  int val = genericFromString<int>(str);
147 
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());
154 
155  return static_cast<T>(val);
156 }
157 
158 } // Private
160 
162 
172 template <typename T>
173 class FromHex
174 {
175 public:
176  operator T() const { return value; }
177  friend std::istream& operator>>(std::istream& iStream, FromHex<T>& oValue)
178  {
179  iStream >> std::hex >> oValue.value;
180  return iStream;
181  }
182 
183  T value;
184 };
185 
192 template <typename T>
193 class ToHex
194 {
195 public:
196  ToHex(T v) : value(v) {}
197  operator T() const { return value; }
198  friend std::ostream& operator<<(std::ostream& oStream, const ToHex<T>& iValue)
199  {
200  oStream << std::hex << iValue.value;
201  return oStream;
202  }
203 
204  T value;
205 };
206 
213 template <typename T>
214 class FromOct
215 {
216 public:
217  operator T() const { return value; }
218  friend std::istream& operator>>(std::istream& iStream, FromOct<T>& oValue)
219  {
220  iStream >> std::oct >> oValue.value;
221  return iStream;
222  }
223 
224  T value;
225 };
226 
233 template <typename T>
234 class ToOct
235 {
236 public:
237  ToOct(T v) : value(v) {}
238  operator T() const { return value; }
239  friend std::ostream& operator<<(std::ostream& oStream, const ToOct<T>& iValue)
240  {
241  oStream << std::oct << iValue.value;
242  return oStream;
243  }
244 
245  T value;
246 };
247 
249 
255 template <typename T>
256 std::string toString(const T& value, int precision=-1)
257 {
258  return Private::genericToString<T>(value, precision);
259 }
260 
268 template <typename T>
269 T fromString(const std::string& str)
270 {
271  return Private::genericFromString<T>(str);
272 }
273 
274 
276 template<>
277 inline std::string toString<char>(const char& value, int precision)
278 {
279  return Private::charToString<char>(value);
280 }
281 
283 template <>
284 inline char fromString<char>(const std::string& str)
285 {
286  return Private::charFromString<char>(str);
287 }
288 
290 template<>
291 inline std::string toString<signed char>(const signed char& value,
292  int precision)
293 {
294  return Private::charToString<signed char>(value);
295 }
296 
298 template <>
299 inline signed char fromString<signed char>(const std::string& str)
300 {
301  return Private::charFromString<signed char>(str);
302 }
303 
305 template<>
306 inline std::string toString<unsigned char>(const unsigned char& value,
307  int precision)
308 {
309  return Private::charToString<unsigned char>(value);
310 }
311 
313 template <>
314 inline unsigned char fromString<unsigned char>(const std::string& str)
315 {
316  return Private::charFromString<unsigned char>(str);
317 }
318 
319 
321 template<>
322 inline std::string toString<std::string>(const std::string& value,
323  int precision)
324 {
325  return value;
326 }
327 
329 template <>
330 inline std::string fromString<std::string>(const std::string& str)
331 {
332  return str;
333 }
334 
336 template<>
337 inline std::string toString<float>(const float& value, int precision)
338 {
339  return Private::fpToString<float>(value, precision);
340 }
341 
343 template <>
344 inline float fromString<float>(const std::string& str)
345 {
346  return Private::fpFromString<float>(str);
347 }
348 
350 template<>
351 inline std::string toString<double>(const double& value, int precision)
352 {
353  return Private::fpToString<double>(value, precision);
354 }
355 
357 template <>
358 inline double fromString<double>(const std::string& str)
359 {
360  return Private::fpFromString<double>(str);
361 }
362 
364 template<>
365 inline std::string toString<long double>(const long double& value,
366  int precision)
367 {
368  return Private::fpToString<long double>(value, precision);
369 }
370 
372 template <>
373 inline long double fromString<long double>(const std::string& str)
374 {
375  return Private::fpFromString<long double>(str);
376 }
377 
379 template<>
380 inline std::string toString<bool>(const bool& value, int precision)
381 {
382  if ( value )
383  return "true";
384  else
385  return "false";
386 }
387 
389 template <>
390 inline bool fromString<bool>(const std::string & str)
391 {
392  return str == "true";
393 }
394 
396 
397 } // namespace
398 
399 #endif
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