19template <
typename T,
typename E>
20 requires(std::is_enum_v<E> && std::is_integral_v<T>)
43 [[nodiscard]]
constexpr operator T()
const {
60 template <
typename... Es>
61 requires(std::is_same_v<Es, E> && ...)
71 template <
typename... Es>
72 requires(std::is_same_v<Es, E> && ...)
83 template <
typename... Es>
84 requires(std::is_same_v<Es, E> && ...)
85 constexpr
TBitFlag<T, E> &changeBit(
bool on, Es... es) {
86 (changeBit_(on, es), ...);
94 template <
typename... Es>
95 requires(std::is_same_v<Es, E> && ...)
97 (toggleBit_(es), ...);
106 template <
typename... Es>
107 requires(std::is_same_v<Es, E> && ...)
108 [[nodiscard]]
constexpr bool onBit(Es... es)
const {
109 return (onAnyBit(es...));
117 template <
typename... Es>
118 requires(std::is_same_v<Es, E> && ...)
119 [[nodiscard]]
constexpr bool onAnyBit(Es... es)
const {
120 return (onBit_(es) || ...);
128 template <
typename... Es>
129 requires(std::is_same_v<Es, E> && ...)
130 [[nodiscard]]
constexpr bool onAllBit(Es... es)
const {
131 return (onBit_(es) && ...);
139 template <
typename... Es>
140 requires(std::is_same_v<Es, E> && ...)
141 [[nodiscard]]
constexpr bool offBit(Es... es)
const {
142 return offAllBit(es...);
150 template <
typename... Es>
151 requires(std::is_same_v<Es, E> && ...)
152 [[nodiscard]]
constexpr bool offAllBit(Es... es)
const {
153 return (offBit_(es) && ...);
161 template <
typename... Es>
162 requires(std::is_same_v<Es, E> && ...)
163 [[nodiscard]]
constexpr bool offAnyBit(Es... es)
const {
164 return (offBit_(es) || ...);
172 template <
typename... Es>
173 requires(std::is_same_v<Es, E> && ...)
174 [[nodiscard]]
constexpr T
maskBit(Es... es)
const {
175 return bits & makeMask(es...);
182 template <
typename... Es>
183 requires(std::is_same_v<Es, E> && ...)
184 [[nodiscard]]
constexpr T
makeMask(Es... es)
const {
185 return (makeMask_(es) | ...);
209 return on ? set(mask) : reset(mask);
215 [[nodiscard]]
constexpr bool on(T mask)
const {
216 return (bits & mask) != 0;
222 [[nodiscard]]
constexpr bool onAll(T mask)
const {
223 return (bits | mask) == bits;
229 [[nodiscard]]
constexpr bool off(T mask)
const {
230 return (bits & mask) == 0;
251 typedef std::underlying_type_t<E> EI;
252 static constexpr size_t MAX_CAPACITY = std::numeric_limits<T>::digits;
258 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
266 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
275 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
276 change(on, makeMask_(e));
283 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
284 changeBit_(offBit_(e), e);
291 [[nodiscard]]
constexpr bool onBit_(E e)
const {
292 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
293 return on(makeMask_(e));
300 [[nodiscard]]
constexpr bool offBit_(E e)
const {
301 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
302 return off(makeMask_(e));
310 ASSERT(
static_cast<EI
>(e) < MAX_CAPACITY);
311 return static_cast<T
>(1) <<
static_cast<T
>(e);
320template <
size_t N,
typename E>
321 requires(std::is_enum_v<E> && N > 64)
333 std::fill(bits.begin(), bits.end(), 0);
342 template <
typename... Es>
343 requires(std::is_same_v<Es, E> && ...)
353 template <
typename... Es>
354 requires(std::is_same_v<Es, E> && ...)
356 (resetBit_(es), ...);
365 template <
typename... Es>
366 requires(std::is_same_v<Es, E> && ...)
368 (changeBit_(on, es), ...);
377 template <
typename... Es>
378 requires(std::is_same_v<Es, E> && ...)
379 [[nodiscard]]
constexpr bool onBit(Es... es)
const {
380 return onAnyBit(es...);
388 template <
typename... Es>
389 requires(std::is_same_v<Es, E> && ...)
390 [[nodiscard]]
constexpr bool onAnyBit(Es... es)
const {
391 return (onBit_(es) || ...);
399 template <
typename... Es>
400 requires(std::is_same_v<Es, E> && ...)
401 [[nodiscard]]
constexpr bool onAllBit(Es... es)
const {
402 return (onBit_(es) && ...);
410 template <
typename... Es>
411 requires(std::is_same_v<Es, E> && ...)
412 [[nodiscard]]
constexpr bool offBit(Es... es)
const {
413 return offAllBit(es...);
421 template <
typename... Es>
422 requires(std::is_same_v<Es, E> && ...)
423 [[nodiscard]]
constexpr bool offAllBit(Es... es)
const {
424 return (offBit_(es) && ...);
432 template <
typename... Es>
433 requires(std::is_same_v<Es, E> && ...)
434 [[nodiscard]]
constexpr bool offAnyBit(Es... es)
const {
435 return (offBit_(es) || ...);
439 typedef std::underlying_type_t<E> EI;
445 auto [field, mask] = makeMask_(e);
453 auto [field, mask] = makeMask_(e);
462 on ? setBit_(e) : resetBit_(e);
469 [[nodiscard]]
constexpr bool onBit_(E e)
const {
470 auto [field, mask] = makeMask_(e);
471 return (field & mask) != 0;
478 [[nodiscard]]
constexpr bool offBit_(E e)
const {
479 auto [field, mask] = makeMask_(e);
480 return (field & mask) == 0;
489 [[nodiscard]]
constexpr std::pair<T, T>
makeMask_(E e)
const {
490 EI ei =
static_cast<EI
>(e);
492 return std::pair<T, T>(bits[ei / C],
static_cast<T
>(1) << (ei % C));
499 [[nodiscard]]
constexpr std::pair<T &, T>
makeMask_(E e) {
500 EI ei =
static_cast<EI
>(e);
502 return std::pair<T &, T>(bits[ei / C],
static_cast<T
>(1) << (ei % C));
505 static constexpr size_t C = 8 *
sizeof(T);
506 std::array<T, (N + C - 1) / C> bits;
Wrapper around a variable length bitfield with an enum corresponding to its bits.
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 TBitFlagExt< N, E > & makeAllZero()
Resets all the bits to zero across the entire bitfield array.
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 bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr void setBit_(E e)
Internal. Sets a specific bit.
constexpr TBitFlagExt< N, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
constexpr bool offBit_(E e) const
Checks if a specific bit is off.
constexpr void resetBit_(E e)
Internal. Resets a specific bit.
constexpr std::pair< T &, T > makeMask_(E e)
Gets bit index and mask for a specific bit.
constexpr bool onAllBit(Es... es) const
Checks if all 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_(E e) const
Checks if a specific bit is on.
constexpr bool onAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
Wrapper around an integral type with an enum corresponding to its bits.
constexpr bool offBit_(E e) const
Checks if a specific bit is off.
constexpr bool on(T mask) const
Checks if any bits are on in the specified mask.
constexpr TBitFlag< T, E > & resetBit(Es... es)
Resets the corresponding bits for the provided enum values.
constexpr TBitFlag(T mask)
Constructor that initializes the bit flags with a given mask.
constexpr bool onBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr T maskBit(Es... es) const
Creates an applied mask of the corresponding bits for the provided enum values.
constexpr bool onAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are on.
constexpr T makeMask(Es... es) const
Creates a mask of the corresponding bits for the provided enum values.
constexpr bool onAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are on.
constexpr void resetBit_(E e)
Internal. Resets a specific bit.
constexpr T getDirect() const
Gets the current bit mask.
T bits
The bit mask representing the flags.
constexpr void toggleBit_(E e)
Internal. Toggles a specific bit.
constexpr void setBit_(E e)
Internal. Sets a specific bit.
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 void makeAllZero()
Resets all the bits to zero.
constexpr TBitFlag< T, E > & set(T mask)
Sets the bits using a direct mask.
constexpr bool offAllBit(Es... es) const
Checks if all of the corresponding bits for the provided enum values are off.
constexpr bool offAnyBit(Es... es) const
Checks if any of the corresponding bits for the provided enum values are off.
constexpr TBitFlag< T, E > & reset(T mask)
Resets the bits using a direct mask.
constexpr bool onAll(T mask) const
Checks if all bits are on in the specified mask.
constexpr TBitFlag(E e)
Constructor that initializes the bit flags with a given enum.
constexpr void changeBit_(bool on, E e)
Internal. Changes a specific bit.
constexpr TBitFlag< T, E > & operator=(const TBitFlag< T, E > &rhs)
Assignment operator.
constexpr bool onBit_(E e) const
Checks if a specific bit is on.
constexpr TBitFlag< T, E > & change(bool on, T mask)
Changes the bits using a direct mask.
constexpr bool off(T mask) const
Checks if all bits are off in the specified mask.
constexpr TBitFlag()
Default constructor, initializes all flags to off.
constexpr T makeMask_(E e) const
Creates a mask for a specific bit.
constexpr TBitFlag< T, E > & toggleBit(Es... es)
Toggles the corresponding bits for the provided enum values.