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 
47 #ifndef _MIRA_TOSTRING_H_
48 #define _MIRA_TOSTRING_H_
49 
50 #include <boost/algorithm/string/predicate.hpp>
51 
52 #include <error/Exceptions.h>
53 
54 #include <math/Math.h>
55 
57 
59 
60 namespace mira {
61 
63 
65 namespace Private {
66 
68 template <typename T>
69 inline std::string genericToString(const T& value, int precision)
70 {
71  std::stringstream ss;
72  if(precision>=0)
73  ss << std::setprecision(precision);
74  ss << value;
75  if(ss.fail())
76  MIRA_THROW(XIO, "Failed to convert value into a string");
77  return ss.str();
78 }
79 
81 template <typename T>
82 inline std::string fpToString(const T& value, int precision)
83 {
84  if(boost::math::isnan(value))
85  return MIRA_TOSTRING_NAN;
86  else if(boost::math::isinf(value)) {
87  if(value>0)
89  else
91  }
92  else
93  return genericToString<T>(value, precision);
94 }
95 
97 template <typename T>
98 inline std::string charToString(const T& value)
99 {
100  return genericToString<int>((int)value, 0);
101 }
102 
103 } // Private
105 
107 
114 template <typename T>
115 class ToHex
116 {
117 public:
118  ToHex(T v) : value(v) {}
119  operator T() const { return value; }
120  friend std::ostream& operator<<(std::ostream& oStream, const ToHex<T>& iValue)
121  {
122  oStream << std::hex << iValue.value;
123  return oStream;
124  }
125 
126  T value;
127 };
128 
135 template <typename T>
136 class ToOct
137 {
138 public:
139  ToOct(T v) : value(v) {}
140  operator T() const { return value; }
141  friend std::ostream& operator<<(std::ostream& oStream, const ToOct<T>& iValue)
142  {
143  oStream << std::oct << iValue.value;
144  return oStream;
145  }
146 
147  T value;
148 };
149 
151 
157 template <typename T>
158 std::string toString(const T& value, int precision=-1)
159 {
160  return Private::genericToString<T>(value, precision);
161 }
162 
164 template<>
165 inline std::string toString<char>(const char& value, int precision)
166 {
167  return Private::charToString<char>(value);
168 }
169 
171 template<>
172 inline std::string toString<signed char>(const signed char& value,
173  int precision)
174 {
175  return Private::charToString<signed char>(value);
176 }
177 
179 template<>
180 inline std::string toString<unsigned char>(const unsigned char& value,
181  int precision)
182 {
183  return Private::charToString<unsigned char>(value);
184 }
185 
187 template<>
188 inline std::string toString<std::string>(const std::string& value,
189  int precision)
190 {
191  return value;
192 }
193 
195 template<>
196 inline std::string toString<float>(const float& value, int precision)
197 {
198  return Private::fpToString<float>(value, precision);
199 }
200 
202 template<>
203 inline std::string toString<double>(const double& value, int precision)
204 {
205  return Private::fpToString<double>(value, precision);
206 }
207 
209 template<>
210 inline std::string toString<long double>(const long double& value,
211  int precision)
212 {
213  return Private::fpToString<long double>(value, precision);
214 }
215 
217 template<>
218 inline std::string toString<bool>(const bool& value, int precision)
219 {
220  if ( value )
221  return "true";
222  else
223  return "false";
224 }
225 
227 
228 } // namespace
229 
230 #endif
std::string toString< bool >(const bool &value, int precision)
Specialization for bool.
Definition: ToString.h:218
T value
Definition: ToString.h:126
Includes often needed math headers and methods and provides additional constants. ...
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Can be used with toString to convert values to their string hex representation.
Definition: ToString.h:115
std::string toString< long double >(const long double &value, int precision)
Specialization for long double.
Definition: ToString.h:210
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
Definitions used by fromString/toString conversion.
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:158
ToOct(T v)
Definition: ToString.h:139
static constexpr auto MIRA_TOSTRING_POSITIVE_INF
Definition: FromToStringConstants.h:55
std::string toString< unsigned char >(const unsigned char &value, int precision)
Specialization for unsigned char.
Definition: ToString.h:180
Commonly used exception classes.
std::string toString< signed char >(const signed char &value, int precision)
Specialization for signed char.
Definition: ToString.h:172
static constexpr auto MIRA_TOSTRING_NEGATIVE_INF
Definition: FromToStringConstants.h:56
ToHex(T v)
Definition: ToString.h:118
std::string toString< float >(const float &value, int precision)
Specialization for float.
Definition: ToString.h:196
Can be used with toString to convert values to their string oct representation.
Definition: ToString.h:136
std::string toString< char >(const char &value, int precision)
Specialization for char.
Definition: ToString.h:165
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:286
static constexpr auto MIRA_TOSTRING_NAN
Definition: FromToStringConstants.h:54
std::string toString< double >(const double &value, int precision)
Specialization for double.
Definition: ToString.h:203
T value
Definition: ToString.h:147