7#ifndef IFW_CORE_UTILS_CONVERSION_HPP_
8#define IFW_CORE_UTILS_CONVERSION_HPP_
15#include <boost/lexical_cast.hpp>
16#include <boost/exception/diagnostic_information.hpp>
26 const std::string& prec_or_format =
"3");
30 const bool long_format =
true);
33 bool Boolean(
const std::string& value,
34 const bool permissive =
false);
38 template <
typename TYPE>
40 const std::string& format =
"") {
43 std::stringstream nb_str;
44 if ((
typeid(number) ==
typeid(uint8_t)) || (
typeid(number) ==
typeid(int8_t))) {
45 nb_str << int(number);
46 }
else if ((
typeid(number) ==
typeid(
float)) || (
typeid(number) ==
typeid(
double))) {
49 dbl_str =
DblToString(
static_cast<double>(number), format);
51 dbl_str =
DblToString(
static_cast<double>(number),
"6");
63 void Convert(
const std::string& str_value,
64 std::string& native_value);
68 template <
typename TYPE>
70 std::variant<TYPE>& native_value) {
74 if (
typeid(TYPE) ==
typeid(int8_t)) {
75 native_value =
static_cast<TYPE
>(std::stoi(str_value));
76 }
else if (
typeid(TYPE) ==
typeid(uint8_t)) {
77 native_value =
static_cast<TYPE
>(std::stoi(str_value));
78 }
else if (
typeid(TYPE) ==
typeid(
bool)) {
79 native_value =
static_cast<TYPE
>(
Boolean(str_value));
81 native_value = boost::lexical_cast<TYPE>(str_value);
86 template <
typename TYPE>
87 void Convert(
const std::string& str_value,
91 std::variant<TYPE> tmp_native_value;
92 Convert_(str_value, tmp_native_value);
93 value = std::get<TYPE>(tmp_native_value);
95 std::string err = (
"Error converting value: |" + str_value +
"| from string to native type (typeid()): |" +
typeid(TYPE).name()
96 +
"|. Error: " + boost::current_exception_diagnostic_information());
97 throw std::runtime_error(err);
log4cplus::Logger & Logger()
Definition tools.cpp:20
Definition conversion.cpp:10
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:39
void Convert_(const std::string &str_value, std::variant< TYPE > &native_value)
Convert a value represented as string into its 'native type'. Data type not given....
Definition conversion.hpp:69
void Convert(const std::string &str_value, std::string &native_value)
Handle case: Conversion of std::string to std::string.
Definition conversion.cpp:53
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:29
std::string NbToStr(const TYPE number, const std::string &format="")
Convert the given value to a string representation.
Definition conversion.hpp:39
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:13