From 6c30d003dfe391e186cc6e08bb00ec99532e5545 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jan 2024 09:33:30 +0100 Subject: a --- serialize/packbits-write.hpp | 63 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) (limited to 'serialize/packbits-write.hpp') diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index 34fc140f..faa3ff00 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -1,28 +1,71 @@ #pragma once +#include "compat/assert.hpp" +#include +#include #include namespace floormat::detail_Pack_output { -template +template +struct output_bits final +{ + static_assert(N > 0); + static_assert(N < sizeof(size_t)*8); + + static constexpr size_t length = N; +}; + +template struct output { - T value = 0; - uint8_t capacity = sizeof(T)*8; + static_assert(std::is_fundamental_v); + static_assert(CAPACITY <= sizeof(T)*8); + static constexpr size_t Capacity = CAPACITY; + + T value{0}; - constexpr inline output next(T x, uint8_t bits) const + template + constexpr T set(T x, output_bits) const { - fm_assert(bits > 0 && bits <= capacity); - auto val = value; - val <<= bits; - T x_ = T(x & (T{1} << bits)- T{1}); + 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}< + struct next_ + { + static_assert(N > 0); + static_assert(N <= CAPACITY); + using type = output; + }; + + template using next = typename next_::type; }; +template +using output_field = std::pair>; +template struct empty_pack_tuple {}; // todo copypasta +template +constexpr T write_(output st, output_field field, Fields... fields) +{ + T value = st.set(field.first, field.second); + using next = typename output::template next; + return write_(next{value}, fields...); +} +template +constexpr T write_(output st) +{ + return st.value; +} } // namespace floormat::detail_Pack_output -- cgit v1.2.3