-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitFieldProxy.h
More file actions
23 lines (18 loc) · 808 Bytes
/
Copy pathBitFieldProxy.h
File metadata and controls
23 lines (18 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef Z7_BITFIELDPROXY_H
#define Z7_BITFIELDPROXY_H
template<typename T, T BitFieldSize>
struct BitFieldProxy {
using value_type = T;
static constexpr T field_mask = (1 << BitFieldSize) - 1;
value_type &value;
const value_type field_offset; ///< Offset to the LSB of the field.
// Assign a new value for the specified field.
constexpr BitFieldProxy &operator=(value_type field_value) {
value &= ~(field_mask << field_offset); // Clear existing bits
value |= (field_value & field_mask) << field_offset; // Set new bits (being defensive with the mask)
return *this;
}
// Dereference to get the current value for the field.
constexpr value_type operator*() const { return (value >> field_offset) & field_mask; }
};
#endif // Z7_BITFIELDPROXY_H