CVB++ 15.0
Loading...
Searching...
No Matches
attribute_map.hpp
1#pragma once
2
3#include <map>
4#include <iomanip>
5
6#include "telemetry.hpp"
7
8#include "../shims/stdvariant.hpp"
9#include "../string.hpp"
10
11namespace Cvb
12{
13 CVB_BEGIN_INLINE_NS
14
15 namespace Telemetry
16 {
17 using Attribute = variant<bool, int64_t, double, std::string>;
18
20
31 class AttributeMap
32 {
33
34 public:
35 AttributeMap(const AttributeMap &other) = delete;
36 AttributeMap& operator=(const AttributeMap &other) = delete;
37 AttributeMap(AttributeMap&& other) noexcept
38 : attributes_(std::move(other.attributes_))
39 {
40 std::swap(handle_, other.handle_);
41 }
42 AttributeMap& operator=(AttributeMap&& other) noexcept
43 {
44 if (this != &other)
45 {
46 attributes_ = std::move(other.attributes_);
47 std::swap(handle_, other.handle_);
48 }
49 return *this;
50 }
51 virtual ~AttributeMap() = default;
52
53 template <class T>
54 AttributeMap &operator()(const std::string &key, const T &value);
55
57 AttributeMap &operator()(const std::string &key, const Char *value);
58
60 AttributeMap &operator()(const std::string &key, void *value);
61
62 template <class T>
63 T Get(const std::string &key) const;
64
65 protected:
66 AttributeMap() noexcept {}
67
68 void Flush() noexcept
69 {
70 if (!handle_)
71 return;
72 try
73 {
75 attributes.reserve(attributes_.size());
76
77 for (const auto &item : attributes_)
78 {
79 const void *data = nullptr;
80 auto type = CExports::CVTAT_Undefined;
81 if (holds_alternative<std::string>(item.second))
82 {
83 data = get<std::string>(item.second).c_str(); // safe raw pointer to the data inside the map (string)
84 type = CExports::CVTAT_String;
85 }
86 else if (holds_alternative<double>(item.second))
87 {
88 data = &get<double>(item.second);
89 type = CExports::CVTAT_Double;
90 }
91 else if (holds_alternative<bool>(item.second))
92 {
93 data = &get<bool>(item.second);
94 type = CExports::CVTAT_Bool;
95 }
96 else if (holds_alternative<int64_t>(item.second))
97 {
98 data = &get<int64_t>(item.second);
99 type = CExports::CVTAT_Int64;
100 }
101 if (!data)
102 continue;
103 CExports::CVTAttribute attribute;
104 attribute.Name = item.first.c_str();
105 attribute.Type = type;
106 attribute.Data = const_cast<void *>(data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
107 attributes.push_back(attribute);
108 }
109
110 CExports::CVTAttributeMapStore(handle_, attributes.data(), attributes.size());
111 }
112 catch (...)
113 {
114
115 }
116 }
117
118 void *handle_ = nullptr; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
119 std::map<std::string, Attribute> attributes_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
120 };
121
122 template <class T>
123 T AttributeMap::Get(const std::string &key) const
124 {
125 auto it = attributes_.find(key);
126 if (it != attributes_.end())
127 {
128 // Check if the type stored in the map matches the requested type
129 if (Cvb::holds_alternative<T>(it->second))
130 {
131 return Cvb::get<T>(it->second);
132 }
133 else
134 {
135 throw std::runtime_error("invalid key and type mismatch");
136 }
137 }
138 throw std::out_of_range("Key not found");
139 }
140
141 inline AttributeMap &AttributeMap::operator()(const std::string &key, void *value)
142 {
143 std::stringstream stream;
144 stream << "0x" << std::setfill('0') << std::setw(sizeof(void *) * 2) << std::hex
145 << reinterpret_cast<uint64_t>(value);
146 attributes_[key] = stream.str();
147 return *this;
148 }
149
150 template <>
151 inline AttributeMap &AttributeMap::operator()(const std::string &key, const Telemetry::Attribute &value)
152 {
153 attributes_[key] = value;
154 return *this;
155 }
156
158 template <>
159 inline AttributeMap &AttributeMap::operator()(const std::string &key, const double &value)
160 {
161 attributes_[key] = value;
162 return *this;
163 }
164
166 template <>
167 inline AttributeMap &AttributeMap::operator()(const std::string &key, const float &value)
168 {
169 attributes_[key] = static_cast<double>(value);
170 return *this;
171 }
172
173 inline AttributeMap &AttributeMap::operator()(const std::string &key, const Char *value)
174 {
175 attributes_[key] = Internal::ToUTF8(String(value));
176 return *this;
177 }
178
180 template <>
181 inline AttributeMap &AttributeMap::operator()(const std::string &key, const String &value)
182 {
183 attributes_[key] = Internal::ToUTF8(value);
184 return *this;
185 }
186
188 template <>
189 inline AttributeMap &AttributeMap::operator()(const std::string &key, const bool &value)
190 {
191 attributes_[key] = value;
192 return *this;
193 }
194
196 template <>
197 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int8_t &value)
198 {
199 attributes_[key] = static_cast<int64_t>(value);
200 return *this;
201 }
202
204 template <>
205 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint8_t &value)
206 {
207 attributes_[key] = static_cast<int64_t>(value);
208 return *this;
209 }
210
212 template <>
213 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int16_t &value)
214 {
215 attributes_[key] = static_cast<int64_t>(value);
216 return *this;
217 }
218
220 template <>
221 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint16_t &value)
222 {
223 attributes_[key] = static_cast<int64_t>(value);
224 return *this;
225 }
226
228 template <>
229 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int32_t &value)
230 {
231 attributes_[key] = static_cast<int64_t>(value);
232 return *this;
233 }
234
236 template <>
237 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint32_t &value)
238 {
239 attributes_[key] = static_cast<int64_t>(value);
240 return *this;
241 }
242
244 template <>
245 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int64_t &value)
246 {
247 attributes_[key] = static_cast<int64_t>(value);
248 return *this;
249 }
250
252 template <>
253 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint64_t &value)
254 {
255 attributes_[key] = static_cast<int64_t>(value);
256 return *this;
257 }
258 } // namespace Telemetry
259
260 CVB_END_INLINE_NS
261} // namespace Cvb
A attribute map for telemetry data.
Definition attribute_map.hpp:32
T hex(T... args)
T move(T... args)
Namespace for telemetry.
Definition attribute_map.hpp:16
Root namespace for the Image Manager interface.
Definition version.hpp:11
char Char
Character type for wide characters or unicode characters.
Definition string.hpp:63
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
T setfill(T... args)
T setw(T... args)
T swap(T... args)