C++ Utilities 5.26.1
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 }
30// clang-format on
31
35namespace FlagEnumClassOperations {
36
37template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
38constexpr FlagEnumClass operator|(FlagEnumClass lhs, FlagEnumClass rhs)
39{
40 return static_cast<FlagEnumClass>(
41 static_cast<typename std::underlying_type<FlagEnumClass>::type>(lhs) | static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs));
42}
43
44template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
45constexpr bool operator&(FlagEnumClass lhs, FlagEnumClass rhs)
46{
47 return static_cast<typename std::underlying_type<FlagEnumClass>::type>(lhs)
48 & static_cast<typename std::underlying_type<FlagEnumClass>::type>(rhs);
49}
50
51template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
52constexpr FlagEnumClass &operator|=(FlagEnumClass &lhs, FlagEnumClass rhs)
53{
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));
56}
57
58template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
59constexpr FlagEnumClass &operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
60{
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));
63}
64
65template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
66constexpr FlagEnumClass &operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
67{
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)));
70}
71
72} // namespace FlagEnumClassOperations
73
77template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
78constexpr FlagEnumClass &modFlagEnum(FlagEnumClass &flagVariable, FlagEnumClass relevantFlags, bool value)
79{
80 return value ? (flagVariable += relevantFlags) : (flagVariable -= relevantFlags);
81}
82
86template <typename FlagEnumClass, Traits::EnableIf<IsFlagEnumClass<FlagEnumClass>> * = nullptr>
87constexpr bool checkFlagEnum(FlagEnumClass flagVariable, FlagEnumClass flagsToCheck)
88{
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);
92}
93
94} // namespace CppUtilities
95
96#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)
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