-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant.hpp
239 lines (171 loc) · 5.92 KB
/
variant.hpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef SLAP_VARIANT_HPP
#define SLAP_VARIANT_HPP
#include <type_traits>
#include <utility>
#include <iosfwd>
#include <cassert>
namespace {
template<std::size_t, class ... Args>
struct helper;
template<std::size_t I, class T>
struct helper<I, T> {
static constexpr std::size_t index(const T*) { return I; }
static void construct(void* dest, const T& source) { new (dest) T(source); }
static void construct(void* dest, T&& source) { new (dest) T{std::move(source)}; }
};
template<std::size_t I, class T, class ... Args>
struct helper<I, T, Args...> : helper<I + 1, Args... >, helper<I, T> {
using this_type = helper<I, T>;
using other_types = helper<I + 1, Args... >;
using this_type::index;
using other_types::index;
using this_type::construct;
using other_types::construct;
};
}
template<class ... Args>
class variant {
using storage_type = typename std::aligned_union<0, Args...>::type;
storage_type storage;
using index_type = unsigned char;
index_type index;
using helper_type = helper<0, Args...>;
public:
index_type type() const { return index; }
template<class T, index_type index=helper_type::index((typename std::decay<T>::type*) 0)>
struct index_of {
static constexpr index_type value = index;
};
// unsafe cast
template<class T, index_type index=index_of<T>::value>
const T& cast() const {
assert(index == this->index);
return reinterpret_cast<const T&>(storage);
}
template<class T, index_type index=index_of<T>::value>
T& cast() {
assert(index == this->index);
return reinterpret_cast<T&>(storage);
}
// accessors
template<class T, index_type index=index_of<T>::value>
const T* get() const {
if(this->index == index) { return &cast<T>(); }
return nullptr;
}
template<class T, index_type index=index_of<T>()>
T* get() {
if(this->index == index) { return &cast<T>(); }
return nullptr;
}
// constructors/destructors
template<class T, index_type index=index_of<T>::value>
variant(T&& value) : index(index) {
helper_type::construct(&storage, std::forward<T>(value));
}
variant(variant&& other) : index(other.index) {
match([&](const Args& self) {
new (&storage) Args(std::move(other.cast<Args>()));
}...);
}
variant(const variant& other) : index(other.index) {
match([&](const Args& self) {
new (&storage) Args(other.cast<Args>());
}...);
}
~variant() {
match([](const Args& self) { self.~Args(); }...);
}
template<class T, class Ret, class Self, class Func, class ... Params>
static Ret thunk(Self& self, Func&& func, Params&& ... params) {
return std::forward<Func>(func)(self.template cast<T>(), std::forward<Params>(params)...);
}
// visitors (const)
template<class Func, class ... Params>
inline auto visit(Func&& func, Params&& ... params) const {
using ret_type = typename std::common_type<decltype(func(this->template cast<Args>(),
std::forward<Params>(params)...))...>::type;
using self_type = decltype(*this);
using thunk_type = ret_type (*) (self_type&&, Func&&, Params&&...);
static constexpr thunk_type table[] = { thunk<Args, ret_type, self_type, Func, Params...>... };
return table[index](*this, std::forward<Func>(func), std::forward<Params>(params)...);
}
// visitors (non-const)
template<class Func, class ... Params>
inline auto visit(Func&& func, Params&& ... params) {
using ret_type = typename std::common_type<decltype(func(this->template cast<Args>(),
std::forward<Params>(params)...))...>::type;
using self_type = decltype(*this);
using thunk_type = ret_type (*) (self_type&&, Func&&, Params&&...);
static constexpr thunk_type table[] = { thunk<Args, ret_type, self_type, Func, Params...>... };
return table[index](*this, std::forward<Func>(func), std::forward<Params>(params)...);
}
// pattern matching with lambdas
template<class ... Func>
inline auto match(const Func& ... func) const {
struct overload : Func... {
overload(const Func& ... func) : Func(func)... { }
};
return visit(overload(func...));
}
template<class ... Func>
inline auto match(const Func& ... func) {
struct overload : Func... {
overload(const Func& ... func) : Func(func)... { }
};
return visit(overload(func...));
}
// copy assignment
variant& operator=(const variant& other) {
if(this == &other) return *this;
if(index != other.index) {
// destuct, change type, copy construct
this->~variant();
index = other.index;
match([&](const Args& self) {
new (&storage) Args(other.cast<Args>());
}...);
} else {
// copy assign
match([&](Args& self) {
self = other.cast<Args>();
}...);
}
return *this;
}
// move-assignment
variant& operator=(variant&& other) {
if(this == &other) return *this;
if(index != other.index) {
// destuct, change type, copy construct
this->~variant();
index = other.index;
match([&](const Args& self) {
new (&storage) Args(std::move(other.cast<Args>()));
}...);
} else {
// copy assign
match([&](Args& self) {
self = std::move(other.cast<Args>());
}...);
}
return *this;
}
// convenience
friend std::ostream& operator<<(std::ostream& out, const variant& self) {
self.match([&](const Args& self) {
out << self;
}...);
return out;
}
bool operator==(const variant& other) const {
if(other.index != index) return false;
return match([&](const Args& self) {
return self == other.cast<Args>();
}...);
}
bool operator!=(const variant& other) const {
return !operator==(other);
}
};
#endif