-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
216 lines (180 loc) · 6.29 KB
/
main.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <cinttypes>
#include <iostream>
namespace legacy_enum_approach {
enum Animals {
ANIMALS_CAT = 1 << 0,
ANIMALS_DOG = 1 << 1,
ANIMALS_WOLF = 1 << 3,
};
static_assert(std::is_same_v<decltype(std::declval<Animals>() | std::declval<Animals>()), int>, //
"default fallback to int");
auto operator|(Animals a, Animals b) -> Animals { return Animals(int(a) | b); }
auto operator&(Animals a, Animals b) -> Animals { return Animals(int(a) & b); }
static_assert(std::is_same_v<decltype(std::declval<Animals>() | std::declval<Animals>()), Animals>,
"operator keeps types");
auto operator<<(std::ostream &out, Animals a) -> std::ostream & {
auto p = [&, first = true ](const char *t) mutable {
if (first)
first = false;
else
out << " | ";
out << t;
};
if (a & ANIMALS_CAT) p("Cat");
if (a & ANIMALS_DOG) p("Dog");
if (a & ANIMALS_WOLF) p("Wolf");
return out;
}
void usage() {
constexpr auto Cat = ANIMALS_CAT;
auto a = Cat | ANIMALS_DOG;
std::cout << Cat << '\n';
std::cout << a << '\n';
std::cout << ((a & Cat) | ANIMALS_WOLF) << '\n';
}
} // namespace legacy_enum_approach
#include "classic/Flags.h"
namespace classic_enum_class_approach {
enum class Animal {
Cat = 1 << 0,
Dog = 1 << 1,
Wolf = 1 << 3,
};
using Animals = classic::Flags<Animal>;
// hint: use this outside of namespace or it wont be usable there!
ENABLE_CLASSIC_FLAGS_OP(Animals)
auto operator<<(std::ostream &out, Animal a) -> std::ostream & {
switch (a) {
case Animal::Cat: return out << "Cat";
case Animal::Dog: return out << "Dog";
case Animal::Wolf: return out << "Wolf";
}
}
void usage() {
constexpr auto Cat = Animal::Cat;
auto a = Cat | Animal::Dog;
std::cout << Cat << '\n';
std::cout << a << '\n';
std::cout << ((a & Cat) | Animal::Wolf) << '\n';
}
} // namespace classic_enum_class_approach
#include "bitnumber/Flags.h"
namespace bit_number_approach {
enum class Animal { Cat, Dog, Wolf = 3 };
using Animals = bitnumber::Flags<Animal>;
// hint: use this outside of namespace or it wont be usable there!
ENABLE_BITNUMBER_FLAGS_OP(Animals)
auto operator<<(std::ostream &out, Animal a) -> std::ostream & {
switch (a) {
case Animal::Cat: return out << "Cat";
case Animal::Dog: return out << "Dog";
case Animal::Wolf: return out << "Wolf";
}
}
void usage() {
constexpr auto Cat = Animal::Cat;
auto a = Cat | Animal::Dog;
std::cout << Cat << '\n';
std::cout << a << '\n';
std::cout << ((a & Cat) | Animal::Wolf) << '\n';
}
} // namespace bit_number_approach
#include "tagtype/Flags.h"
namespace tag_type_approach {
struct Animal {
struct Cat;
struct Dog;
struct Wolf;
};
using Animals = tagtype::Flags<Animal::Cat, Animal::Dog, void, Animal::Wolf>;
using AnimalCat = Animal::Cat;
auto operator<<(std::ostream &out, tagtype::Flag<AnimalCat>) -> std::ostream & { return out << "Cat"; }
auto operator<<(std::ostream &out, tagtype::Flag<Animal::Dog>) -> std::ostream & { return out << "Dog"; }
auto operator<<(std::ostream &out, tagtype::Flag<Animal::Wolf>) -> std::ostream & { return out << "Wolf"; }
template<class T>
constexpr auto Flag = tagtype::Flag<T>{};
void usage() {
using Cat = Animal::Cat; // use type aliases for shorter names
constexpr auto DogF = Flag<Animal::Dog>; // flag types works only if you use them one by one
auto a = Animals(Flag<Cat>, DogF);
std::cout << Flag<Cat> << '\n';
std::cout << a << '\n';
std::cout << (a & Flag<Cat> | Flag<Animal::Wolf>) << '\n';
}
} // namespace tag_type_approach
#include "tagvalue/Flags.h"
namespace tag_value_approach {
enum class Animal { Cat, Dog };
enum class AnimalWolf {};
constexpr auto AnimalCat = Animal::Cat;
using Animals = tagvalue::Flags<AnimalCat, Animal::Dog, nullptr, AnimalWolf{}>;
#if true // one stream operator per type
using tagvalue::operator<<;
auto operator<<(std::ostream &out, tagvalue::Typed<Animal> a) -> std::ostream & {
switch (a.value) {
case AnimalCat: return out << "Cat";
case Animal::Dog: return out << "Dog";
}
}
auto operator<<(std::ostream &out, tagvalue::Typed<AnimalWolf>) -> std::ostream & { return out << "Wolf"; }
#else // individual stream operators
} // namespace tag_value_approach
namespace meta {
auto operator<<(std::ostream &out, tagvalue::Flag<tag_value_approach::AnimalCat>) -> std::ostream & {
return out << "Cat";
}
auto operator<<(std::ostream &out, tagvalue::Flag<tag_value_approach::Animal::Dog>) -> std::ostream & {
return out << "Dog";
}
auto operator<<(std::ostream &out, tagvalue::Flag<tag_value_approach::AnimalWolf{}>) -> std::ostream & {
return out << "Wolf";
}
} // namespace meta
namespace tag_value_approach {
#endif
template<auto V>
constexpr auto Flag = tagvalue::Flag<V>{};
void usage() {
constexpr auto Cat = Animal::Cat;
constexpr auto DogF = Flag<Animal::Dog>;
auto a = Animals(Flag<Cat>, DogF);
std::cout << Flag<Cat> << '\n';
std::cout << a << '\n';
std::cout << (a & Flag<Cat> | Flag<AnimalWolf{}>) << '\n';
}
} // namespace tag_value_approach
#include "repeated/Flags.h"
namespace repeated_bit_approach {
enum class Animal { Cat, Dog, Wolf = 3 };
constexpr auto AnimalCat = Animal::Cat;
using Animals = repeated::Flags<AnimalCat, Animal::Dog, Animal::Wolf>;
ENABLE_REPEATED_FLAGS_OP(Animals)
auto operator<<(std::ostream &out, Animal a) -> std::ostream & {
switch (a) {
case AnimalCat: return out << "Cat";
case Animal::Dog: return out << "Dog";
case Animal::Wolf: return out << "Wolf";
}
}
void usage() {
constexpr auto Cat = Animal::Cat;
auto a = Cat | Animal::Dog;
std::cout << Cat << '\n';
std::cout << a << '\n';
std::cout << ((a & Cat) | Animal::Wolf) << '\n';
}
} // namespace repeated_bit_approach
int main() {
std::cout << "\n-- legacy enum approach --\n";
legacy_enum_approach::usage();
std::cout << "\n-- classic enum class approach --\n";
classic_enum_class_approach::usage();
std::cout << "\n-- bit number approach --\n";
bit_number_approach::usage();
std::cout << "\n-- tag type approach --\n";
tag_type_approach::usage();
std::cout << "\n-- tag value approach --\n";
tag_value_approach::usage();
std::cout << "\n-- repeated bit approach --\n";
repeated_bit_approach::usage();
}