A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
BitFlag.hh
1#pragma once
2
3#include <Logger.hh>
4
5#include <cstddef>
6#include <limits>
7#include <type_traits>
8
9namespace EGG {
10
14template <typename T, typename E>
15 requires(std::is_enum_v<E> && std::is_integral_v<T>)
16struct TBitFlag {
19 constexpr TBitFlag() {
20 makeAllZero();
21 }
22
26 constexpr TBitFlag(T mask) {
27 setDirect(mask);
28 }
29
33 constexpr TBitFlag(E e) : TBitFlag() {
34 setBit(e);
35 }
36
38 [[nodiscard]] constexpr operator T() const {
39 return getDirect();
40 }
41
45 constexpr TBitFlag<T, E> &operator=(const TBitFlag<T, E> &rhs) {
46 bits = rhs.bits;
47 return *this;
48 }
49
55 template <typename... Es>
56 requires(std::is_same_v<Es, E> && ...)
57 constexpr TBitFlag<T, E> &setBit(Es... es) {
58 (setBit_(es), ...);
59 return *this;
60 }
61
66 template <typename... Es>
67 requires(std::is_same_v<Es, E> && ...)
68 constexpr TBitFlag<T, E> &resetBit(Es... es) {
69 (resetBit_(es), ...);
70 return *this;
71 }
72
78 template <typename... Es>
79 requires(std::is_same_v<Es, E> && ...)
80 constexpr TBitFlag<T, E> &changeBit(bool on, Es... es) {
81 (changeBit_(on, es), ...);
82 return *this;
83 }
84
89 template <typename... Es>
90 requires(std::is_same_v<Es, E> && ...)
91 constexpr TBitFlag<T, E> &toggleBit(Es... es) {
92 (toggleBit_(es), ...);
93 return *this;
94 }
95
101 template <typename... Es>
102 requires(std::is_same_v<Es, E> && ...)
103 [[nodiscard]] constexpr bool onBit(Es... es) const {
104 return (onAnyBit(es...));
105 }
106
112 template <typename... Es>
113 requires(std::is_same_v<Es, E> && ...)
114 [[nodiscard]] constexpr bool onAnyBit(Es... es) const {
115 return (onBit_(es) || ...);
116 }
117
123 template <typename... Es>
124 requires(std::is_same_v<Es, E> && ...)
125 [[nodiscard]] constexpr bool onAllBit(Es... es) const {
126 return (onBit_(es) && ...);
127 }
128
134 template <typename... Es>
135 requires(std::is_same_v<Es, E> && ...)
136 [[nodiscard]] constexpr bool offBit(Es... es) const {
137 return offAllBit(es...);
138 }
139
145 template <typename... Es>
146 requires(std::is_same_v<Es, E> && ...)
147 [[nodiscard]] constexpr bool offAllBit(Es... es) const {
148 return (offBit_(es) && ...);
149 }
150
156 template <typename... Es>
157 requires(std::is_same_v<Es, E> && ...)
158 [[nodiscard]] constexpr bool offAnyBit(Es... es) const {
159 return (offBit_(es) || ...);
160 }
161
167 template <typename... Es>
168 requires(std::is_same_v<Es, E> && ...)
169 [[nodiscard]] constexpr T maskBit(Es... es) const {
170 return bits & makeMask(es...);
171 }
172
177 template <typename... Es>
178 requires(std::is_same_v<Es, E> && ...)
179 [[nodiscard]] constexpr T makeMask(Es... es) const {
180 return (makeMask_(es) | ...);
181 }
182
186 constexpr TBitFlag<T, E> &set(T mask) {
187 bits |= mask;
188 return *this;
189 }
190
194 constexpr TBitFlag<T, E> &reset(T mask) {
195 bits &= ~mask;
196 return *this;
197 }
198
203 constexpr TBitFlag<T, E> &change(bool on, T mask) {
204 return on ? set(mask) : reset(mask);
205 }
206
210 [[nodiscard]] constexpr bool on(T mask) const {
211 return (bits & mask) != 0;
212 }
213
217 [[nodiscard]] constexpr bool onAll(T mask) const {
218 return (bits | mask) == bits;
219 }
220
224 [[nodiscard]] constexpr bool off(T mask) const {
225 return (bits & mask) == 0;
226 }
227
229 constexpr void makeAllZero() {
230 bits = 0;
231 }
232
235 [[nodiscard]] constexpr T getDirect() const {
236 return bits;
237 }
238
241 constexpr void setDirect(T mask) {
242 bits = mask;
243 }
244
245private:
246 typedef std::underlying_type_t<E> EI;
247 static constexpr size_t MAX_CAPACITY = std::numeric_limits<T>::digits;
248
252 constexpr void setBit_(E e) {
253 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
254 set(makeMask_(e));
255 }
256
260 constexpr void resetBit_(E e) {
261 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
262 reset(makeMask_(e));
263 }
264
269 constexpr void changeBit_(bool on, E e) {
270 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
271 change(on, makeMask_(e));
272 }
273
277 constexpr void toggleBit_(E e) {
278 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
279 changeBit_(offBit_(e), e);
280 }
281
286 [[nodiscard]] constexpr bool onBit_(E e) const {
287 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
288 return on(makeMask_(e));
289 }
290
295 [[nodiscard]] constexpr bool offBit_(E e) const {
296 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
297 return off(makeMask_(e));
298 }
299
304 [[nodiscard]] constexpr T makeMask_(E e) const {
305 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
306 return static_cast<T>(1) << static_cast<T>(e);
307 }
308
310};
311
312} // namespace EGG
EGG core library.
Definition Archive.cc:6
Wrapper around an integral type with an enum corresponding to its bits.
Definition BitFlag.hh:16
constexpr bool offBit_(E e) const
Checks if a specific bit is off.
Definition BitFlag.hh:295
constexpr bool on(T mask) const
Checks if any bits are on in the specified mask.
Definition BitFlag.hh:210
constexpr TBitFlag< T, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
Definition BitFlag.hh:68
constexpr TBitFlag(T mask)
Constructor that initializes the bit flags with a given mask.
Definition BitFlag.hh:26
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
Definition BitFlag.hh:103
constexpr T maskBit(Es... es) const
Creates an applied mask of the corresponding bits for the provided enum values.
Definition BitFlag.hh:169
constexpr bool onAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are on.
Definition BitFlag.hh:125
constexpr T makeMask(Es... es) const
Creates a mask of the corresponding bits for the provided enum values.
Definition BitFlag.hh:179
constexpr bool onAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
Definition BitFlag.hh:114
constexpr void resetBit_(E e)
Internal. Resets a specific bit.
Definition BitFlag.hh:260
constexpr T getDirect() const
Gets the current bit mask.
Definition BitFlag.hh:235
T bits
The bit mask representing the flags.
Definition BitFlag.hh:309
constexpr void toggleBit_(E e)
Internal. Toggles a specific bit.
Definition BitFlag.hh:277
constexpr void setBit_(E e)
Internal. Sets a specific bit.
Definition BitFlag.hh:252
constexpr bool offBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
Definition BitFlag.hh:136
constexpr void setDirect(T mask)
Sets the bits using a direct mask.
Definition BitFlag.hh:241
constexpr void makeAllZero()
Resets all the bits to zero.
Definition BitFlag.hh:229
constexpr TBitFlag< T, E > & set(T mask)
Sets the bits using a direct mask.
Definition BitFlag.hh:186
constexpr bool offAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
Definition BitFlag.hh:147
constexpr bool offAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are off.
Definition BitFlag.hh:158
constexpr TBitFlag< T, E > & reset(T mask)
Resets the bits using a direct mask.
Definition BitFlag.hh:194
constexpr bool onAll(T mask) const
Checks if all bits are on in the specified mask.
Definition BitFlag.hh:217
constexpr TBitFlag(E e)
Constructor that initializes the bit flags with a given enum.
Definition BitFlag.hh:33
constexpr void changeBit_(bool on, E e)
Internal. Changes a specific bit.
Definition BitFlag.hh:269
constexpr TBitFlag< T, E > & operator=(const TBitFlag< T, E > &rhs)
Assignment operator.
Definition BitFlag.hh:45
constexpr bool onBit_(E e) const
Checks if a specific bit is on.
Definition BitFlag.hh:286
constexpr TBitFlag< T, E > & change(bool on, T mask)
Changes the bits using a direct mask.
Definition BitFlag.hh:203
constexpr bool off(T mask) const
Checks if all bits are off in the specified mask.
Definition BitFlag.hh:224
constexpr TBitFlag()
Default constructor, initializes all flags to off.
Definition BitFlag.hh:19
constexpr T makeMask_(E e) const
Creates a mask for a specific bit.
Definition BitFlag.hh:304
constexpr TBitFlag< T, E > & toggleBit(Es... es)
Toggles the corresponding bits for the provided enum values.
Definition BitFlag.hh:91