8#ifndef SEN_CORE_META_VAR_H
9#define SEN_CORE_META_VAR_H
44[[nodiscard]] std::string
toJson(
const Var& var,
int indent = 2);
51[[nodiscard]] std::vector<std::uint8_t>
toBson(
const Var& var);
54[[nodiscard]]
Var fromBson(
const std::vector<std::uint8_t>& bson);
58[[nodiscard]] std::vector<std::uint8_t>
toCbor(
const Var& var);
61[[nodiscard]]
Var fromCbor(
const std::vector<std::uint8_t>& cbor);
72[[nodiscard]] std::vector<std::uint8_t>
toUbjson(
const Var& var);
100template <
typename To,
bool checkedConversion = false>
107using VarMap = std::map<std::string, Var, std::less<>>;
110using KeyedVar = std::tuple<uint32_t, std::shared_ptr<Var>>;
148 template <
typename T>
151 if constexpr (std::is_move_constructible_v<T>)
176 template <
typename T>
177 [[nodiscard]]
constexpr bool holds() const noexcept
179 return std::holds_alternative<T>(value_);
183 template <
typename... Ts>
199 [[nodiscard]]
constexpr bool isEmpty() const noexcept {
return std::holds_alternative<std::monostate>(value_); }
202 template <
typename T,
bool checkedConversion = false>
205 return ::sen::getCopyAs<T>(*
this);
209 template <
typename T>
210 [[nodiscard]]
constexpr T&
get()
212 return std::get<T>(value_);
216 template <
typename T>
217 [[nodiscard]]
constexpr const T&
get()
const
219 return std::get<T>(value_);
223 template <
typename T>
224 [[nodiscard]]
const T*
getIf() const noexcept
226 return std::get_if<T>(&value_);
230 template <
typename T>
233 return std::get_if<T>(&value_);
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); }
243 [[nodiscard]]
bool isEqual(
const Var& rhs)
const noexcept;
259[[nodiscard]]
const Var&
findElement(
const VarMap& map,
const std::string& key,
const std::string& errorMessage);
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);
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);
324template <
typename To,
bool checkedConversion>
332 return var.
get<To>();
336 if constexpr (checkedConversion)
338 impl::getCheckedCopyAsImpl(var, value);
342 impl::getCopyAsImpl(var, value);
356 if (
auto itr = map.find(key); itr != map.end())
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
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
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
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