Sen API
Sen Libraries
Loading...
Searching...
No Matches
optional_traits.h
Go to the documentation of this file.
1// === optional_traits.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_OPTIONAL_TRAITS_H
9#define SEN_CORE_META_OPTIONAL_TRAITS_H
10
11// sen
15#include "sen/core/io/util.h"
17#include "sen/core/meta/var.h"
18
19// std
20#include <cstdint>
21#include <optional>
22#include <string>
23
24namespace sen
25{
26
27struct Var;
28
31
33template <typename T>
35{
36 static constexpr bool available = true;
37
38 static void write(OutputStream& out, T val);
39 static void read(InputStream& in, T& val);
40 static void valueToVariant(const T& val, Var& var);
41 static void variantToValue(const Var& var, T& val);
42 [[nodiscard]] static uint32_t serializedSize(T val) noexcept;
43
44 static std::string toJsonString(const T& val)
45 {
46 Var var;
47 valueToVariant(val, var);
48 return toJson(var);
49 }
50
51 static void fromJsonString(const std::string& str, T& val)
52 {
53 const Var var = fromJson(str);
54 variantToValue(var, val);
55 }
56};
57
59
60//----------------------------------------------------------------------------------------------------------------------
61// Inline implementation
62//----------------------------------------------------------------------------------------------------------------------
63
64template <typename T>
66{
67 auto hasValue = val.has_value();
68 out.writeBool(hasValue);
69 if (hasValue)
70 {
72 }
73}
74
75template <typename T>
77{
78 bool hasValue = false;
79 in.readBool(hasValue);
80
81 if (hasValue)
82 {
83 typename T::value_type content {};
85 val = T(content);
86 }
87 else
88 {
89 val = T(std::nullopt);
90 }
91}
92
93template <typename T>
94inline uint32_t OptionalTraitsBase<T>::serializedSize(T val) noexcept
95{
96 auto hasValue = val.has_value();
97 auto result = impl::getSerializedSize(hasValue);
98
99 if (hasValue)
100 {
102 }
103
104 return result;
105}
106
107template <typename T>
108inline void OptionalTraitsBase<T>::valueToVariant(const T& val, Var& var)
109{
110 if (val.has_value())
111 {
113 }
114 else
115 {
116 var = {};
117 }
118}
119
120template <typename T>
121inline void OptionalTraitsBase<T>::variantToValue(const Var& var, T& val)
122{
123 // the var might be empty or hold an empty map
124 if (var.isEmpty() || (var.holds<VarMap>() && var.get<VarMap>().empty()))
125 {
126 val = T(std::nullopt);
127 }
128 else
129 {
130 typename T::value_type content {};
132 val = T(content);
133 }
134}
135
136} // namespace sen
137
138#endif // SEN_CORE_META_OPTIONAL_TRAITS_H
void readBool(bool &val)
Definition input_stream.h:93
void writeBool(bool val)
Definition output_stream.h:36
InputStreamTemplate< LittleEndian > InputStream
Definition input_stream.h:84
Definition type_traits.h:47
Definition type_traits.h:34
std::map< std::string, Var, std::less<> > VarMap
A map of vars to represent structures.
Definition var.h:107
std::string toJson(const Var &var, int indent=2)
Converts a variant into its Json representation. See https://www.Json.org/Json-en....
Var fromJson(const std::string &str)
Inverse as toJson.
Definition assert.h:17
OutputStreamTemplate< LittleEndian > OutputStream
Definition output_stream.h:64
Base class for enum traits.
Definition optional_traits.h:35
static void fromJsonString(const std::string &str, T &val)
Definition optional_traits.h:51
static void variantToValue(const Var &var, T &val)
Definition optional_traits.h:121
static std::string toJsonString(const T &val)
Definition optional_traits.h:44
static void valueToVariant(const T &val, Var &var)
Definition optional_traits.h:108
static uint32_t serializedSize(T val) noexcept
Definition optional_traits.h:94
static void read(InputStream &in, T &val)
Definition optional_traits.h:76
static constexpr bool available
Definition optional_traits.h:36
static void write(OutputStream &out, T val)
Definition optional_traits.h:65
Can hold any supported value type. Wraps std::variant to allow recursion and implements some helpers.
Definition var.h:119
constexpr bool isEmpty() const noexcept
True if the Var holds a value of std::monostate.
Definition var.h:199
constexpr T & get()
Same as std::get<T>(this->value);.
Definition var.h:210
constexpr bool holds() const noexcept
True if the Var holds a value of T.
Definition var.h:177