This repository was archived by the owner on Apr 27, 2026. It is now read-only.
forked from StrikerX3/Ymir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmask_enum.hpp
More file actions
289 lines (251 loc) · 9.46 KB
/
bitmask_enum.hpp
File metadata and controls
289 lines (251 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#pragma once
/**
@file
@brief Type-safe enum class bitmasks
To enable enum classes to be used as bitmasks, use the `ENABLE_BITMASK_OPERATORS(x)` macro:
```cpp
enum class MyBitmask {
None = 0b0000,
One = 0b0001,
Two = 0b0010,
Three = 0b0100,
Four = 0b1000,
};
ENABLE_BITMASK_OPERATORS(MyBitmask)
```
From now on, `MyBitmask`'s values can be used with bitwise operators.
The macro must be used at the global namespace scope. To enable `BitmaskEnum` for types living under a namespace, use
the following idiom:
```cpp
namespace ns {
enum class MyBitmask {
None = 0b0000,
One = 0b0001,
Two = 0b0010,
Three = 0b0100,
Four = 0b1000,
};
}
ENABLE_BITMASK_OPERATORS(ns::MyBitmask)
```
You may find it cumbersome to check for the presence or absence of specific values in enum class bitmasks. For example:
```cpp
MyBitmask bm = ...;
MyBitmask oneAndThree = (MyBitmask::One | MyBitmask::Three);
// Check if either bit one or three is set
if ((bm & oneAndThree) != MyBitmask::None) {
...
}
// Check if both bits one and three are set
if ((bm & oneAndThree) == oneAndThree) {
...
}
```
To help with that, you can wrap the bitmask into the BitmaskEnum type, which provides a set of bitmask checks and useful
conversions:
```cpp
MyBitmask bm = ...;
MyBitmask oneAndThree = (MyBitmask::One | MyBitmask::Three);
auto wbm = BitmaskEnum(bm);
// Check if either bit one or three is set
if (wbm.AnyOf(oneAndThree)) {
...
}
// Check if both bits one and three are set
if (wbm.AllOf(oneAndThree)) {
...
}
// Check if neither bit one nor three is set
if (wbm.NoneOf(oneAndThree)) {
...
}
// Check if any bits other than one and three are set
if (wbm.AnyExcept(oneAndThree)) {
...
}
// Check if no bits other than one and three are set
if (wbm.NoneExcept(oneAndThree)) {
...
}
// Check if any bit is set
if (wbm.Any()) { // or just (wbm)
...
}
// Check if no bits are set
if (wbm.None()) { // or just (!wbm)
...
}
// Convert back to the enum class
MyBitmask backToEnum = wbm;
```
*/
#include <type_traits>
/// @brief Enables usage of bitwise operators with type `x`.
///
/// @param[in] x the enum type
#define ENABLE_BITMASK_OPERATORS(x) \
template <> \
struct is_bitmask_enum<x> { \
static const bool enable = true; \
};
/// @brief Identifies enumerable types.
/// @tparam Enum the type to check
template <typename T>
concept Enumerable = std::is_enum_v<T>;
/// @brief Enables or disables bitwise operators for type `Enum`.
///
/// Specializations for enum types must contain a `static constexpr bool` field called `enabled` with the value `true`
/// to enable bitwise operators for that type. By default, bitwise operators are disabled for all types.
///
/// The easiest way to enable bitwise operators for enums is by using `ENABLE_BITMASK_OPERATORS(x)`.
///
/// @tparam Enum the enum type
template <Enumerable Enum>
struct is_bitmask_enum {
/// @brief Whether bitwise operators are enabled for type `Enum`.
static constexpr bool enable = false;
};
/// @brief Determines if bitwise operators are enabled for type `Enum`.
/// @tparam Enum the enum type
template <class Enum>
constexpr bool is_bitmask_enum_v = is_bitmask_enum<Enum>::enable;
/// @brief Identifies enum types with bitwise operators enabled.
///
/// Matches types which have bitmask operators enabled with `ENABLE_BITMASK_OPERATORS(x)`. Such types can be wrapped in
/// a `BitmaskEnum` to perform common bitmask query operations.
///
/// @tparam Enum the enum type
template <class Enum>
concept BitmaskType = is_bitmask_enum_v<Enum>;
// ----- Bitwise operators ----------------------------------------------------
/// @brief Bitwise OR operator applied to two `Enum` values.
/// @tparam Enum the enum type
/// @param[in] lhs the left-hand side value
/// @param[in] rhs the right-hand side value
/// @return `lhs | rhs`
template <BitmaskType Enum>
constexpr Enum operator|(Enum lhs, Enum rhs) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
return static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
}
/// @brief Bitwise AND operator applied to two `Enum` values.
/// @tparam Enum the enum type
/// @param[in] lhs the left-hand side value
/// @param[in] rhs the right-hand side value
/// @return `lhs & rhs`
template <BitmaskType Enum>
constexpr Enum operator&(Enum lhs, Enum rhs) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
return static_cast<Enum>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
}
/// @brief Bitwise XOR operator applied to two `Enum` values.
/// @tparam Enum the enum type
/// @param[in] lhs the left-hand side value
/// @param[in] rhs the right-hand side value
/// @return `lhs ^ rhs`
template <BitmaskType Enum>
constexpr Enum operator^(Enum lhs, Enum rhs) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
return static_cast<Enum>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs));
}
/// @brief Bitwise NOT operator applied to an `Enum` value.
/// @tparam Enum the enum type
/// @param[in] value the value
/// @return `~value`
template <BitmaskType Enum>
constexpr Enum operator~(Enum value) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
return static_cast<Enum>(~static_cast<underlying>(value));
}
// ----- Bitwise assignment operators -----------------------------------------
/// @brief Bitwise OR assignment between two `Enum` values.
/// @tparam Enum the enum type
/// @param[in,out] lhs the left-hand side value, where the result is stored
/// @param[in] rhs the right-hand side value
/// @return a reference to `lhs` after ORing with `rhs`
template <BitmaskType Enum>
constexpr Enum operator|=(Enum &lhs, Enum rhs) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
lhs = static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
return lhs;
}
/// @brief Bitwise AND assignment between two `Enum` values.
/// @tparam Enum the enum type
/// @param[in,out] lhs the left-hand side value, where the result is stored
/// @param[in] rhs the right-hand side value
/// @return a reference to `lhs` after ANDing with `rhs`
template <BitmaskType Enum>
constexpr Enum operator&=(Enum &lhs, Enum rhs) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
lhs = static_cast<Enum>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
return lhs;
}
/// @brief Bitwise XOR assignment between two `Enum` values.
/// @tparam Enum the enum type
/// @param[in,out] lhs the left-hand side value, where the result is stored
/// @param[in] rhs the right-hand side value
/// @return a reference to `lhs` after XORing with `rhs`
template <BitmaskType Enum>
constexpr Enum operator^=(Enum &lhs, Enum rhs) noexcept {
using underlying = typename std::underlying_type_t<Enum>;
lhs = static_cast<Enum>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs));
return lhs;
}
// ----- Bitwise mask checks --------------------------------------------------
/// @brief Wraps the enumerated type `Enum` to simplify bitmask queries.
///
/// The `Enum` must have bitwise operators enabled with `ENABLE_BITMASK_OPERATORS(x)`.
///
/// @tparam Enum the enum type
template <BitmaskType Enum>
struct BitmaskEnum {
/// @brief The bitmask enum value.
const Enum value;
/// @brief A representation of an empty bitmask of the `Enum` type.
static constexpr Enum none = static_cast<Enum>(0);
/// @brief Creates a `BitmaskEnum` from the given value.
/// @param[in] value the enum value
constexpr BitmaskEnum(Enum value) noexcept
: value(value) {}
/// @brief Converts back to `Enum`.
constexpr operator Enum() const noexcept {
return value;
}
/// @brief Converts to true if there is any bit set in the bitmask.
constexpr operator bool() const noexcept {
return Any();
}
/// @brief Returns true if any bit is set.
[[nodiscard]] constexpr bool Any() const noexcept {
return value != none;
}
/// @brief Returns true if all bits are clear.
[[nodiscard]] constexpr bool None() const noexcept {
return value == none;
}
/// @brief Returns true if any bit in the given mask is set.
/// @param[in] mask the bitmask to check against the value in this `BitmaskEnum`
[[nodiscard]] constexpr bool AnyOf(Enum mask) const noexcept {
return (value & mask) != none;
}
/// @brief Returns true if all bits in the given mask are set.
/// @param[in] mask the bitmask to check against the value in this `BitmaskEnum`
[[nodiscard]] constexpr bool AllOf(Enum mask) const noexcept {
return (value & mask) == mask;
}
/// @brief Returns true if none of the bits in the given mask are set.
/// @param[in] mask the bitmask to check against the value in this `BitmaskEnum`
[[nodiscard]] constexpr bool NoneOf(Enum mask) const noexcept {
return (value & mask) == none;
}
/// @brief Returns true if any bits excluding the mask are set.
/// @param[in] mask the bitmask to check against the value in this `BitmaskEnum`
[[nodiscard]] constexpr bool AnyExcept(Enum mask) const noexcept {
return (value & ~mask) != none;
}
/// @brief Returns true if no bits excluding the mask are set.
/// @param[in] mask the bitmask to check against the value in this `BitmaskEnum`
[[nodiscard]] constexpr bool NoneExcept(Enum mask) const noexcept {
return (value & ~mask) == none;
}
};