You.i Engine
YiEnumBitmaskOperators.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_ENUM_BITMASK_OPERATOR_H_
3 #define _YI_ENUM_BITMASK_OPERATOR_H_
4 
5 #include <type_traits>
6 
25 template<typename E>
27 {
28  static const bool enable = false;
29 };
30 
35 template<typename E>
36 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type
37 operator|(E a, E b)
38 {
39  typedef typename std::underlying_type<E>::type Type;
40  return static_cast<E>(static_cast<Type>(a) | static_cast<Type>(b));
41 }
42 
47 template<typename E>
48 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type
49 operator&(E a, E b)
50 {
51  typedef typename std::underlying_type<E>::type Type;
52  return static_cast<E>(static_cast<Type>(a) & static_cast<Type>(b));
53 }
54 
59 template<typename E>
60 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type
61 operator^(E a, E b)
62 {
63  typedef typename std::underlying_type<E>::type Type;
64  return static_cast<E>(static_cast<Type>(a) ^ static_cast<Type>(b));
65 }
66 
71 template<typename E>
72 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type
74 {
75  typedef typename std::underlying_type<E>::type Type;
76  return static_cast<E>(~static_cast<Type>(a));
77 }
78 
83 template<typename E>
84 inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type &
85 operator|=(E &a, E b)
86 {
87  typedef typename std::underlying_type<E>::type Type;
88  a = static_cast<E>(static_cast<Type>(a) | static_cast<Type>(b));
89  return a;
90 }
91 
96 template<typename E>
97 inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type &
98 operator&=(E &a, E b)
99 {
100  typedef typename std::underlying_type<E>::type Type;
101  a = static_cast<E>(static_cast<Type>(a) & static_cast<Type>(b));
102  return a;
103 }
104 
109 template<typename E>
110 inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, E>::type &
111 operator^=(E &a, E b)
112 {
113  typedef typename std::underlying_type<E>::type Type;
114  a = static_cast<E>(static_cast<Type>(a) ^ static_cast<Type>(b));
115  return a;
116 }
117 
122 template<typename E>
123 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, bool>::type
124 Any(E a)
125 {
126  typedef typename std::underlying_type<E>::type Type;
127  return static_cast<Type>(a) != 0;
128 }
129 
134 template<typename E>
135 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, bool>::type
136 None(E a)
137 {
138  typedef typename std::underlying_type<E>::type Type;
139  return static_cast<Type>(a) == 0;
140 }
141 
146 template<typename E>
147 constexpr inline typename std::enable_if<YiEnableBitmaskOperators<E>::enable, typename std::underlying_type<E>::type>::type
149 {
150  typedef typename std::underlying_type<E>::type Type;
151  return static_cast<Type>(a);
152 }
153 
156 #endif
static const bool enable
Definition: YiEnumBitmaskOperators.h:28
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type operator &(E a, E b)
Definition: YiEnumBitmaskOperators.h:49
A struct used to enable bitmask operators for an enum class type.
Definition: YiEnumBitmaskOperators.h:26
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type operator~(E a)
Definition: YiEnumBitmaskOperators.h:73
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, bool >::type None(E a)
Definition: YiEnumBitmaskOperators.h:136
std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type & operator^=(E &a, E b)
Definition: YiEnumBitmaskOperators.h:111
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type operator|(E a, E b)
Definition: YiEnumBitmaskOperators.h:37
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type operator^(E a, E b)
Definition: YiEnumBitmaskOperators.h:61
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, typename std::underlying_type< E >::type >::type ToInteger(E a)
Definition: YiEnumBitmaskOperators.h:148
constexpr std::enable_if< YiEnableBitmaskOperators< E >::enable, bool >::type Any(E a)
Definition: YiEnumBitmaskOperators.h:124
std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type & operator|=(E &a, E b)
Definition: YiEnumBitmaskOperators.h:85
std::enable_if< YiEnableBitmaskOperators< E >::enable, E >::type & operator &=(E &a, E b)
Definition: YiEnumBitmaskOperators.h:98