Sen API
Sen Libraries
Loading...
Searching...
No Matches
var.h
Go to the documentation of this file.
1// === var.h ===========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_META_VAR_H
9#define SEN_CORE_META_VAR_H
10
11// sen
17
18// std
19#include <cstdlib>
20#include <map>
21#include <memory>
22#include <string>
23#include <tuple>
24#include <type_traits>
25#include <utility>
26#include <variant>
27#include <vector>
28
29namespace sen
30{
31
34
36struct Var;
37
38//--------------------------------------------------------------------------------------------------------------
39// Conversion functions
40//--------------------------------------------------------------------------------------------------------------
41
44[[nodiscard]] std::string toJson(const Var& var, int indent = 2);
45
47[[nodiscard]] Var fromJson(const std::string& str);
48
51[[nodiscard]] std::vector<std::uint8_t> toBson(const Var& var);
52
54[[nodiscard]] Var fromBson(const std::vector<std::uint8_t>& bson);
55
58[[nodiscard]] std::vector<std::uint8_t> toCbor(const Var& var);
59
61[[nodiscard]] Var fromCbor(const std::vector<std::uint8_t>& cbor);
62
65[[nodiscard]] std::vector<std::uint8_t> toMsgpack(const Var& var);
66
68[[nodiscard]] Var fromMsgpack(const std::vector<std::uint8_t>& msgpack);
69
72[[nodiscard]] std::vector<std::uint8_t> toUbjson(const Var& var);
73
75[[nodiscard]] Var fromUbjson(const std::vector<std::uint8_t>& ubson);
76
78[[nodiscard]] Duration stringToDuration(const std::string& str);
79
100template <typename To, bool checkedConversion = false>
101[[nodiscard]] To getCopyAs(const Var& var);
102
104using VarList = std::vector<Var>;
105
107using VarMap = std::map<std::string, Var, std::less<>>;
108
110using KeyedVar = std::tuple<uint32_t, std::shared_ptr<Var>>;
111
112//--------------------------------------------------------------------------------------------------------------
113// Var
114//--------------------------------------------------------------------------------------------------------------
115
118struct Var
119{
120 SEN_COPY_MOVE(Var)
121
122public:
124 using ValueType = std::variant<std::monostate,
125 int32_t,
126 uint32_t,
127 int64_t,
128 uint64_t,
129 float32_t,
130 float64_t,
131 Duration,
132 TimeStamp,
133 uint8_t,
134 int16_t,
135 uint16_t,
136 bool,
137 std::string,
138 VarList,
139 VarMap,
140 KeyedVar>;
141
142public: // default construction and destruction
143 Var() = default;
144 ~Var() = default;
145
146public:
148 template <typename T>
149 constexpr Var(T t) // NOLINT
150 {
151 if constexpr (std::is_move_constructible_v<T>)
152 {
153 value_ = ValueType(std::move(t));
154 }
155 else
156 {
157 value_ = ValueType(t);
158 }
159 }
160
161public:
163 constexpr operator ValueType&() noexcept // NOLINT
164 {
165 return value_;
166 }
167
169 constexpr operator const ValueType&() const noexcept // NOLINT
170 {
171 return value_;
172 }
173
174public:
176 template <typename T>
177 [[nodiscard]] constexpr bool holds() const noexcept
178 {
179 return std::holds_alternative<T>(value_);
180 }
181
183 template <typename... Ts>
184 [[nodiscard]] constexpr bool holdsAnyOff() const noexcept
185 {
186 return (holds<Ts>() || ...);
187 }
188
190 [[nodiscard]] constexpr bool holdsIntegralValue() const noexcept
191 {
193 }
194
196 [[nodiscard]] constexpr bool holdsFloatingPointValue() const noexcept { return holdsAnyOff<float32_t, float64_t>(); }
197
199 [[nodiscard]] constexpr bool isEmpty() const noexcept { return std::holds_alternative<std::monostate>(value_); }
200
202 template <typename T, bool checkedConversion = false>
203 [[nodiscard]] T getCopyAs() const
204 {
205 return ::sen::getCopyAs<T>(*this);
206 }
207
209 template <typename T>
210 [[nodiscard]] constexpr T& get()
211 {
212 return std::get<T>(value_);
213 }
214
216 template <typename T>
217 [[nodiscard]] constexpr const T& get() const
218 {
219 return std::get<T>(value_);
220 }
221
223 template <typename T>
224 [[nodiscard]] const T* getIf() const noexcept
225 {
226 return std::get_if<T>(&value_);
227 }
228
230 template <typename T>
231 [[nodiscard]] T* getIf() noexcept
232 {
233 return std::get_if<T>(&value_);
234 }
235
237 void swap(Var& rhs) noexcept;
238
239 [[nodiscard]] friend bool operator==(const Var& lhs, const Var& rhs) noexcept { return lhs.isEqual(rhs); }
240 [[nodiscard]] friend bool operator!=(const Var& lhs, const Var& rhs) noexcept { return !lhs.isEqual(rhs); }
241
242private:
243 [[nodiscard]] bool isEqual(const Var& rhs) const noexcept;
244
245private:
246 ValueType value_ {};
247};
248
249template <typename T>
250struct IsVariantMember<T, Var>: public IsVariantMember<T, typename Var::ValueType>
251{
252};
253
254template <typename T>
256
259[[nodiscard]] const Var& findElement(const VarMap& map, const std::string& key, const std::string& errorMessage);
260
263template <typename T>
264[[nodiscard]] T findElementAs(const VarMap& map, const std::string& key, const std::string& errorMessage)
265{
266 return getCopyAs<T>(findElement(map, key, errorMessage));
267}
268
270
271//----------------------------------------------------------------------------------------------------------------------
272// Inline implementation
273//----------------------------------------------------------------------------------------------------------------------
274
275//--------------------------------------------------------------------------------------------------------------
276// Helpers
277//--------------------------------------------------------------------------------------------------------------
278
279namespace impl
280{
281
282void getCopyAsImpl(const Var& var, std::monostate& val);
283void getCopyAsImpl(const Var& var, int32_t& val);
284void getCopyAsImpl(const Var& var, uint32_t& val);
285void getCopyAsImpl(const Var& var, int64_t& val);
286void getCopyAsImpl(const Var& var, uint64_t& val);
287void getCopyAsImpl(const Var& var, float32_t& val);
288void getCopyAsImpl(const Var& var, float64_t& val);
289void getCopyAsImpl(const Var& var, Duration& val);
290void getCopyAsImpl(const Var& var, TimeStamp& val);
291void getCopyAsImpl(const Var& var, uint8_t& val);
292void getCopyAsImpl(const Var& var, int16_t& val);
293void getCopyAsImpl(const Var& var, uint16_t& val);
294void getCopyAsImpl(const Var& var, bool& val);
295void getCopyAsImpl(const Var& var, std::string& val);
296void getCopyAsImpl(const Var& var, VarList& val);
297void getCopyAsImpl(const Var& var, VarMap& val);
298void getCopyAsImpl(const Var& var, KeyedVar& val);
299
300void getCheckedCopyAsImpl(const Var& var, std::monostate& val);
301void getCheckedCopyAsImpl(const Var& var, int32_t& val);
302void getCheckedCopyAsImpl(const Var& var, uint32_t& val);
303void getCheckedCopyAsImpl(const Var& var, int64_t& val);
304void getCheckedCopyAsImpl(const Var& var, uint64_t& val);
305void getCheckedCopyAsImpl(const Var& var, float32_t& val);
306void getCheckedCopyAsImpl(const Var& var, float64_t& val);
307void getCheckedCopyAsImpl(const Var& var, Duration& val);
308void getCheckedCopyAsImpl(const Var& var, TimeStamp& val);
309void getCheckedCopyAsImpl(const Var& var, uint8_t& val);
310void getCheckedCopyAsImpl(const Var& var, int16_t& val);
311void getCheckedCopyAsImpl(const Var& var, uint16_t& val);
312void getCheckedCopyAsImpl(const Var& var, bool& val);
313void getCheckedCopyAsImpl(const Var& var, std::string& val);
314void getCheckedCopyAsImpl(const Var& var, VarList& val);
315void getCheckedCopyAsImpl(const Var& var, VarMap& val);
316void getCheckedCopyAsImpl(const Var& var, KeyedVar& val);
317
318} // namespace impl
319
320//--------------------------------------------------------------------------------------------------------------
321// Free functions
322//--------------------------------------------------------------------------------------------------------------
323
324template <typename To, bool checkedConversion>
325inline To getCopyAs(const Var& var)
326{
327 if constexpr (isVarTypeMemberV<To>)
328 {
329 // don't transform if already the same type
330 if (var.holds<To>())
331 {
332 return var.get<To>();
333 }
334
335 To value {};
336 if constexpr (checkedConversion)
337 {
338 impl::getCheckedCopyAsImpl(var, value);
339 }
340 else
341 {
342 impl::getCopyAsImpl(var, value);
343 }
344 return value;
345 }
346 else
347 {
348 To value {};
350 return value;
351 }
352}
353
354inline const Var& findElement(const VarMap& map, const std::string& key, const std::string& errorMessage)
355{
356 if (auto itr = map.find(key); itr != map.end())
357 {
358 return itr->second;
359 }
360
361 ::sen::throwRuntimeError(errorMessage);
363}
364
365} // namespace sen
366
367#endif // SEN_CORE_META_VAR_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...
A time duration.
Definition duration.h:25
A point in time.
Definition timestamp.h:26
void throwRuntimeError(const std::string &err)
Throws std::exception that attempts to collect the stack trace. We also wrap it to avoid including st...
#define SEN_UNREACHABLE()
Definition compiler_macros.h:420
Checks if T is on of the member types of the given VariantType.
Definition class_helpers.h:186
Definition type_traits.h:34
std::tuple< uint32_t, std::shared_ptr< Var > > KeyedVar
A key-var tuple, to represent variants.
Definition var.h:110
std::vector< std::uint8_t > toCbor(const Var &var)
Converts a variant into its MessagePack representation. See https://msgpack.org/ for details.
Var fromBson(const std::vector< std::uint8_t > &bson)
Inverse as toBson.
To getCopyAs(const Var &var)
Tries to transform the stored value to T. For expensive types, like strings, maps or lists is better ...
Definition var.h:325
std::map< std::string, Var, std::less<> > VarMap
A map of vars to represent structures.
Definition var.h:107
Var fromUbjson(const std::vector< std::uint8_t > &ubson)
Inverse as toUbjson.
Var fromCbor(const std::vector< std::uint8_t > &cbor)
Inverse as toCbor.
std::string toJson(const Var &var, int indent=2)
Converts a variant into its Json representation. See https://www.Json.org/Json-en....
std::vector< Var > VarList
A list of vars to represent sequences.
Definition var.h:104
Var fromJson(const std::string &str)
Inverse as toJson.
Var fromMsgpack(const std::vector< std::uint8_t > &msgpack)
Inverse as toMsgpack.
T findElementAs(const VarMap &map, const std::string &key, const std::string &errorMessage)
Finds an element in a map or throws std::exception containing the provided string otherwise.
Definition var.h:264
const Var & findElement(const VarMap &map, const std::string &key, const std::string &errorMessage)
Finds an element in a map or throws std::exception containing the provided string otherwise.
Definition var.h:354
constexpr bool isVarTypeMemberV
Definition var.h:255
std::vector< std::uint8_t > toBson(const Var &var)
Converts a variant into its bson (Binary JSON) representation. See https://bsonspec....
std::vector< std::uint8_t > toMsgpack(const Var &var)
Converts a variant into its msgpack representation. See https://cbor.io/ for details.
Duration stringToDuration(const std::string &str)
Converts a string to a duration.
std::vector< std::uint8_t > toUbjson(const Var &var)
Converts a variant into its UBJSON (Universal Binary JSON Specification) representation....
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
Definition assert.h:17
Can hold any supported value type. Wraps std::variant to allow recursion and implements some helpers.
Definition var.h:119
constexpr bool holdsIntegralValue() const noexcept
Checks whether the contained value is a integral type.
Definition var.h:190
constexpr bool isEmpty() const noexcept
True if the Var holds a value of std::monostate.
Definition var.h:199
~Var()=default
friend bool operator==(const Var &lhs, const Var &rhs) noexcept
Definition var.h:239
friend bool operator!=(const Var &lhs, const Var &rhs) noexcept
Definition var.h:240
constexpr bool holdsAnyOff() const noexcept
True if the Var holds a value of type T, where T is in Ts .
Definition var.h:184
T getCopyAs() const
See sen::getCopyAs().
Definition var.h:203
constexpr bool holdsFloatingPointValue() const noexcept
Checks whether the contained value is a floating point type.
Definition var.h:196
Var()=default
T * getIf() noexcept
Same as std::get_if<T>(&(this->value)).
Definition var.h:231
constexpr Var(T t)
Construction from a value (explicit).
Definition var.h:149
constexpr T & get()
Same as std::get<T>(this->value);.
Definition var.h:210
constexpr const T & get() const
Same as std::get<T>(this->value);.
Definition var.h:217
constexpr bool holds() const noexcept
True if the Var holds a value of T.
Definition var.h:177
void swap(Var &rhs) noexcept
Swaps contents with another Var.
const T * getIf() const noexcept
Same as std::get_if<T>(&(this->value)).
Definition var.h:224
std::variant< std::monostate, int32_t, uint32_t, int64_t, uint64_t, float32_t, float64_t, Duration, TimeStamp, uint8_t, int16_t, uint16_t, bool, std::string, VarList, VarMap, KeyedVar > ValueType
The std::variant that we wrap.
Definition var.h:124