diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-15 06:13:27 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-15 06:13:27 +0100 |
commit | b38c2ac1d97a2c0090ce7f63a40b98fcd174b356 (patch) | |
tree | 119a370ab48377b8f5c013f738ef767357149a85 /serialize/packbits.hpp | |
parent | d168b52140c9eb08b6fdbb68665e1b07a6fda7a9 (diff) |
a
Diffstat (limited to 'serialize/packbits.hpp')
-rw-r--r-- | serialize/packbits.hpp | 83 |
1 files changed, 2 insertions, 81 deletions
diff --git a/serialize/packbits.hpp b/serialize/packbits.hpp index 81971c71..ee8aeaec 100644 --- a/serialize/packbits.hpp +++ b/serialize/packbits.hpp @@ -1,71 +1,17 @@ #pragma once -#include <type_traits> #include <concepts> -#include <tuple> -#include "compat/assert.hpp" +#include <utility> namespace floormat::detail_Pack { template<std::unsigned_integral T, size_t N> -struct Bits final +struct bits final { static_assert(std::is_fundamental_v<T>); static_assert(N > 0); static_assert(N < sizeof(T)*8); using type = T; - static constexpr auto bits = N; -}; - -template<std::unsigned_integral T, size_t CAPACITY> -struct pack_input -{ - static_assert(CAPACITY <= sizeof(T)*8); - - using Type = T; - static constexpr size_t Capacity = CAPACITY; - T value; - - template<size_t N> - constexpr T get() const - { - static_assert(N > 0); - static_assert(N <= sizeof(T)*8); - static_assert(N <= Capacity); - return T(value & (T(1) << N) - T(1)); - } - - template<size_t N> - constexpr T advance() const - { - static_assert(N <= sizeof(T)*8); - static_assert(N <= Capacity); - return T(value >> N); - } - - constexpr bool operator==(const pack_input&) const noexcept = default; - [[nodiscard]] constexpr inline bool check_zero() const { return value == T(0); } - - template<size_t N> using next = pack_input<T, Capacity - N>; -}; - -template<std::unsigned_integral T> -struct pack_input<T, 0> -{ - using Type = T; - static constexpr size_t Capacity = 0; - T value; - - template<size_t N> [[maybe_unused]] constexpr T get() const = delete; - template<size_t N> [[maybe_unused]] constexpr T advance() const = delete; - constexpr bool operator==(const pack_input&) const noexcept = default; - [[nodiscard]] constexpr inline bool check_zero() const { return true; } - - template<size_t N> struct next - { - static_assert(N == 0, "reading past the end"); - static_assert(N != 0, "reading past the end"); - }; }; template<std::unsigned_integral T, size_t N> @@ -84,31 +30,6 @@ template<std::unsigned_integral T, size_t N> using make_tuple_type = typename ma template<typename... Ts> struct empty_pack_tuple {}; -template<std::unsigned_integral T, typename Place, size_t Left, size_t I, size_t... Is, size_t Size, typename... Sizes> -requires requires() { sizeof...(Is) == sizeof...(Sizes); } -constexpr void read_pack(Place& p, pack_input<T, Left> st, std::index_sequence<I, Is...>, empty_pack_tuple<Bits<T, Size>, Sizes...>) -{ - static_assert(Size <= Left, "too many bits requested"); - static_assert(I < std::tuple_size_v<Place>, "too few tuple members"); - using S = pack_input<T, Left>; - using next_type = typename S::template next<Size>; - get<I>(p) = st.template get<Size>(); - T next_value = st.template advance<Size>(); - read_pack(p, next_type{next_value}, std::index_sequence<Is...>{}, empty_pack_tuple<Sizes...>{}); -} - -template<std::unsigned_integral T, typename Place, size_t Left> -constexpr void read_pack(Place&, pack_input<T, Left> st, std::index_sequence<>, empty_pack_tuple<>) -{ - fm_assert(st.check_zero()); -} - -template<std::unsigned_integral T, typename Place, size_t Left, size_t... Is, typename... Sizes> -requires(sizeof...(Is) != sizeof...(Sizes)) -constexpr void read_pack(Place&, pack_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<Bits<T, Ns>...>; - } // namespace floormat::detail_Pack namespace floormat::Pack { |