diff options
Diffstat (limited to 'serialize/packbits-read.hpp')
-rw-r--r-- | serialize/packbits-read.hpp | 82 |
1 files changed, 35 insertions, 47 deletions
diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp index 2ea50848..9e5f0cc8 100644 --- a/serialize/packbits-read.hpp +++ b/serialize/packbits-read.hpp @@ -9,45 +9,31 @@ namespace floormat::Pack_impl { -template<std::unsigned_integral T, size_t N> -struct input_bits final +template<std::unsigned_integral T, size_t LENGTH> +struct input_field final { - static_assert(std::is_fundamental_v<T>); - static_assert(N > 0); - static_assert(N < sizeof(T)*8); - - using type = T; -}; - -template<std::unsigned_integral T, size_t N> -struct make_tuple_type_ -{ - template<size_t> using index_to_type = T; - template<typename> struct aux; - template<size_t... Is> struct aux<std::index_sequence<Is...>> - { - static_assert(sizeof...(Is) > 0); - using type = std::tuple<index_to_type<Is>...>; - }; - using Seq = typename aux<std::make_index_sequence<N>>::type; + static_assert(LENGTH > 0); + static_assert(LENGTH <= sizeof(T)*8); + static constexpr size_t Length = LENGTH; + T value; + constexpr T operator*() const { return value; } + constexpr operator T() const { return value; } }; -template<std::unsigned_integral T, size_t N> using make_tuple_type = typename make_tuple_type_<T, N>::Seq; - -template<typename... Ts> struct empty_pack_tuple {}; -template<std::unsigned_integral T, size_t CAPACITY> +template<std::unsigned_integral T, size_t LEFT> struct input { - static_assert(CAPACITY <= sizeof(T)*8); - static constexpr size_t Capacity = CAPACITY; + static_assert(LEFT > 0); + static_assert(LEFT <= sizeof(T)*8); + static constexpr size_t Left = LEFT; T value; template<size_t N> struct next_ { - static_assert(N <= Capacity); - using type = input<T, Capacity - N>; + static_assert(N <= LEFT); + using type = input<T, LEFT - N>; }; template<size_t N> using next = typename next_<N>::type; @@ -56,7 +42,7 @@ struct input { static_assert(N > 0); static_assert(N <= sizeof(T)*8); - static_assert(N <= Capacity); + static_assert(N <= LEFT); return T(value & (T{1} << N) - T{1}); } @@ -64,7 +50,7 @@ struct input constexpr T advance() const { static_assert(N <= sizeof(T)*8); - static_assert(N <= Capacity); + static_assert(N <= LEFT); return T(value >> N); } @@ -75,8 +61,8 @@ struct input template<std::unsigned_integral T> struct input<T, 0> { + static constexpr size_t Left = 0; using Type = T; - static constexpr size_t Capacity = 0; T value; template<size_t N> [[maybe_unused]] constexpr T get() const = delete; @@ -91,32 +77,34 @@ struct input<T, 0> }; }; -template<std::unsigned_integral T, typename Place, size_t Left, size_t I, size_t... Is, size_t Size, typename... Sizes> -constexpr void read_(Place& p, input<T, Left> st, std::index_sequence<I, Is...>, empty_pack_tuple<input_bits<T, Size>, Sizes...>) +template<typename T> struct is_input_field : std::bool_constant<false> {}; +template<std::unsigned_integral T, size_t N> struct is_input_field<input_field<T, N>> : std::bool_constant<true> { static_assert(N > 0); }; + +template<std::unsigned_integral T, typename Tuple, size_t Left, size_t I, size_t... Is> +constexpr void read_(Tuple&& tuple, input<T, Left> st, std::index_sequence<I, Is...>) { - static_assert(sizeof...(Is) == sizeof...(Sizes)); + using U = std::decay_t<Tuple>; + static_assert(Left <= sizeof(T)*8); + static_assert(Left > 0); + static_assert(std::tuple_size_v<U> >= sizeof...(Is)+1); + static_assert(I < std::tuple_size_v<U>, "too few tuple elements"); + using Field = std::decay_t<std::tuple_element_t<I, U>>; + static_assert(is_input_field<Field>::value); + constexpr size_t Size = Field::Length; static_assert(Size <= Left, "data type too small"); - static_assert(I < std::tuple_size_v<Place>, "too few tuple members"); - using S = input<T, Left>; - using next_type = typename S::template next<Size>; - get<I>(p) = st.template get<Size>(); + using next_type = typename input<T, Left>::template next<Size>; + std::get<I>(tuple).value = st.template get<Size>(); T next_value = st.template advance<Size>(); - read_(p, next_type{ next_value }, std::index_sequence<Is...>{}, empty_pack_tuple<Sizes...>{}); + read_(tuple, next_type{ next_value }, std::index_sequence<Is...>{}); } -template<std::unsigned_integral T, typename Place, size_t Left> -constexpr void read_(Place&, input<T, Left> st, std::index_sequence<>, empty_pack_tuple<>) +template<std::unsigned_integral T, typename Tuple, size_t Left> +constexpr void read_(Tuple&&, input<T, Left> st, std::index_sequence<>) { if (!st.check_zero()) [[unlikely]] throw_on_read_nonzero(); } -template<std::unsigned_integral T, typename Place, size_t Left, size_t... Is, typename... Sizes> -requires(sizeof...(Is) != sizeof...(Sizes)) -constexpr void read_(Place&, input<T, Left>, std::index_sequence<Is...>, empty_pack_tuple<Sizes...>) = delete; - -template<std::unsigned_integral T, size_t... Ns> using make_pack = empty_pack_tuple<input_bits<T, Ns>...>; - } // namespace floormat::Pack_impl namespace floormat { |