commonlib
type_converter.h
Go to the documentation of this file.
1 
11 #pragma once
12 #include "xml_exception.h"
13 #include <cstdint>
14 #include <string>
15 #include <deque>
16 #include <forward_list>
17 #include <vector>
18 #include <list>
19 #include <array>
20 #include <sstream>
21 #include <cfloat>
22 #include <algorithm>
24 #include "commonlib/datatypes/stringhelper.h"
25 
26 BEGIN_NAMESPACE_SERIALIZATION
27 
28 template <typename T>
30 {
31  static bool const value = false;
32 };
33 
34 template <typename T, size_t N>
35 struct is_container<std::array<T, N>>
36 {
37  static bool const value = true;
38 };
39 
40 template <typename... Args>
41 struct is_container<std::vector<Args...>>
42 {
43  static bool const value = true;
44 };
45 
46 template <typename... Args>
47 struct is_container<std::deque<Args...>>
48 {
49  static bool const value = true;
50 };
51 
52 template <typename... Args>
53 struct is_container<std::list<Args...>>
54 {
55  static bool const value = true;
56 };
57 
58 template <typename... Args>
59 struct is_container<std::forward_list<Args...>>
60 {
61  static bool const value = true;
62 };
63 
64 template <typename T>
66 {
67  static bool const value = false;
68 };
69 
70 template <typename... Args>
71 struct has_back_insert<std::vector<Args...>>
72 {
73  static bool const value = true;
74 };
75 
76 template <typename... Args>
77 struct has_back_insert<std::list<Args...>>
78 {
79  static bool const value = true;
80 };
81 
82 template <typename... Args>
83 struct has_back_insert<std::deque<Args...>>
84 {
85  static bool const value = true;
86 };
87 
88 template <typename T, typename T2 = void>
90 
91 template <>
92 struct TypeConverter<std::string>
93 {
94  std::string defaultValue = "";
95 
96  static std::string toString(const std::string& value)
97  {
98  return value;
99  }
100 
101  static std::string convertStringToProperType(const std::string& value)
102  {
103  return value;
104  }
105 };
106 
107 template <>
108 struct TypeConverter<const char*>
109 {
110  std::string defaultValue = "";
111 
112  static std::string toString(const char* value)
113  {
114  return value;
115  }
116 
117  static std::string convertStringToProperType(const std::string& value)
118  {
119  return value;
120  }
121 };
122 
123 template <int N>
124 struct TypeConverter<char[N]>
125 {
126  std::string defaultValue = "";
127 
128  static std::string toString(const char* value)
129  {
130  return value;
131  }
132 
133  static std::string convertStringToProperType(const std::string& value)
134  {
135  return value;
136  }
137 };
138 
139 template <>
140 struct TypeConverter<bool>
141 {
142  bool defaultValue = false;
143 
144  static std::string toString(const bool value)
145  {
146  if (value)
147  return "True";
148 
149  return "False";
150  }
151 
152  static bool convertStringToProperType(const std::string& value)
153  {
154  std::string boolString = value;
155  std::transform(boolString.begin(), boolString.end(), boolString.begin(), [](char c) {
156  return char(tolower(c));
157  });
158 
159  if (boolString == "true")
160  return true;
161 
162  if (boolString == "false")
163  return false;
164 
165  char* pEnd;
166  if (!Mathematics::MathFuncs::areSame(strtod(boolString.c_str(), &pEnd), 0.0) && std::string(pEnd).empty())
167  return true;
168 
169  return false;
170  }
171 };
172 
173 template <typename T>
174 struct TypeConverter<T, typename std::enable_if<std::is_floating_point<T>::value && !is_container<T>::value>::type>
175 {
176  T defaultValue = 0.0;
177 
178  static void changeDecimalToCLocale(std::string& value)
179  {
180  const auto decPoint = std::use_facet<std::numpunct<char>>(std::locale(setlocale(LC_NUMERIC, nullptr))).decimal_point();
181  for (char& c : value)
182  {
183  if (c == ',' || c == '.')
184  {
185  c = decPoint;
186  return;
187  }
188  }
189  }
190 
191  static std::string toString(const T& value)
192  {
193  std::stringstream ssFloatingPoint;
194 
195  if (std::is_same<T, float>::value)
196  {
197  ssFloatingPoint.precision(FLT_DIG);
198  }
199  else
200  {
201  ssFloatingPoint.precision(DBL_DIG);
202  }
203  ssFloatingPoint << std::fixed << value;
204  return ssFloatingPoint.str();
205  }
206 
207  static T convertStringToProperType(const std::string& value)
208  {
209  std::string tempStr(value);
210  changeDecimalToCLocale(tempStr);
211 
212  if (std::is_same<T, float>::value)
213  {
214  return static_cast<T>(std::stof(tempStr));
215  }
216  else
217  {
218  return static_cast<T>(std::stod(tempStr));
219  }
220  }
221 };
222 
223 template <typename T>
224 struct TypeConverter<T, typename std::enable_if<std::is_signed<T>::value && std::is_integral<T>::value && !is_container<T>::value>::type>
225 {
226  T defaultValue = 0;
227 
228  static std::string toString(const T& value)
229  {
230  return std::to_string(value);
231  }
232 
233  static int64_t convertStringToProperType(const std::string& value)
234  {
235  return std::stoll(value);
236  }
237 };
238 
239 template <typename T>
240 struct TypeConverter<T, typename std::enable_if<std::is_unsigned<T>::value && std::is_integral<T>::value && !is_container<T>::value>::type>
241 {
242  T defaultValue = 0;
243 
244  static std::string toString(const T& value)
245  {
246  return std::to_string(value);
247  }
248 
249  static uint64_t convertStringToProperType(const std::string& value)
250  {
251  return std::stoull(value);
252  }
253 };
254 
255 template <typename T, typename T2 = void>
257 
258 template <typename T>
259 struct ContainerInputIterators<T, typename std::enable_if<is_container<T>::value>::type>
260 {
261  static auto begin(const T& container)
262  {
263  return std::begin(container);
264  }
265 
266  static auto end(const T& container)
267  {
268  return std::end(container);
269  }
270 };
271 
272 template <typename T, typename T2 = void>
274 
275 template <typename T>
276 struct ContainerOutputIterators<T, typename std::enable_if<is_container<T>::value>::type>
277 {
278  static auto begin(T& container)
279  {
280  return std::begin(container);
281  }
282 
283  static auto end(T& container)
284  {
285  return std::end(container);
286  }
287 };
288 
289 template <typename IteratorCategory, typename ValueType>
291 {
292 public:
293  using iterator_category = IteratorCategory;
294  using value_type = ValueType;
295  using difference_type = std::ptrdiff_t;
296  using pointer = ValueType*;
297  using reference = ValueType&;
298 };
299 
300 template <class T>
301 struct output_iterator_traits : std::iterator_traits<T>
302 {
303 };
304 
305 template <class Container>
306 struct output_iterator_traits<std::back_insert_iterator<Container>>
307  : CustomIteratorTypes<std::output_iterator_tag, typename Container::value_type>
308 {
309 };
310 
311 END_NAMESPACE_SERIALIZATION
312 
MathFuncs class implements helper functions for mathematical operations.
Definition: type_converter.h:256
XmlException class that inherits from std::exception and is used for exception handling in xml serial...
STL namespace.
Definition: type_converter.h:273
Definition: type_converter.h:65
Definition: type_converter.h:290
Definition: type_converter.h:89
Definition: type_converter.h:29
Definition: type_converter.h:301