C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
flagenumclass.h
Go to the documentation of this file.
1#ifndef CPP_UTILITIES_FLAG_ENUM_CLASS_H
2#define CPP_UTILITIES_FLAG_ENUM_CLASS_H
3
4#include "./traits.h"
5
6namespace CppUtilities {
7
11template <typename T> struct IsFlagEnumClass : public Traits::Bool<false> {};
12
13// clang-format off
18#define CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Namespace, EnumClassType) \
19 namespace CppUtilities { \
20 template <> struct IsFlagEnumClass<EnumClassType> : Traits::Bool<true> { \
21 }; \
22 } \
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-=; \
30 }
31// clang-format on
32
36namespace FlagEnumClassOperations {
37
38template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
39constexpr FlagEnumClass operator|(FlagEnumClass lhs, FlagEnumClass rhs)
40{
41 return static_cast<FlagEnumClass>(
42 static_cast<typename std::underlying_type<FlagEnumClass>::type>(lhs) | static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs));
43}
44
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)
48{
49 return static_cast<FlagEnumClass>(
50 static_cast<typename std::underlying_type<FlagEnumClass>::type>(lhs) & static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs));
51}
52#else
53template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
54constexpr bool operator&(FlagEnumClass lhs, FlagEnumClass rhs)
55{
56 return static_cast<typename std::underlying_type<FlagEnumClass>::type>(lhs)
57 & static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs);
58}
59#endif
60
61template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
62constexpr bool operator&&(FlagEnumClass lhs, FlagEnumClass rhs)
63{
64 return static_cast<typename std::underlying_type<FlagEnumClass>::type>(lhs)
65 & static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs);
66}
67
68template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
69constexpr FlagEnumClass &operator|=(FlagEnumClass &lhs, FlagEnumClass rhs)
70{
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));
73}
74
75template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
76constexpr FlagEnumClass &operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
77{
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));
80}
81
82template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
83constexpr FlagEnumClass &operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
84{
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)));
87}
88
89} // namespace FlagEnumClassOperations
90
94template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
95constexpr FlagEnumClass &modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags, bool value)
96{
97 return value ? (flagVariable += relevantFlags) : (flagVariable -= relevantFlags);
98}
99
103template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
104constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
105{
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);
109}
110
111} // namespace CppUtilities
112
113#endif // CPP_UTILITIES_FLAG_ENUM_CLASS_H
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.
Definition traits.h:23