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 <boost/algorithm/string/predicate.hpp>
52 
53 #include <error/Exceptions.h>
54 
55 #include <math/Math.h>
56 
58 
59 
60 namespace mira {
61 
62 static constexpr auto MIRA_TOSTRING_NAN = "nan";
63 static constexpr auto MIRA_TOSTRING_POSITIVE_INF = "inf";
64 static constexpr auto MIRA_TOSTRING_NEGATIVE_INF = "-inf";
65 
67 
69 namespace Private {
70 
72 template <typename T>
73 inline std::string genericToString(const T& value, int precision)
74 {
75  std::stringstream ss;
76  if(precision>=0)
77  ss << std::setprecision(precision);
78  ss << value;
79  if(ss.fail())
80  MIRA_THROW(XIO, "Failed to convert value into a string");
81  return ss.str();
82 }
83 
85 template <typename T>
86 inline T genericFromString(const std::string& str)
87 {
88  T value;
89  std::stringstream ss(str);
90  ss >> value;
91  if(ss.fail()) // make sure that we have no error flag set
92  MIRA_THROW(XIO, "Failed to convert value from string '" << str << "'");
93 
94  return value;
95 }
96 
98 template <typename T>
99 inline std::string fpToString(const T& value, int precision)
100 {
101  if(boost::math::isnan(value))
102  return MIRA_TOSTRING_NAN;
103  else if(boost::math::isinf(value)) {
104  if(value>0)
106  else
108  }
109  else
110  return genericToString<T>(value, precision);
111 }
112 
114 template <typename T>
115 inline T fpFromString(const std::string& str)
116 {
117  try {
118  return genericFromString<T>(str);
119  } catch(XIO&) {
120  if(str==MIRA_TOSTRING_NAN)
121  return std::numeric_limits<T>::quiet_NaN();
122  else if(str==MIRA_TOSTRING_POSITIVE_INF)
123  return std::numeric_limits<T>::infinity();
124  else if(str==MIRA_TOSTRING_NEGATIVE_INF)
125  return -std::numeric_limits<T>::infinity();
126  else
127  throw; // we could not handle the exception, so rethrow it
128  }
129 }
130 
132 template <typename T>
133 inline std::string charToString(const T& value)
134 {
135  return genericToString<int>((int)value, 0);
136 }
137 
139 template <typename T>
140 inline T charFromString(const std::string& str)
141 {
142  int val = genericFromString<int>(str);
143 
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());
150 
151  return static_cast<T>(val);
152 }
153 
154 } // Private
156 
158 
168 template <typename T>
169 class FromHex
170 {
171 public:
172  operator T() const { return value; }
173  friend std::istream& operator>>(std::istream& iStream, FromHex<T>& oValue)
174  {
175  iStream >> std::hex >> oValue.value;
176  return iStream;
177  }
178 
179  T value;
180 };
181 
188 template <typename T>
189 class ToHex
190 {
191 public:
192  ToHex(T v) : value(v) {}
193  operator T() const { return value; }
194  friend std::ostream& operator<<(std::ostream& oStream, const ToHex<T>& iValue)
195  {
196  oStream << std::hex << iValue.value;
197  return oStream;
198  }
199 
200  T value;
201 };
202 
209 template <typename T>
210 class FromOct
211 {
212 public:
213  operator T() const { return value; }
214  friend std::istream& operator>>(std::istream& iStream, FromOct<T>& oValue)
215  {
216  iStream >> std::oct >> oValue.value;
217  return iStream;
218  }
219 
220  T value;
221 };
222 
229 template <typename T>
230 class ToOct
231 {
232 public:
233  ToOct(T v) : value(v) {}
234  operator T() const { return value; }
235  friend std::ostream& operator<<(std::ostream& oStream, const ToOct<T>& iValue)
236  {
237  oStream << std::oct << iValue.value;
238  return oStream;
239  }
240 
241  T value;
242 };
243 
245 
251 template <typename T>
252 std::string toString(const T& value, int precision=-1)
253 {
254  return Private::genericToString<T>(value, precision);
255 }
256 
264 template <typename T>
265 T fromString(const std::string& str)
266 {
267  return Private::genericFromString<T>(str);
268 }
269 
270 
272 template<>
273 inline std::string toString<char>(const char& value, int precision)
274 {
275  return Private::charToString<char>(value);
276 }
277 
279 template <>
280 inline char fromString<char>(const std::string& str)
281 {
282  return Private::charFromString<char>(str);
283 }
284 
286 template<>
287 inline std::string toString<signed char>(const signed char& value,
288  int precision)
289 {
290  return Private::charToString<signed char>(value);
291 }
292 
294 template <>
295 inline signed char fromString<signed char>(const std::string& str)
296 {
297  return Private::charFromString<signed char>(str);
298 }
299 
301 template<>
302 inline std::string toString<unsigned char>(const unsigned char& value,
303  int precision)
304 {
305  return Private::charToString<unsigned char>(value);
306 }
307 
309 template <>
310 inline unsigned char fromString<unsigned char>(const std::string& str)
311 {
312  return Private::charFromString<unsigned char>(str);
313 }
314 
315 
317 template<>
318 inline std::string toString<std::string>(const std::string& value,
319  int precision)
320 {
321  return value;
322 }
323 
325 template <>
326 inline std::string fromString<std::string>(const std::string& str)
327 {
328  return str;
329 }
330 
332 template<>
333 inline std::string toString<float>(const float& value, int precision)
334 {
335  return Private::fpToString<float>(value, precision);
336 }
337 
339 template <>
340 inline float fromString<float>(const std::string& str)
341 {
342  return Private::fpFromString<float>(str);
343 }
344 
346 template<>
347 inline std::string toString<double>(const double& value, int precision)
348 {
349  return Private::fpToString<double>(value, precision);
350 }
351 
353 template <>
354 inline double fromString<double>(const std::string& str)
355 {
356  return Private::fpFromString<double>(str);
357 }
358 
360 template<>
361 inline std::string toString<long double>(const long double& value,
362  int precision)
363 {
364  return Private::fpToString<long double>(value, precision);
365 }
366 
368 template <>
369 inline long double fromString<long double>(const std::string& str)
370 {
371  return Private::fpFromString<long double>(str);
372 }
373 
375 template<>
376 inline std::string toString<bool>(const bool& value, int precision)
377 {
378  if ( value )
379  return "true";
380  else
381  return "false";
382 }
383 
385 template <>
386 inline bool fromString<bool>(const std::string & str)
387 {
388  // case-insensitive compare
389  if (boost::iequals(str, "true"))
390  return true;
391 
392  if (!boost::iequals(str, "false"))
393  MIRA_THROW(XIO, "Failed to convert bool value from string '" << str << "'");
394 
395  return false;
396 }
397 
399 
400 } // namespace
401 
402 #endif
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