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
66
72 explicit operator bool() const noexcept
73 {
74 return handle_ != nullptr;
75 }
76
77 protected:
78 AttributeMap() noexcept {}
79
80 void Flush() noexcept
81 {
82 if (!handle_)
83 return;
84 try
85 {
87 attributes.reserve(attributes_.size());
88
89 for (const auto &item : attributes_)
90 {
91 const void *data = nullptr;
92 auto type = CExports::CVTAT_Undefined;
93 if (holds_alternative<std::string>(item.second))
94 {
95 data = get<std::string>(item.second).c_str(); // safe raw pointer to the data inside the map (string)
96 type = CExports::CVTAT_String;
97 }
98 else if (holds_alternative<double>(item.second))
99 {
100 data = &get<double>(item.second);
101 type = CExports::CVTAT_Double;
102 }
103 else if (holds_alternative<bool>(item.second))
104 {
105 data = &get<bool>(item.second);
106 type = CExports::CVTAT_Bool;
107 }
108 else if (holds_alternative<int64_t>(item.second))
109 {
110 data = &get<int64_t>(item.second);
111 type = CExports::CVTAT_Int64;
112 }
113 if (!data)
114 continue;
115 CExports::CVTAttribute attribute;
116 attribute.Name = item.first.c_str();
117 attribute.Type = type;
118 attribute.Data = const_cast<void *>(data); // NOLINT(cppcoreguidelines-pro-type-const-cast)
119 attributes.push_back(attribute);
120 }
121
122 CExports::CVTAttributeMapStore(handle_, attributes.data(), attributes.size());
123 }
124 catch (...)
125 {
126
127 }
128 }
129
130 void *handle_ = nullptr; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
131 std::map<std::string, Attribute> attributes_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
132 };
133
134 template <class T>
135 T AttributeMap::Get(const std::string &key) const
136 {
137 auto it = attributes_.find(key);
138 if (it != attributes_.end())
139 {
140 // Check if the type stored in the map matches the requested type
141 if (Cvb::holds_alternative<T>(it->second))
142 {
143 return Cvb::get<T>(it->second);
144 }
145 else
146 {
147 throw std::runtime_error("invalid key and type mismatch");
148 }
149 }
150 throw std::out_of_range("Key not found");
151 }
152
153 inline AttributeMap &AttributeMap::operator()(const std::string &key, void *value)
154 {
155 std::stringstream stream;
156 stream << "0x" << std::setfill('0') << std::setw(sizeof(void *) * 2) << std::hex
157 << reinterpret_cast<uint64_t>(value);
158 attributes_[key] = stream.str();
159 return *this;
160 }
161
162 template <>
163 inline AttributeMap &AttributeMap::operator()(const std::string &key, const Telemetry::Attribute &value)
164 {
165 attributes_[key] = value;
166 return *this;
167 }
168
170 template <>
171 inline AttributeMap &AttributeMap::operator()(const std::string &key, const double &value)
172 {
173 attributes_[key] = value;
174 return *this;
175 }
176
178 template <>
179 inline AttributeMap &AttributeMap::operator()(const std::string &key, const float &value)
180 {
181 attributes_[key] = static_cast<double>(value);
182 return *this;
183 }
184
185 inline AttributeMap &AttributeMap::operator()(const std::string &key, const Char *value)
186 {
187 attributes_[key] = Internal::ToUTF8(String(value));
188 return *this;
189 }
190
192 template <>
193 inline AttributeMap &AttributeMap::operator()(const std::string &key, const String &value)
194 {
195 attributes_[key] = Internal::ToUTF8(value);
196 return *this;
197 }
198
200 template <>
201 inline AttributeMap &AttributeMap::operator()(const std::string &key, const bool &value)
202 {
203 attributes_[key] = value;
204 return *this;
205 }
206
208 template <>
209 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int8_t &value)
210 {
211 attributes_[key] = static_cast<int64_t>(value);
212 return *this;
213 }
214
216 template <>
217 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint8_t &value)
218 {
219 attributes_[key] = static_cast<int64_t>(value);
220 return *this;
221 }
222
224 template <>
225 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int16_t &value)
226 {
227 attributes_[key] = static_cast<int64_t>(value);
228 return *this;
229 }
230
232 template <>
233 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint16_t &value)
234 {
235 attributes_[key] = static_cast<int64_t>(value);
236 return *this;
237 }
238
240 template <>
241 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int32_t &value)
242 {
243 attributes_[key] = static_cast<int64_t>(value);
244 return *this;
245 }
246
248 template <>
249 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint32_t &value)
250 {
251 attributes_[key] = static_cast<int64_t>(value);
252 return *this;
253 }
254
256 template <>
257 inline AttributeMap &AttributeMap::operator()(const std::string &key, const int64_t &value)
258 {
259 attributes_[key] = static_cast<int64_t>(value);
260 return *this;
261 }
262
264 template <>
265 inline AttributeMap &AttributeMap::operator()(const std::string &key, const uint64_t &value)
266 {
267 attributes_[key] = static_cast<int64_t>(value);
268 return *this;
269 }
270 } // namespace Telemetry
271
272 CVB_END_INLINE_NS
273} // 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)