-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_operators.h
75 lines (64 loc) · 2.27 KB
/
enum_operators.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
#ifndef _BITMASK_H
#define _BITMASK_H
#include<experimental/type_traits>
template<typename _Enum>
constexpr bool enable_bitmask_operators = false;
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum>
operator|(_Enum __a, _Enum __b)
{
using _UnderT = std::underlying_type_t<_Enum>;
return static_cast<_Enum>(static_cast<_UnderT>(__a)
| static_cast<_UnderT>(__b));
}
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum>
operator&(_Enum __a, _Enum __b)
{
using _UnderT = std::underlying_type_t<_Enum>;
return static_cast<_Enum>(static_cast<_UnderT>(__a)
& static_cast<_UnderT>(__b));
}
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum>
operator^(_Enum __a, _Enum __b)
{
using _UnderT = std::underlying_type_t<_Enum>;
return static_cast<_Enum>(static_cast<_UnderT>(__a)
^ static_cast<_UnderT>(__b));
}
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum>
operator~(_Enum __a)
{
using _UnderT = std::underlying_type_t<_Enum>;
return static_cast<_Enum>(~static_cast<_UnderT>(__a));
}
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum&>
operator|=(_Enum& __a, _Enum __b)
{
using _UnderT = std::underlying_type_t<_Enum>;
__a = static_cast<_Enum>(static_cast<_UnderT>(__a)
| static_cast<_UnderT>(__b));
return __a;
}
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum&>
operator&=(_Enum& __a, _Enum __b)
{
using _UnderT = std::underlying_type_t<_Enum>;
__a = static_cast<_Enum>(static_cast<_UnderT>(__a)
& static_cast<_UnderT>(__b));
return __a;
}
template<typename _Enum>
std::enable_if_t<enable_bitmask_operators<_Enum>, _Enum&>
operator^=(_Enum& __a, _Enum __b)
{
using _UnderT = std::underlying_type_t<_Enum>;
__a = static_cast<_Enum>(static_cast<_UnderT>(__a)
^ static_cast<_UnderT>(__b));
return __a;
}
#endif // _BITMASK_H