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//#include "Log.h"
41//#include "MapTools.h"
42//#include "Sprinter.h"
43// #include "StringTools.h"
44
45namespace drain {
46
48
51class Convert {
52
53public:
54
56 template <class T>
57 static
58 void convert(const T & src, T & dst){
59 dst = src;
60 }
61
62
63 template <class S, class D>
64 static
65 void convert(const S & src, D & dst){
66 //std::ostringstream / std::istringstream,
67 std::stringstream sstr;
68 sstr << src;
69 sstr >> dst;
70 }
71
72 template <class D>
73 static
74 void convert(const char * src, D & dst){
75 std::ostringstream sstr(src);
76 //sstr << src;
77 sstr >> dst;
78 if (sstr.failbit){
79 std::cerr << __FILE__ << " read of " << src << " failed with " << sstr.failbit << std::endl;
80 }
81 }
82
83
84
85};
86
88
91template <class T>
92class Convert2 {
93
94public:
95
97 static
98 void convert(const T & src, T & dst){
99 dst = src;
100 }
101
102 static
103 void convert(const char * src, T & dst){
104 std::stringstream sstr(src);
105 sstr >> dst;
106 }
107
108
109
110 template <class D>
111 static inline
112 void convert(const T & src, D & dst){
113 convertFrom(src, dst);
114 }
115
116
117 template <class S>
118 static inline
119 void convert(const S & src, T & dst){
120 convertTo(src, dst);
121 }
122
123
125 template <class D>
126 static
127 void convertFrom(const T & src, D & dst){
128 std::stringstream sstr;
129 sstr << src;
130 sstr >> dst;
131 }
132
134 template <class S>
135 static
136 void convertTo(const S & src, T & dst){
137 std::stringstream sstr;
138 sstr << src;
139 sstr >> dst;
140 }
141
142
143};
144
145
146/*
147inline
148std::ostream & operator<<(std::ostream & ostr, const StringMapper & strmap){
149 return strmap.toStream(ostr);
150}
151*/
152
153
154} // drain
155
156#endif /* STRINGMAPPER_H_ */
157
158// Drain
Utility class with static conversions.
Definition Convert.h:92
static void convertTo(const S &src, T &dst)
Convert with cast target type.
Definition Convert.h:136
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition Convert.h:98
static void convertFrom(const T &src, D &dst)
Convert with cast source type.
Definition Convert.h:127
Utility class with static conversions.
Definition Convert.h:51
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition Convert.h:58
Definition DataSelector.cpp:1277