ifw-core  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
conversion.hpp
Go to the documentation of this file.
1 
9 #ifndef IFW_CTD_CONVERSION_HPP_
10 #define IFW_CTD_CONVERSION_HPP_
11 
12 #include <typeinfo>
13 #include <iomanip>
14 #include <variant>
15 
16 #include "ctd/defines/defines.hpp"
17 #include "ctd/string/string.hpp"
18 
19 
20 namespace ctd {
21  namespace conversion {
22 
24  // TODO: Check if this function can be replaced with some C++/Boost/fmtlib.net function.
25  template <typename TYPE>
26  std::string NbToStr(const TYPE number,
27  const std::string& format = "") {
28  RAD_LOG_TRACE();
29 
30  std::stringstream nb_str;
31  if ((typeid(number) == typeid(uint8_t)) || (typeid(number) == typeid(int8_t))) {
32  nb_str << int(number);
33  } else if ((typeid(number) == typeid(float)) || (typeid(number) == typeid(double))) {
34  char tmp_buf[64];
35  char tmp_format[64];
36  if (format != "") {
37  strncpy(tmp_format, format.c_str(), sizeof(tmp_format));
38  } else {
39  strncpy(tmp_format, "%%.%df", sizeof(tmp_format));
40  }
41  double tmp_dbl = static_cast<double>(number);
42  snprintf(tmp_buf, sizeof(tmp_buf), tmp_format, tmp_dbl);
43  nb_str << tmp_buf;
44  } else {
45  nb_str << number;
46  }
47  return nb_str.str();
48  }
49 
51  std::string DblToString(const double dbl_val,
52  const std::string& prec_or_format = "3");
53 
55  std::string BoolToString(const bool bool_val,
56  const bool long_format = true);
57 
59  bool Boolean(const std::string& value,
60  const bool permissive = false);
61 
63  template <typename TYPE>
64  void Convert(const std::string& value,
65  ctd::defines::DataType data_type,
66  std::variant<TYPE>& native_value) {
67  RAD_LOG_TRACE();
68 
69  switch (data_type) {
71  native_value = Boolean(value);
72  break;
74  native_value = static_cast<int8_t>(std::stoi(value));
75  break;
77  native_value = static_cast<uint8_t>(std::stoi(value));
78  break;
80  native_value = static_cast<int16_t>(std::stoi(value));
81  break;
83  native_value = static_cast<uint16_t>(std::stoi(value));
84  break;
86  native_value = static_cast<int32_t>(std::stoi(value));
87  break;
89  native_value = static_cast<uint32_t>(std::stol(value));
90  break;
92  native_value = static_cast<int64_t>(std::stoll(value));
93  break;
95  native_value = static_cast<uint64_t>(std::stoll(value));
96  break;
98  native_value = static_cast<float>(std::atof(value.c_str()));
99  break;
101  native_value = static_cast<double>(std::strtod(value.c_str(), nullptr));
102  break;
103 
104  // @todo: Handle:
105  // ctd::defines::DataType::DATA_TYPE_NON_BASIC
106  // ctd::defines::DataType::DATA_TYPE_TIME_DATE
107  // ctd::defines::DataType::DATA_TYPE_TIMESTAMP
108  // ctd::defines::DataType::DATA_TYPE_BLOB
109 
110  default:
111  throw rad::Exception("Illegal data type encountered");
112  }
113  }
114 
116  void Convert(const std::string& str_value,
117  std::string& native_value);
118 
120  template <typename TYPE>
121  void Convert(const std::string& str_value,
122  TYPE& native_value) {
123  RAD_LOG_TRACE();
124 
125  const auto type_it = ctd::defines::TYPE_ID_2_DATA_TYPE_MAP.find(typeid(native_value).name());
126  if (type_it == ctd::defines::TYPE_ID_2_DATA_TYPE_MAP.end()) {
127  throw rad::Exception("Unknown datatype encountered");
128  }
129  ctd::defines::DataType data_type = type_it->second;
130  std::variant<TYPE> tmp_native_value;
131  Convert(str_value, data_type, tmp_native_value);
132  native_value = std::get<TYPE>(tmp_native_value);
133  }
134 
135  }
136 }
137 
138 #endif // IFW_CTD_CONVERSION_HPP_
const std::map< std::string, DataType > TYPE_ID_2_DATA_TYPE_MAP
Mapping from system typeid() string representation to IFW numeric data type.
Definition: types.hpp:150
Definition: types.hpp:37
Definition: types.hpp:38
std::string NbToStr(const TYPE number, const std::string &format="")
Convert the given value to a string representation.
Definition: conversion.hpp:26
std::string BoolToString(const bool value, const bool long_format)
Convert a boolean value to a string (Short format: T/F; Long format: True/False). ...
Definition: conversion.cpp:25
Common definitions.
Definition: types.hpp:41
Definition: types.hpp:36
DataType
Data types numeric representations.
Definition: types.hpp:34
Definition: types.hpp:39
Definition: types.hpp:42
Definition: types.hpp:40
Definition: types.hpp:44
void Convert(const std::string &str_value, std::string &native_value)
Handle case: Conversion of std::string to std::string.
Definition: conversion.cpp:52
Definition: types.hpp:43
std::string DblToString(const double value, const std::string &prec_or_format)
Convert a double value to its string representation, applying the given precision.
Definition: conversion.cpp:9
Utilities for string manipulation.
Definition: types.hpp:45
Definition: types.hpp:35
bool Boolean(const std::string &value, const bool permissive)
Interpret a string as a boolean (t/T/true/TRUE -&gt; true; f/F/false/FALSE -&gt; false).
Definition: conversion.cpp:37