TrinityCore
EnumFlag.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef EnumFlag_h__
19#define EnumFlag_h__
20
21#include <type_traits>
22
23template<typename T>
24constexpr bool IsEnumFlag(T) { return false; }
25
26#define DEFINE_ENUM_FLAG(enumType) constexpr bool IsEnumFlag(enumType) { return true; }
27
28namespace EnumTraits
29{
30 template<typename T>
31 using IsFlag = std::conjunction<std::is_enum<T>, std::integral_constant<bool, IsEnumFlag(T{})>>;
32}
33
34template<typename T, std::enable_if_t<EnumTraits::IsFlag<T>::value, std::nullptr_t> = nullptr>
35inline constexpr T operator&(T left, T right)
36{
37 return static_cast<T>(static_cast<std::underlying_type_t<T>>(left) & static_cast<std::underlying_type_t<T>>(right));
38}
39
40template<typename T, std::enable_if_t<EnumTraits::IsFlag<T>::value, std::nullptr_t> = nullptr>
41inline constexpr T& operator&=(T& left, T right)
42{
43 return left = left & right;
44}
45
46template<typename T, std::enable_if_t<EnumTraits::IsFlag<T>::value, std::nullptr_t> = nullptr>
47inline constexpr T operator|(T left, T right)
48{
49 return static_cast<T>(static_cast<std::underlying_type_t<T>>(left) | static_cast<std::underlying_type_t<T>>(right));
50}
51
52template<typename T, std::enable_if_t<EnumTraits::IsFlag<T>::value, std::nullptr_t> = nullptr>
53inline constexpr T& operator|=(T& left, T right)
54{
55 return left = left | right;
56}
57
58template<typename T, std::enable_if_t<EnumTraits::IsFlag<T>::value, std::nullptr_t> = nullptr>
59inline constexpr T operator~(T value)
60{
61 return static_cast<T>(~static_cast<std::underlying_type_t<T>>(value));
62}
63
64template<typename T>
66{
67 static_assert(EnumTraits::IsFlag<T>::value, "EnumFlag must be used only with enums that are marked as flags by DEFINE_ENUM_FLAG macro");
68
69public:
70 /*implicit*/ constexpr EnumFlag(T value) : _value(value)
71 {
72 }
73
74 constexpr EnumFlag& operator&=(EnumFlag right)
75 {
76 _value &= right._value;
77 return *this;
78 }
79
80 constexpr friend EnumFlag operator&(EnumFlag left, EnumFlag right)
81 {
82 return left &= right;
83 }
84
85 constexpr EnumFlag& operator|=(EnumFlag right)
86 {
87 _value |= right._value;
88 return *this;
89 }
90
91 constexpr friend EnumFlag operator|(EnumFlag left, EnumFlag right)
92 {
93 return left |= right;
94 }
95
96 constexpr EnumFlag operator~() const
97 {
98 return static_cast<T>(~static_cast<std::underlying_type_t<T>>(_value));
99 }
100
101 constexpr void RemoveFlag(EnumFlag flag)
102 {
103 _value &= ~flag._value;
104 }
105
106 constexpr bool HasFlag(T flag) const
107 {
108 using i = std::underlying_type_t<T>;
109 return static_cast<i>(_value & flag) != static_cast<i>(0);
110 }
111
112 constexpr bool HasAllFlags(T flags) const
113 {
114 return (_value & flags) == flags;
115 }
116
117 constexpr operator T() const
118 {
119 return _value;
120 }
121
122 constexpr std::underlying_type_t<T> AsUnderlyingType() const
123 {
124 return static_cast<std::underlying_type_t<T>>(_value);
125 }
126
127private:
129};
130
131#endif // EnumFlag_h__
uint16 flags
Definition: DisableMgr.cpp:49
constexpr T & operator&=(T &left, T right)
Definition: EnumFlag.h:41
constexpr T operator&(T left, T right)
Definition: EnumFlag.h:35
constexpr T & operator|=(T &left, T right)
Definition: EnumFlag.h:53
constexpr T operator~(T value)
Definition: EnumFlag.h:59
constexpr T operator|(T left, T right)
Definition: EnumFlag.h:47
constexpr bool IsEnumFlag(T)
Definition: EnumFlag.h:24
constexpr void RemoveFlag(EnumFlag flag)
Definition: EnumFlag.h:101
constexpr EnumFlag operator~() const
Definition: EnumFlag.h:96
constexpr bool HasFlag(T flag) const
Definition: EnumFlag.h:106
T _value
Definition: EnumFlag.h:128
constexpr EnumFlag(T value)
Definition: EnumFlag.h:70
constexpr friend EnumFlag operator&(EnumFlag left, EnumFlag right)
Definition: EnumFlag.h:80
constexpr EnumFlag & operator|=(EnumFlag right)
Definition: EnumFlag.h:85
constexpr bool HasAllFlags(T flags) const
Definition: EnumFlag.h:112
constexpr friend EnumFlag operator|(EnumFlag left, EnumFlag right)
Definition: EnumFlag.h:91
constexpr std::underlying_type_t< T > AsUnderlyingType() const
Definition: EnumFlag.h:122
constexpr EnumFlag & operator&=(EnumFlag right)
Definition: EnumFlag.h:74
std::conjunction< std::is_enum< T >, std::integral_constant< bool, IsEnumFlag(T{})> > IsFlag
Definition: EnumFlag.h:31