diff --git a/include/duckhandy/bitfield_pack.hpp b/include/duckhandy/bitfield_pack.hpp index 93a2bfc..a6fb188 100644 --- a/include/duckhandy/bitfield_pack.hpp +++ b/include/duckhandy/bitfield_pack.hpp @@ -55,29 +55,47 @@ namespace dhandy { struct SizeAndOffs { std::size_t size, offset; }; + struct IndexAndSize { + std::size_t index, size; + }; template - [[gnu::pure]] constexpr inline SizeAndOffs size_and_offset_impl (std::size_t) { throw std::out_of_range("Requested pack index out of range"); } - template - [[gnu::pure]] constexpr inline SizeAndOffs size_and_offset_impl (std::size_t index) { return (index ? size_and_offset_impl(index - 1) : SizeAndOffs{Sz, Sum}); } - template - [[gnu::pure]] constexpr inline SizeAndOffs size_and_offset (std::size_t index) { return size_and_offset_impl<0, Sizes...>(index); } + template + constexpr inline IndexAndSize index_and_size_impl() { + throw std::runtime_error("Should not be reached"); + } + template + constexpr inline IndexAndSize index_and_size_impl() { + return (Idx ? index_and_size_impl< + (CurrMax > Sz ? CurrMax : Sz), + (CurrMax > Sz ? MaxIdx : Idx), + Idx - 1, + Sizes... + >() : IndexAndSize{(CurrMax > Sz ? MaxIdx : Idx) - 1, CurrMax > Sz ? CurrMax : Sz}); + } + template + constexpr inline IndexAndSize index_and_size() { + return index_and_size_impl(); + } + template struct BitfieldFacts { static const constexpr std::size_t Sum = size_and_offset(sizeof...(Sizes) - 1).size + size_and_offset(sizeof...(Sizes) - 1).offset; static const constexpr std::size_t ElementCount = (Sum + CHAR_BIT * sizeof(T) - 1) / (CHAR_BIT *sizeof(T)); + static const constexpr std::size_t LargestIndex = index_and_size().index; + static const constexpr std::size_t LargestSize = index_and_size().size; }; template @@ -96,9 +114,11 @@ namespace dhandy { template class BitfieldPack : private implem::BitfieldData::ElementCount> { + typedef implem::BitfieldFacts Facts; template friend U get (const BitfieldPack&); template friend void set (BitfieldPack&, V); - typedef implem::BitfieldData::ElementCount> base_type; + typedef implem::BitfieldData base_type; + static_assert(Facts::LargestSize <= sizeof(T) * CHAR_BIT, "Bitfield size can't be larget than the data type itself"); public: BitfieldPack (T init=T{}) : base_type(init) {} }; @@ -109,6 +129,7 @@ namespace dhandy { using implem::BitfieldData; using implem::make_mask; + static_assert(Idx < sizeof...(Sizes), "Index out of bounds"); const constexpr auto so = size_and_offset(Idx); const constexpr std::size_t index = so.offset / (sizeof(T) * CHAR_BIT); const constexpr std::size_t reloffs = so.offset % (sizeof(T) * CHAR_BIT); @@ -131,6 +152,7 @@ namespace dhandy { using implem::BitfieldData; using implem::make_mask; + static_assert(Idx < sizeof...(Sizes), "Index out of bounds"); const constexpr auto so = size_and_offset(Idx); const constexpr std::size_t index = so.offset / (sizeof(T) * CHAR_BIT); const constexpr std::size_t reloffs = so.offset % (sizeof(T) * CHAR_BIT); @@ -138,7 +160,7 @@ namespace dhandy { T& bottom = static_cast&>(pack).value(); bottom &= ~(make_mask(so.size) << reloffs); - bottom |= static_cast((val << reloffs) & make_mask(so.size)); + bottom |= static_cast((val & make_mask(so.size)) << reloffs); if (bleeds_into_next) { const std::size_t safe_index = index + 2 - not bleeds_into_next; diff --git a/test/unit/bitfield_pack_test.cpp b/test/unit/bitfield_pack_test.cpp index cad75bb..0ab818d 100644 --- a/test/unit/bitfield_pack_test.cpp +++ b/test/unit/bitfield_pack_test.cpp @@ -20,16 +20,61 @@ #include TEST_CASE ("Check correctness in BitfieldPack", "[BitfieldPack][containers]") { - dhandy::BitfieldPack pack(0xAA); - CHECK(sizeof(pack) == sizeof(uint8_t) * 2); - - CHECK(dhandy::get<0>(pack) == 0b1010U); - CHECK(dhandy::get<1>(pack) == 0b0101010U); - CHECK(dhandy::get<2>(pack) == 0b1U); - - dhandy::set<0>(pack, 2); - CHECK(dhandy::get<0>(pack) == 2); - dhandy::set<1>(pack, 50); - CHECK(dhandy::get<1>(pack) == 50); - CHECK(dhandy::get<2>(pack) == 0b1U); + using dhandy::set; + using dhandy::get; + + { + dhandy::BitfieldPack pack(0xAA); + CHECK(sizeof(pack) == sizeof(uint8_t) * 2); + + CHECK(get<0>(pack) == 0b1010U); + CHECK(get<1>(pack) == 0b0101010U); + CHECK(get<2>(pack) == 0b1U); + + set<0>(pack, 2); + CHECK(get<0>(pack) == 2); + set<1>(pack, 50); + CHECK(get<1>(pack) == 50); + CHECK(get<2>(pack) == 0b1U); + CHECK(get<0>(pack) == 2); + } + + { + dhandy::BitfieldPack pack; + set<6>(pack, 1); + CHECK(get<0>(pack) == 0); + CHECK(get<1>(pack) == 0); + CHECK(get<2>(pack) == 0); + CHECK(get<3>(pack) == 0); + CHECK(get<4>(pack) == 0); + CHECK(get<5>(pack) == 0); + CHECK(get<6>(pack) == 1); + + set<6>(pack, 0); + CHECK(get<6>(pack) == 0); + + set<6>(pack, 1); + set<1>(pack, 5); + CHECK(get<0>(pack) == 0); + CHECK(get<1>(pack) == 5); + CHECK(get<2>(pack) == 0); + CHECK(get<3>(pack) == 0); + CHECK(get<4>(pack) == 0); + CHECK(get<5>(pack) == 0); + CHECK(get<6>(pack) == 1); + + set<1>(pack, 0); + CHECK(get<1>(pack) == 0); + + set<6>(pack, 1); + set<1>(pack, 5); + set<3>(pack, 0b10011001101); + CHECK(get<0>(pack) == 0); + CHECK(get<1>(pack) == 5); + CHECK(get<2>(pack) == 0); + CHECK(get<3>(pack) == 0b10011001101); + CHECK(get<4>(pack) == 0); + CHECK(get<5>(pack) == 0); + CHECK(get<6>(pack) == 1); + } }