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