From 68d0784fb7d0f2be40aec9fc5113a9a5351ccda0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 18 Jan 2024 18:58:30 +0100 Subject: a --- serialize/packbits-read.hpp | 18 +++++++++++++----- serialize/packbits-write.hpp | 29 +++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp index 9c814d22..0fea0ba5 100644 --- a/serialize/packbits-read.hpp +++ b/serialize/packbits-read.hpp @@ -71,14 +71,23 @@ struct input template struct next { - static_assert(N == (size_t)-1, "reading past the end"); - static_assert(N != (size_t)-1); + static_assert(!N, "reading past the end"); + static_assert( N, "reading past the end"); }; }; template struct is_input_field : std::bool_constant {}; template struct is_input_field> : std::bool_constant { static_assert(N > 0); }; +template +requires requires (Field& x) +{ + { size_t{Field::Length} > 0 }; + sizeof(std::decay_t); + std::unsigned_integral>; +} +struct is_input_field : std::bool_constant {}; + template constexpr CORRADE_ALWAYS_INLINE void read_(Tuple&& tuple, input st, std::index_sequence) { @@ -88,7 +97,7 @@ constexpr CORRADE_ALWAYS_INLINE void read_(Tuple&& tuple, input st, std static_assert(std::tuple_size_v >= sizeof...(Is)+1, "index count larger than tuple element count"); static_assert(I < std::tuple_size_v, "too few tuple elements"); using Field = std::decay_t>; - static_assert(is_input_field::value, "tuple element must be input"); + static_assert(is_input_field{}, "tuple element must be input_field"); constexpr size_t Size = Field::Length; static_assert(Size <= Left, "data type too small"); using next_type = typename input::template next; @@ -108,8 +117,7 @@ constexpr CORRADE_ALWAYS_INLINE void read_(Tuple&&, input st, std::inde namespace floormat { -template -constexpr void pack_read(Tuple&& tuple, T value) +template constexpr void pack_read(Tuple&& tuple, T value) { constexpr size_t nbits = sizeof(T)*8, tuple_size = std::tuple_size_v>; diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index 7cece32b..7df095c1 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -11,7 +11,6 @@ namespace floormat::Pack_impl { template struct output { - static_assert(std::is_fundamental_v); static_assert(LEFT >= 0); static_assert(LEFT <= CAPACITY); static_assert(CAPACITY <= sizeof(T)*8); @@ -31,6 +30,14 @@ struct output_field template struct is_output_field : std::bool_constant {}; template struct is_output_field> : std::bool_constant { static_assert(N > 0); }; +template +requires requires (const Field& x) { + { size_t{Field::Length} > 0 }; + sizeof(std::decay_t); + std::unsigned_integral>; +} +struct is_output_field : std::bool_constant {}; + template constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple& tuple, output st, std::index_sequence) { @@ -39,7 +46,8 @@ constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple& tuple, output 0, "too many bits to write"); static_assert(Left <= Capacity, "too many bits to write"); static_assert(I < std::tuple_size_v, "too few tuple elements"); - static_assert(is_output_field(tuple))>>{}, "tuple element must be output"); + static_assert(is_output_field>>{}, + "tuple element must be output_field"); constexpr size_t N = std::tuple_element_t::Length; static_assert(N <= Left, "too many bits to write"); @@ -61,11 +69,20 @@ constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple&, output namespace floormat { -template -[[nodiscard]] constexpr T pack_write(const std::tuple...>& tuple) +template +requires requires (const Tuple& tuple) { + std::tuple_size_v > size_t{0}; + Pack_impl::is_output_field(tuple))>>::value; +} +[[nodiscard]] constexpr auto pack_write(const Tuple& tuple) { - constexpr size_t nbits = sizeof(T)*8; - return Pack_impl::write_(tuple, Pack_impl::output{T{0}}, make_reverse_index_sequence{}); + using Field = std::decay_t>; + static_assert(Pack_impl::is_output_field{}); + using T = std::decay_t().value)>; + constexpr size_t nbits = sizeof(T)*8, tuple_size = std::tuple_size_v; + return Pack_impl::write_(tuple, + Pack_impl::output{T{0}}, + make_reverse_index_sequence{}); } constexpr uint8_t pack_write(const std::tuple<>&) = delete; -- cgit v1.2.3