1#ifndef CPP_UTILITIES_FLAG_ENUM_CLASS_H
2#define CPP_UTILITIES_FLAG_ENUM_CLASS_H
18#define CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Namespace, EnumClassType) \
19 namespace CppUtilities { \
20 template <> struct IsFlagEnumClass<EnumClassType> : Traits::Bool<true> { \
23 namespace Namespace { \
24 using CppUtilities::FlagEnumClassOperations::operator|; \
25 using CppUtilities::FlagEnumClassOperations::operator&; \
26 using CppUtilities::FlagEnumClassOperations::operator|=; \
27 using CppUtilities::FlagEnumClassOperations::operator+=; \
28 using CppUtilities::FlagEnumClassOperations::operator-=; \
35namespace FlagEnumClassOperations {
37template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
38constexpr FlagEnumClass
operator|(FlagEnumClass lhs, FlagEnumClass rhs)
40 return static_cast<FlagEnumClass
>(
41 static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs) |
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
44template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
45constexpr bool operator&(FlagEnumClass lhs, FlagEnumClass rhs)
47 return static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
48 &
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs);
51template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
52constexpr FlagEnumClass &
operator|=(FlagEnumClass &lhs, FlagEnumClass rhs)
54 return lhs =
static_cast<FlagEnumClass
>(
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
55 |
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
58template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
59constexpr FlagEnumClass &
operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
61 return lhs =
static_cast<FlagEnumClass
>(
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
62 |
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
65template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
66constexpr FlagEnumClass &
operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
68 return lhs =
static_cast<FlagEnumClass
>(
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
69 & (~static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs)));
77template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
78constexpr FlagEnumClass &
modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags,
bool value)
80 return value ? (flagVariable += relevantFlags) : (flagVariable -= relevantFlags);
86template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
87constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
89 return (
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(flagVariable)
90 &
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(flagsToCheck))
91 ==
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(flagsToCheck);
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
constexpr bool operator&(FlagEnumClass lhs, FlagEnumClass rhs)
constexpr FlagEnumClass operator|(FlagEnumClass lhs, FlagEnumClass rhs)
constexpr FlagEnumClass & operator|=(FlagEnumClass &lhs, FlagEnumClass rhs)
Contains all utilities provides by the c++utilities library.
constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
Returns whether the specified flagVariable has set all flags specified via flagsToCheck to true.
constexpr FlagEnumClass & modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags, bool value)
Sets the specified relevantFlags in the specified flagVariable to the specified value.
The IsFlagEnumClass class is used to decide whether to enable operations for flag enums for.
Wraps a static boolean constant.