C++ Utilities: misc/flagenumclass.h Source File

C++ Utilities 5.35.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
11 template <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
36 namespace FlagEnumClassOperations {
37
38template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
39 constexpr 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>
54 constexpr 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>
62 constexpr 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>
69 constexpr 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>
76 constexpr 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>
83 constexpr 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>
95 constexpr 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>
104 constexpr 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
CppUtilities::FlagEnumClassOperations
The FlagEnumClassOperations namespace contains operations for flag enums.
Definition flagenumclass.h:36
CppUtilities::FlagEnumClassOperations::operator-=
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition flagenumclass.h:83
CppUtilities::FlagEnumClassOperations::operator+=
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition flagenumclass.h:76
CppUtilities::FlagEnumClassOperations::operator&
constexpr bool operator&(FlagEnumClass lhs, FlagEnumClass rhs)
Definition flagenumclass.h:54
CppUtilities::FlagEnumClassOperations::operator|
constexpr FlagEnumClass operator|(FlagEnumClass lhs, FlagEnumClass rhs)
Definition flagenumclass.h:39
CppUtilities::FlagEnumClassOperations::operator|=
constexpr FlagEnumClass & operator|=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition flagenumclass.h:69
CppUtilities::FlagEnumClassOperations::operator&&
constexpr bool operator&&(FlagEnumClass lhs, FlagEnumClass rhs)
Definition flagenumclass.h:62
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::checkFlagEnum
constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
Returns whether the specified flagVariable has set all flags specified via flagsToCheck to true.
Definition flagenumclass.h:104
CppUtilities::modFlagEnum
constexpr FlagEnumClass & modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags, bool value)
Sets the specified relevantFlags in the specified flagVariable to the specified value.
Definition flagenumclass.h:95
CppUtilities::IsFlagEnumClass
The IsFlagEnumClass class is used to decide whether to enable operations for flag enums for.
Definition flagenumclass.h:11
CppUtilities::Traits::Bool
Wraps a static boolean constant.
Definition traits.h:23
traits.h