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 (onBit_(es) || ...);
105 }
106
112 template <typename... Es>
113 requires(std::is_same_v<Es, E> && ...)
114 [[nodiscard]] constexpr bool offBit(Es... es) const {
115 return (offBit_(es) && ...);
116 }
117
123 template <typename... Es>
124 requires(std::is_same_v<Es, E> && ...)
125 [[nodiscard]] constexpr T maskBit(Es... es) const {
126 return bits & makeMask(es...);
127 }
128
133 template <typename... Es>
134 requires(std::is_same_v<Es, E> && ...)
135 [[nodiscard]] constexpr T makeMask(Es... es) const {
136 return (makeMask_(es) | ...);
137 }
138
142 constexpr TBitFlag<T, E> &set(T mask) {
143 bits |= mask;
144 return *this;
145 }
146
150 constexpr TBitFlag<T, E> &reset(T mask) {
151 bits &= ~mask;
152 return *this;
153 }
154
159 constexpr TBitFlag<T, E> &change(bool on, T mask) {
160 return on ? set(mask) : reset(mask);
161 }
162
166 [[nodiscard]] constexpr bool on(T mask) const {
167 return (bits & mask) != 0;
168 }
169
173 [[nodiscard]] constexpr bool onAll(T mask) const {
174 return (bits | mask) == bits;
175 }
176
180 [[nodiscard]] constexpr bool off(T mask) const {
181 return (bits & mask) == 0;
182 }
183
185 constexpr void makeAllZero() {
186 bits = 0;
187 }
188
191 [[nodiscard]] constexpr T getDirect() const {
192 return bits;
193 }
194
197 constexpr void setDirect(T mask) {
198 bits = mask;
199 }
200
201private:
202 typedef std::underlying_type_t<E> EI;
203 static constexpr size_t MAX_CAPACITY = std::numeric_limits<T>::digits;
204
208 constexpr void setBit_(E e) {
209 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
210 set(makeMask_(e));
211 }
212
216 constexpr void resetBit_(E e) {
217 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
218 reset(makeMask_(e));
219 }
220
225 constexpr void changeBit_(bool on, E e) {
226 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
227 change(on, makeMask_(e));
228 }
229
233 constexpr void toggleBit_(E e) {
234 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
235 changeBit_(offBit_(e), e);
236 }
237
242 [[nodiscard]] constexpr bool onBit_(E e) const {
243 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
244 return on(makeMask_(e));
245 }
246
251 [[nodiscard]] constexpr bool offBit_(E e) const {
252 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
253 return off(makeMask_(e));
254 }
255
260 [[nodiscard]] constexpr T makeMask_(E e) const {
261 ASSERT(static_cast<EI>(e) < MAX_CAPACITY);
262 return static_cast<T>(1) << static_cast<T>(e);
263 }
264
266};
267
268} // 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:251
constexpr bool on(T mask) const
Checks if any bits are on in the specified mask.
Definition BitFlag.hh:166
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:125
constexpr T makeMask(Es... es) const
Creates a mask of the corresponding bits for the provided enum values.
Definition BitFlag.hh:135
constexpr void resetBit_(E e)
Internal. Resets a specific bit.
Definition BitFlag.hh:216
constexpr T getDirect() const
Gets the current bit mask.
Definition BitFlag.hh:191
T bits
The bit mask representing the flags.
Definition BitFlag.hh:265
constexpr void toggleBit_(E e)
Internal. Toggles a specific bit.
Definition BitFlag.hh:233
constexpr void setBit_(E e)
Internal. Sets a specific bit.
Definition BitFlag.hh:208
constexpr bool offBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
Definition BitFlag.hh:114
constexpr void setDirect(T mask)
Sets the bits using a direct mask.
Definition BitFlag.hh:197
constexpr void makeAllZero()
Resets all the bits to zero.
Definition BitFlag.hh:185
constexpr TBitFlag< T, E > & set(T mask)
Sets the bits using a direct mask.
Definition BitFlag.hh:142
constexpr TBitFlag< T, E > & reset(T mask)
Resets the bits using a direct mask.
Definition BitFlag.hh:150
constexpr bool onAll(T mask) const
Checks if all bits are on in the specified mask.
Definition BitFlag.hh:173
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:225
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:242
constexpr TBitFlag< T, E > & change(bool on, T mask)
Changes the bits using a direct mask.
Definition BitFlag.hh:159
constexpr bool off(T mask) const
Checks if all bits are off in the specified mask.
Definition BitFlag.hh:180
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:260
constexpr TBitFlag< T, E > & toggleBit(Es... es)
Toggles the corresponding bits for the provided enum values.
Definition BitFlag.hh:91