MIRA
FromString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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_FROMSTRING_H_
48 #define _MIRA_FROMSTRING_H_
49 
50 #include <boost/algorithm/string/predicate.hpp>
51 #include <boost/algorithm/string/trim.hpp>
52 
53 #include <error/Exceptions.h>
54 
55 #include <math/Math.h>
56 
58 
60 
61 namespace mira {
62 
64 
66 namespace Private {
67 
69 template <typename T>
70 inline T genericFromString(const std::string& str)
71 {
72  auto trimmed = boost::algorithm::trim_copy(str);
73 
74  try {
75  return boost::lexical_cast<T>(trimmed);
76  }
77  catch (boost::bad_lexical_cast &e) {
78  MIRA_THROW(XIO, "Failed to convert value from string '" << str << "': " << e.what());
79  }
80 }
81 
83 template <typename T>
84 inline T fpFromString(const std::string& str)
85 {
86  try {
87  return genericFromString<T>(str);
88  } catch(XIO&) {
89  if(str==MIRA_TOSTRING_NAN)
90  return std::numeric_limits<T>::quiet_NaN();
91  else if(str==MIRA_TOSTRING_POSITIVE_INF)
92  return std::numeric_limits<T>::infinity();
93  else if(str==MIRA_TOSTRING_NEGATIVE_INF)
94  return -std::numeric_limits<T>::infinity();
95  else
96  throw; // we could not handle the exception, so rethrow it
97  }
98 }
99 
101 template <typename T>
102 inline T charFromString(const std::string& str)
103 {
104  int val = genericFromString<int>(str);
105 
106  if(val>std::numeric_limits<T>::max())
107  MIRA_THROW(XIO, "Overflow while converting value from string '" <<
108  str << "', max: " << (int)std::numeric_limits<T>::max());
109  if(val<std::numeric_limits<T>::min())
110  MIRA_THROW(XIO, "Underflow while converting value from string '" <<
111  str << "', min: " << (int)std::numeric_limits<T>::min());
112 
113  return static_cast<T>(val);
114 }
115 
116 } // Private
118 
120 
130 template <typename T>
131 class FromHex
132 {
133 public:
134  operator T() const { return value; }
135  friend std::istream& operator>>(std::istream& iStream, FromHex<T>& oValue)
136  {
137  iStream >> std::hex >> oValue.value;
138  return iStream;
139  }
140 
141  T value;
142 };
143 
150 template <typename T>
151 class FromOct
152 {
153 public:
154  operator T() const { return value; }
155  friend std::istream& operator>>(std::istream& iStream, FromOct<T>& oValue)
156  {
157  iStream >> std::oct >> oValue.value;
158  return iStream;
159  }
160 
161  T value;
162 };
163 
165 
173 template <typename T>
174 T fromString(const std::string& str)
175 {
176  return Private::genericFromString<T>(str);
177 }
178 
180 template <>
181 inline char fromString<char>(const std::string& str)
182 {
183  return Private::charFromString<char>(str);
184 }
185 
187 template <>
188 inline signed char fromString<signed char>(const std::string& str)
189 {
190  return Private::charFromString<signed char>(str);
191 }
192 
194 template <>
195 inline unsigned char fromString<unsigned char>(const std::string& str)
196 {
197  return Private::charFromString<unsigned char>(str);
198 }
199 
201 template <>
202 inline std::string fromString<std::string>(const std::string& str)
203 {
204  return str;
205 }
206 
208 template <>
209 inline float fromString<float>(const std::string& str)
210 {
211  return Private::fpFromString<float>(str);
212 }
213 
215 template <>
216 inline double fromString<double>(const std::string& str)
217 {
218  return Private::fpFromString<double>(str);
219 }
220 
222 template <>
223 inline long double fromString<long double>(const std::string& str)
224 {
225  return Private::fpFromString<long double>(str);
226 }
227 
229 template <>
230 inline bool fromString<bool>(const std::string & str)
231 {
232  auto trimmed = boost::algorithm::trim_copy(str);
233 
234  // case-insensitive compare
235  if (boost::iequals(trimmed, "true"))
236  return true;
237 
238  if (!boost::iequals(trimmed, "false"))
239  MIRA_THROW(XIO, "Failed to convert bool value from string '" << str << "'");
240 
241  return false;
242 }
243 
245 
246 } // namespace
247 
248 #endif
Can be used with fromString to convert hex strings into numbers.
Definition: FromString.h:131
signed char fromString< signed char >(const std::string &str)
Specialization for signed char.
Definition: FromString.h:188
T value
Definition: FromString.h:141
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: FromString.h:174
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: FromString.h:155
float fromString< float >(const std::string &str)
Specialization for float.
Definition: FromString.h:209
Can be used with fromString to convert oct strings into numbers.
Definition: FromString.h:151
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:78
Definitions used by fromString/toString conversion.
static constexpr auto MIRA_TOSTRING_POSITIVE_INF
Definition: FromToStringConstants.h:55
Commonly used exception classes.
char fromString< char >(const std::string &str)
Specialization for char.
Definition: FromString.h:181
T value
Definition: FromString.h:161
static constexpr auto MIRA_TOSTRING_NEGATIVE_INF
Definition: FromToStringConstants.h:56
unsigned char fromString< unsigned char >(const std::string &str)
Specialization for unsigned char.
Definition: FromString.h:195
double fromString< double >(const std::string &str)
Specialization for double.
Definition: FromString.h:216
friend std::istream & operator>>(std::istream &iStream, FromHex< T > &oValue)
Definition: FromString.h:135
static constexpr auto MIRA_TOSTRING_NAN
Definition: FromToStringConstants.h:54
bool fromString< bool >(const std::string &str)
Specialization for bool.
Definition: FromString.h:230
long double fromString< long double >(const std::string &str)
Specialization for long double.
Definition: FromString.h:223