21template <
typename T,
typename E>
22 requires(std::is_enum_v<E> && std::is_integral_v<T>)
45 [[nodiscard]]
constexpr operator T()
const {
62 template <
typename... Es>
63 requires(std::is_same_v<Es, E> && ...)
73 template <
typename... Es>
74 requires(std::is_same_v<Es, E> && ...)
85 template <
typename... Es>
86 requires(std::is_same_v<Es, E> && ...)
87 constexpr
TBitFlag<T, E> &changeBit(
bool on, Es... es) {
88 (changeBit_(on, es), ...);
96 template <
typename... Es>
97 requires(std::is_same_v<Es, E> && ...)
99 (toggleBit_(es), ...);
108 template <
typename... Es>
109 requires(std::is_same_v<Es, E> && ...)
110 [[nodiscard]]
constexpr bool onBit(Es... es)
const {
111 return (onAnyBit(es...));
119 template <
typename... Es>
120 requires(std::is_same_v<Es, E> && ...)
121 [[nodiscard]]
constexpr bool onAnyBit(Es... es)
const {
122 return (onBit_(es) || ...);
130 template <
typename... Es>
131 requires(std::is_same_v<Es, E> && ...)
132 [[nodiscard]]
constexpr bool onAllBit(Es... es)
const {
133 return (onBit_(es) && ...);
141 template <
typename... Es>
142 requires(std::is_same_v<Es, E> && ...)
143 [[nodiscard]]
constexpr bool offBit(Es... es)
const {
144 return offAllBit(es...);
152 template <
typename... Es>
153 requires(std::is_same_v<Es, E> && ...)
154 [[nodiscard]]
constexpr bool offAllBit(Es... es)
const {
155 return (offBit_(es) && ...);
163 template <
typename... Es>
164 requires(std::is_same_v<Es, E> && ...)
165 [[nodiscard]]
constexpr bool offAnyBit(Es... es)
const {
166 return (offBit_(es) || ...);
174 template <
typename... Es>
175 requires(std::is_same_v<Es, E> && ...)
176 [[nodiscard]]
constexpr T
maskBit(Es... es)
const {
177 return bits & makeMask(es...);
184 template <
typename... Es>
185 requires(std::is_same_v<Es, E> && ...)
186 [[nodiscard]]
constexpr T
makeMask(Es... es)
const {
187 return (makeMask_(es) | ...);
211 return on ? set(mask) : reset(mask);
217 [[nodiscard]]
constexpr bool on(T mask)
const {
218 return (bits & mask) != 0;
224 [[nodiscard]]
constexpr bool onAll(T mask)
const {
225 return (bits | mask) == bits;
231 [[nodiscard]]
constexpr bool off(T mask)
const {
232 return (bits & mask) == 0;
253 typedef std::underlying_type_t<E> EI;
254 static constexpr size_t MAX_CAPACITY = std::numeric_limits<T>::digits;
260 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
268 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
277 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
278 change(on, makeMask_(e));
285 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
286 changeBit_(offBit_(e), e);
293 [[nodiscard]]
constexpr bool onBit_(E e)
const {
294 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
295 return on(makeMask_(e));
302 [[nodiscard]]
constexpr bool offBit_(E e)
const {
303 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
304 return off(makeMask_(e));
312 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
313 return static_cast<T
>(1) <<
static_cast<T
>(e);
322template <
size_t N,
typename E>
323 requires(std::is_enum_v<E> && N > 64)
335 std::fill(bits.begin(), bits.end(), 0);
344 template <
typename... Es>
345 requires(std::is_same_v<Es, E> && ...)
355 template <
typename... Es>
356 requires(std::is_same_v<Es, E> && ...)
358 (resetBit_(es), ...);
367 template <
typename... Es>
368 requires(std::is_same_v<Es, E> && ...)
370 (changeBit_(on, es), ...);
379 template <
typename... Es>
380 requires(std::is_same_v<Es, E> && ...)
381 [[nodiscard]]
constexpr bool onBit(Es... es)
const {
382 return onAnyBit(es...);
390 template <
typename... Es>
391 requires(std::is_same_v<Es, E> && ...)
392 [[nodiscard]]
constexpr bool onAnyBit(Es... es)
const {
393 return (onBit_(es) || ...);
401 template <
typename... Es>
402 requires(std::is_same_v<Es, E> && ...)
403 [[nodiscard]]
constexpr bool onAllBit(Es... es)
const {
404 return (onBit_(es) && ...);
412 template <
typename... Es>
413 requires(std::is_same_v<Es, E> && ...)
414 [[nodiscard]]
constexpr bool offBit(Es... es)
const {
415 return offAllBit(es...);
423 template <
typename... Es>
424 requires(std::is_same_v<Es, E> && ...)
425 [[nodiscard]]
constexpr bool offAllBit(Es... es)
const {
426 return (offBit_(es) && ...);
434 template <
typename... Es>
435 requires(std::is_same_v<Es, E> && ...)
436 [[nodiscard]]
constexpr bool offAnyBit(Es... es)
const {
437 return (offBit_(es) || ...);
441 typedef std::underlying_type_t<E> EI;
447 auto [field, mask] = makeMask_(e);
455 auto [field, mask] = makeMask_(e);
464 on ? setBit_(e) : resetBit_(e);
471 [[nodiscard]]
constexpr bool onBit_(E e)
const {
472 auto [field, mask] = makeMask_(e);
473 return (field & mask) != 0;
480 [[nodiscard]]
constexpr bool offBit_(E e)
const {
481 auto [field, mask] = makeMask_(e);
482 return (field & mask) == 0;
491 [[nodiscard]]
constexpr std::pair<T, T>
makeMask_(E e)
const {
492 EI ei =
static_cast<EI
>(e);
494 return std::pair<T, T>(bits[ei / C],
static_cast<T
>(1) << (ei % C));
501 [[nodiscard]]
constexpr std::pair<T &, T>
makeMask_(E e) {
502 EI ei =
static_cast<EI
>(e);
504 return std::pair<T &, T>(bits[ei / C],
static_cast<T
>(1) << (ei % C));
507 static constexpr size_t C = 8 *
sizeof(T);
508 std::array<T, (N + C - 1) / C> bits;
Wrapper around a variable length bitfield with an enum corresponding to its bits.
constexpr bool offBit_(E e) const
Checks if a specific bit is off.
constexpr void changeBit_(bool on, E e)
Internal. Changes a specific bit.
constexpr bool offBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
constexpr bool onAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr bool offAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are off.
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr bool onAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are on.
constexpr TBitFlagExt< N, E > & makeAllZero()
Resets all the bits to zero across the entire bitfield array.
constexpr bool onBit_(E e) const
Checks if a specific bit is on.
constexpr std::pair< T &, T > makeMask_(E e)
Gets bit index and mask for a specific bit.
constexpr void setBit_(E e)
Internal. Sets a specific bit.
constexpr bool offAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
constexpr TBitFlagExt()
Default constructor, initializes all flags to off.
constexpr std::pair< T, T > makeMask_(E e) const
Gets bit index and mask for a specific bit.
constexpr void resetBit_(E e)
Internal. Resets a specific bit.
constexpr TBitFlagExt< N, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
Wrapper around an integral type with an enum corresponding to its bits.
constexpr TBitFlag< T, E > & change(bool on, T mask)
Changes the bits using a direct mask.
constexpr bool onAll(T mask) const
Checks if all bits are on in the specified mask.
constexpr void makeAllZero()
Resets all the bits to zero.
constexpr void changeBit_(bool on, E e)
Internal. Changes a specific bit.
constexpr TBitFlag< T, E > & toggleBit(Es... es)
Toggles the corresponding bits for the provided enum values.
constexpr TBitFlag< T, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
constexpr bool offBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
constexpr void setDirect(T mask)
Sets the bits using a direct mask.
constexpr bool off(T mask) const
Checks if all bits are off in the specified mask.
constexpr TBitFlag< T, E > & reset(T mask)
Resets the bits using a direct mask.
constexpr bool onAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are on.
constexpr TBitFlag()
Default constructor, initializes all flags to off.
constexpr bool offAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
constexpr TBitFlag(T mask)
Constructor that initializes the bit flags with a given mask.
constexpr T makeMask(Es... es) const
Creates a mask of the corresponding bits for the provided enum values.
constexpr bool onBit_(E e) const
Checks if a specific bit is on.
constexpr bool offAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are off.
constexpr void resetBit_(E e)
Internal. Resets a specific bit.
T bits
The bit mask representing the flags.
constexpr TBitFlag< T, E > & set(T mask)
Sets the bits using a direct mask.
constexpr TBitFlag< T, E > & operator=(const TBitFlag< T, E > &rhs)
Assignment operator.
constexpr bool on(T mask) const
Checks if any bits are on in the specified mask.
constexpr bool offBit_(E e) const
Checks if a specific bit is off.
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr bool onAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr TBitFlag(E e)
Constructor that initializes the bit flags with a given enum.
constexpr T maskBit(Es... es) const
Creates an applied mask of the corresponding bits for the provided enum values.
constexpr T getDirect() const
Gets the current bit mask.
constexpr T makeMask_(E e) const
Creates a mask for a specific bit.
constexpr void toggleBit_(E e)
Internal. Toggles a specific bit.
constexpr void setBit_(E e)
Internal. Sets a specific bit.