Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
Latest commit
216 lines (180 loc) · 6.29 KB
/
Copy pathmain.cpp
File metadata and controls
216 lines (180 loc) · 6.29 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
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>
namespacelegacy_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");
autooperator|(Animals a, Animals b) -> Animals { returnAnimals(int(a) | b); }
autooperator&(Animals a, Animals b) -> Animals { returnAnimals(int(a) & b); }
static_assert(std::is_same_v<decltype(std::declval<Animals>() | std::declval<Animals>()), Animals>,
"operator keeps types");
autooperator<<(std::ostream &out, Animals a) -> std::ostream & {
auto p = [&, first = true ](constchar *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;
}
voidusage() {
constexprauto 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"
namespaceclassic_enum_class_approach {
enumclassAnimal {
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)
autooperator<<(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";
}
}
voidusage() {
constexprauto 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"
namespacebit_number_approach {
enumclassAnimal { 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)
autooperator<<(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";
}
}
voidusage() {
constexprauto 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"
namespacetag_type_approach {
structAnimal {
structCat;
structDog;
structWolf;
};
using Animals = tagtype::Flags<Animal::Cat, Animal::Dog, void, Animal::Wolf>;
using AnimalCat = Animal::Cat;
autooperator<<(std::ostream &out, tagtype::Flag<AnimalCat>) -> std::ostream & { return out << "Cat"; }
autooperator<<(std::ostream &out, tagtype::Flag<Animal::Dog>) -> std::ostream & { return out << "Dog"; }
autooperator<<(std::ostream &out, tagtype::Flag<Animal::Wolf>) -> std::ostream & { return out << "Wolf"; }
template<classT>
constexprauto Flag = tagtype::Flag<T>{};
voidusage() {
using Cat = Animal::Cat; // use type aliases for shorter names
constexprauto 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"
namespacetag_value_approach {
enumclassAnimal { Cat, Dog };
enumclassAnimalWolf {};
constexprauto AnimalCat = Animal::Cat;
using Animals = tagvalue::Flags<AnimalCat, Animal::Dog, nullptr, AnimalWolf{}>;
#if true // one stream operator per type
using tagvalue::operator<<;
autooperator<<(std::ostream &out, tagvalue::Typed<Animal> a) -> std::ostream & {
switch (a.value) {
case AnimalCat: return out << "Cat";
case Animal::Dog: return out << "Dog";
}
}
autooperator<<(std::ostream &out, tagvalue::Typed<AnimalWolf>) -> std::ostream & { return out << "Wolf"; }
#else// individual stream operators
} // namespace tag_value_approach
namespacemeta {
autooperator<<(std::ostream &out, tagvalue::Flag<tag_value_approach::AnimalCat>) -> std::ostream & {
return out << "Cat";
}
autooperator<<(std::ostream &out, tagvalue::Flag<tag_value_approach::Animal::Dog>) -> std::ostream & {
return out << "Dog";
}
autooperator<<(std::ostream &out, tagvalue::Flag<tag_value_approach::AnimalWolf{}>) -> std::ostream & {
return out << "Wolf";
}
} // namespace meta
namespacetag_value_approach {
#endif
template<auto V>
constexprauto Flag = tagvalue::Flag<V>{};
voidusage() {
constexprauto Cat = Animal::Cat;
constexprauto 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"
namespacerepeated_bit_approach {
enumclassAnimal { Cat, Dog, Wolf = 3 };
constexprauto AnimalCat = Animal::Cat;
using Animals = repeated::Flags<AnimalCat, Animal::Dog, Animal::Wolf>;
ENABLE_REPEATED_FLAGS_OP(Animals)
autooperator<<(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";
}
}
voidusage() {
constexprauto 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
intmain() {
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();
}