From b38c2ac1d97a2c0090ce7f63a40b98fcd174b356 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jan 2024 06:13:27 +0100 Subject: a --- serialize/packbits-read.hpp | 92 ++++++++++++++++++++++++++++++++++++++++++++ serialize/packbits-write.hpp | 8 ++++ serialize/packbits.cpp | 51 ++++++++++++------------ serialize/packbits.hpp | 83 +-------------------------------------- 4 files changed, 127 insertions(+), 107 deletions(-) create mode 100644 serialize/packbits-read.hpp create mode 100644 serialize/packbits-write.hpp (limited to 'serialize') diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp new file mode 100644 index 00000000..32eca8df --- /dev/null +++ b/serialize/packbits-read.hpp @@ -0,0 +1,92 @@ +#pragma once +#include "packbits.hpp" +#include +#include +#include +#include "compat/assert.hpp" + +namespace floormat::detail_Pack { + +template +struct input +{ + static_assert(CAPACITY <= sizeof(T)*8); + + using Type = T; + static constexpr size_t Capacity = CAPACITY; + T value; + + template + 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 + constexpr T advance() const + { + static_assert(N <= sizeof(T)*8); + static_assert(N <= Capacity); + return T(value >> N); + } + + constexpr bool operator==(const input&) const noexcept = default; + [[nodiscard]] constexpr inline bool check_zero() const { return value == T(0); } + + template using next = input; +}; + +template +struct input +{ + using Type = T; + static constexpr size_t Capacity = 0; + T value; + + template [[maybe_unused]] constexpr T get() const = delete; + template [[maybe_unused]] constexpr T advance() const = delete; + constexpr bool operator==(const input&) const noexcept = default; + [[nodiscard]] constexpr inline bool check_zero() const { return true; } + + template struct next + { + static_assert(N == 0, "reading past the end"); + static_assert(N != 0, "reading past the end"); + }; +}; + +template +constexpr void read(Place& p, input st, std::index_sequence, empty_pack_tuple, Sizes...>) +{ + static_assert(sizeof...(Is) == sizeof...(Sizes)); + static_assert(Size <= Left, "too many bits requested"); + 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(); + T next_value = st.template advance(); + read(p, next_type{ next_value }, std::index_sequence{}, empty_pack_tuple{}); +} + +template +constexpr void read(Place&, input st, std::index_sequence<>, empty_pack_tuple<>) +{ + fm_assert(st.check_zero()); +} + +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::detail_Pack + +namespace floormat::Pack { + + + +} // namespace floormat::Pack diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp new file mode 100644 index 00000000..4741a462 --- /dev/null +++ b/serialize/packbits-write.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "packbits.hpp" + +namespace floormat::detail_Pack { + + + +} // namespace floormat::detail_Pack diff --git a/serialize/packbits.cpp b/serialize/packbits.cpp index c22c621e..84d91c28 100644 --- a/serialize/packbits.cpp +++ b/serialize/packbits.cpp @@ -1,4 +1,4 @@ -#include "packbits.hpp" +#include "packbits-read.hpp" #include "compat/assert.hpp" namespace floormat { @@ -8,35 +8,34 @@ using namespace floormat::detail_Pack; namespace { template constexpr inline T lowbits = (T{1} << N)-T{1}; -template using us_bits = Bits; +template using us_bits = bits; -static_assert(!pack_input{65535}.check_zero()); -static_assert(pack_input{65535}.advance<16>() == 0); +static_assert(!input{65535}.check_zero()); +static_assert(input{65535}.advance<16>() == 0); -static_assert(pack_input::next<16>{ - pack_input{65535}.advance<16>() +static_assert(input::next<16>{ input{65535}.advance<16>() }.check_zero()); -static_assert(pack_input::next<16>{}.Capacity == 14); +static_assert(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 = pack_input{0b10111011}; - constexpr auto S1 = pack_input{0b00000101}; - constexpr auto S2 = pack_input{0b00000001}; - constexpr auto S3 = pack_input{0b00000000}; + constexpr auto S0 = input{0b10111011}; + constexpr auto S1 = input{0b00000101}; + constexpr auto S2 = input{0b00000001}; + constexpr auto S3 = 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); @@ -54,9 +53,9 @@ static_assert(test1()); constexpr bool test3() { - constexpr auto S0 = pack_input{0b1110100110001011}; - constexpr auto S1 = pack_input{0b1110}; - constexpr auto S2 = pack_input{0b1}; + constexpr auto S0 = input{0b1110100110001011}; + constexpr auto S1 = input{0b1110}; + constexpr auto S2 = input{0b1}; static_assert(S0.get<12>() == 0b100110001011); static_assert(S0.advance<12>() == S1.value); @@ -81,7 +80,7 @@ constexpr bool test4() { Tuple_u32 tuple{}; static_assert(lowbits == 0x1ffffU); - read_pack(tuple, pack_input{(uint32_t)-1}, std::make_index_sequence<3>{}, make_pack{}); + read(tuple, input{(uint32_t)-1}, std::make_index_sequence<3>{}, make_pack{}); auto [a, b, c] = tuple; fm_assert(a == lowbits); fm_assert(b == lowbits); @@ -89,7 +88,7 @@ constexpr bool test4() } { Tuple_u8 tuple{}; - read_pack(tuple, pack_input{0b101011}, std::make_index_sequence<3>{}, make_pack{}); + read(tuple, input{0b101011}, std::make_index_sequence<3>{}, make_pack{}); auto [a, b, c] = tuple; fm_assert(a == 0b1); fm_assert(b == 0b101); @@ -97,13 +96,13 @@ constexpr bool test4() } { std::tuple<> empty_tuple; - read_pack(empty_tuple, pack_input{0}, std::index_sequence<>{}, make_pack{}); + read(empty_tuple, input{0}, std::index_sequence<>{}, make_pack{}); Tuple_u8 tuple{}; (void)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{}); + //read(empty_tuple, input{1}, std::index_sequence<>{}, make_tuple{}); + //read(tuple, input{0b11111}, std::make_index_sequence<3>{}, make_tuple{}); + //(void)input{}; + //read(empty_tuple, input{}, std::index_sequence<0>{}, make_tuple{}); + //read(empty_tuple, input{1}, std::index_sequence<>{}, make_tuple{}); } return true; 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 #include -#include -#include "compat/assert.hpp" +#include namespace floormat::detail_Pack { template -struct Bits final +struct bits final { static_assert(std::is_fundamental_v); static_assert(N > 0); static_assert(N < sizeof(T)*8); using type = T; - static constexpr auto bits = N; -}; - -template -struct pack_input -{ - static_assert(CAPACITY <= sizeof(T)*8); - - using Type = T; - static constexpr size_t Capacity = CAPACITY; - T value; - - template - 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 - 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 using next = pack_input; -}; - -template -struct pack_input -{ - using Type = T; - static constexpr size_t Capacity = 0; - T value; - - template [[maybe_unused]] constexpr T get() const = delete; - template [[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 struct next - { - static_assert(N == 0, "reading past the end"); - static_assert(N != 0, "reading past the end"); - }; }; template @@ -84,31 +30,6 @@ template using make_tuple_type = typename ma template struct empty_pack_tuple {}; -template -requires requires() { sizeof...(Is) == sizeof...(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 = pack_input; - using next_type = typename S::template next; - get(p) = st.template get(); - T next_value = st.template advance(); - read_pack(p, next_type{next_value}, std::index_sequence{}, empty_pack_tuple{}); -} - -template -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 read_pack(Place&, pack_input, std::index_sequence, empty_pack_tuple) = delete; - -template using make_pack = empty_pack_tuple...>; - } // namespace floormat::detail_Pack namespace floormat::Pack { -- cgit v1.2.3