-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathgen_variant.h
142 lines (139 loc) · 3.37 KB
/
gen_variant.h
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
struct VARIANT_NAME {
VARIANT_NAME() : index(0), elem0() {}
int index;
template <typename T>
optional<T> getValueMaybe() const {
return none;
}
template <typename T>
optional<const T&> getReferenceMaybe() const {
return none;
}
template <typename T>
optional<T&> getReferenceMaybe() {
return none;
}
template <typename T>
bool contains() const {
return !!getReferenceMaybe<T>();
}
#define X(Type, Index) \
VARIANT_NAME(Type&& t) noexcept : index(Index), elem##Index(std::move(t)) {}\
VARIANT_NAME(const Type& t) noexcept : index(Index), elem##Index(t) {}
VARIANT_TYPES_LIST
#undef X
template<typename RetType = void, typename... Fs>
RetType visit(Fs... fs) const {
auto f = variant_helpers::LambdaVisitor<Fs...>(fs...);
switch (index) {
#define X(Type, Index)\
case Index: return f(elem##Index); break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
}
template<typename RetType = void, typename... Fs>
RetType visit(Fs... fs) {
auto f = variant_helpers::LambdaVisitor<Fs...>(fs...);
switch (index) {
#define X(Type, Index)\
case Index: return f(elem##Index); break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
}
VARIANT_NAME(const VARIANT_NAME& t) noexcept : index(t.index) {
switch (index) {
#define X(Type, Index)\
case Index: new(&elem##Index) Type(t.elem##Index); break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
}
VARIANT_NAME(VARIANT_NAME&& t) noexcept : index(t.index) {
switch (index) {
#define X(Type, Index)\
case Index: new(&elem##Index) Type(std::move(t.elem##Index)); break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
}
VARIANT_NAME& operator = (const VARIANT_NAME& t) noexcept {
if (index == t.index)
switch (index) {
#define X(Type, Index)\
case Index: elem##Index = t.elem##Index; break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
else {
this->~VARIANT_NAME();
new (this) VARIANT_NAME(t);
}
return *this;
}
VARIANT_NAME& operator = (VARIANT_NAME&& t) noexcept {
if (index == t.index)
switch (index) {
#define X(Type, Index)\
case Index: elem##Index = std::move(t.elem##Index); break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
else {
this->~VARIANT_NAME();
new (this) VARIANT_NAME(std::move(t));
}
return *this;
}
~VARIANT_NAME() {
switch (index) {
#define X(Type, Index)\
case Index: elem##Index.~Type(); break;
VARIANT_TYPES_LIST
#undef X
default: fail();
}
}
union {
#define X(Type, Index) \
Type elem##Index;
VARIANT_TYPES_LIST
#undef X
};
#define X(Type, Index) \
static const auto Type##Tag = Index;
VARIANT_TYPES_LIST
#undef X
#define X(Type, Index) \
using Type = Type;
VARIANT_TYPES_LIST
#undef X
};
#define X(Type, Index) \
template<>\
inline optional<Type> VARIANT_NAME::getValueMaybe<Type>() const {\
if (index == Index)\
return elem##Index;\
return none;\
}\
template<>\
inline optional<const Type&> VARIANT_NAME::getReferenceMaybe() const {\
if (index == Index)\
return elem##Index;\
return none;\
}\
template<>\
inline optional<Type&> VARIANT_NAME::getReferenceMaybe() {\
if (index == Index)\
return elem##Index;\
return none;\
}
VARIANT_TYPES_LIST
#undef X