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+=; \
29 using CppUtilities::FlagEnumClassOperations::operator-=; \
36namespace FlagEnumClassOperations {
38template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
39constexpr FlagEnumClass
operator|(FlagEnumClass lhs, FlagEnumClass rhs)
41 return static_cast<FlagEnumClass
>(
42 static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs) |
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
45#ifdef CPP_UTILITIES_FLAG_ENUM_CLASS_NO_LEGACY_AND
46template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
47constexpr FlagEnumClass
operator&(FlagEnumClass lhs, FlagEnumClass rhs)
49 return static_cast<FlagEnumClass
>(
50 static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs) &
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
53template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
54constexpr bool operator&(FlagEnumClass lhs, FlagEnumClass rhs)
56 return static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
57 &
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs);
61template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
62constexpr bool operator&&(FlagEnumClass lhs, FlagEnumClass rhs)
64 return static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
65 &
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs);
68template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
69constexpr FlagEnumClass &
operator|=(FlagEnumClass &lhs, FlagEnumClass rhs)
71 return lhs =
static_cast<FlagEnumClass
>(
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
72 |
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
75template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
76constexpr FlagEnumClass &
operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
78 return lhs =
static_cast<FlagEnumClass
>(
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
79 |
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(rhs));
82template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
83constexpr FlagEnumClass &
operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
85 return lhs =
static_cast<FlagEnumClass
>(
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(lhs)
86 & (~static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs)));
94template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
95constexpr FlagEnumClass &
modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags,
bool value)
97 return value ? (flagVariable += relevantFlags) : (flagVariable -= relevantFlags);
103template <
typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * =
nullptr>
104constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
106 return (
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(flagVariable)
107 &
static_cast<typename std::underlying_type<FlagEnumClass>::type
>(flagsToCheck))
108 ==
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)
constexpr bool 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.