From 68351c6de75ee4ca835c57b223335c6df4f6a9d0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 18 Jan 2024 17:50:45 +0100 Subject: a --- serialize/packbits-read.cpp | 38 ++++++++++---------- serialize/packbits-read.hpp | 82 +++++++++++++++++++------------------------- serialize/packbits-write.hpp | 6 ++-- 3 files changed, 59 insertions(+), 67 deletions(-) (limited to 'serialize') diff --git a/serialize/packbits-read.cpp b/serialize/packbits-read.cpp index cc3fc3bd..d71d06ac 100644 --- a/serialize/packbits-read.cpp +++ b/serialize/packbits-read.cpp @@ -21,7 +21,7 @@ static_assert(!input{65535}.check_zero()); static_assert(input{65535}.advance<16>() == 0); static_assert(input::next<16>{ input{65535}.advance<16>() }.check_zero()); -static_assert(input::next<16>{}.Capacity == 14); +static_assert(input::next<16>{}.Left == 14); constexpr bool test1() { @@ -76,39 +76,41 @@ constexpr bool test3() } static_assert(test3()); -static_assert(std::is_same_v< make_tuple_type, std::tuple >); +template using f32 = input_field; +template using f8 = input_field; constexpr bool test4() { - 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); - read_(tuple, input{(uint32_t)-1}, std::make_index_sequence<3>{}, make_pack{}); - auto [a, b, c] = tuple; + f32<17> a; + f32<14> b; + f32< 1> c; + //auto tuple = std::tuple&, f32<14>&, f32<1>&>{a, b, c}; + auto tuple = std::tie(a, b, c); + read_(tuple, input{(uint32_t)-1}, std::make_index_sequence<3>{}); fm_assert(a == lowbits); fm_assert(b == lowbits); fm_assert(c & 1); } { - Tuple_u8 tuple{}; - read_(tuple, input{0b101011}, std::make_index_sequence<3>{}, make_pack{}); - auto [a, b, c] = tuple; + f8<1> a; + f8<3> b; + f8<2> c; + read_(std::tie(a, b, c), input{0b101011}, std::make_index_sequence<3>{}); fm_assert(a == 0b1); fm_assert(b == 0b101); fm_assert(c == 0b10); } { - std::tuple<> empty_tuple; - read_(empty_tuple, input{0}, std::index_sequence<>{}, make_pack{}); - Tuple_u8 tuple{}; (void)tuple; - //read_(empty_tuple, input{1}, std::index_sequence<>{}, make_tuple{}); - //read_(tuple, input{0b11111}, std::make_index_sequence<3>{}, make_tuple{}); + read_(std::tuple<>{}, input{0}, std::index_sequence<>{}); + [[maybe_unused]] f32<2> a, b, c; + //read_(std::tuple<>{}, input{1}, std::index_sequence<>{}); + //read_(std::tie(a, b, c), input{0b11111}, std::make_index_sequence<3>{}); //(void)input{}; - //read_(empty_tuple, input{}, std::index_sequence<0>{}, make_tuple{}); - //read_(empty_tuple, input{1}, std::index_sequence<>{}, make_tuple{}); + //read_(std::tie(a), input{3}, std::index_sequence<0>{}); fm_assert(a == 3); + //f8<1> d; read_(std::tie(d), input{1}, std::index_sequence<>{}); } return true; 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 -struct input_bits final +template +struct input_field final { - static_assert(std::is_fundamental_v); - static_assert(N > 0); - static_assert(N < sizeof(T)*8); - - using type = T; -}; - -template -struct make_tuple_type_ -{ - template using index_to_type = T; - template struct aux; - template struct aux> - { - static_assert(sizeof...(Is) > 0); - using type = std::tuple...>; - }; - using Seq = typename aux>::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 using make_tuple_type = typename make_tuple_type_::Seq; - -template struct empty_pack_tuple {}; -template +template 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 struct next_ { - static_assert(N <= Capacity); - using type = input; + static_assert(N <= LEFT); + using type = input; }; template using next = typename next_::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 struct input { + static constexpr size_t Left = 0; using Type = T; - static constexpr size_t Capacity = 0; T value; template [[maybe_unused]] constexpr T get() const = delete; @@ -91,32 +77,34 @@ struct input }; }; -template -constexpr void read_(Place& p, input st, std::index_sequence, empty_pack_tuple, Sizes...>) +template struct is_input_field : std::bool_constant {}; +template struct is_input_field> : std::bool_constant { static_assert(N > 0); }; + +template +constexpr void read_(Tuple&& tuple, input st, std::index_sequence) { - static_assert(sizeof...(Is) == sizeof...(Sizes)); + using U = std::decay_t; + static_assert(Left <= sizeof(T)*8); + static_assert(Left > 0); + static_assert(std::tuple_size_v >= sizeof...(Is)+1); + static_assert(I < std::tuple_size_v, "too few tuple elements"); + using Field = std::decay_t>; + static_assert(is_input_field::value); + constexpr size_t Size = Field::Length; static_assert(Size <= Left, "data type too small"); - static_assert(I < std::tuple_size_v, "too few tuple members"); - using S = input; - using next_type = typename S::template next; - get(p) = st.template get(); + using next_type = typename input::template next; + std::get(tuple).value = st.template get(); T next_value = st.template advance(); - read_(p, next_type{ next_value }, std::index_sequence{}, empty_pack_tuple{}); + read_(tuple, next_type{ next_value }, std::index_sequence{}); } -template -constexpr void read_(Place&, input st, std::index_sequence<>, empty_pack_tuple<>) +template +constexpr void read_(Tuple&&, input st, std::index_sequence<>) { if (!st.check_zero()) [[unlikely]] throw_on_read_nonzero(); } -template -requires(sizeof...(Is) != sizeof...(Sizes)) -constexpr void read_(Place&, input, std::index_sequence, empty_pack_tuple) = delete; - -template using make_pack = empty_pack_tuple...>; - } // namespace floormat::Pack_impl namespace floormat { diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index ca18cfbb..234b1632 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -12,9 +12,9 @@ template struct output { static_assert(std::is_fundamental_v); - static_assert(CAPACITY > 0); - static_assert(CAPACITY <= sizeof(T)*8); + static_assert(LEFT >= 0); static_assert(LEFT <= CAPACITY); + static_assert(CAPACITY <= sizeof(T)*8); static constexpr size_t Capacity = CAPACITY, Left = LEFT; T value{0}; }; @@ -23,6 +23,7 @@ template struct output_field { static_assert(LENGTH > 0); + static_assert(LENGTH <= sizeof(T)*8); static constexpr size_t Length = LENGTH; T value; }; @@ -37,6 +38,7 @@ constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple& tuple, output 0); static_assert(Capacity <= sizeof(T)*8); static_assert(Left <= Capacity); + static_assert(I < std::tuple_size_v, "too few tuple elements"); static_assert(is_output_field(tuple))>>{}); constexpr size_t N = std::tuple_element_t::Length; static_assert(N <= Left); -- cgit v1.2.3