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 <cstdint>
17#include <limits>
18#include <string>
19#include <tuple>
20#include <type_traits>
21#include <vector>
22
23namespace sen::impl
24{
25
26// clang-format off
27
29template <typename T>
30inline constexpr bool isPureIntegral = std::is_same_v<int8_t, T> || std::is_same_v<uint8_t, T> ||
31 std::is_same_v<int16_t, T> || std::is_same_v<uint16_t, T> ||
32 std::is_same_v<int32_t, T> || std::is_same_v<uint32_t, T> ||
33 std::is_same_v<int64_t, T> || std::is_same_v<uint64_t, T>;
34
35// clang-format on
36
38template <typename T>
39inline constexpr bool isNumeric =
40 isPureIntegral<T> || (std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559);
41
43template <typename T>
44inline constexpr bool isBasic = isNumeric<T> || std::is_same_v<char, T> || std::is_same_v<unsigned char, T>;
45
48template <typename T>
49inline constexpr bool allowsContiguousIO =
50 !std::is_same_v<T, bool> &&
51 (isBasic<T> || std::is_enum_v<T> || (std::is_trivially_copyable_v<T> && !std::is_class_v<T>)) &&
52 (hostIsLittleEndian() || sizeof(T) == 1U);
53
54static_assert(!allowsContiguousIO<std::vector<std::string>>);
55static_assert(!allowsContiguousIO<std::vector<bool>>);
56
58template <typename T, typename R>
59using IfBasic = typename std::enable_if_t<isBasic<T>, R>;
60
62template <typename T, typename R>
63using IfEnum = typename std::enable_if_t<std::is_enum_v<T>, R>;
64
66using BoolTransportType = uint8_t;
67
69template <typename S, typename T>
70struct IsStreamable
71{
72private:
73 template <typename SS, typename TT>
74 static auto outputTest(unsigned) -> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
75
76 template <typename, typename>
77 static auto outputTest(...) -> std::false_type;
78
79 template <typename SS, typename TT>
80 static auto inputTest(unsigned) -> decltype(std::declval<SS&>() >> std::declval<TT&>(), std::true_type());
81
82 template <typename, typename>
83 static auto inputTest(...) -> std::false_type;
84
85public:
86 static constexpr bool output = decltype(outputTest<S, T>(0U))::value;
87 static constexpr bool input = decltype(inputTest<S, T>(0U))::value;
88};
89
90[[nodiscard]] constexpr uint32_t getSerializedSize(bool val) noexcept
91{
92 std::ignore = val;
93 return sizeof(BoolTransportType);
94}
95
96template <typename T>
97[[nodiscard]] constexpr IfBasic<T, uint32_t> getSerializedSize(T val) noexcept
98{
99 std::ignore = val;
100 return sizeof(T);
101}
102
103template <typename T>
104[[nodiscard]] constexpr IfEnum<T, uint32_t> getSerializedSize(T val) noexcept
105{
106 return getSerializedSize(static_cast<std::underlying_type_t<T>>(val));
107}
108
109[[nodiscard]] inline uint32_t getSerializedSize(const std::string& val) noexcept
110{
111 const auto size = static_cast<uint32_t>(val.size());
112 const auto sizeSize = getSerializedSize(size);
113
114 // early exit
115 if (size == 0U)
116 {
117 return sizeSize;
118 }
119
120 return sizeSize + size;
121}
122
123[[nodiscard]] constexpr uint32_t getSerializedSize(TimeStamp val) noexcept
124{
125 std::ignore = val;
126 return getSerializedSize(int64_t {0});
127}
128
129} // namespace sen::impl
130
131#endif // SEN_CORE_IO_DETAIL_SERIALIZATION_TRAITS_H