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
117class Convert {
118
119public:
120
122
130 template <class T1, class T2>
131 static void convert(const T1 & src, T2 & dst){
132 Converter<T1>::to(src, dst);
133 }
134
135 template <class T2>
136 static void convert(const char *src, T2 & dst){
138 }
139
141 template <class T>
142 static
143 typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value>::type
144 convert(const T & src, bool & dst){
145 dst = (src != T(0));
146 }
147
149 template <class T>
150 static void convert(const T & src, std::ostream & ostr, const char * format){
151 std::stringstream sstr(src);
152 ostr << sstr.str();
153 }
154
155 template <class T>
156 static void convert(const T & src, std::ostream & ostr, const std::string & format){
157 convert(src, ostr, format.c_str());
158 }
159
160};
161
162
163
164
166
169template <class T>
171
172public:
173
175 static
176 void convert(const T & src, T & dst){
177 dst = src;
178 }
179
180 static
181 void convert(const char * src, T & dst){
182 std::stringstream sstr(src);
183 sstr >> dst;
184 }
185
186
187
188 template <class D>
189 static inline
190 void convert(const T & src, D & dst){
191 convertFrom(src, dst);
192 }
193
194
195 template <class S>
196 static inline
197 void convert(const S & src, T & dst){
198 convertTo(src, dst);
199 }
200
201
203 template <class D>
204 static
205 void convertFrom(const T & src, D & dst){
206 std::stringstream sstr;
207 sstr << src;
208 sstr >> dst;
209 }
210
212 template <class S>
213 static
214 void convertTo(const S & src, T & dst){
215 std::stringstream sstr;
216 sstr << src;
217 sstr >> dst;
218 }
219
220
221};
222
223
224/*
225inline
226std::ostream & operator<<(std::ostream & ostr, const StringMapper & strmap){
227 return strmap.toStream(ostr);
228}
229*/
230
231
232} // drain
233
234#endif /* STRINGMAPPER_H_ */
235
236// Drain
Utility class with static conversions.
Definition Convert.h:170
static void convertTo(const S &src, T &dst)
Convert with cast target type.
Definition Convert.h:214
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition Convert.h:176
static void convertFrom(const T &src, D &dst)
Convert with cast source type.
Definition Convert.h:205
DOC: Add class description.
Definition Convert.h:117
static void convert(const T1 &src, T2 &dst)
General case: dispatch to Converter<T1> traits.
Definition Convert.h:131
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:144
static void convert(const T &src, std::ostream &ostr, const char *format)
Formatted output.
Definition Convert.h:150
Definition Data.h:1473
Utility class with static conversions.
Definition Convert.h:49