8#ifndef SEN_CORE_IO_DETAIL_SERIALIZATION_TRAITS_H
9#define SEN_CORE_IO_DETAIL_SERIALIZATION_TRAITS_H
29[[nodiscard]]
constexpr bool isPureIntegral() noexcept
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>;
41[[nodiscard]]
constexpr bool isNumeric() noexcept
43 return isPureIntegral<T>() || (std::is_floating_point_v<T> && std::numeric_limits<T>::is_iec559);
48[[nodiscard]]
constexpr bool isBasic() noexcept
50 return isNumeric<T>() || std::is_same_v<char, T> || std::is_same_v<unsigned char, T>;
56[[nodiscard]]
constexpr bool allowsContiguousIO() noexcept
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;
62 return !std::is_same_v<T, bool> && (isBasicBuiltinType || isTrivialType) && wouldNotRequireSwap;
65static_assert(!allowsContiguousIO<std::vector<std::string>>());
66static_assert(!allowsContiguousIO<std::vector<bool>>());
69template <
typename T,
typename R>
70using IfBasic =
typename std::enable_if_t<isBasic<T>(), R>;
73template <
typename T,
typename R>
74using IfEnum =
typename std::enable_if_t<std::is_enum_v<T>, R>;
77using BoolTransportType = uint8_t;
80template <
typename S,
typename T>
84 template <
typename SS,
typename TT>
85 static auto outputTest(
unsigned) ->
decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
87 template <
typename,
typename>
88 static auto outputTest(...) -> std::false_type;
90 template <
typename SS,
typename TT>
91 static auto inputTest(
unsigned) ->
decltype(std::declval<SS&>() >> std::declval<TT&>(), std::true_type());
93 template <
typename,
typename>
94 static auto inputTest(...) -> std::false_type;
97 static constexpr bool output =
decltype(outputTest<S, T>(0U))::value;
98 static constexpr bool input =
decltype(inputTest<S, T>(0U))::value;
101[[nodiscard]]
constexpr uint32_t getSerializedSize(
bool val)
noexcept
104 return sizeof(BoolTransportType);
108[[nodiscard]]
constexpr IfBasic<T, uint32_t> getSerializedSize(T val)
noexcept
115[[nodiscard]]
constexpr IfEnum<T, uint32_t> getSerializedSize(T val)
noexcept
117 return getSerializedSize(
static_cast<std::underlying_type_t<T>
>(val));
120[[nodiscard]]
inline uint32_t getSerializedSize(
const std::string& val)
noexcept
122 const auto size =
static_cast<uint32_t
>(val.size());
123 const auto sizeSize = getSerializedSize(size);
131 return sizeSize + size;
134[[nodiscard]]
constexpr uint32_t getSerializedSize(TimeStamp val)
noexcept
137 return getSerializedSize(int64_t {0});