blob: ba5b37f2087c58eee11d1e61f036901531675aa1 (
plain)
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
|
#pragma once
#include <functional>
namespace cru {
template <typename Tag, typename TUnderlying = unsigned>
struct Bitmask final {
using Underlying = TUnderlying;
constexpr Bitmask() : value(0) {}
constexpr explicit Bitmask(Underlying value) : value(value) {}
// Start from 1.
static constexpr Bitmask FromOffset(int offset) {
if (offset == 0) return {};
return Bitmask(static_cast<Underlying>(Underlying(1) << (offset - 1)));
}
constexpr bool Has(Bitmask rhs) const { return (value & rhs.value) != 0; }
constexpr Bitmask operator|(Bitmask rhs) const {
return Bitmask(value | rhs.value);
}
constexpr Bitmask operator&(Bitmask rhs) const {
return Bitmask(value & rhs.value);
}
constexpr Bitmask operator^(Bitmask rhs) const {
return Bitmask(value ^ rhs.value);
}
constexpr Bitmask operator~() const { return Bitmask(~value); }
constexpr Bitmask& operator|=(Bitmask rhs) {
value |= rhs.value;
return *this;
}
constexpr Bitmask& operator&=(Bitmask rhs) {
value &= rhs.value;
return *this;
}
constexpr Bitmask& operator^=(Bitmask rhs) {
value ^= rhs.value;
return *this;
}
constexpr bool operator==(Bitmask rhs) const {
return this->value == rhs.value;
}
constexpr explicit operator Underlying() const { return value; }
constexpr explicit operator bool() const { return value != 0; }
Underlying value;
};
} // namespace cru
namespace std {
template <typename Tag, typename TUnderlying>
struct hash<cru::Bitmask<Tag, TUnderlying>> {
constexpr std::size_t operator()(
cru::Bitmask<Tag, TUnderlying> bitmask) const {
return std::hash<TUnderlying>{}(bitmask.value);
}
};
} // namespace std
#define CRU_DEFINE_BITMASK(name) \
namespace details { \
struct name##Tag {}; \
} \
using name = ::cru::Bitmask<details::name##Tag>; \
struct name##s
|