From 6b81ceb060a0e985380bc33d2f51dcc76aad3eb3 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 25 Jan 2019 12:16:23 -0500 Subject: common/bitfield: make it endianness-aware --- src/common/bit_field.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/common/bit_field.h') diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 21e07925d..bd9e21e1e 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -34,6 +34,7 @@ #include #include #include "common/common_funcs.h" +#include "common/swap.h" /* * Abstract bitfield class @@ -108,7 +109,7 @@ * symptoms. */ #pragma pack(1) -template +template struct BitField { private: // We hide the copy assigment operator here, because the default copy @@ -127,6 +128,8 @@ private: // We store the value as the unsigned type to avoid undefined behaviour on value shifting using StorageType = std::make_unsigned_t; + using StorageTypeWithEndian = typename AddEndian::type; + public: /// Constants to allow limited introspection of fields if needed static constexpr std::size_t position = Position; @@ -172,7 +175,7 @@ public: } constexpr FORCE_INLINE void Assign(const T& value) { - storage = (storage & ~mask) | FormatValue(value); + storage = (static_cast(storage) & ~mask) | FormatValue(value); } constexpr T Value() const { @@ -184,7 +187,7 @@ public: } private: - StorageType storage; + StorageTypeWithEndian storage; static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); @@ -195,3 +198,6 @@ private: static_assert(std::is_trivially_copyable_v, "T must be trivially copyable in a BitField"); }; #pragma pack() + +template +using BitFieldBE = BitField; -- cgit v1.2.3 From efd83570bdb70597b3e06eeb3bced5486ac85eab Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Wed, 5 Sep 2018 02:24:44 +0200 Subject: Make bitfield assignment operator public This change needs to be made to get the code compiling again. It was suggested after a conversation with Lioncash. The conversation can be seen here: https://user-images.githubusercontent.com/20753089/45064197-b6107800-b0b2-11e8-9db8-f696299fb86a.PNG --- src/common/bit_field.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/common/bit_field.h') diff --git a/src/common/bit_field.h b/src/common/bit_field.h index bd9e21e1e..639efe22d 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -112,12 +112,6 @@ template struct BitField { private: - // We hide the copy assigment operator here, because the default copy - // assignment would copy the full storage value, rather than just the bits - // relevant to this particular bit field. - // We don't delete it because we want BitField to be trivially copyable. - constexpr BitField& operator=(const BitField&) = default; - // UnderlyingType is T for non-enum types and the underlying type of T if // T is an enumeration. Note that T is wrapped within an enable_if in the // former case to workaround compile errors which arise when using @@ -131,6 +125,8 @@ private: using StorageTypeWithEndian = typename AddEndian::type; public: + BitField& operator=(const BitField&) = default; + /// Constants to allow limited introspection of fields if needed static constexpr std::size_t position = Position; static constexpr std::size_t bits = Bits; -- cgit v1.2.3