SimpleWindow
载入中...
搜索中...
未找到
EnumBit.h
1#pragma once
2
3#include <type_traits>
4
5#define _SW_ENUM_ENABLE_BIT_OPERATIONS(T) \
6 template <> \
7 struct _EnumSupportBitOperations<T> : std::true_type { \
8 }
9
10namespace sw
11{
15 template <typename T>
16 struct _EnumSupportBitOperations : std::false_type {
17 };
18
22 template <typename T>
23 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T>::type
24 operator|(T a, T b)
25 {
26 using TUnderlying = typename std::underlying_type<T>::type;
27 return static_cast<T>(static_cast<TUnderlying>(a) | static_cast<TUnderlying>(b));
28 }
29
33 template <typename T>
34 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T &>::type
35 operator|=(T &a, T b)
36 {
37 return a = a | b;
38 }
39
43 template <typename T>
44 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T>::type
45 operator&(T a, T b)
46 {
47 using TUnderlying = typename std::underlying_type<T>::type;
48 return static_cast<T>(static_cast<TUnderlying>(a) & static_cast<TUnderlying>(b));
49 }
50
54 template <typename T>
55 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T &>::type
56 operator&=(T &a, T b)
57 {
58 return a = a & b;
59 }
60
64 template <typename T>
65 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T>::type
66 operator^(T a, T b)
67 {
68 using TUnderlying = typename std::underlying_type<T>::type;
69 return static_cast<T>(static_cast<TUnderlying>(a) ^ static_cast<TUnderlying>(b));
70 }
71
75 template <typename T>
76 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T &>::type
77 operator^=(T &a, T b)
78 {
79 return a = a ^ b;
80 }
81
85 template <typename T>
86 inline constexpr typename std::enable_if<std::is_enum<T>::value && _EnumSupportBitOperations<T>::value, T>::type
87 operator~(T a)
88 {
89 using TUnderlying = typename std::underlying_type<T>::type;
90 return static_cast<T>(~static_cast<TUnderlying>(a));
91 }
92}
用于标记枚举是否支持位运算
Definition EnumBit.h:16