-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnumBitmask.hpp
More file actions
74 lines (69 loc) · 2.17 KB
/
EnumBitmask.hpp
File metadata and controls
74 lines (69 loc) · 2.17 KB
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
////////////////////////////////////////////////////////////////
//
// EnumBitmask
// https://github.com/Reputeless/EnumBitmask
// License: CC0 1.0 Universal
//
# pragma once
# include <type_traits>
namespace EnumBitmask
{
template <class Enum>
struct EnumWrapper
{
Enum e;
constexpr explicit operator bool() const noexcept
{
return (e != Enum{ 0 });
}
constexpr operator Enum() const noexcept
{
return e;
}
};
template <class Enum>
EnumWrapper(Enum e)->EnumWrapper<Enum>;
}
# define DEFINE_BITMASK_OPERATORS(ENUM) \
static_assert(std::is_enum_v<ENUM>, "ENUM must be an enum type"); \
[[nodiscard]] \
inline constexpr auto operator &(ENUM lhs, ENUM rhs) noexcept \
{ \
using U = std::underlying_type_t<ENUM>; \
return EnumBitmask::EnumWrapper{ \
ENUM(static_cast<U>(lhs) & static_cast<U>(rhs))}; \
} \
[[nodiscard]] \
inline constexpr auto operator |(ENUM lhs, ENUM rhs) noexcept \
{ \
using U = std::underlying_type_t<ENUM>; \
return EnumBitmask::EnumWrapper{ \
ENUM(static_cast<U>(lhs) | static_cast<U>(rhs))}; \
} \
[[nodiscard]] \
inline constexpr auto operator ^(ENUM lhs, ENUM rhs) noexcept \
{ \
using U = std::underlying_type_t<ENUM>; \
return EnumBitmask::EnumWrapper{ \
ENUM(static_cast<U>(lhs) ^ static_cast<U>(rhs)) }; \
} \
[[nodiscard]] \
inline constexpr ENUM operator ~(ENUM value) noexcept \
{ \
using U = std::underlying_type_t<ENUM>; \
return ENUM(~static_cast<U>(value)); \
} \
inline constexpr ENUM& operator &=(ENUM& lhs, ENUM rhs) noexcept \
{ \
return lhs = (lhs & rhs); \
} \
inline constexpr ENUM& operator |=(ENUM& lhs, ENUM rhs) noexcept \
{ \
return lhs = (lhs | rhs); \
} \
inline constexpr ENUM& operator ^=(ENUM& lhs, ENUM rhs) noexcept \
{ \
return lhs = (lhs ^ rhs); \
}
//
////////////////////////////////////////////////////////////////