Flags¶
Utilities for enabling bitmask operations on enum classes.
Overview¶
Typical use cases:
Enabling bitmask operations on enum classes
Header¶
<RaeptorCogs/Flags.hpp>
Metadata¶
- Author
Estorc
- Version
v1.0
- Copyright
Copyright (c) 2025 Estorc MIT License.
Structs¶
Struct |
Description |
|---|---|
|
Storage structure for enum flags. |
|
Check if a enum is a class enum. |
|
Check if a enum is a plain enum. |
-
template<typename Enum>
struct EnumStorage¶ Storage structure for enum flags.
Public Types
-
using T = std::underlying_type_t<Enum>¶
-
using T = std::underlying_type_t<Enum>¶
-
template<class T>
struct isClassEnum¶ Check if a enum is a class enum.
Public Static Attributes
-
static bool value = std::is_enum<T>::is_enum && !std::is_convertible<T, int>::is_convertible¶
-
static bool value = std::is_enum<T>::is_enum && !std::is_convertible<T, int>::is_convertible¶
-
template<class T>
struct isPlainEnum¶ Check if a enum is a plain enum.
Public Static Attributes
-
static bool value = std::is_enum<T>::is_enum && std::is_convertible<T, int>::is_convertible¶
-
static bool value = std::is_enum<T>::is_enum && std::is_convertible<T, int>::is_convertible¶
Classes¶
Class |
Description |
|---|---|
|
FlagSet class for managing multiple enum class flags. |
-
template<typename ...Enums>
class FlagSet¶ FlagSet class for managing multiple enum class flags.
This class allows you to manage flags from multiple enum classes in a single structure.
enum class FlagEnumA : uint32_t { FLAG_A1 = 1 << 0, FLAG_A2 = 1 << 1, }; enum class FlagEnumB : uint32_t { FLAG_B1 = 1 << 0, FLAG_B2 = 1 << 1, }; RaeptorCogs::FlagSet<FlagEnumA, FlagEnumB> flagSet; flagSet.setFlag(FlagEnumA::FLAG_A1); flagSet.setFlag(FlagEnumB::FLAG_B2); bool hasA1 = flagSet.hasFlag(FlagEnumA::FLAG_A1); // true bool hasB1 = flagSet.hasFlag(FlagEnumB::FLAG_B1); // false
Public Functions
-
template<typename Enum>
inline void toggleFlag(Enum flag)¶ Toggle a flag.
- Parameters:
flag – Flag to toggle.
-
template<typename Enum>
inline void clearFlag(Enum flag)¶ Clear a flag.
- Parameters:
flag – Flag to clear.
-
inline void clearFlag()¶
Clear all flags.
Private Members
-
std::tuple<EnumStorage<Enums>...> storage¶
Stored flags.
Holds the combined flags for all enum types.
Private Static Functions
-
template<typename Enum>
static inline auto &getStorage(std::tuple<EnumStorage<Enums>...> &t)¶ Get the storage for a specific enum type.
- Parameters:
t – The enum class type.
- Returns:
Reference to the storage for the specified enum type.
-
template<typename Enum>
static inline const auto &getStorage(const std::tuple<EnumStorage<Enums>...> &t)¶ Get the storage for a specific enum type (const version).
- Parameters:
t – The enum class type.
- Returns:
Const reference to the storage for the specified enum type.
-
template<typename Enum>
Functions¶
Function |
Description |
|---|---|
|
Enable NOT operator for a given enum class. |
|
Enable inequality operator for a given enum class. |
|
Enable AND bitmask operator for a given enum class. |
|
Enable AND assignment bitmask operator for a given enum class. |
|
Enable equality operator for a given enum class. |
|
Enable XOR bitmask operator for a given enum class. |
|
Enable XOR assignment bitmask operator for a given enum class. |
|
Enable OR bitmask operator for a given enum class. |
|
Enable OR assignment bitmask operator for a given enum class. |
|
Enable NOT bitmask operator for a given enum class. |
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, bool> RaeptorCogs::operator!(Enum a)¶ Enable NOT operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, FLAG_B = 1 << 1, }; MyFlags flags = MyFlags::NONE; bool isNone = !flags; // true if no flags are set
- Parameters:
a – The enum class value.
- Returns:
true if the enum value is zero, false otherwise.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, bool> RaeptorCogs::operator!=(Enum a, Enum b)¶ Enable inequality operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, FLAG_B = 1 << 1, }; bool isNotEqual = (MyFlags::FLAG_A != MyFlags::FLAG_B); // true
- Parameters:
a – First operand.
b – Second operand.
- Returns:
true if both enum values are not equal, false otherwise.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum> RaeptorCogs::operator&(Enum a, Enum b)¶ Enable AND bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, // 01 FLAG_B = 1 << 1, // 10 }; MyFlags intersection = MyFlags::FLAG_A & MyFlags::FLAG_B; // 00
- Parameters:
a – First operand.
b – Second operand.
- Returns:
The result of the bitwise AND operation.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum&> RaeptorCogs::operator&=(Enum &a, Enum b)¶ Enable AND assignment bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, FLAG_B = 1 << 1, }; MyFlags flags = MyFlags::FLAG_A | MyFlags::FLAG_B; flags &= MyFlags::FLAG_A; // flags now only has FLAG_A set
- Parameters:
a – Variable to modify.
b – Value to AND with.
- Returns:
Reference to the modified enum after the operation.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, bool> RaeptorCogs::operator==(Enum a, Enum b)¶ Enable equality operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, FLAG_B = 1 << 1, }; bool isEqual = (MyFlags::FLAG_A == MyFlags::FLAG_A); // true
- Parameters:
a – First operand.
b – Second operand.
- Returns:
true if both enum values are equal, false otherwise.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum> RaeptorCogs::operator^(Enum a, Enum b)¶ Enable XOR bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, // 01 FLAG_B = 1 << 1, // 10 }; MyFlags xorFlags = MyFlags::FLAG_A ^ MyFlags::FLAG_B; // 11
- Parameters:
a – First operand.
b – Second operand.
- Returns:
The result of the bitwise XOR operation.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum&> RaeptorCogs::operator^=(Enum &a, Enum b)¶ Enable XOR assignment bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, FLAG_B = 1 << 1, }; MyFlags flags = MyFlags::FLAG_A; flags ^= MyFlags::FLAG_B; // flags now has both FLAG_A and FLAG_B set
- Parameters:
a – Variable to modify.
b – Value to XOR with.
- Returns:
Reference to the modified enum after the operation.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum> RaeptorCogs::operator|(Enum a, Enum b)¶ Enable OR bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, // 01 FLAG_B = 1 << 1, // 10 }; MyFlags union = MyFlags::FLAG_A | MyFlags::FLAG_B; // 11
- Parameters:
a – First operand.
b – Second operand.
- Returns:
The result of the bitwise OR operation.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum&> RaeptorCogs::operator|=(Enum &a, Enum b)¶ Enable OR assignment bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, FLAG_B = 1 << 1, }; MyFlags flags = MyFlags::FLAG_A; flags |= MyFlags::FLAG_B; // flags now has both FLAG_A and FLAG_B set
- Parameters:
a – Variable to modify.
b – Value to OR with.
- Returns:
Reference to the modified enum after the operation.
-
template<typename Enum>
std::enable_if_t<isClassEnum<Enum>::value, Enum> RaeptorCogs::operator~(Enum a)¶ Enable NOT bitmask operator for a given enum class.
enum class MyFlags : uint32_t { FLAG_A = 1 << 0, // 01 FLAG_B = 1 << 1, // 10 }; MyFlags notA = ~MyFlags::FLAG_A; // 10
- Parameters:
a – The enum class value.
- Returns:
The result of the bitwise NOT operation.