Convert.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 "String.h"
44 
45 namespace drain {
46 
48 
51 class Convert {
52 
53 public:
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::stringstream sstr;
67  sstr << src;
68  sstr >> dst;
69  }
70 
71  template <class D>
72  static
73  void convert(const char * src, D & dst){
74  std::stringstream sstr(src);
75  //sstr << src;
76  sstr >> dst;
77  }
78 
79 
80 
81 };
82 
84 
87 template <class T>
88 class Convert2 {
89 
90 public:
91 
93  static
94  void convert(const T & src, T & dst){
95  dst = src;
96  }
97 
98  static
99  void convert(const char * src, T & dst){
100  std::stringstream sstr(src);
101  sstr >> dst;
102  }
103 
104 
105 
106  template <class D>
107  static inline
108  void convert(const T & src, D & dst){
109  convertFrom(src, dst);
110  }
111 
112 
113  template <class S>
114  static inline
115  void convert(const S & src, T & dst){
116  convertTo(src, dst);
117  }
118 
119 
121  template <class D>
122  static
123  void convertFrom(const T & src, D & dst){
124  std::stringstream sstr;
125  sstr << src;
126  sstr >> dst;
127  }
128 
130  template <class S>
131  static
132  void convertTo(const S & src, T & dst){
133  std::stringstream sstr;
134  sstr << src;
135  sstr >> dst;
136  }
137 
138 
139 };
140 
141 
142 /*
143 inline
144 std::ostream & operator<<(std::ostream & ostr, const StringMapper & strmap){
145  return strmap.toStream(ostr);
146 }
147 */
148 
149 
150 } // drain
151 
152 #endif /* STRINGMAPPER_H_ */
153 
154 // Drain
Utility class with static conversions.
Definition: Convert.h:88
static void convertTo(const S &src, T &dst)
Convert with cast target type.
Definition: Convert.h:132
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition: Convert.h:94
static void convertFrom(const T &src, D &dst)
Convert with cast source type.
Definition: Convert.h:123
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