Loading...
Searching...
No Matches
Convert.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef DRAIN_CONVERT_FUNDAMENTAL
33#define DRAIN_CONVERT_FUNDAMENTAL
34
35#include <map>
36#include <list>
37#include <iterator>
38#include <sstream>
39
40
41namespace drain {
42
44
45
47
48template <class T>
49struct Converter {
50
51 static void to(const T & src, T & dst){
52 dst = src;
53 }
54
55 template <class U>
56 static void to(const T & src, U & dst){
57 std::stringstream sstr;
58 sstr << std::boolalpha << src;
59 sstr >> dst;
60 }
61};
62
64template <>
65struct Converter<std::string> {
66
67 static void to(const std::string & src, std::string & dst){ dst = src; }
68
69 static void to(const std::string & src, bool & dst){
70 dst = (src == "true" || src == "True" || src == "TRUE");
71 }
72
73 template <class U>
74 static void to(const std::string & src, U & dst){
75 std::stringstream sstr(src);
76 sstr >> dst;
77 }
78};
79
81template <>
82struct Converter<const char *> {
83 template <class U>
84 static void to(const char * src, U & dst){
85 Converter<std::string>::to(std::string(src), dst);
86 }
87};
88
90template <>
91struct Converter<char *> {
92 template <class U>
93 static void to(const char * src, U & dst){
95 }
96};
97
99template <>
100struct Converter<bool> {
101
102 static void to(const bool & src, bool & dst){ dst = src; }
103
104 static void to(const bool & src, std::string & dst){ dst = src ? "true" : "false"; }
105
106 template <class U>
107 static
108 typename std::enable_if<std::is_arithmetic<U>::value && !std::is_same<U, bool>::value>::type
109 to(const bool & src, U & dst){
110 dst = static_cast<U>(src);
111 }
112};
113
114
115
116class Convert {
117
118public:
119
121
129 template <class T1, class T2>
130 static void convert(const T1 & src, T2 & dst){
131 Converter<T1>::to(src, dst);
132 }
133
134 template <class T2>
135 static void convert(const char *src, T2 & dst){
137 }
138
140 template <class T>
141 static
142 typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value>::type
143 convert(const T & src, bool & dst){
144 dst = (src != T(0));
145 }
146
148 template <class T>
149 static void convert(const T & src, std::ostream & ostr, const char * format){
150 std::stringstream sstr(src);
151 ostr << sstr.str();
152 }
153
154 template <class T>
155 static void convert(const T & src, std::ostream & ostr, const std::string & format){
156 convert(src, ostr, format.c_str());
157 }
158
159};
160
161
162
163
165
168template <class T>
170
171public:
172
174 static
175 void convert(const T & src, T & dst){
176 dst = src;
177 }
178
179 static
180 void convert(const char * src, T & dst){
181 std::stringstream sstr(src);
182 sstr >> dst;
183 }
184
185
186
187 template <class D>
188 static inline
189 void convert(const T & src, D & dst){
190 convertFrom(src, dst);
191 }
192
193
194 template <class S>
195 static inline
196 void convert(const S & src, T & dst){
197 convertTo(src, dst);
198 }
199
200
202 template <class D>
203 static
204 void convertFrom(const T & src, D & dst){
205 std::stringstream sstr;
206 sstr << src;
207 sstr >> dst;
208 }
209
211 template <class S>
212 static
213 void convertTo(const S & src, T & dst){
214 std::stringstream sstr;
215 sstr << src;
216 sstr >> dst;
217 }
218
219
220};
221
222
223/*
224inline
225std::ostream & operator<<(std::ostream & ostr, const StringMapper & strmap){
226 return strmap.toStream(ostr);
227}
228*/
229
230
231} // drain
232
233#endif /* STRINGMAPPER_H_ */
234
235// Drain
Utility class with static conversions.
Definition Convert.h:169
static void convertTo(const S &src, T &dst)
Convert with cast target type.
Definition Convert.h:213
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition Convert.h:175
static void convertFrom(const T &src, D &dst)
Convert with cast source type.
Definition Convert.h:204
Definition Convert.h:116
static void convert(const T1 &src, T2 &dst)
General case: dispatch to Converter<T1> traits.
Definition Convert.h:130
static std::enable_if< std::is_arithmetic< T >::value &&!std::is_same< T, bool >::value >::type convert(const T &src, bool &dst)
Numeric -> bool: zero is false, any other value is true.
Definition Convert.h:143
static void convert(const T &src, std::ostream &ostr, const char *format)
Formatted output.
Definition Convert.h:149
Definition DataSelector.cpp:1277
Utility class with static conversions.
Definition Convert.h:49