Sen API
Sen Libraries
Loading...
Searching...
No Matches
serialization_traits.h
Go to the documentation of this file.
1// === serialization_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_IO_DETAIL_SERIALIZATION_TRAITS_H
9#define SEN_CORE_IO_DETAIL_SERIALIZATION_TRAITS_H
10
11// sen
14
15// std
16#include <limits>
17#include <string>
18#include <tuple>
19#include <type_traits>
20#include <vector>
21
22namespace sen::impl
23{
24
25// clang-format off
26
28template <typename T>
29[[nodiscard]] constexpr bool isPureIntegral() noexcept
30{
31 return std::is_same_v<int8_t, T> || std::is_same_v<uint8_t, T> ||
32 std::is_same_v<int16_t, T> || std::is_same_v<uint16_t, T> ||
33 std::is_same_v<int32_t, T> || std::is_same_v<uint32_t, T> ||
34 std::is_same_v<int64_t, T> || std::is_same_v<uint64_t, T>;
35}
36
37// clang-format on
38
40template <typename T>
41[[nodiscard]] constexpr bool isNumeric() noexcept
42{
43 return isPureIntegral<T>() || (std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559);
44}
45
47template <typename T>
48[[nodiscard]] constexpr bool isBasic() noexcept
49{
50 return isNumeric<T>() || std::is_same_v<char, T> || std::is_same_v<unsigned char, T>;
51}
52
55template <typename T>
56[[nodiscard]] constexpr bool allowsContiguousIO() noexcept
57{
58 constexpr bool isBasicBuiltinType = isBasic<T>() || std::is_enum_v<T>;
59 constexpr bool isTrivialType = std::is_trivially_copyable_v<T> && !std::is_class_v<T>;
60 constexpr bool wouldNotRequireSwap = hostIsLittleEndian() || sizeof(T) == 1U;
61
62 return !std::is_same_v<T, bool> && (isBasicBuiltinType || isTrivialType) && wouldNotRequireSwap;
63}
64
65static_assert(!allowsContiguousIO<std::vector<std::string>>());
66static_assert(!allowsContiguousIO<std::vector<bool>>());
67
69template <typename T, typename R>
70using IfBasic = typename std::enable_if_t<isBasic<T>(), R>;
71
73template <typename T, typename R>
74using IfEnum = typename std::enable_if_t<std::is_enum_v<T>, R>;
75
77using BoolTransportType = uint8_t;
78
80template <typename S, typename T>
81struct IsStreamable
82{
83private:
84 template <typename SS, typename TT>
85 static auto outputTest(unsigned) -> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
86
87 template <typename, typename>
88 static auto outputTest(...) -> std::false_type;
89
90 template <typename SS, typename TT>
91 static auto inputTest(unsigned) -> decltype(std::declval<SS&>() >> std::declval<TT&>(), std::true_type());
92
93 template <typename, typename>
94 static auto inputTest(...) -> std::false_type;
95
96public:
97 static constexpr bool output = decltype(outputTest<S, T>(0U))::value;
98 static constexpr bool input = decltype(inputTest<S, T>(0U))::value;
99};
100
101[[nodiscard]] constexpr uint32_t getSerializedSize(bool val) noexcept
102{
103 std::ignore = val;
104 return sizeof(BoolTransportType);
105}
106
107template <typename T>
108[[nodiscard]] constexpr IfBasic<T, uint32_t> getSerializedSize(T val) noexcept
109{
110 std::ignore = val;
111 return sizeof(T);
112}
113
114template <typename T>
115[[nodiscard]] constexpr IfEnum<T, uint32_t> getSerializedSize(T val) noexcept
116{
117 return getSerializedSize(static_cast<std::underlying_type_t<T>>(val));
118}
119
120[[nodiscard]] inline uint32_t getSerializedSize(const std::string& val) noexcept
121{
122 const auto size = static_cast<uint32_t>(val.size());
123 const auto sizeSize = getSerializedSize(size);
124
125 // early exit
126 if (size == 0U)
127 {
128 return sizeSize;
129 }
130
131 return sizeSize + size;
132}
133
134[[nodiscard]] constexpr uint32_t getSerializedSize(TimeStamp val) noexcept
135{
136 std::ignore = val;
137 return getSerializedSize(int64_t {0});
138}
139
140} // namespace sen::impl
141
142#endif // SEN_CORE_IO_DETAIL_SERIALIZATION_TRAITS_H