Skip to content
Navigation Menu
{{ message }}
forked from insooth/libcppa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional_variant.hpp
More file actions
457 lines (389 loc) · 14.7 KB
/
Copy pathoptional_variant.hpp
File metadata and controls
457 lines (389 loc) · 14.7 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef OPTIONAL_VARIANT_HPP
#define OPTIONAL_VARIANT_HPP
#include <ostream>
#include <stdexcept>
#include <type_traits>
#include "cppa/none.hpp"
#include "cppa/unit.hpp"
#include "cppa/optional.hpp"
#include "cppa/skip_message.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/type_traits.hpp"
#include "cppa/detail/optional_variant_data.hpp"
#define CPPA_OPTIONAL_VARIANT_CASE(x) \
case x: return do_visit(from, visitor, \
make_bool_token<void_pos == x >(), \
make_int_token< x >()) \
namespace cppa {
template<int Value>
constexpr std::integral_constant<int, Value> make_int_token() { return {}; }
template<bool Value>
constexpr std::integral_constant<bool, Value> make_bool_token() { return {}; }
template<typename T>
struct optional_variant_copy_helper {
T& lhs;
optional_variant_copy_helper(T& lhs_ref) : lhs(lhs_ref) { }
template<typename U>
inline void operator()(const U& rhs) const {
lhs = rhs;
}
inline void operator()() const {
lhs = unit;
}
inline void operator()(const none_t&) const {
lhs = none;
}
};
template<typename T>
struct optional_variant_move_helper {
T& lhs;
optional_variant_move_helper(T& lhs_ref) : lhs(lhs_ref) { }
template<typename U>
inline void operator()(U& rhs) const {
lhs = std::move(rhs);
}
inline void operator()() const {
lhs = unit;
}
inline void operator()(const none_t&) const {
lhs = none;
}
};
template<typename... Ts>
class optional_variant;
template<typename T>
struct is_optional_variant {
static constexpr bool value = false;
};
template<typename... Ts>
struct is_optional_variant<optional_variant<Ts...>> {
static constexpr bool value = true;
};
/**
* @brief A optional_variant is either invalid or holds
a value of one of the types <tt>Ts</tt>.
*/
template<typename... Ts>
class optional_variant {
public:
typedef util::type_list<Ts...> types;
static constexpr int void_pos = util::tl_find<types, void>::value;
/**
* @brief Checks whether this objects holds a value of type @p T.
*/
template<typename T>
inline bool is() const {
return m_type != -1 && m_type == util::tl_find<types, T>::value;
}
template<typename U>
optional_variant& operator=(U&& arg) {
destroy_data();
set(std::forward<U>(arg));
return *this;
}
optional_variant& operator=(const optional_variant& other) {
destroy_data();
optional_variant_copy_helper<optional_variant> helper{*this};
other.apply(helper);
return *this;
}
optional_variant& operator=(optional_variant&& other) {
destroy_data();
optional_variant_move_helper<optional_variant> helper{*this};
other.apply(helper);
return *this;
}
optional_variant() : m_type(-1) { }
template<typename U>
optional_variant(U&& arg) {
set(std::forward<U>(arg));
}
optional_variant(const optional_variant& other) : m_type(-1) {
optional_variant_copy_helper<optional_variant> helper{*this};
other.apply(helper);
}
optional_variant(optional_variant&& other) : m_type(-1) {
optional_variant_move_helper<optional_variant> helper{*this};
other.apply(helper);
}
~optional_variant() {
destroy_data();
}
/**
* @brief Checks whether this optional_variant is valid.
*/
inline explicit operator bool() const {
return m_type != -1;
}
/**
* @brief Checks whether this optional_variant is invalid.
*/
inline bool operator!() const {
return m_type == -1;
}
/** @cond PRIVATE */
template<int Pos>
inline const typename util::tl_at<types, Pos>::type&
get(std::integral_constant<int, Pos> token,
typename std::enable_if<Pos < sizeof...(Ts) && Pos != void_pos>::type* = nullptr) const {
return m_data.get(token);
}
template<int Pos>
inline void get(std::integral_constant<int, Pos>,
typename std::enable_if<Pos >= sizeof...(Ts) || Pos == void_pos>::type* = nullptr) const { }
template<int Pos>
inline typename util::tl_at<types, Pos>::type&
get(std::integral_constant<int, Pos> token,
typename std::enable_if<Pos < sizeof...(Ts) && Pos != void_pos>::type* = nullptr) {
return m_data.get(token);
}
template<typename Visitor>
auto apply(Visitor& visitor) const -> decltype(visitor(none_t{})) {
return do_apply(*this, visitor);
}
template<typename Visitor>
auto apply(Visitor& visitor) -> decltype(visitor(none_t{})) {
return do_apply(*this, visitor);
}
/** @endcond */
private:
template<typename Self, typename Visitor, int Pos>
static auto do_visit(Self& from,
const Visitor& visitor,
std::integral_constant<bool, false> /* is_not_void */,
std::integral_constant<int, Pos> token,
typename std::enable_if<Pos < sizeof...(Ts)>::type* = nullptr)
-> decltype(visitor(none_t{})) {
return visitor(from.get(token));
}
template<typename Self, typename Visitor, int Pos>
static auto do_visit(Self&,
const Visitor& visitor,
std::integral_constant<bool, true> /* is_void */,
std::integral_constant<int, Pos>,
typename std::enable_if<Pos < sizeof...(Ts)>::type* = nullptr)
-> decltype(visitor(none_t{})) {
return visitor();
}
template<typename Self, typename Visitor, int Pos>
static auto do_visit(Self&,
const Visitor& visitor,
std::integral_constant<bool, false>,
std::integral_constant<int, Pos>,
typename std::enable_if<Pos >= sizeof...(Ts)>::type* = nullptr)
-> decltype(visitor(none_t{})) {
// this is not the function you are looking for
throw std::runtime_error("invalid type found");
}
template<typename Self, typename Visitor>
static auto do_apply(Self& from, Visitor& visitor) -> decltype(visitor(none_t{})) {
switch (from.m_type) {
default: throw std::runtime_error("invalid type found");
case -1: return visitor(none_t{});
CPPA_OPTIONAL_VARIANT_CASE(0);
CPPA_OPTIONAL_VARIANT_CASE(1);
CPPA_OPTIONAL_VARIANT_CASE(2);
CPPA_OPTIONAL_VARIANT_CASE(3);
CPPA_OPTIONAL_VARIANT_CASE(4);
CPPA_OPTIONAL_VARIANT_CASE(5);
CPPA_OPTIONAL_VARIANT_CASE(6);
CPPA_OPTIONAL_VARIANT_CASE(7);
CPPA_OPTIONAL_VARIANT_CASE(8);
CPPA_OPTIONAL_VARIANT_CASE(9);
}
}
inline void destroy_data() {
detail::optional_variant_data_destructor f;
apply(f);
}
template<typename U>
typename std::enable_if<
!std::is_same<typename util::rm_const_and_ref<U>::type, none_t>::value
&& !is_optional_variant<typename util::rm_const_and_ref<U>::type>::value
&& !is_optional<typename util::rm_const_and_ref<U>::type>::value
>::type
set(U&& arg) {
typedef typename util::rm_const_and_ref<U>::type stripped_type;
typedef typename detail::unlift_void<stripped_type>::type type;
static constexpr int type_id = util::tl_find<types, type>::value;
static_assert(type_id != -1, "invalid type");
m_type = type_id;
auto& ref = m_data.get(make_int_token<type_id>());
new (&ref) stripped_type (std::forward<U>(arg));
}
inline void set(const optional_variant& other) {
optional_variant_copy_helper<optional_variant> helper{*this};
other.apply(helper);
}
inline void set(optional_variant&& other) {
optional_variant_move_helper<optional_variant> helper{*this};
other.apply(helper);
}
template<typename T>
inline void set(const optional<T>& arg) {
if (arg) set(*arg);
else set(none);
}
template<typename T>
inline void set(optional<T>&& arg) {
if (arg) set(std::move(*arg));
else set(none);
}
inline void set(const none_t&) { m_type = -1; }
int m_type;
detail::optional_variant_data<typename detail::lift_void<Ts>::type...> m_data;
};
/**
* @relates optional_variant
*/
template<typename T, typename... Us>
const T& get(const optional_variant<Us...>& value) {
std::integral_constant<int, util::tl_find<util::type_list<Us...>, T>::value> token;
return value.get(token);
}
/**
* @relates optional_variant
*/
template<typename T, typename... Us>
T& get_ref(optional_variant<Us...>& value) {
std::integral_constant<int, util::tl_find<util::type_list<Us...>, T>::value> token;
return value.get(token);
}
/**
* @relates optional_variant
*/
template<typename Visitor, typename... Ts>
auto apply_visitor(Visitor& visitor, const optional_variant<Ts...>& data) -> decltype(visitor(none_t{})) {
return data.apply(visitor);
}
/**
* @relates optional_variant
*/
template<typename Visitor, typename... Ts>
auto apply_visitor(const Visitor& visitor, const optional_variant<Ts...>& data) -> decltype(visitor(none_t{})) {
return data.apply(visitor);
}
/**
* @relates optional_variant
*/
template<typename Visitor, typename... Ts>
auto apply_visitor(Visitor& visitor, optional_variant<Ts...>& data) -> decltype(visitor(none_t{})) {
return data.apply(visitor);
}
/**
* @relates optional_variant
*/
template<typename Visitor, typename... Ts>
auto apply_visitor(const Visitor& visitor, optional_variant<Ts...>& data) -> decltype(visitor(none_t{})) {
return data.apply(visitor);
}
template<typename T>
struct optional_variant_from_type_list;
template<typename... Ts>
struct optional_variant_from_type_list<util::type_list<Ts...>> {
typedef optional_variant<Ts...> type;
};
namespace detail {
template<typename T>
struct optional_variant_cmp_helper {
const T& lhs;
optional_variant_cmp_helper(const T& value) : lhs{value} { }
// variant is comparable
template<typename U>
typename std::enable_if<util::is_comparable<T, U>::value, bool>::type
operator()(const U& rhs) const { return lhs == rhs; }
// variant is not comparable
template<typename U>
typename std::enable_if<!util::is_comparable<T, U>::value, bool>::type
operator()(const U&) const { return false; }
// variant is void
bool operator()() const { return false; }
// variant is undefined
bool operator()(const none_t&) const { return false; }
};
} // namespace detail
/**
* @relates optional_variant
*/
template<typename T, typename... Ts>
bool operator==(const T& lhs, const optional_variant<Ts...>& rhs) {
return apply_visitor(detail::optional_variant_cmp_helper<T>{lhs}, rhs);
}
/**
* @relates optional_variant
*/
template<typename T, typename... Ts>
bool operator==(const optional_variant<Ts...>& lhs, const T& rhs) {
return rhs == lhs;
}
namespace detail {
struct optional_variant_ostream_helper {
std::ostream& lhs;
inline optional_variant_ostream_helper(std::ostream& os) : lhs(os) { }
template<typename T>
std::ostream& operator()(const T& value) const {
return lhs << value;
}
inline std::ostream& operator()(const none_t&) const {
return lhs << "<none>";
}
inline std::ostream& operator()() const {
return lhs << "<void>";
}
template<typename T0>
std::ostream& operator()(const cow_tuple<T0>& value) const {
return lhs << get<0>(value);
}
template<typename T0, typename T1, typename... Ts>
std::ostream& operator()(const cow_tuple<T0, T1, Ts...>& value) const {
lhs << get<0>(value) << " ";
return (*this)(value.drop_left());
}
};
} // namespace detail
/**
* @relates optional_variant
*/
template<typename T0, typename... Ts>
std::ostream& operator<<(std::ostream& lhs, const optional_variant<T0, Ts...>& rhs) {
return apply_visitor(detail::optional_variant_ostream_helper{lhs}, rhs);
}
template<typename T, typename... Ts>
optional_variant<T, typename util::rm_const_and_ref<Ts>::type...>
make_optional_variant(T value, Ts&&... args) {
return {std::move(value), std::forward<Ts>(args)...};
}
template<typename... Ts>
inline optional_variant<Ts...> make_optional_variant(optional_variant<Ts...> value) {
return value;
}
} // namespace cppa
#endif // OPTIONAL_VARIANT_HPP
You can’t perform that action at this time.
