From b5f96dda3de339b2db61c531f6f486f9d3d45866 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Jan 2024 20:47:15 +0100 Subject: a --- serialize/packbits.cpp | 63 ++++++++++++++++++++++++++------------------- serialize/packbits.hpp | 70 ++++++++------------------------------------------ 2 files changed, 47 insertions(+), 86 deletions(-) diff --git a/serialize/packbits.cpp b/serialize/packbits.cpp index 634191fc..ffca868f 100644 --- a/serialize/packbits.cpp +++ b/serialize/packbits.cpp @@ -7,10 +7,11 @@ using namespace floormat::detail_Pack; namespace { +#if 0 template struct check_size_overflow; template -struct check_size_overflow, Xs...> +struct check_size_overflow, Xs...> { static_assert(std::is_same_v); static constexpr auto acc = Sum + size_t{N}; @@ -25,10 +26,10 @@ 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_; +template using us_bits = Bits; static_assert(!Storage{65535}.check_zero()); static_assert(Storage{65535}.advance<16>() == 0); @@ -72,6 +73,7 @@ constexpr bool test1() } static_assert(test1()); +#if 0 constexpr bool test2() { using foo1 = us_bits<2>; @@ -107,6 +109,7 @@ constexpr bool test2() return true; } static_assert(test2()); +#endif constexpr bool test3() { @@ -131,30 +134,36 @@ static_assert(std::is_same_v< make_tuple_type, std::tuple; - Tuple tuple{}; - assign_tuple2(tuple, Storage{(uint32_t)-1}, std::make_index_sequence<3>{}, - Bits_{}, Bits_{}, Bits_{}); - auto [a, b, c] = tuple; - - static_assert(lowbits != 0); - fm_assert(a == lowbits); - fm_assert(b == lowbits); - fm_assert(c & 1); - - //Assign::do_tuple(tuple, Storage{(uint32_t)-1}); - - return true; -} -static_assert(test4()); - -constexpr bool test5() -{ - auto st = Storage{0xB16B00B5}; - uint32_t a, b, c; - using Tuple = std::tuple; - auto t = Tuple{a, b, c}; - //assign_tuple, Tuple, + using Tuple_u32 = make_tuple_type; + static_assert(std::is_same_v>); + using Tuple_u8 = make_tuple_type; + { + Tuple_u32 tuple{}; + static_assert(lowbits == 0x1ffffU); + assign_tuple(tuple, Storage{(uint32_t)-1}, std::make_index_sequence<3>{}, Bits{}, Bits{}, Bits{}); + auto [a, b, c] = tuple; + fm_assert(a == lowbits); + fm_assert(b == lowbits); + fm_assert(c & 1); + } + { + Tuple_u8 tuple{}; + assign_tuple(tuple, Storage{0b101011}, std::make_index_sequence<3>{}, Bits{}, Bits{}, Bits{}); + auto [a, b, c] = tuple; + fm_assert(a == 0b1); + fm_assert(b == 0b101); + fm_assert(c == 0b10); + } + { + std::tuple<> empty_tuple; + assign_tuple(empty_tuple, Storage{0}, std::index_sequence<>{}); + Tuple_u8 tuple{}; (void)tuple; + // assign_tuple(empty_tuple, Storage{1}, std::index_sequence<>{}); + // assign_tuple(tuple, Storage{0b11111}, std::make_index_sequence<3>{}, Bits{}, Bits{}, Bits{}); + // (void)Storage{}; + //assign_tuple(empty_tuple, Storage{}, std::index_sequence<0>{}, Bits{}); + // assign_tuple(empty_tuple, Storage{1}, std::index_sequence<>{}, Bits{}); + } return true; } diff --git a/serialize/packbits.hpp b/serialize/packbits.hpp index c94f96d7..8a828f94 100644 --- a/serialize/packbits.hpp +++ b/serialize/packbits.hpp @@ -5,14 +5,13 @@ #include "compat/assert.hpp" namespace floormat::Pack { -template struct Bits_; +template struct Bits; } // namespace floormat::Pack namespace floormat::detail_Pack { using namespace floormat::Pack; - template struct Storage { @@ -79,80 +78,33 @@ struct make_tuple_type_ template using make_tuple_type = typename make_tuple_type_::Seq; template -constexpr void assign_tuple2(Place& p, Storage st, std::index_sequence, Bits_, Sizes... sizes) +constexpr void assign_tuple(Place& p, Storage st, std::index_sequence, Bits, Sizes... sizes) { + static_assert(Size <= Left, "too many bits requested"); + static_assert(I < std::tuple_size_v, "too few tuple members"); using S = Storage; using next_type = typename S::template next; get(p) = st.template get(); T next_value = st.template advance(); - assign_tuple2(p, next_type{next_value}, std::index_sequence{}, sizes...); + assign_tuple(p, next_type{next_value}, std::index_sequence{}, sizes...); } template -constexpr void assign_tuple2(Place&, Storage, std::index_sequence<>) +constexpr void assign_tuple(Place&, Storage st, std::index_sequence<>) { + fm_assert(st.check_zero()); } -#if 0 -template struct assign_tuple; - -template -struct assign_tuple, Bits_, Sizes...> -{ - static_assert(Size <= sizeof(T)*8, "bit count can't be larger than sizeof(T)*8"); - static_assert(Size > 0, "bit count can't be zero"); - - template - static constexpr inline void do_tuple(Place& p, Storage st) - { - static_assert(requires (Place& p) { std::get<0>(p) = T{0}; }); - static_assert(std::tuple_size_v >= sizeof...(Indexes) + 1); - static_assert(Size <= Left, "not enough bits for element"); - - get(p) = st.template get(st.value); - using Next = typename Storage::template next; - assign_tuple, Sizes...>:: - template do_tuple(p, Next{st.template advance()}); - } - - static constexpr bool is_empty = false; -}; - -template -struct assign_tuple> -{ - static_assert(sizeof(T) == (size_t)-1, "too few lhs elements"); - static_assert(sizeof(T) != (size_t)-1); -}; - -template -struct assign_tuple, Bits_, Sizes...> -{ - static_assert(sizeof(T) == (size_t)-1, "too few rhs elements"); - static_assert(sizeof(T) != (size_t)-1); -}; - -template -struct assign_tuple> -{ - template - static constexpr inline void do_tuple(Place&, Storage st) - { - fm_assert(st.check_zero()); - } - - static constexpr bool is_empty = true; - using type = T; -}; - -#endif +template +requires(sizeof...(Is) != sizeof...(Sizes)) +constexpr void assign_tuple(Place&, Storage, std::index_sequence, Sizes...) = delete; } // namespace floormat::detail_Pack namespace floormat::Pack { template -struct Bits_ final +struct Bits final { static_assert(std::is_fundamental_v); static_assert(N > 0); -- cgit v1.2.3