diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-15 07:24:08 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-15 07:24:08 +0100 |
commit | 152f9ef5feff7ea41c647e004f6e98082abf4b1c (patch) | |
tree | 59c2cabb1291aa8d938d2768b8183f14e8b6d8ad /serialize | |
parent | 8c7d8405b7c88d0553ee505061ade27127410431 (diff) |
w
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/packbits-read.hpp | 42 | ||||
-rw-r--r-- | serialize/packbits-write.hpp | 34 | ||||
-rw-r--r-- | serialize/packbits.cpp | 2 | ||||
-rw-r--r-- | serialize/packbits.hpp | 39 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 4 |
5 files changed, 54 insertions, 67 deletions
diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp index 362ec604..67b2efcb 100644 --- a/serialize/packbits-read.hpp +++ b/serialize/packbits-read.hpp @@ -1,11 +1,37 @@ #pragma once -#include "packbits.hpp" #include <type_traits> #include <concepts> #include <tuple> +#include <utility> #include "compat/assert.hpp" -namespace floormat::detail_Pack { +namespace floormat::detail_Pack_input { + +template<std::unsigned_integral T, size_t N> +struct input_bits final +{ + static_assert(std::is_fundamental_v<T>); + static_assert(N > 0); + static_assert(N < sizeof(T)*8); + + using type = T; +}; + +template<std::unsigned_integral T, size_t N> +struct make_tuple_type_ +{ + template<size_t> using index_to_type = T; + template<typename> struct aux; + template<size_t... Is> struct aux<std::index_sequence<Is...>> + { + static_assert(sizeof...(Is) > 0); + using type = std::tuple<index_to_type<Is>...>; + }; + using Seq = typename aux<std::make_index_sequence<N>>::type; +}; +template<std::unsigned_integral T, size_t N> using make_tuple_type = typename make_tuple_type_<T, N>::Seq; + +template<typename... Ts> struct empty_pack_tuple {}; template<std::unsigned_integral T, size_t CAPACITY> struct input @@ -57,16 +83,16 @@ struct input<T, 0> template<size_t N> struct next { - static_assert(N == 0, "reading past the end"); - static_assert(N != 0, "reading past the end"); + static_assert(N == (size_t)-1, "reading past the end"); + static_assert(N != (size_t)-1); }; }; template<std::unsigned_integral T, typename Place, size_t Left, size_t I, size_t... Is, size_t Size, typename... Sizes> -constexpr void read(Place& p, input<T, Left> st, std::index_sequence<I, Is...>, empty_pack_tuple<bits<T, Size>, Sizes...>) +constexpr void read(Place& p, input<T, Left> st, std::index_sequence<I, Is...>, empty_pack_tuple<input_bits<T, Size>, Sizes...>) { static_assert(sizeof...(Is) == sizeof...(Sizes)); - static_assert(Size <= Left, "too many bits requested"); + static_assert(Size <= Left, "data type too small"); static_assert(I < std::tuple_size_v<Place>, "too few tuple members"); using S = input<T, Left>; using next_type = typename S::template next<Size>; @@ -85,9 +111,9 @@ template<std::unsigned_integral T, typename Place, size_t Left, size_t... Is, ty requires(sizeof...(Is) != sizeof...(Sizes)) constexpr void read(Place&, input<T, Left>, std::index_sequence<Is...>, empty_pack_tuple<Sizes...>) = delete; -template<std::unsigned_integral T, size_t... Ns> using make_pack = empty_pack_tuple<bits<T, Ns>...>; +template<std::unsigned_integral T, size_t... Ns> using make_pack = empty_pack_tuple<input_bits<T, Ns>...>; -} // namespace floormat::detail_Pack +} // namespace floormat::detail_Pack_input namespace floormat::pack { diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index 7b69069a..34fc140f 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -1,28 +1,28 @@ #pragma once -#include "packbits.hpp" +#include <concepts> -namespace floormat::detail_Pack { +namespace floormat::detail_Pack_output { -template<std::unsigned_integral T, size_t CAPACITY> +template<std::unsigned_integral T> struct output { - static_assert(CAPACITY <= sizeof(T)*8); - static constexpr size_t Capacity = CAPACITY; - T value; + T value = 0; + uint8_t capacity = sizeof(T)*8; - template<size_t N> - constexpr void set(T x) const + constexpr inline output next(T x, uint8_t bits) const { - static_assert(N > 0); - static_assert(N <= sizeof(T)*8); - static_assert(N <= Capacity); - if constexpr(CAPACITY < sizeof(T)*8) - value <<= CAPACITY; - T x_ = T(x & (1 << N)-1); + fm_assert(bits > 0 && bits <= capacity); + auto val = value; + val <<= bits; + T x_ = T(x & (T{1} << bits)- T{1}); fm_assert(x_ == x); - value |= x; + val |= x_; + return { val | x_, capacity - bits }; } - template<size_t N> using next = output<T, CAPACITY - N>; }; -} // namespace floormat::detail_Pack + + + + +} // namespace floormat::detail_Pack_output diff --git a/serialize/packbits.cpp b/serialize/packbits.cpp index 1a7f5de8..e9ce54bc 100644 --- a/serialize/packbits.cpp +++ b/serialize/packbits.cpp @@ -4,7 +4,7 @@ namespace floormat { -using namespace floormat::detail_Pack; +using namespace floormat::detail_Pack_input; namespace { diff --git a/serialize/packbits.hpp b/serialize/packbits.hpp deleted file mode 100644 index 7d612942..00000000 --- a/serialize/packbits.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include <concepts> -#include <utility> - -namespace floormat::detail_Pack { - -template<std::unsigned_integral T, size_t N> -struct bits final -{ - static_assert(std::is_fundamental_v<T>); - static_assert(N > 0); - static_assert(N < sizeof(T)*8); - - using type = T; -}; - -template<std::unsigned_integral T, size_t N> -struct make_tuple_type_ -{ - template<size_t> using index_to_type = T; - template<typename> struct aux; - template<size_t... Is> struct aux<std::index_sequence<Is...>> - { - static_assert(sizeof...(Is) > 0); - using type = std::tuple<index_to_type<Is>...>; - }; - using Seq = typename aux<std::make_index_sequence<N>>::type; -}; -template<std::unsigned_integral T, size_t N> using make_tuple_type = typename make_tuple_type_<T, N>::Seq; - -template<typename... Ts> struct empty_pack_tuple {}; - -} // namespace floormat::detail_Pack - -namespace floormat::pack { - - - -} // namespace floormat::pack diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index 2e1c7daa..fd533cee 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -417,8 +417,8 @@ void writer_state::serialize_scenery(const chunk& c, writer_t& s) fm_assert(L.falloff < light_falloff_COUNT); uint8_t flags = 0; flags |= (uint8_t)exact; // 1 bit - flags |= ((uint8_t)L.r & lowbits<rotation_BITS>) << 1; // 3 bits - flags |= ((uint8_t)L.falloff & lowbits<light_falloff_BITS>) << 4; // 2 bits + flags |= ((uint8_t)L.r & lowbits<rotation_BITS>) << 1; // 3 input_bits + flags |= ((uint8_t)L.falloff & lowbits<light_falloff_BITS>) << 4; // 2 input_bits flags |= (uint8_t)!!L.enabled << 7; // 1 bit s << flags; } |