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
18// std
19#include <optional>
20
21namespace sen
22{
23
24struct Var;
25
28
30template <typename T>
32{
33 static constexpr bool available = true;
34
35 static void write(OutputStream& out, T val);
36 static void read(InputStream& in, T& val);
37 static void valueToVariant(const T& val, Var& var);
38 static void variantToValue(const Var& var, T& val);
39 [[nodiscard]] static uint32_t serializedSize(T val) noexcept;
40};
41
43
44//----------------------------------------------------------------------------------------------------------------------
45// Inline implementation
46//----------------------------------------------------------------------------------------------------------------------
47
48template <typename T>
50{
51 auto hasValue = val.has_value();
52 out.writeBool(hasValue);
53 if (hasValue)
54 {
56 }
57}
58
59template <typename T>
61{
62 bool hasValue = false;
63 in.readBool(hasValue);
64
65 if (hasValue)
66 {
67 typename T::value_type content;
69 val = T(content);
70 }
71 else
72 {
73 val = T(std::nullopt);
74 }
75}
76
77template <typename T>
78inline uint32_t OptionalTraitsBase<T>::serializedSize(T val) noexcept
79{
80 auto hasValue = val.has_value();
81 auto result = impl::getSerializedSize(hasValue);
82
83 if (hasValue)
84 {
86 }
87
88 return result;
89}
90
91template <typename T>
92inline void OptionalTraitsBase<T>::valueToVariant(const T& val, Var& var)
93{
94 if (val.has_value())
95 {
97 }
98 else
99 {
100 var = {};
101 }
102}
103
104template <typename T>
105inline void OptionalTraitsBase<T>::variantToValue(const Var& var, T& val)
106{
107 // the var might be empty or hold an empty map
108 if (var.isEmpty() || (var.holds<VarMap>() && var.get<VarMap>().empty()))
109 {
110 val = T(std::nullopt);
111 }
112 else
113 {
114 typename T::value_type content;
116 val = T(content);
117 }
118}
119
120} // namespace sen
121
122#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
Definition assert.h:17
OutputStreamTemplate< LittleEndian > OutputStream
Definition output_stream.h:64
Base class for enum traits.
Definition optional_traits.h:32
static void variantToValue(const Var &var, T &val)
Definition optional_traits.h:105
static void valueToVariant(const T &val, Var &var)
Definition optional_traits.h:92
static uint32_t serializedSize(T val) noexcept
Definition optional_traits.h:78
static void read(InputStream &in, T &val)
Definition optional_traits.h:60
static constexpr bool available
Definition optional_traits.h:33
static void write(OutputStream &out, T val)
Definition optional_traits.h:49
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