blob: 1fb9661ad601ef2067828ebfb15c43bf9f171465 (
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
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
|
#pragma once
#include <compare>
template <class TSelf>
struct BasicEnum
{
using Self = TSelf;
using ValueType = int;
int value;
BasicEnum()
: value{ 0 } {}
BasicEnum(int value)
: value{ value } {}
/// Comparison between 2 values of enum. Useful for situations like `a == b` where both a and b are TSelf.
friend auto operator<=>(const TSelf& a, const TSelf& b)
{
return a.value <=> b.value;
}
/// Comparison between a enum and a raw value. Useful for situations like `a == TSelf::Option` where a is a enum and TSelf::Option is a raw value.
friend auto operator<=>(const TSelf& self, int value)
{
return self.value <=> value;
}
operator int() const
{
return value;
}
operator bool() const
{
return value;
}
};
#define ENUM(Name) struct Name : public BasicEnum<Name>
#define ENUM_MEMBERS() \
enum Enum : int; \
operator Enum() const \
{ \
return (Enum)value; \
} \
using BasicEnum<Self>::BasicEnum; \
enum Enum : int
template <class TSelf>
struct BasicFlag
{
using Self = TSelf;
using ValueType = int;
int value;
BasicFlag()
: value{ 0 } {}
BasicFlag(int value)
: value{ value } {}
bool IsSet(TSelf mask) const
{
return (value & mask.value) == mask.value;
}
bool IsSetExclusive(TSelf mask) const
{
return value == mask.value;
}
void Set(TSelf mask, bool state)
{
if (state) {
value = (int)(value | mask.value);
} else {
value = (int)(value & ~mask.value);
}
}
/// Comparsion between 2 values of flag. Useful for situations like `a == b` where both a and b are TSelf.
friend bool operator==(const TSelf& a, const TSelf& b)
{
return a.value == b.value;
}
friend auto operator<=>(const TSelf& a, const TSelf& b)
{
return a.value <=> b.value;
}
/// Comparsion between a flag and a raw value. Useful for situations like `a == TSelf::Option` where a is a flag and TSelf::Option is a raw value.
friend bool operator==(const TSelf& self, int value)
{
return self.value == value;
}
friend auto operator<=>(const TSelf& self, int value)
{
return self.value <=> value;
}
friend TSelf operator&(const TSelf& a, const TSelf& b)
{
return TSelf(a.value & b.value);
}
friend TSelf operator|(const TSelf& a, const TSelf& b)
{
return TSelf(a.value | b.value);
}
friend TSelf operator^(const TSelf& a, const TSelf& b)
{
return TSelf(a.value ^ b.value);
}
TSelf operator~() const
{
return TSelf(~value);
}
TSelf& operator&=(int that)
{
value = value & that;
return *this;
}
TSelf& operator|=(int that)
{
value = value | that;
return *this;
}
TSelf& operator^=(int that)
{
value = value ^ that;
return *this;
}
};
|