-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvariant-impl.hpp
714 lines (598 loc) · 18.4 KB
/
variant-impl.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//-----------------------------------------
// Detail implementation of utils::variant
//-----------------------------------------
//
// Copyright kennytm (auraHT Ltd.) 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file doc/LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef VARIANT_IMPL_HPP_90M3SQU09PV
#define VARIANT_IMPL_HPP_90M3SQU09PV 1
#include <cassert>
#include <type_traits>
#include <climits>
#include "traits.hpp"
#if !defined(BOOST_NO_TYPEID)
#include <typeinfo>
#endif
namespace utils { namespace xx_impl {
//{{{ Utilities
static inline constexpr size_t max2(size_t a, size_t b) noexcept
{
return a > b ? a : b;
}
template <typename... F>
struct common_result_type
{
typedef typename std::common_type<typename function_traits<F>::result_type...>::type type;
};
//}}}
//{{{ Variadic unrestricted union (collapsed tuple)
template <typename...>
union union_impl;
template <typename T, typename... Rest>
union union_impl<T, Rest...>
{
T head;
union_impl<Rest...> rest;
typedef T First;
union_impl() noexcept {}
~union_impl() noexcept {}
};
template <typename T>
struct is_empty
{
enum {
value = std::is_same<
union_impl<>,
typename std::remove_cv<typename std::remove_reference<T>::type>::type
>::value
};
};
template <typename SV, typename T>
typename std::enable_if<!is_empty<T>::value, typename SV::result_type>::type
apply(T&& un, size_t index, SV& visitor)
{
if (index == 0)
return visitor(forward_like<T>(un.head));
else
return apply(forward_like<T>(un.rest), index-1, visitor);
}
template <typename SV, typename T>
typename std::enable_if<is_empty<T>::value, typename SV::result_type>::type
apply(T&&, size_t, SV&) { assert(false); }
template <typename SV, typename T1, typename T2>
typename std::enable_if<!is_empty<T1>::value && !is_empty<T2>::value,
typename SV::result_type>::type
apply2(T1&& un1, size_t index1, T2&& un2, size_t index2, SV& visitor)
{
if (index1 != 0)
return apply2(forward_like<T1>(un1.rest), index1-1, std::forward<T2>(un2), index2, visitor);
else if (index2 != 0)
return apply2(std::forward<T1>(un1), index1, forward_like<T2>(un2.rest), index2-1, visitor);
else
return visitor(forward_like<T1>(un1.head), forward_like<T2>(un2.head));
}
template <typename SV, typename T1, typename T2>
typename std::enable_if<is_empty<T1>::value || is_empty<T2>::value,
typename SV::result_type>::type
apply2(T1&&, size_t, T2&&, size_t, SV&) { assert(false); }
template <typename SV, typename T1, typename T2>
typename std::enable_if<!is_empty<T1>::value && !is_empty<T2>::value,
typename SV::result_type>::type
apply(T1&& un1, T2&& un2, size_t index, SV& visitor)
{
if (index == 0)
return visitor(forward_like<T1>(un1.head), forward_like<T2>(un2.head));
else
return apply(forward_like<T1>(un1.rest), forward_like<T2>(un2.rest), index-1, visitor);
}
template <typename SV, typename T1, typename T2>
typename std::enable_if<is_empty<T1>::value || is_empty<T2>::value,
typename SV::result_type>::type
apply(T1&&, T2&&, size_t, SV&) { assert(false); }
template <size_t index>
struct static_applier
{
template <typename V, typename union_type>
typename V::result_type operator()(union_type& un, V& visitor)
{
return static_applier<index-1>()(un.rest, visitor);
}
};
template <>
struct static_applier<0>
{
template <typename V, typename union_type>
typename V::result_type operator()(union_type& un, V& visitor)
{
return visitor(un.head);
}
};
template <>
union union_impl<> {};
//}}}
//{{{ type_traits missing in std
template <typename T, typename U, typename = bool>
struct is_equatable
{
enum { value = false };
};
template <typename T, typename U>
struct is_equatable<T, U, decltype(std::declval<T>() == std::declval<U>())>
{
enum { value = true };
};
template <typename T, typename U, typename = bool>
struct is_less_than_comparable
{
enum { value = false };
};
template <typename T, typename U>
struct is_less_than_comparable<T, U, decltype(std::declval<T>() < std::declval<U>())>
{
enum { value = true };
};
template <typename T, typename U, typename = bool>
struct is_greater_than_comparable
{
enum { value = false };
};
template <typename T, typename U>
struct is_greater_than_comparable<T, U, decltype(std::declval<U>() < std::declval<T>())>
{
enum { value = true };
};
template <typename T, typename U, typename = bool>
struct is_assignable
{
enum { value = false };
};
template <typename T, typename U>
struct is_assignable<T, U, decltype(std::declval<T&>() = std::declval<U>(), true)>
{
enum { value = true };
};
template <typename T, typename U, typename = bool>
struct is_constructible
{
enum { value = std::is_constructible<T, U>::value };
};
template <typename T, typename U, typename = bool>
struct is_same
{
enum { value = std::is_same<T, U>::value };
};
template <typename To, typename From, typename = bool>
struct is_convertible
{
enum { value = std::is_convertible<From, To>::value };
};
template <typename T, typename U, typename = bool>
struct is_nothrow_assignable_helper
{
enum { value = false };
};
template <typename T, typename U>
struct is_nothrow_assignable_helper<T, U, typename std::enable_if<is_assignable<T, U>::value, bool>::type>
{
enum { value = noexcept(std::declval<T&>() = std::declval<U>()) };
};
template <typename T, typename U>
static constexpr bool is_nothrow_assignable() noexcept
{
return is_nothrow_assignable_helper<T, U>::value;
}
template <typename T>
static constexpr bool is_nothrow_move_constructible() noexcept
{
return std::is_nothrow_constructible<typename std::remove_reference<T>::type,
typename std::remove_reference<T>::type&&>::value;
}
template <typename T>
static constexpr bool is_nothrow_copy_constructible() noexcept
{
return std::is_nothrow_constructible<typename std::remove_reference<T>::type,
const T&>::value;
}
template <typename U>
static constexpr bool is_generic_nothrow_assignable() noexcept
{
return
(std::is_lvalue_reference<U>::value &&
xx_impl::is_nothrow_copy_constructible<U>()) ||
(std::is_rvalue_reference<U>::value &&
xx_impl::is_nothrow_move_constructible<U>());
}
//}}}
//{{{ Get index of type matching condition
static inline constexpr bool check_unambiguous(bool me_exact, bool else_exact, bool rest) noexcept
{
return me_exact && else_exact ? false : me_exact != else_exact ? true : rest;
}
template <typename T, typename U>
static inline constexpr bool is_same_upto_cv() noexcept
{
return std::is_same<typename std::remove_cv<
typename std::remove_reference<T
>::type>::type,
typename std::remove_cv<
typename std::remove_reference<U>::type
>::type>::value;
}
template <typename T, typename U>
static inline constexpr bool relaxed_same() noexcept
{
return (std::is_integral<T>::value && std::is_integral<U>::value)
|| (std::is_floating_point<T>::value && std::is_floating_point<U>::value);
}
template <typename, typename>
struct get_index_of_variant;
template <typename, template <typename, typename, typename=bool> class, typename...>
struct get_index;
// Given the type 'From', find the index of (T, Rest...) which are the same
// under the binary predicate 'Checker'.
template <typename From, template <typename, typename, typename=bool> class Checker, typename T, typename... Rest>
struct get_index<From, Checker, T, Rest...>
{
typedef get_index<From, Checker, Rest...> Tail;
static const bool is_exact_match = is_same_upto_cv<T, From>();
static const bool is_relaxed_match = relaxed_same<T, From>();
static const bool is_variant = is_variant<T>::value;
static const bool is_ud_match = Checker<T, From>::value;
static const bool is_strict_ud_match = is_ud_match && !is_variant;
typedef typename get_index_of_variant<From, T>::type variant_indices;
static const int var_quality = is_variant ? variant_indices::quality - 2 : 0;
// Match quality:
// exact > su > rel > strict_ud > ud
static const int local_quality_0 = is_exact_match ? INT_MAX
: is_relaxed_match ? INT_MAX-1
: is_strict_ud_match ? 2
: is_ud_match ? 1
: 0;
static const int local_quality = local_quality_0 < var_quality ? var_quality : local_quality_0;
static const bool is_match_here = local_quality > Tail::quality;
static const int quality = is_match_here ? local_quality : Tail::quality;
static const size_t index = is_match_here ? 0 : 1 + Tail::index;
static const bool ambiguous = local_quality == Tail::quality;
static const bool is_exact = quality == INT_MAX;
static const bool found = quality > 0;
/*
static void debug() noexcept
{
printf("%lu (%s vs %s) :: (lqual) %d (vqual) %d (tqual) %d (qual) %d (index) %lu (ambig) %d\n",
sizeof...(Rest)+1, typeid(From).name(), typeid(T).name(),
local_quality, var_quality, Tail::quality, quality, index, ambiguous);
if (is_variant)
{
printf("<< var <<\n");
variant_indices::debug();
printf(">> var >>\n");
}
Tail::debug();
}
*/
};
template <typename From, template <typename, typename, typename=bool> class Checker>
struct get_index<From, Checker>
{
static const int quality = 0;
static const size_t index = 0;
//static void debug() noexcept {}
};
template <typename From, typename>
struct get_index_of_variant
{
typedef get_index<From, is_same> type;
};
template <typename From, typename... T>
struct get_index_of_variant<From, variant<T...>>
{
typedef get_index<From, is_same, T...> type;
};
//}}}
//{{{ Visitors
template <typename T>
class getter_visitor : public static_visitor<T*>
{
public:
T* operator()(T& obj) const noexcept { return &obj; }
};
class destroy_visitor : public static_visitor<void>
{
public:
template <typename T>
void operator()(T& obj) const noexcept
{
obj.~T();
}
};
template <typename U>
class init_visitor_1 : public static_visitor<void>
{
public:
init_visitor_1(U&& field) noexcept : _field(std::forward<U>(field)) {}
template <typename T>
void operator()(T& obj) const noexcept(std::is_nothrow_constructible<T, U>::value)
{
new(&obj) T(std::forward<U>(_field));
}
private:
U&& _field;
};
class init_visitor_2 : public static_visitor<void>
{
public:
template <typename T, typename U>
void operator()(T& dest, U&& src) const noexcept(std::is_nothrow_constructible<T, U>::value)
{
new(&dest) T(std::forward<U>(src));
}
};
template <typename U>
class assign_visitor_1 : public static_visitor<void>
{
public:
assign_visitor_1(U&& field) : _field(std::forward<U>(field)) {}
template <typename T, typename = typename std::enable_if<is_assignable<T, U>::value>::type>
void operator()(T& obj) const noexcept(is_nothrow_assignable<T, U>())
{
obj = std::forward<U>(_field);
}
template <typename T, typename = typename std::enable_if<!is_assignable<T, U>::value>::type>
void operator()(T&&) const
{
assert(false);
}
private:
U&& _field;
};
template <typename V>
class assign_to_visitor : public static_visitor<void>
{
public:
assign_to_visitor(V& var) : _var(var) {}
template <typename T>
void operator()(T&& obj) const noexcept(is_nothrow_assignable<V, T>())
{
_var = std::forward<T>(obj);
}
private:
V& _var;
};
class is_nothrow_movable_checker : public static_visitor<bool>
{
public:
template <typename T>
bool operator()(const T&) const noexcept
{
return is_nothrow_move_constructible<T>();
}
};
class is_nothrow_copyable_checker : public static_visitor<bool>
{
public:
template <typename T>
bool operator()(const T&) const noexcept
{
return is_nothrow_copy_constructible<T>();
}
};
class is_nothrow_movable_checker_2 : public static_visitor<bool>
{
public:
template <typename T, typename U>
bool operator()(const T&, const U&) const noexcept
{
return std::is_nothrow_constructible<T, typename std::remove_reference<U>::type&&>::value;
}
};
class is_nothrow_copyable_checker_2 : public static_visitor<bool>
{
public:
template <typename T, typename U>
bool operator()(const T&, const U&) const noexcept
{
return std::is_nothrow_constructible<T, const U&>::value;
}
};
class assign_visitor_2 : public static_visitor<void>
{
public:
template <typename T, typename U, typename = typename std::enable_if<is_assignable<T, U>::value>::type>
void operator()(T& dest, U&& src) const noexcept(is_nothrow_assignable<T, U>())
{
dest = std::forward<U>(src);
}
template <typename T, typename U, typename = typename std::enable_if<!is_assignable<T, U>::value>::type>
void operator()(T&&, U&&) const
{
assert(false);
}
};
class swap_visitor_2 : public static_visitor<void>
{
public:
template <typename T, typename U>
void operator()(T& first, U& second) const
{
std::swap(first, second);
}
};
template <typename U>
class equals_visitor_1 : public static_visitor<bool>
{
public:
equals_visitor_1(const U& field) : _field(field) {}
template <typename T>
bool operator()(const T& other) const
{
return other == _field;
}
private:
const U& _field;
};
template <typename U>
class less_than_visitor_1 : public static_visitor<bool>
{
public:
less_than_visitor_1(const U& field) : _field(field) {}
template <typename T>
bool operator()(const T& other) const
{
return other < _field;
}
private:
const U& _field;
};
template <typename U>
class greater_than_visitor_1 : public static_visitor<bool>
{
public:
greater_than_visitor_1(const U& field) : _field(field) {}
template <typename T>
bool operator()(const T& other) const
{
return _field < other;
}
private:
const U& _field;
};
class equals_visitor_2 : public static_visitor<bool>
{
public:
template <typename T, typename U>
bool operator()(const T& first, const U& second) const
{
return first == second;
}
};
class less_than_visitor_2 : public static_visitor<bool>
{
public:
template <typename T, typename U>
bool operator()(const T& first, const U& second) const
{
return first < second;
}
};
class ostream_visitor : public static_visitor<std::ostream&>
{
public:
ostream_visitor(std::ostream& stream) : _stream(stream) {}
template <typename T>
std::ostream& operator()(const T& value)
{
return _stream << value;
}
private:
std::ostream& _stream;
};
class is_assignable_visitor : public static_visitor<bool>
{
public:
template <typename T, typename U>
bool operator()(const T&, const U&) const noexcept
{
return is_assignable<T, U>::value;
}
};
class is_same_visitor : public static_visitor<std::pair<bool, bool>>
{
public:
template <typename T, typename U>
std::pair<bool, bool> operator()(const T&, const U&) const noexcept
{
return std::make_pair(std::is_same<T, U>::value, is_assignable<T, U>::value);
}
};
template <typename... T>
class is_one_of_visitor : public static_visitor<bool>
{
public:
template <typename U>
bool operator()(const U&) const noexcept
{
return get_index<U, is_same, T...>::is_exact;
}
};
#ifndef BOOST_NO_TYPEID
class typeid_visitor : public static_visitor<const std::type_info&>
{
public:
template <typename U>
const std::type_info& operator()(const U& u) const noexcept
{
return typeid(u);
}
};
#endif
//}}}
template <typename SV>
class delayed_visitor
{
public:
explicit delayed_visitor(SV& visitor) : _visitor(visitor) {}
typedef typename SV::result_type result_type;
template <typename... T>
result_type operator()(T&&... val) const
{
return apply_visitor(_visitor, std::forward<T>(val)...);
}
private:
SV& _visitor;
};
//{{{ Functional-style apply
template <size_t index, typename T, typename... Rest>
struct apply_funcs_walker;
template <size_t index, typename T, typename Head, typename... Rest>
struct apply_funcs_walker<index, T, Head, Rest...>
{
auto operator()(T&& value, Head&& head_func, Rest&&... rest_funcs)
-> typename function_traits<apply_funcs_walker<index-1, T, Rest...>>::result_type
{
apply_funcs_walker<index-1, T, Rest...> functor;
return functor(std::forward<T>(value), std::forward<Rest>(rest_funcs)...);
}
};
template <typename T, typename Head, typename... Rest>
struct apply_funcs_walker<0, T, Head, Rest...>
{
auto operator()(T&& value, Head&& head_func, Rest&&...)
-> decltype(head_func(std::forward<T>(value)))
{
return head_func(std::forward<T>(value));
}
};
template <typename T, typename... F>
typename common_result_type<F...>::type
apply_funcs_check(T&& value, F&&... functions)
{
typedef get_index<T, is_convertible,
typename function_traits<F>::template arg<0>::type...> index_tmpl;
static_assert(index_tmpl::found, "Some variants are not handled.");
static_assert(!index_tmpl::ambiguous, "Applying variant to ambiguous list of functions.");
static constexpr size_t index_of_F = index_tmpl::index;
return apply_funcs_walker<index_of_F, T, F...>()(std::forward<T>(value), std::forward<F>(functions)...);
}
template <typename T, typename... F>
typename std::enable_if<!is_empty<T>::value, typename common_result_type<F...>::type>::type
apply_funcs_run(T&& un, size_t index, F&&... functions)
{
if (index != 0)
{
return apply_funcs_run(forward_like<T>(un.rest), index-1,
std::forward<F>(functions)...);
}
else
{
return apply_funcs_check(forward_like<T>(un.head),
std::forward<F>(functions)...);
}
}
template <typename T, typename... F>
typename std::enable_if<is_empty<T>::value, typename common_result_type<F...>::type>::type
apply_funcs_run(T&&, size_t, F&&...) { assert(false); }
//}}}
}}
#endif