diff options
Diffstat (limited to 'src/common/bit_field.h')
| -rw-r--r-- | src/common/bit_field.h | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 7433c39ba..902e668e3 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h | |||
| @@ -34,6 +34,7 @@ | |||
| 34 | #include <limits> | 34 | #include <limits> |
| 35 | #include <type_traits> | 35 | #include <type_traits> |
| 36 | #include "common/common_funcs.h" | 36 | #include "common/common_funcs.h" |
| 37 | #include "common/swap.h" | ||
| 37 | 38 | ||
| 38 | /* | 39 | /* |
| 39 | * Abstract bitfield class | 40 | * Abstract bitfield class |
| @@ -108,7 +109,7 @@ | |||
| 108 | * symptoms. | 109 | * symptoms. |
| 109 | */ | 110 | */ |
| 110 | #pragma pack(1) | 111 | #pragma pack(1) |
| 111 | template <std::size_t Position, std::size_t Bits, typename T> | 112 | template <std::size_t Position, std::size_t Bits, typename T, typename EndianTag = LETag> |
| 112 | struct BitField { | 113 | struct BitField { |
| 113 | private: | 114 | private: |
| 114 | // UnderlyingType is T for non-enum types and the underlying type of T if | 115 | // UnderlyingType is T for non-enum types and the underlying type of T if |
| @@ -121,6 +122,8 @@ private: | |||
| 121 | // We store the value as the unsigned type to avoid undefined behaviour on value shifting | 122 | // We store the value as the unsigned type to avoid undefined behaviour on value shifting |
| 122 | using StorageType = std::make_unsigned_t<UnderlyingType>; | 123 | using StorageType = std::make_unsigned_t<UnderlyingType>; |
| 123 | 124 | ||
| 125 | using StorageTypeWithEndian = typename AddEndian<StorageType, EndianTag>::type; | ||
| 126 | |||
| 124 | public: | 127 | public: |
| 125 | /// Constants to allow limited introspection of fields if needed | 128 | /// Constants to allow limited introspection of fields if needed |
| 126 | static constexpr std::size_t position = Position; | 129 | static constexpr std::size_t position = Position; |
| @@ -170,7 +173,7 @@ public: | |||
| 170 | } | 173 | } |
| 171 | 174 | ||
| 172 | constexpr FORCE_INLINE void Assign(const T& value) { | 175 | constexpr FORCE_INLINE void Assign(const T& value) { |
| 173 | storage = (storage & ~mask) | FormatValue(value); | 176 | storage = (static_cast<StorageType>(storage) & ~mask) | FormatValue(value); |
| 174 | } | 177 | } |
| 175 | 178 | ||
| 176 | constexpr T Value() const { | 179 | constexpr T Value() const { |
| @@ -182,7 +185,7 @@ public: | |||
| 182 | } | 185 | } |
| 183 | 186 | ||
| 184 | private: | 187 | private: |
| 185 | StorageType storage; | 188 | StorageTypeWithEndian storage; |
| 186 | 189 | ||
| 187 | static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); | 190 | static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); |
| 188 | 191 | ||
| @@ -193,3 +196,6 @@ private: | |||
| 193 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable in a BitField"); | 196 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable in a BitField"); |
| 194 | }; | 197 | }; |
| 195 | #pragma pack() | 198 | #pragma pack() |
| 199 | |||
| 200 | template <std::size_t Position, std::size_t Bits, typename T> | ||
| 201 | using BitFieldBE = BitField<Position, Bits, T, BETag>; | ||