From d168b52140c9eb08b6fdbb68665e1b07a6fda7a9 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jan 2024 05:59:18 +0100 Subject: a --- serialize/packbits.cpp | 107 +++++++++++-------------------------------------- serialize/packbits.hpp | 24 +++++------ 2 files changed, 36 insertions(+), 95 deletions(-) diff --git a/serialize/packbits.cpp b/serialize/packbits.cpp index 74b9ff90..c22c621e 100644 --- a/serialize/packbits.cpp +++ b/serialize/packbits.cpp @@ -7,57 +7,36 @@ using namespace floormat::detail_Pack; namespace { -#if 0 -template struct check_size_overflow; - -template -struct check_size_overflow, Xs...> -{ - static_assert(std::is_same_v); - static constexpr auto acc = Sum + size_t{N}; - using next_check = check_size_overflow; - static constexpr auto size = next_check::size; - static constexpr bool result = next_check::result; -}; - -template -struct check_size_overflow -{ - static constexpr size_t size = Sum; - static constexpr bool result = Sum <= sizeof(T)*8; -}; -#endif - template constexpr inline T lowbits = (T{1} << N)-T{1}; template using us_bits = Bits; -static_assert(!Storage{65535}.check_zero()); -static_assert(Storage{65535}.advance<16>() == 0); +static_assert(!pack_input{65535}.check_zero()); +static_assert(pack_input{65535}.advance<16>() == 0); -static_assert(Storage::next<16>{ - Storage{65535}.advance<16>() +static_assert(pack_input::next<16>{ + pack_input{65535}.advance<16>() }.check_zero()); -static_assert(Storage::next<16>{}.Capacity == 14); +static_assert(pack_input::next<16>{}.Capacity == 14); constexpr bool test1() { constexpr size_t bits[] = { 5, 2, 1 }; constexpr size_t vals[] = { 8, 3, 1, 0 }; - constexpr auto S0 = Storage{0b10111011}; - constexpr auto S1 = Storage{0b00000101}; - constexpr auto S2 = Storage{0b00000001}; - constexpr auto S3 = Storage{0b00000000}; + constexpr auto S0 = pack_input{0b10111011}; + constexpr auto S1 = pack_input{0b00000101}; + constexpr auto S2 = pack_input{0b00000001}; + constexpr auto S3 = pack_input{0b00000000}; using P0 = std::decay_t; using P1 = P0::next; using P2 = P1::next; using P3 = P2::next; - static_assert(std::is_same_v>); - static_assert(std::is_same_v>); - static_assert(std::is_same_v>); - static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); static_assert(S0.advance< 0>() == S0.value); static_assert(S0.advance() == S1.value); @@ -73,49 +52,11 @@ constexpr bool test1() } static_assert(test1()); -#if 0 -constexpr bool test2() -{ - using foo1 = us_bits<2>; - using foo2 = us_bits<10>; - using foo3 = us_bits<4>; - using bar1 = check_size_overflow; - static_assert(bar1::result); - static_assert(bar1::size == 12); - - using bar2 = check_size_overflow; - static_assert(bar2::result); - static_assert(bar2::size == 10); - - using bar3 = check_size_overflow; - static_assert(bar3::result); - static_assert(bar3::size == 16); - - using foo4 = us_bits<1>; - using bar4 = check_size_overflow; - static_assert(!bar4::result); - static_assert(bar4::size == 17); - - using foo5 = us_bits<20>; - using bar5 = check_size_overflow; - static_assert(!bar5::result); - static_assert(bar5::size == 37); - - using foo6 = us_bits<40>; - using bar6 = check_size_overflow; - static_assert(!bar6::result); - static_assert(bar6::size == 57); - - return true; -} -static_assert(test2()); -#endif - constexpr bool test3() { - constexpr auto S0 = Storage{0b1110100110001011}; - constexpr auto S1 = Storage{0b1110}; - constexpr auto S2 = Storage{0b1}; + constexpr auto S0 = pack_input{0b1110100110001011}; + constexpr auto S1 = pack_input{0b1110}; + constexpr auto S2 = pack_input{0b1}; static_assert(S0.get<12>() == 0b100110001011); static_assert(S0.advance<12>() == S1.value); @@ -140,7 +81,7 @@ constexpr bool test4() { Tuple_u32 tuple{}; static_assert(lowbits == 0x1ffffU); - assign_tuple(tuple, Storage{(uint32_t)-1}, std::make_index_sequence<3>{}, make_pack{}); + read_pack(tuple, pack_input{(uint32_t)-1}, std::make_index_sequence<3>{}, make_pack{}); auto [a, b, c] = tuple; fm_assert(a == lowbits); fm_assert(b == lowbits); @@ -148,7 +89,7 @@ constexpr bool test4() } { Tuple_u8 tuple{}; - assign_tuple(tuple, Storage{0b101011}, std::make_index_sequence<3>{}, make_pack{}); + read_pack(tuple, pack_input{0b101011}, std::make_index_sequence<3>{}, make_pack{}); auto [a, b, c] = tuple; fm_assert(a == 0b1); fm_assert(b == 0b101); @@ -156,13 +97,13 @@ constexpr bool test4() } { std::tuple<> empty_tuple; - assign_tuple(empty_tuple, Storage{0}, std::index_sequence<>{}, make_pack{}); + read_pack(empty_tuple, pack_input{0}, std::index_sequence<>{}, make_pack{}); Tuple_u8 tuple{}; (void)tuple; - //assign_tuple(empty_tuple, Storage{1}, std::index_sequence<>{}, make_tuple{}); - //assign_tuple(tuple, Storage{0b11111}, std::make_index_sequence<3>{}, make_tuple{}); - //(void)Storage{}; - //assign_tuple(empty_tuple, Storage{}, std::index_sequence<0>{}, make_tuple{}); - //assign_tuple(empty_tuple, Storage{1}, std::index_sequence<>{}, make_tuple{}); + //read_pack(empty_tuple, pack_input{1}, std::index_sequence<>{}, make_tuple{}); + //read_pack(tuple, pack_input{0b11111}, std::make_index_sequence<3>{}, make_tuple{}); + //(void)pack_input{}; + //read_pack(empty_tuple, pack_input{}, std::index_sequence<0>{}, make_tuple{}); + //read_pack(empty_tuple, pack_input{1}, std::index_sequence<>{}, make_tuple{}); } return true; diff --git a/serialize/packbits.hpp b/serialize/packbits.hpp index 3fd859d2..81971c71 100644 --- a/serialize/packbits.hpp +++ b/serialize/packbits.hpp @@ -18,7 +18,7 @@ struct Bits final }; template -struct Storage +struct pack_input { static_assert(CAPACITY <= sizeof(T)*8); @@ -43,14 +43,14 @@ struct Storage return T(value >> N); } - constexpr bool operator==(const Storage&) const noexcept = default; + constexpr bool operator==(const pack_input&) const noexcept = default; [[nodiscard]] constexpr inline bool check_zero() const { return value == T(0); } - template using next = Storage; + template using next = pack_input; }; template -struct Storage +struct pack_input { using Type = T; static constexpr size_t Capacity = 0; @@ -58,7 +58,7 @@ struct Storage template [[maybe_unused]] constexpr T get() const = delete; template [[maybe_unused]] constexpr T advance() const = delete; - constexpr bool operator==(const Storage&) const noexcept = default; + constexpr bool operator==(const pack_input&) const noexcept = default; [[nodiscard]] constexpr inline bool check_zero() const { return true; } template struct next @@ -82,32 +82,32 @@ struct make_tuple_type_ }; template using make_tuple_type = typename make_tuple_type_::Seq; -template struct pack_tuple {}; +template struct empty_pack_tuple {}; template requires requires() { sizeof...(Is) == sizeof...(Sizes); } -constexpr void assign_tuple(Place& p, Storage st, std::index_sequence, pack_tuple, Sizes...>) +constexpr void read_pack(Place& p, pack_input st, std::index_sequence, empty_pack_tuple, Sizes...>) { static_assert(Size <= Left, "too many bits requested"); static_assert(I < std::tuple_size_v, "too few tuple members"); - using S = Storage; + using S = pack_input; using next_type = typename S::template next; get(p) = st.template get(); T next_value = st.template advance(); - assign_tuple(p, next_type{next_value}, std::index_sequence{}, pack_tuple{}); + read_pack(p, next_type{next_value}, std::index_sequence{}, empty_pack_tuple{}); } template -constexpr void assign_tuple(Place&, Storage st, std::index_sequence<>, pack_tuple<>) +constexpr void read_pack(Place&, pack_input st, std::index_sequence<>, empty_pack_tuple<>) { fm_assert(st.check_zero()); } template requires(sizeof...(Is) != sizeof...(Sizes)) -constexpr void assign_tuple(Place&, Storage, std::index_sequence, pack_tuple) = delete; +constexpr void read_pack(Place&, pack_input, std::index_sequence, empty_pack_tuple) = delete; -template using make_pack = pack_tuple...>; +template using make_pack = empty_pack_tuple...>; } // namespace floormat::detail_Pack -- cgit v1.2.3