Limbo 3.5.4
Loading...
Searching...
No Matches
ConversionHelpers.h
Go to the documentation of this file.
1
10
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
20namespace limbo
21{
24{
25
28
34template <typename T>
36{
40 inline bool operator()(T& target, const char* value) const
41 {
42 target = value;
43 return true;
44 }
45};
46
52template <typename T>
54{
58 inline void operator()(std::ostream& os, T const& target) const
59 {
60 os << target;
61 }
62};
63
69template <typename T>
71{
75 inline void operator()(T& target, T const& source) const
76 {
77 target = source;
78 }
79};
80
86template <typename T>
88{
91 inline bool operator()(T const&) const
92 {
93 return false;
94 }
95};
96
98
101template <>
102struct 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};
117
120template <>
121struct parse_helper<char>
122{
126 inline bool operator()(char& target, const char* value) const
127 {
128 target = *value;
129 return true;
130 }
131};
132
135template <>
136struct 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};
147
150template <>
151struct parse_helper<int>
152{
156 inline bool operator()(int& target, const char* value) const
157 {
158 target = atoi(value);
159 return true;
160 }
161};
162
165template <>
166struct 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};
177
180template <>
181struct parse_helper<long>
182{
186 inline bool operator()(long& target, const char* value) const
187 {
188 target = atol(value);
189 return true;
190 }
191};
192
195template <>
196struct 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};
207
210template <>
211struct 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};
222
225template <>
226struct 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};
237
240template <>
241struct parse_helper<float>
242{
246 inline bool operator()(float& target, const char* value) const
247 {
248 target = atof(value);
249 return true;
250 }
251};
252
255template <>
256struct parse_helper<double>
257{
261 inline bool operator()(double& target, const char* value) const
262 {
263 target = atof(value);
264 return true;
265 }
266};
267
271template <typename T>
272struct 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};
285
289template <typename T>
290struct 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
308template <>
309struct print_helper<bool>
310{
314 inline void operator()(std::ostream& os, bool const& target) const
315 {
316 os << ((target)? "true" : "false");
317 }
318};
319
323template <typename T>
324struct 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};
340
344template <typename T>
345struct 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
368template <>
369struct 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
Check string is integer, floating point, number... Convert string to upper/lower cases.
namespace for Limbo.ProgramOptions
namespace for Limbo
bool iequals(string const &s1, string const &s2)
check two strings equal, case-insensitive
Definition String.h:108
metafunction for assign a source data type to a target data type
void operator()(T &target, T const &source) const
assign source to target
bool operator()(bool const &target) const
generic boolean operation
metafunction for boolean operation
bool operator()(T const &) const
generic boolean operation
bool operator()(bool &target, const char *value) const
parse a char-based string to target
bool operator()(char &target, const char *value) const
parse a char-based string to target
bool operator()(double &target, const char *value) const
parse a char-based string to target
bool operator()(float &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
bool operator()(long &target, const char *value) const
parse a char-based string to target
bool operator()(long long &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()(std::vector< 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()(unsigned int &target, const char *value) const
parse a char-based string to target
bool operator()(unsigned long &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
metafunction for parsing a char-based string to a target data type
bool operator()(T &target, const char *value) const
parse a char-based string to target
void operator()(std::ostream &os, bool const &target) const
printing target
void operator()(std::ostream &os, std::set< T > const &target) const
printing target
void operator()(std::ostream &os, std::vector< T > const &target) const
printing target
metafunction for printing a target data type
void operator()(std::ostream &os, T const &target) const
printing target