-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathEnumClassHelp.h
170 lines (168 loc) · 5.6 KB
/
EnumClassHelp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
// This macro defines some global operators, and hence must be used at global scope
#define ENUM_CLASS_HELPERS(TEnum, TUnderlying) \
inline TEnum operator +(const TEnum e, const TUnderlying n) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e) + n); \
} \
\
inline TEnum operator +(const TUnderlying n, const TEnum e) \
{ \
return static_cast<TEnum>(n + static_cast<TUnderlying>(e)); \
} \
\
inline TEnum operator +(const TEnum e0, const TEnum e1) \
{ \
return static_cast<TUnderlying>(e0) + e1; \
} \
\
inline TEnum &operator +=(TEnum &e, const TUnderlying n) \
{ \
return e = e + n; \
} \
\
inline TEnum &operator ++(TEnum &e) \
{ \
return e += 1; \
} \
\
inline TEnum operator ++(TEnum &e, const int) \
{ \
const TEnum old = e; \
++e; \
return old; \
} \
\
inline TEnum operator -(const TEnum e, const TUnderlying n) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e) - n); \
} \
\
inline TEnum operator -(const TUnderlying n, const TEnum e) \
{ \
return static_cast<TEnum>(n - static_cast<TUnderlying>(e)); \
} \
\
inline TEnum operator -(const TEnum e0, const TEnum e1) \
{ \
return static_cast<TUnderlying>(e0) - e1; \
} \
\
inline TEnum &operator -=(TEnum &e, const TUnderlying n) \
{ \
return e = e - n; \
} \
\
inline TEnum &operator --(TEnum &e) \
{ \
return e -= 1; \
} \
\
inline TEnum operator --(TEnum &e, const int) \
{ \
const TEnum old = e; \
--e; \
return old; \
} \
\
inline TEnum operator &(const TEnum e0, const TEnum e1) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e0) & static_cast<TUnderlying>(e1)); \
} \
\
inline TEnum &operator &=(TEnum &e0, const TEnum e1) \
{ \
return e0 = e0 & e1; \
} \
\
inline TEnum operator ^(const TEnum e0, const TEnum e1) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e0) ^ static_cast<TUnderlying>(e1)); \
} \
\
inline TEnum &operator ^=(TEnum &e0, const TEnum e1) \
{ \
return e0 = e0 ^ e1; \
} \
\
inline TEnum operator |(const TEnum e0, const TEnum e1) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e0) | static_cast<TUnderlying>(e1)); \
} \
\
inline TEnum &operator |=(TEnum &e0, const TEnum e1) \
{ \
return e0 = e0 | e1; \
} \
\
inline TEnum operator <<(const TEnum e, const TUnderlying n) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e) << n); \
} \
\
inline TEnum &operator <<=(TEnum &e, const TUnderlying n) \
{ \
return e = e << n; \
} \
\
inline TEnum operator >>(const TEnum e, const TUnderlying n) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(e) >> n); \
} \
\
inline TEnum &operator >>=(TEnum &e, const TUnderlying n) \
{ \
return e = e >> n; \
} \
\
inline TEnum operator ~(const TEnum e) \
{ \
return static_cast<TEnum>(~static_cast<TUnderlying>(e)); \
} \
\
inline TEnum operator +(const TEnum e) \
{ \
return e; \
} \
\
inline TEnum operator -(const TEnum e) \
{ \
return static_cast<TEnum>(static_cast<TUnderlying>(0) - static_cast<TUnderlying>(e)); \
} \
\
inline bool operator !(const TEnum e) \
{ \
return !static_cast<TUnderlying>(e); \
} \
// For private enum classes defined inside other classes, this macro can be used inside the class to declare friends
#define ENUM_CLASS_HELPER_FRIENDS(TEnum, TUnderlying) \
friend TEnum operator +(const TEnum e, const TUnderlying n); \
friend TEnum operator +(const TUnderlying n, const TEnum e); \
friend TEnum operator +(const TEnum e0, const TEnum e1); \
friend TEnum &operator +=(TEnum &e, const TUnderlying n); \
friend TEnum &operator ++(TEnum &e); \
friend TEnum operator ++(TEnum &e, const int); \
friend TEnum operator -(const TEnum e, const TUnderlying n); \
friend TEnum operator -(const TUnderlying n, const TEnum e); \
friend TEnum operator -(const TEnum e0, const TEnum e1); \
friend TEnum &operator -=(TEnum &e, const TUnderlying n); \
friend TEnum &operator --(TEnum &e); \
friend TEnum operator --(TEnum &e, const int); \
friend TEnum operator &(const TEnum e0, const TEnum e1); \
friend TEnum &operator &=(TEnum &e0, const TEnum e1); \
friend TEnum operator ^(const TEnum e0, const TEnum e1); \
friend TEnum &operator ^=(TEnum &e0, const TEnum e1); \
friend TEnum operator |(const TEnum e0, const TEnum e1); \
friend TEnum &operator |=(TEnum &e0, const TEnum e1); \
friend TEnum operator <<(const TEnum e, const TUnderlying n); \
friend TEnum &operator <<=(TEnum &e, const TUnderlying n); \
friend TEnum operator >>(const TEnum e, const TUnderlying n); \
friend TEnum &operator >>=(TEnum &e, const TUnderlying n); \
friend TEnum operator ~(const TEnum e); \
friend TEnum operator +(const TEnum e); \
friend TEnum operator -(const TEnum e); \
friend bool operator !(const TEnum e);