ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
conversion.hpp
Go to the documentation of this file.
1 
6 #ifndef IFW_CTD_CONVERSION_HPP_
7 #define IFW_CTD_CONVERSION_HPP_
8 
9 #include <typeinfo>
10 #include <iomanip>
11 
12 #include "ctd/defines/defines.hpp"
13 #include "ctd/string/string.hpp"
14 
15 
16 namespace ctd {
17 namespace conversion {
18 
20 template <class TYPE> std::string NbToStr(const TYPE number,
21  const std::string& format = "") {
22  RAD_LOG_TRACE();
23 
24  std::stringstream nb_str;
25  if ((typeid(number) == typeid(uint8_t)) || (typeid(number) == typeid(int8_t))) {
26  nb_str << int(number);
27  } else if ((typeid(number) == typeid(float)) || (typeid(number) == typeid(double))) {
28  char tmp_buf[64];
29  char tmp_format[64];
30  if (format != "") {
31  strncpy(tmp_format, format.c_str(), sizeof(tmp_format));
32  } else {
33  strncpy(tmp_format, "%%.%df", sizeof(tmp_format));
34  }
35  double tmp_dbl = static_cast<double>(number);
36  snprintf(tmp_buf, sizeof(tmp_buf), tmp_format, tmp_dbl);
37  nb_str << tmp_buf;
38  } else {
39  nb_str << number;
40  }
41  return nb_str.str();
42 }
43 
45 std::string DblToString(const double dbl_val,
46  const std::string& prec_or_format = "3");
47 
49 std::string BoolToString(const bool bool_val,
50  const bool long_format = true);
51 
53 bool Boolean(const std::string& value,
54  const bool permissive = true);
55 
57 template <class TYPE> std::string TypeName(TYPE instance) {
58  RAD_LOG_TRACE();
59 
60  return typeid(instance).name();
61 }
62 
64 class Value {
65 public:
66  explicit Value(const std::string& name = "") {
67  RAD_LOG_TRACE();
68 
69  m_value = NULL;
70  m_name = name;
71  }
72 
73  ~Value() {
74  RAD_LOG_TRACE();
75 
76  if (m_value != NULL) {
77  free(m_value);
78  }
79  }
80 
82  template <class TYPE> void SetValue(const TYPE& value) {
83  RAD_LOG_TRACE();
84 
85  if (!ctd::defines::SupportedType(value)) {
86  if (m_name != "") {
87  throw rad::Exception(ifw::Print("Illegal data type encountered %s. Variable: %s",
88  typeid(value).name()), m_name.c_str());
89  } else {
90  throw rad::Exception(ifw::Print("Illegal data type encountered %s",
91  typeid(value).name()));
92  }
93  }
94  if (typeid(value) == typeid(static_cast<std::string>(""))) {
95  std::stringstream tmp_str;
96  tmp_str << value;
97  m_string_value = tmp_str.str();
98  } else {
99  m_value = malloc(sizeof(TYPE));
100  memcpy(m_value, &value, sizeof(TYPE));
101  }
102  m_type = ctd::defines::TypeId2DataTypeMap().at(typeid(value).name());
103  }
104 
106  void* GetValue() {
107  RAD_LOG_TRACE();
108 
109  if (m_type == ctd::defines::DATA_TYPE_STRING) {
110  return &m_string_value;
111  }
112  return m_value;
113  }
114 
115 private:
116  std::string m_name;
117  ctd::defines::DataType m_type;
118  void* m_value;
119  std::string m_string_value;
120 };
121 
123 void Convert(const std::string& value,
124  ctd::defines::DataType data_type,
125  Value& native_value);
126 
128 template <class TYPE> void Convert(const std::string& str_value,
129  TYPE& native_value) {
130  RAD_LOG_TRACE();
131 
132  std::map<std::string, ctd::defines::DataType>::const_iterator type_it =
133  ctd::defines::TypeId2DataTypeMap().find(typeid(native_value).name());
134  if (type_it == ctd::defines::TypeId2DataTypeMap().end()) {
135  throw rad::Exception("Unknown datatype encountered");
136  }
137  ctd::defines::DataType data_type = type_it->second;
138  Value tmp_native_value;
139  Convert(str_value, data_type, tmp_native_value);
140  native_value = *(static_cast<TYPE*>(tmp_native_value.GetValue()));
141 }
142 
143 } // namespace conversion
144 } // namespace ctd
145 
146 
147 #endif // IFW_CTD_CONVERSION_HPP_
double value
Definition: easylogging++.h:814
void Convert(const std::string &value, ctd::defines::DataType data_type, Value &native_value)
Convert a value represented as a string into its &#39;native type&#39;. Data type given.
Definition: conversion.cpp:52
const std::map< std::string, DataType > & TypeId2DataTypeMap()
Return reference to data types numeric to string map.
Definition: defines.cpp:60
std::string NbToStr(const TYPE number, const std::string &format="")
Convert the given value to a string representation.
Definition: conversion.hpp:20
Definition: defines.hpp:45
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
DataType
Data types numeric representations.
Definition: defines.hpp:33
~Value()
Definition: conversion.hpp:73
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
bool Boolean(const std::string &value, const bool permissive)
Interpret a string as a boolean (t/T/true/TRUE -&gt; true; f/F/false/FALSE -&gt; false).
Definition: conversion.cpp:38
Definition: Exceptions.hpp:44
bool SupportedType(const TYPE &variable)
Check if the variable has a data type which is supported by the ELT ICS.
Definition: defines.hpp:183
std::string TypeName(TYPE instance)
Get the typename of the given variable as a string.
Definition: conversion.hpp:57
std::string Print(const char *format,...)
Create formatted string, return formatted string (C formatting convention).
Definition: defines.cpp:71
#define RAD_LOG_TRACE()
Definition: Logger.hpp:319
Value(const std::string &name="")
Definition: conversion.hpp:66
void * GetValue()
Return value as void*. Calling function must cast to the appropriate type.
Definition: conversion.hpp:106
optional string name
Definition: topics.proto:50
void SetValue(const TYPE &value)
Set the internal value.
Definition: conversion.hpp:82