44struct IsBitmaskEnum : std::is_same<decltype(isBitmaskEnum(std::declval<T>())), std::true_type> {};
52constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator&(T lhs, T rhs)
noexcept {
53 using U =
typename std::underlying_type_t<T>;
54 return static_cast<T
>(
static_cast<U
>(lhs) &
static_cast<U
>(rhs));
59constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator|(T lhs, T rhs)
noexcept {
60 using U =
typename std::underlying_type_t<T>;
61 return static_cast<T
>(
static_cast<U
>(lhs) |
static_cast<U
>(rhs));
66constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator^(T lhs, T rhs)
noexcept {
67 using U =
typename std::underlying_type_t<T>;
68 return static_cast<T
>(
static_cast<U
>(lhs) ^
static_cast<U
>(rhs));
73constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator~(T rhs)
noexcept {
74 using U =
typename std::underlying_type_t<T>;
75 return static_cast<T
>(~static_cast<U>(rhs));
82constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator|=(T& lhs, T rhs)
noexcept {
83 using U =
typename std::underlying_type_t<T>;
84 lhs =
static_cast<T
>(
static_cast<U
>(lhs) |
static_cast<U
>(rhs));
90constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator&=(T& lhs, T rhs)
noexcept {
91 using U =
typename std::underlying_type_t<T>;
92 lhs =
static_cast<T
>(
static_cast<U
>(lhs) &
static_cast<U
>(rhs));
98constexpr typename std::enable_if_t<IsBitmaskEnum<T>::value, T>
operator^=(T& lhs, T rhs)
noexcept {
99 using U =
typename std::underlying_type_t<T>;
100 lhs =
static_cast<T
>(
static_cast<U
>(lhs) ^
static_cast<U
>(rhs));
127 static_assert(std::is_enum_v<T>);
137 : mask_(toUnderlying(
mask)) {}
144 constexpr explicit operator T() const noexcept {
145 return static_cast<T
>(mask_);
166 mask_ |= toUnderlying(
mask);
178 mask_ &= ~toUnderlying(
mask);
189 constexpr bool all() const noexcept {
195 return (mask_ & toUnderlying(
mask)) == toUnderlying(
mask);
199 constexpr bool any() const noexcept {
200 return mask_ != empty;
205 return (mask_ & toUnderlying(
mask)) != empty;
209 constexpr bool none() const noexcept {
210 return mask_ == empty;
215 return (mask_ & toUnderlying(
mask)) == empty;
228template <
typename T,
typename U>
234template <
typename T,
typename U>
240template <
typename T,
typename U>
246template <
typename T,
typename U>
Bitmask using (scoped) enums.
std::underlying_type_t< T > Underlying
constexpr bool allOf(T mask) const noexcept
Check if all of the specified bits are set.
constexpr Bitmask & reset(T mask) noexcept
Reset specified bits.
constexpr bool noneOf(T mask) const noexcept
Check if none of the specified bits are set.
constexpr bool all() const noexcept
Check if all bits are set.
constexpr Bitmask(Underlying mask) noexcept
Create a bitmask from the underlying type.
constexpr Bitmask & set(T mask) noexcept
Set specified bits.
constexpr bool any() const noexcept
Check if any bits are set.
constexpr Bitmask & reset() noexcept
Reset all bits.
constexpr Bitmask & flip() noexcept
Flip all bits.
constexpr bool none() const noexcept
Check if none bits are set.
constexpr Bitmask() noexcept=default
Create an empty bitmask.
constexpr Bitmask & set() noexcept
Set all bits.
constexpr bool anyOf(T mask) const noexcept
Check if any of the specified bits are set.
constexpr Underlying get() const noexcept
Get the bitmask as the underlying type (integer).
constexpr bool operator!=(Bitmask< T > lhs, U rhs) noexcept
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator&=(T &lhs, T rhs) noexcept
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator|(T lhs, T rhs) noexcept
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator~(T rhs) noexcept
constexpr bool operator==(U lhs, Bitmask< T > rhs) noexcept
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator|=(T &lhs, T rhs) noexcept
constexpr std::false_type isBitmaskEnum(T)
Function template to define an enum (class) as a bitmask and allow bitwise operations.
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator&(T lhs, T rhs) noexcept
constexpr bool operator!=(U lhs, Bitmask< T > rhs) noexcept
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator^(T lhs, T rhs) noexcept
constexpr std::enable_if_t< IsBitmaskEnum< T >::value, T > operator^=(T &lhs, T rhs) noexcept
constexpr bool operator==(Bitmask< T > lhs, U rhs) noexcept
Trait to define an enum (class) as a bitmask and allow bitwise operations.
Exposes the possibilities of a client to write the attributes of the node In contrast to the write mask