Loading...
Searching...
No Matches
juce_EnumHelpers.h File Reference

Namespaces

namespace  juce

Macros

#define JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS(EnumType)
 Macro to enable bitwise operations for scoped enums (enum struct/class).

Functions

template<typename EnumType, std::enable_if_t< std::is_enum_v< EnumType >, int > = 0>
constexpr bool juce::hasBitValueSet (EnumType enumValue, EnumType valueToLookFor) noexcept
template<typename EnumType, std::enable_if_t< std::is_enum_v< EnumType >, int > = 0>
constexpr EnumType juce::withBitValueSet (EnumType enumValue, EnumType valueToAdd) noexcept
template<typename EnumType, std::enable_if_t< std::is_enum_v< EnumType >, int > = 0>
constexpr EnumType juce::withBitValueCleared (EnumType enumValue, EnumType valueToRemove) noexcept

Macro Definition Documentation

◆ JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS

#define JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS ( EnumType)
Value:
static_assert (std::is_enum_v<EnumType>, \
"JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS " \
"should only be used with enum types"); \
constexpr auto operator& (EnumType a, EnumType b) \
{ \
using base_type = std::underlying_type<EnumType>::type; \
return static_cast<EnumType> (base_type (a) & base_type (b)); \
} \
constexpr auto operator| (EnumType a, EnumType b) \
{ \
using base_type = std::underlying_type<EnumType>::type; \
return static_cast<EnumType> (base_type (a) | base_type (b)); \
} \
constexpr auto operator~ (EnumType a) \
{ \
using base_type = std::underlying_type<EnumType>::type; \
return static_cast<EnumType> (~base_type (a)); \
} \
constexpr auto& operator|= (EnumType& a, EnumType b) \
{ \
a = (a | b); \
return a; \
} \
constexpr auto& operator&= (EnumType& a, EnumType b) \
{ \
a = (a & b); \
return a; \
}

Macro to enable bitwise operations for scoped enums (enum struct/class).

To use this, add the line JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS (MyEnum) after your enum declaration at file scope level.

e.g.

enum class MyEnum
{
one = 1 << 0,
two = 1 << 1,
three = 1 << 2
};
MyEnum e = MyEnum::one | MyEnum::two;
bool hasTwo = (e & MyEnum::two) != MyEnum{}; // true
bool hasTwo = hasBitValueSet (e, MyEnum::two); // true
e = withBitValueCleared (e, MyEnum::two);
bool hasTwo = hasBitValueSet (e, MyEnum::two); // false
#define JUCE_DECLARE_SCOPED_ENUM_BITWISE_OPERATORS(EnumType)
Macro to enable bitwise operations for scoped enums (enum struct/class).
Definition juce_EnumHelpers.h:64
constexpr EnumType withBitValueCleared(EnumType enumValue, EnumType valueToRemove) noexcept
Definition juce_EnumHelpers.h:111
constexpr bool hasBitValueSet(EnumType enumValue, EnumType valueToLookFor) noexcept
Definition juce_EnumHelpers.h:99
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram