-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtyped_enum3.cxx
68 lines (58 loc) · 1.74 KB
/
typed_enum3.cxx
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
#include <vector>
#include <algorithm>
#include <string>
#include <cstdio>
// A nice order-preserving unique function.
template<typename value_t>
void stable_unique(std::vector<value_t>& vec) {
auto begin = vec.begin();
auto end = begin;
for(value_t& value : vec) {
if(end == std::find(begin, end, value))
*end++ = std::move(value);
}
vec.resize(end - begin);
}
enum typename typelist_t {
int,
double,
char[4],
int,
char[4],
void*,
};
// Create a collection of the unique types. Use an order-preserving unique
// function.
enum typename unique_list_t {
// Convert each type in joined_list_t to an @mtype. @mtype is a builtin
// type that encapsulates a type and has comparison/relational operators
// defined. You can sort or unique with it.
@meta std::vector types {
@dynamic_type(@enum_types(typelist_t)) ...
};
// Create a unique set of @mtypes.
@meta stable_unique(types);
// Convert all the unique types into enumerator declarations.
@pack_type(types)...;
};
// We can also sort the type lexicographically by their string representations.
enum typename sorted_list_t {
// Expand the string spellings of the types into an array, along with the
// index into the type.
@meta std::array types {
std::make_pair<std::string, int>(
@enum_type_strings(typelist_t),
int...
)...
};
// Lexicographically sort the types.
@meta std::sort(types.begin(), types.end());
// Gather the types and define enumerators.
@enum_type(typelist_t, @pack_nontype(types).second) ...;
};
int main() {
printf("unique_list_t:\n");
printf(" %2d: %s\n", int..., @enum_type_strings(unique_list_t))...;
printf("\nsorted_list_t:\n");
printf(" %2d: %s\n", int..., @enum_type_strings(sorted_list_t))...;
}