summaryrefslogtreecommitdiffhomepage
path: root/serialize/binary-serializer.hpp
blob: a320e086648a23c8ceaca034f6d4603d6e465057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once

#include <bit>
#include <concepts>
#include <type_traits>

namespace floormat::Serialize {

static_assert(std::endian::native == std::endian::big || std::endian::native == std::endian::little);

template<size_t N> struct make_integer;
template<size_t N> using make_integer_t = typename make_integer<N>::type;

#define FM_SERIALIZE_MAKE_INTEGER(T) template<> struct make_integer<sizeof(T)> { using type = T; }
FM_SERIALIZE_MAKE_INTEGER(uint8_t);
FM_SERIALIZE_MAKE_INTEGER(uint16_t);
FM_SERIALIZE_MAKE_INTEGER(uint32_t);
FM_SERIALIZE_MAKE_INTEGER(uint64_t);
#undef FN_SERIALIZE_MAKE_INTEGER

template<typename T>
concept integer = requires(T x) {
    requires std::integral<T>;
    requires sizeof(T) == sizeof(make_integer_t<sizeof(T)>);
};

template<typename T>
concept serializable = requires(T x) {
    requires std::same_as<T, std::decay_t<T>>;
    requires std::floating_point<T> || integer<T>;
};

template<typename T>
constexpr CORRADE_ALWAYS_INLINE T maybe_byteswap(T x) noexcept
{
    return x;
}

template<integer T>
requires (sizeof(T) > 1)
constexpr CORRADE_ALWAYS_INLINE T maybe_byteswap(T x) noexcept
{
    if constexpr(std::endian::native == std::endian::big)
        return std::byteswap(x);
    else
        return x;
}

} // namespace floormat::Serialize