6#ifndef IFW_CORE_UTILS_CONVERSION_HPP_
7#define IFW_CORE_UTILS_CONVERSION_HPP_
14#include <boost/lexical_cast.hpp>
15#include <boost/exception/diagnostic_information.hpp>
25 const std::string& prec_or_format =
"3");
29 const bool long_format =
true);
32 bool Boolean(
const std::string& value,
33 const bool permissive =
false);
37 template <
typename TYPE>
39 const std::string& format =
"") {
42 std::stringstream nb_str;
43 if ((
typeid(number) ==
typeid(uint8_t)) || (
typeid(number) ==
typeid(int8_t))) {
44 nb_str << int(number);
45 }
else if ((
typeid(number) ==
typeid(
float)) || (
typeid(number) ==
typeid(
double))) {
48 dbl_str =
DblToString(
static_cast<double>(number), format);
50 dbl_str =
DblToString(
static_cast<double>(number),
"6");
62 void Convert(
const std::string& str_value,
63 std::string& native_value);
67 template <
typename TYPE>
69 std::variant<TYPE>& native_value) {
73 if (
typeid(TYPE) ==
typeid(int8_t)) {
74 native_value =
static_cast<TYPE
>(std::stoi(str_value));
75 }
else if (
typeid(TYPE) ==
typeid(uint8_t)) {
76 native_value =
static_cast<TYPE
>(std::stoi(str_value));
77 }
else if (
typeid(TYPE) ==
typeid(bool)) {
78 native_value =
static_cast<TYPE
>(
Boolean(str_value));
80 native_value = boost::lexical_cast<TYPE>(str_value);
85 template <
typename TYPE>
86 void Convert(
const std::string& str_value,
90 std::variant<TYPE> tmp_native_value;
91 Convert_(str_value, tmp_native_value);
92 value = std::get<TYPE>(tmp_native_value);
94 std::string err = (
"Error converting value: |" + str_value +
"| from string to native type (typeid()): |" +
typeid(TYPE).name()
95 +
"|. Error: " + boost::current_exception_diagnostic_information());
96 throw std::runtime_error(err);
log4cplus::Logger & Logger()
Definition tools.cpp:19
Definition conversion.cpp:9
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:38
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:68
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 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:28
std::string NbToStr(const TYPE number, const std::string &format="")
Convert the given value to a string representation.
Definition conversion.hpp:38
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:12