ifw-core 7.0.0
 
Loading...
Searching...
No Matches
conversion.hpp
Go to the documentation of this file.
1
6
7#ifndef IFW_CORE_UTILS_CONVERSION_HPP_
8#define IFW_CORE_UTILS_CONVERSION_HPP_
9
10#include <typeinfo>
11#include <iomanip>
12#include <variant>
13#include <iostream>
14
15#include <boost/lexical_cast.hpp>
16#include <boost/exception/diagnostic_information.hpp>
17
20
21
23
25 std::string DblToString(const double dbl_val,
26 const std::string& prec_or_format = "3");
27
29 std::string BoolToString(const bool bool_val,
30 const bool long_format = true);
31
33 bool Boolean(const std::string& value,
34 const bool permissive = false);
35
37 // TODO: Check if this function can be replaced with some C++/Boost/fmtlib.net function.
38 template <typename TYPE>
39 std::string NbToStr(const TYPE number,
40 const std::string& format = "") {
41 LOG4CPLUS_TRACE_METHOD(ifw::core::utils::base::Logger(), __PRETTY_FUNCTION__);
42
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))) {
47 std::string dbl_str;
48 try {
49 dbl_str = DblToString(static_cast<double>(number), format);
50 } catch (...) {
51 dbl_str = DblToString(static_cast<double>(number), "6");
52 }
53 nb_str << dbl_str;
54 } else {
55 nb_str << number;
56 }
57 return nb_str.str();
58 }
59
60
61 // This function is needed to avoid a compilation warning.
63 void Convert(const std::string& str_value,
64 std::string& native_value);
65
68 template <typename TYPE>
69 void Convert_(const std::string& str_value,
70 std::variant<TYPE>& native_value) {
71 LOG4CPLUS_TRACE_METHOD(ifw::core::utils::base::Logger(), __PRETTY_FUNCTION__);
72 // Special handling for the types: int8_t, uint8_t, bool.
73 // if (std::type_index(typeid(TYPE)) == std::type_index(typeid(int8_t))) {
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));
80 } else {
81 native_value = boost::lexical_cast<TYPE>(str_value);
82 }
83 }
84
86 template <typename TYPE>
87 void Convert(const std::string& str_value,
88 TYPE& value) {
89 LOG4CPLUS_TRACE_METHOD(ifw::core::utils::base::Logger(), __PRETTY_FUNCTION__);
90 try {
91 std::variant<TYPE> tmp_native_value;
92 Convert_(str_value, tmp_native_value);
93 value = std::get<TYPE>(tmp_native_value);
94 } catch (...) {
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);
98 }
99 }
100
101}
102
103#endif // IFW_CORE_UTILS_CONVERSION_HPP_
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