summaryrefslogtreecommitdiff
path: root/src/common/bit_field.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/bit_field.h')
-rw-r--r--src/common/bit_field.h14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/common/bit_field.h b/src/common/bit_field.h
index 7433c39ba..8e35c463f 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)
111template <std::size_t Position, std::size_t Bits, typename T> 112template <std::size_t Position, std::size_t Bits, typename T, typename EndianTag = LETag>
112struct BitField { 113struct BitField {
113private: 114private:
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,7 +122,11 @@ 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
124public: 127public:
128 BitField& operator=(const BitField&) = default;
129
125 /// Constants to allow limited introspection of fields if needed 130 /// Constants to allow limited introspection of fields if needed
126 static constexpr std::size_t position = Position; 131 static constexpr std::size_t position = Position;
127 static constexpr std::size_t bits = Bits; 132 static constexpr std::size_t bits = Bits;
@@ -170,7 +175,7 @@ public:
170 } 175 }
171 176
172 constexpr FORCE_INLINE void Assign(const T& value) { 177 constexpr FORCE_INLINE void Assign(const T& value) {
173 storage = (storage & ~mask) | FormatValue(value); 178 storage = (static_cast<StorageType>(storage) & ~mask) | FormatValue(value);
174 } 179 }
175 180
176 constexpr T Value() const { 181 constexpr T Value() const {
@@ -182,7 +187,7 @@ public:
182 } 187 }
183 188
184private: 189private:
185 StorageType storage; 190 StorageTypeWithEndian storage;
186 191
187 static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); 192 static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range");
188 193
@@ -193,3 +198,6 @@ private:
193 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable in a BitField"); 198 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable in a BitField");
194}; 199};
195#pragma pack() 200#pragma pack()
201
202template <std::size_t Position, std::size_t Bits, typename T>
203using BitFieldBE = BitField<Position, Bits, T, BETag>;