Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConversionHelpers.h
Go to the documentation of this file.
1 
11 #ifndef _LIMBO_PROGRAMOPTIONS_CONVERSIONHELPERS_H
12 #define _LIMBO_PROGRAMOPTIONS_CONVERSIONHELPERS_H
13 
14 #include <iostream>
15 #include <vector>
16 #include <set>
17 #include <limbo/string/String.h>
18 
20 namespace limbo
21 {
23 namespace programoptions
24 {
25 
28 
34 template <typename T>
35 struct parse_helper
36 {
40  inline bool operator()(T& target, const char* value) const
41  {
42  target = value;
43  return true;
44  }
45 };
46 
52 template <typename T>
54 {
58  inline void operator()(std::ostream& os, T const& target) const
59  {
60  os << target;
61  }
62 };
63 
69 template <typename T>
71 {
75  inline void operator()(T& target, T const& source) const
76  {
77  target = source;
78  }
79 };
80 
86 template <typename T>
88 {
91  inline bool operator()(T const&) const
92  {
93  return false;
94  }
95 };
96 
98 
101 template <>
102 struct parse_helper<bool>
103 {
107  inline bool operator()(bool& target, const char* value) const
108  {
109  if (limbo::iequals(value, "true") || limbo::iequals(value, "1"))
110  target = true;
111  else if (limbo::iequals(value, "false") || limbo::iequals(value, "0"))
112  target = false;
113  else return false;
114  return true;
115  }
116 };
120 template <>
121 struct parse_helper<char>
122 {
126  inline bool operator()(char& target, const char* value) const
127  {
128  target = *value;
129  return true;
130  }
131 };
135 template <>
136 struct parse_helper<unsigned char>
137 {
141  inline bool operator()(unsigned char& target, const char* value) const
142  {
143  target = *value;
144  return true;
145  }
146 };
150 template <>
151 struct parse_helper<int>
152 {
156  inline bool operator()(int& target, const char* value) const
157  {
158  target = atoi(value);
159  return true;
160  }
161 };
165 template <>
166 struct parse_helper<unsigned int>
167 {
171  inline bool operator()(unsigned int& target, const char* value) const
172  {
173  target = atoi(value);
174  return true;
175  }
176 };
180 template <>
181 struct parse_helper<long>
182 {
186  inline bool operator()(long& target, const char* value) const
187  {
188  target = atol(value);
189  return true;
190  }
191 };
195 template <>
196 struct parse_helper<unsigned long>
197 {
201  inline bool operator()(unsigned long& target, const char* value) const
202  {
203  target = atol(value);
204  return true;
205  }
206 };
210 template <>
211 struct parse_helper<long long>
212 {
216  inline bool operator()(long long& target, const char* value) const
217  {
218  target = atol(value);
219  return true;
220  }
221 };
225 template <>
226 struct parse_helper<unsigned long long>
227 {
231  inline bool operator()(unsigned long long& target, const char* value) const
232  {
233  target = atol(value);
234  return true;
235  }
236 };
240 template <>
241 struct parse_helper<float>
242 {
246  inline bool operator()(float& target, const char* value) const
247  {
248  target = atof(value);
249  return true;
250  }
251 };
255 template <>
256 struct parse_helper<double>
257 {
261  inline bool operator()(double& target, const char* value) const
262  {
263  target = atof(value);
264  return true;
265  }
266 };
271 template <typename T>
272 struct parse_helper<std::vector<T> >
273 {
277  inline bool operator()(std::vector<T>& target, const char* value) const
278  {
279  T v;
280  parse_helper<T>()(v, value);
281  target.push_back(v);
282  return true;
283  }
284 };
289 template <typename T>
290 struct parse_helper<std::set<T> >
291 {
295  inline bool operator()(std::set<T>& target, const char* value) const
296  {
297  T v;
298  parse_helper<T>()(v, value);
299  target.insert(v);
300  return true;
301  }
302 };
303 
305 
308 template <>
309 struct print_helper<bool>
310 {
314  inline void operator()(std::ostream& os, bool const& target) const
315  {
316  os << ((target)? "true" : "false");
317  }
318 };
323 template <typename T>
324 struct print_helper<std::vector<T> >
325 {
329  inline void operator()(std::ostream& os, std::vector<T> const& target) const
330  {
331  const char* prefix = "";
332  for (typename std::vector<T>::const_iterator it = target.begin(); it != target.end(); ++it)
333  {
334  os << prefix;
335  print_helper<T>()(os, *it);
336  prefix = ",";
337  }
338  }
339 };
344 template <typename T>
345 struct print_helper<std::set<T> >
346 {
350  inline void operator()(std::ostream& os, std::set<T> const& target) const
351  {
352  const char* prefix = "";
353  for (typename std::set<T>::const_iterator it = target.begin(); it != target.end(); ++it)
354  {
355  os << prefix;
356  print_helper<T>()(os, *it);
357  prefix = ",";
358  }
359  }
360 };
361 
363 
365 
368 template <>
369 struct boolean_helper<bool>
370 {
373  inline bool operator()(bool const& target) const
374  {
375  return target;
376  }
377 };
378 
379 } // programoptions
380 } // namespace limbo
381 
382 #endif
void operator()(std::ostream &os, bool const &target) const
printing target
bool operator()(long &target, const char *value) const
parse a char-based string to target
metafunction for parsing a char-based string to a target data type
bool operator()(std::vector< T > &target, const char *value) const
parse a char-based string to target
bool operator()(std::set< T > &target, const char *value) const
parse a char-based string to target
bool operator()(unsigned char &target, const char *value) const
parse a char-based string to target
bool operator()(bool &target, const char *value) const
parse a char-based string to target
bool operator()(unsigned int &target, const char *value) const
parse a char-based string to target
bool operator()(unsigned long long &target, const char *value) const
parse a char-based string to target
void operator()(std::ostream &os, T const &target) const
printing target
bool operator()(float &target, const char *value) const
parse a char-based string to target
bool operator()(bool const &target) const
generic boolean operation
metafunction for printing a target data type
bool operator()(unsigned long &target, const char *value) const
parse a char-based string to target
void operator()(T &target, T const &source) const
assign source to target
bool operator()(double &target, const char *value) const
parse a char-based string to target
bool operator()(int &target, const char *value) const
parse a char-based string to target
void operator()(std::ostream &os, std::set< T > const &target) const
printing target
bool operator()(T &target, const char *value) const
parse a char-based string to target
namespace for Limbo
Definition: GraphUtility.h:22
bool iequals(string const &s1, string const &s2)
check two strings equal, case-insensitive
Definition: String.h:108
Check string is integer, floating point, number... Convert string to upper/lower cases.
metafunction for assign a source data type to a target data type
void operator()(std::ostream &os, std::vector< T > const &target) const
printing target
bool operator()(char &target, const char *value) const
parse a char-based string to target
metafunction for boolean operation
bool operator()(long long &target, const char *value) const
parse a char-based string to target
bool operator()(T const &) const
generic boolean operation