ifw-core 7.0.0
 
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
6
7#ifndef CORE_PROTOCOL_BASE_UTILS_HPP
8#define CORE_PROTOCOL_BASE_UTILS_HPP
9
10#include <sstream>
11#include <ostream>
12#include <string>
13#include <vector>
14
15#include <boost/variant.hpp>
16
18
19 // Visitor class to convert the value of the variant to an std::string
20 class VariantStr {
21public:
22 // Overload for scalar types other than char
23 template <typename T>
24 std::string operator()(const T& value) const {
25 if constexpr (std::is_same_v<T, std::string>) {
26 return "[scalar type: std::string]= " + value;
27 } else {
28 return "[scalar type: " + demangle_type<T>() + "]= " + std::to_string(value);
29 }
30 }
31
32 // Special handling for std::vector<T>
33 template <typename T>
34 std::string operator()(const std::vector<T>& vec) const {
35
36 std::ostringstream oss;
37 oss << "[vector type: " << demangle_type<T>() << "]= {";
38
39 for (size_t i = 0; i < vec.size(); ++i) {
40 if (i != 0) oss << ", ";
41 oss << (*this)(vec[i]); // Recursive processing of elements
42 }
43
44 oss << "}";
45 return oss.str();
46 }
47
48private:
49 template <typename T>
50 std::string demangle_type() const {
51 return boost::core::demangle(typeid(T).name());
52 // If you want full standard C++ without Boost, I can also replace this.
53 }
54};
55
57public:
58 // Overload for scalar types
59 template <typename T>
60 std::string operator()(const T& value) const {
61 if constexpr (std::is_same_v<T, std::string>) {
62 return value;
63 } else {
64 return std::to_string(value);
65 }
66 }
67
68 // Special handling for vector types
69 template <typename T>
70 std::string operator()(const std::vector<T>& vec) const {
71
72 std::ostringstream oss;
73
74 for (size_t i = 0; i < vec.size(); ++i) {
75 if (i != 0) oss << ", ";
76 oss << (*this)(vec[i]); // recursive call
77 }
78
79 return oss.str();
80 }
81};
82
83
84 }
85
86#endif //CORE_PROTOCOL_BASE_UTILS_HPP
std::string operator()(const std::vector< T > &vec) const
Definition utils.hpp:34
std::string operator()(const T &value) const
Definition utils.hpp:24
std::string operator()(const T &value) const
Definition utils.hpp:60
std::string operator()(const std::vector< T > &vec) const
Definition utils.hpp:70
Definition commFactory.cpp:13