summaryrefslogtreecommitdiffhomepage
path: root/serialize/packbits-write.hpp
blob: faa3ff00c573530cf5ad42ed6ffa820b07cec736 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include "compat/assert.hpp"
#include <type_traits>
#include <utility>
#include <concepts>

namespace floormat::detail_Pack_output {

template<size_t N>
struct output_bits final
{
    static_assert(N > 0);
    static_assert(N < sizeof(size_t)*8);

    static constexpr size_t length = N;
};

template<std::unsigned_integral T, size_t CAPACITY>
struct output
{
    static_assert(std::is_fundamental_v<T>);
    static_assert(CAPACITY <= sizeof(T)*8);
    static constexpr size_t Capacity = CAPACITY;

    T value{0};

    template<size_t N>
    constexpr T set(T x, output_bits<N>) const
    {
        static_assert(N <= CAPACITY, "data type too small");
        static_assert(N > 0);
        T value_{value};
        if constexpr(CAPACITY != sizeof(T)*8)
            value_ <<= CAPACITY;
        auto x_ = T(x & (T{1}<<N)-T{1});
        fm_assert(x_ == x);
        value_ |= x_;
        return value_;
    }

    template<size_t N>
    struct next_
    {
        static_assert(N > 0);
        static_assert(N <= CAPACITY);
        using type = output<T, CAPACITY - N>;
    };

    template<size_t N> using next = typename next_<N>::type;
};

template<typename T, size_t N>
using output_field = std::pair<T, output_bits<N>>;

template<typename... Ts> struct empty_pack_tuple {}; // todo copypasta

template<typename T, size_t Left, size_t F, typename... Fields>
constexpr T write_(output<T, Left> st, output_field<T, F> field, Fields... fields)
{
    T value = st.set(field.first, field.second);
    using next = typename output<T, Left>::template next<F>;
    return write_(next{value}, fields...);
}

template<typename T, size_t Left>
constexpr T write_(output<T, Left> st)
{
    return st.value;
}

} // namespace floormat::detail_Pack_output