9 #ifndef IFW_CTD_CONVERSION_HPP_
10 #define IFW_CTD_CONVERSION_HPP_
21 namespace conversion {
25 template <
typename TYPE>
27 const std::string& format =
"") {
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))) {
37 strncpy(tmp_format, format.c_str(),
sizeof(tmp_format));
39 strncpy(tmp_format,
"%%.%df",
sizeof(tmp_format));
41 double tmp_dbl =
static_cast<double>(number);
42 snprintf(tmp_buf,
sizeof(tmp_buf), tmp_format, tmp_dbl);
52 const std::string& prec_or_format =
"3");
56 const bool long_format =
true);
59 bool Boolean(
const std::string& value,
60 const bool permissive =
false);
63 template <
typename TYPE>
66 std::variant<TYPE>& native_value) {
74 native_value =
static_cast<int8_t
>(std::stoi(value));
77 native_value =
static_cast<uint8_t
>(std::stoi(value));
80 native_value =
static_cast<int16_t
>(std::stoi(value));
83 native_value =
static_cast<uint16_t
>(std::stoi(value));
86 native_value =
static_cast<int32_t
>(std::stoi(value));
89 native_value =
static_cast<uint32_t
>(std::stol(value));
92 native_value =
static_cast<int64_t
>(std::stoll(value));
95 native_value =
static_cast<uint64_t
>(std::stoll(value));
98 native_value =
static_cast<float>(std::atof(value.c_str()));
101 native_value =
static_cast<double>(std::strtod(value.c_str(),
nullptr));
111 throw rad::Exception(
"Illegal data type encountered");
116 void Convert(
const std::string& str_value,
117 std::string& native_value);
120 template <
typename TYPE>
122 TYPE& native_value) {
127 throw rad::Exception(
"Unknown datatype encountered");
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);
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
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
DataType
Data types numeric representations.
Definition: types.hpp:34
void Convert(const std::string &str_value, std::string &native_value)
Handle case: Conversion of std::string to std::string.
Definition: conversion.cpp:52
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.
bool Boolean(const std::string &value, const bool permissive)
Interpret a string as a boolean (t/T/true/TRUE -> true; f/F/false/FALSE -> false).
Definition: conversion.cpp:37