-
Notifications
You must be signed in to change notification settings - Fork 39
/
te.hpp
600 lines (536 loc) · 16.3 KB
/
te.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
//
// Copyright (c) 2018-2019 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#if !defined(__cpp_variadic_templates) || \
!defined(__cpp_rvalue_references) || !defined(__cpp_decltype) || \
!defined(__cpp_alias_templates) || \
!defined(__cpp_generic_lambdas) || !defined(__cpp_constexpr) || \
!defined(__cpp_return_type_deduction) || \
!defined(__cpp_fold_expressions) || !defined(__cpp_static_assert) || \
!defined(__cpp_delegating_constructors)
#error "[Boost].TE requires C++17 support"
#else
#pragma GCC system_header
#include <type_traits>
#include <utility>
#include <stdexcept>
#include <memory>
namespace boost {
inline namespace ext {
namespace te {
inline namespace v1 {
namespace detail {
template <class...>
struct type_list {};
template <class, std::size_t>
struct mappings final {
friend auto get(mappings);
template <class T>
struct set {
friend auto get(mappings) { return T{}; }
};
};
template <std::size_t, class...>
constexpr std::size_t mappings_size_impl(...) {
return {};
}
template <std::size_t N, class T, class... Ts>
constexpr auto mappings_size_impl(bool dummy)
-> decltype(get(mappings<T, N>{}), std::size_t{}) {
return 1 + mappings_size_impl<N + 1, T, Ts...>(dummy);
}
template <class... Ts>
constexpr auto mappings_size() {
return mappings_size_impl<1, Ts...>(bool{});
}
template <class T, class = decltype(sizeof(T))>
std::true_type is_complete_impl(bool);
template <class>
std::false_type is_complete_impl(...);
template <class T>
struct is_complete : decltype(is_complete_impl<T>(bool{})) {};
template <class T>
constexpr auto requires__(bool)
-> decltype(std::declval<T>().template requires__<T>());
template <class>
constexpr auto requires__(...) -> void;
template <class TExpr>
class expr_wrapper final {
static_assert(std::is_empty<TExpr>{});
public:
template <class... Ts>
decltype(auto) operator()(Ts &&... args) const {
return reinterpret_cast<const TExpr &>(*this)(std::forward<Ts>(args)...);
}
};
/*! Same as std::exchange() but is guaranteed constexpr !*/
template<class T, class U>
constexpr
inline T exchange(T& obj, U&& new_value)
noexcept(
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_assignable<T&, U>::value
)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
} // namespace detail
struct non_owning_storage
{
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_,non_owning_storage>, bool> = true
>
constexpr explicit non_owning_storage(T &&t) noexcept
: ptr{&t}
{
}
void* ptr = nullptr;
};
struct shared_storage
{
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_,shared_storage>, bool> = true
>
constexpr explicit shared_storage(T &&t) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: ptr{std::make_shared<T_>(std::forward<T>(t))}
{
}
std::shared_ptr<void> ptr;
};
struct dynamic_storage
{
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_,dynamic_storage>, bool> = true
>
constexpr explicit dynamic_storage(T &&t) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: ptr{new T_{std::forward<T>(t)}},
del{[](void *self) {
delete reinterpret_cast<T_ *>(self);
}},
copy{[](const void *other) -> void * {
if constexpr(std::is_copy_constructible_v<T_>)
return new T_{*reinterpret_cast<const T_*>(other)};
else
throw std::runtime_error("dynamic_storage : erased type is not copy constructible");
}}
{
}
constexpr dynamic_storage(const dynamic_storage& other)
: ptr{other.ptr ? other.copy(other.ptr) : nullptr},
del{other.del},
copy{other.copy}
{
}
constexpr dynamic_storage& operator=(const dynamic_storage& other)
{
if (other.ptr != ptr) {
reset();
ptr = other.ptr ? other.copy(other.ptr) : nullptr;
del = other.del;
copy = other.copy;
}
return *this;
}
constexpr dynamic_storage(dynamic_storage&& other) noexcept
: ptr{detail::exchange(other.ptr, nullptr)},
del{detail::exchange(other.del, nullptr)},
copy{detail::exchange(other.copy, nullptr)}
{
}
constexpr dynamic_storage& operator=(dynamic_storage&& other) noexcept
{
if (other.ptr != ptr) {
reset();
ptr = detail::exchange(other.ptr, nullptr);
del = detail::exchange(other.del, nullptr);
copy = detail::exchange(other.copy, nullptr);
}
return *this;
}
~dynamic_storage()
{
reset();
}
constexpr void reset() noexcept
{
if (ptr)
del(ptr);
ptr = nullptr;
}
void* ptr = nullptr;
void (*del)(void*) = nullptr;
void* (*copy)(const void*) = nullptr;
};
template <std::size_t Size, std::size_t Alignment = 8>
struct local_storage
{
using mem_t = std::aligned_storage_t<Size, Alignment>;
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_,local_storage>, bool> = true
>
constexpr explicit local_storage(T &&t) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: ptr{new (&data) T_{std::forward<T>(t)}},
del{[](mem_t& self) {
reinterpret_cast<T_ *>(&self)->~T_();
}},
copy{[](mem_t& self, const void* other) -> void* {
if constexpr(std::is_copy_constructible_v<T_>)
return new (&self) T_{*reinterpret_cast<const T_ *>(other)};
else
throw std::runtime_error("local_storage : erased type is not copy constructible");
}},
move{[](mem_t& self, void* other) -> void* {
if constexpr(std::is_move_constructible_v<T_>)
return new (&self) T_{std::move(*reinterpret_cast<T_ *>(other))};
else
throw std::runtime_error("local_storage : erased type is not move constructible");
}}
{
static_assert(sizeof(T_) <= Size, "insufficient size");
static_assert(Alignment % alignof(T_) == 0, "bad alignment");
}
constexpr local_storage(const local_storage& other)
: ptr{other.ptr ? other.copy(data, other.ptr) : nullptr},
del{other.del},
copy{other.copy},
move{other.move}
{
}
constexpr local_storage& operator=(const local_storage& other)
{
if (other.ptr != ptr) {
reset();
ptr = other.ptr ? other.copy(data, other.ptr) : nullptr;
del = other.del;
copy = other.copy;
move = other.move;
}
return *this;
}
constexpr local_storage(local_storage&& other)
: ptr{other.ptr ? other.move(data, other.ptr) : nullptr},
del{other.del},
copy{other.copy},
move{other.move}
{
}
constexpr local_storage& operator=(local_storage&& other)
{
if (other.ptr != ptr) {
reset();
ptr = other.ptr ? other.move(data, other.ptr) : nullptr;
del = other.del;
copy = other.copy;
move = other.move;
}
return *this;
}
~local_storage()
{
reset();
}
constexpr void reset() noexcept
{
if (ptr)
del(data);
ptr = nullptr;
}
mem_t data;
void* ptr = nullptr;
void (*del)(mem_t&) = nullptr;
void* (*copy)(mem_t& mem, const void*) = nullptr;
void* (*move)(mem_t& mem, void*) = nullptr;
};
template <std::size_t Size, std::size_t Alignment = 8>
struct sbo_storage
{
template<typename T_>
struct type_fits : std::integral_constant<bool, sizeof(T_) <= Size && Alignment % alignof(T_) == 0>{};
using mem_t = std::aligned_storage_t<Size, Alignment>;
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_,sbo_storage>, bool> = true,
std::enable_if_t<type_fits<T_>::value, bool> = true
>
constexpr explicit sbo_storage(T &&t) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: ptr{new (&data) T_{std::forward<T>(t)}},
del{[](mem_t& self_mem, void*) {
reinterpret_cast<T_ *>(&self_mem)->~T_();
}},
copy{[](mem_t& self_mem, const void* other) -> void* {
if constexpr(std::is_copy_constructible_v<T_>)
return new (&self_mem) T_{*reinterpret_cast<const T_ *>(other)};
else
throw std::runtime_error("sbo_storage : erased type is not copy constructible");
}},
move{[](mem_t& self_mem, void*& other) -> void* {
if constexpr(std::is_move_constructible_v<T_>)
return new (&self_mem) T_{std::move(*reinterpret_cast<T_ *>(other))};
else
throw std::runtime_error("sbo_storage : erased type is not move constructible");
}}
{
}
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_,sbo_storage>, bool> = true,
std::enable_if_t<!type_fits<T_>::value, bool> = true
>
constexpr explicit sbo_storage(T &&t) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: ptr{new T_{std::forward<T>(t)}},
del{[](mem_t&, void* self_ptr) {
delete reinterpret_cast<T_ *>(self_ptr);
}},
copy{[](mem_t&, const void* other) -> void* {
if constexpr(std::is_copy_constructible_v<T_>)
return new T_{*reinterpret_cast<const T_*>(other)};
else
throw std::runtime_error("dynamic_storage : erased type is not copy constructible");
}},
move{[](mem_t&, void*& other) {
return detail::exchange(other, nullptr);
}}
{
}
constexpr sbo_storage(const sbo_storage& other)
: ptr{other.ptr ? other.copy(data, other.ptr) : nullptr},
del{other.del},
copy{other.copy},
move{other.move}
{
}
constexpr sbo_storage& operator=(const sbo_storage& other)
{
if (other.ptr != ptr) {
reset();
ptr = other.ptr ? other.copy(data, other.ptr) : nullptr;
del = other.del;
copy = other.copy;
move = other.move;
}
return *this;
}
constexpr sbo_storage(sbo_storage&& other)
: ptr{other.ptr ? other.move(data, other.ptr) : nullptr},
del{other.del},
copy{other.copy},
move{other.move}
{
}
constexpr sbo_storage& operator=(sbo_storage&& other)
{
if (other.ptr != ptr) {
reset();
ptr = other.ptr ? other.move(data, other.ptr) : nullptr;
del = other.del;
copy = other.copy;
move = other.move;
}
return *this;
}
~sbo_storage()
{
reset();
}
constexpr void reset() noexcept
{
if (ptr)
del(data, ptr);
ptr = nullptr;
}
mem_t data;
void* ptr = nullptr;
void (*del)(mem_t&, void*) = nullptr;
void* (*copy)(mem_t&, const void*) = nullptr;
void* (*move)(mem_t&, void*&) = nullptr;
};
class static_vtable {
using ptr_t = void *;
public:
template <class T, std::size_t Size>
explicit static_vtable(T &&, ptr_t *&vtable,
std::integral_constant<std::size_t, Size>) noexcept {
static ptr_t vt[Size]{};
vtable = vt;
}
};
namespace detail {
struct poly_base {
void** vptr = nullptr;
virtual void* ptr() const noexcept = 0;
};
} // namespace detail
template <
class I,
class TStorage = dynamic_storage,
class TVtable = static_vtable
>
class poly : detail::poly_base,
public std::conditional_t<detail::is_complete<I>{}, I,
detail::type_list<I> > {
public:
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same_v<T_, poly>, bool> = true
>
constexpr poly(T &&t) // cppcheck-suppress noExplicitConstructor
noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: poly{std::forward<T>(t),
detail::type_list<decltype(detail::requires__<I>(bool{}))>{}} {}
constexpr poly(poly const &)
noexcept(std::is_nothrow_copy_constructible_v<TStorage>) = default;
constexpr poly &operator=(poly const &)
noexcept(std::is_nothrow_copy_constructible_v<TStorage>) = default;
constexpr poly(poly &&)
noexcept(std::is_nothrow_move_constructible_v<TStorage>) = default;
constexpr poly &operator=(poly &&)
noexcept(std::is_nothrow_move_constructible_v<TStorage>) = default;
private:
template <
class T,
class T_ = std::decay_t<T>,
class TRequires
>
constexpr explicit poly(T &&t, const TRequires) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: poly{std::forward<T>(t),
std::make_index_sequence<detail::mappings_size<I>()>{}} {}
template <
class T,
class T_ = std::decay_t<T>,
std::size_t... Ns
>
constexpr explicit poly(T &&t, std::index_sequence<Ns...>) noexcept(std::is_nothrow_constructible_v<T_,T&&>)
: detail::poly_base{},
vtable{std::forward<T>(t), vptr,
std::integral_constant<std::size_t, sizeof...(Ns)>{}},
storage{std::forward<T>(t)} {
static_assert(sizeof...(Ns) > 0);
static_assert(std::is_destructible_v<T_>, "type must be desctructible");
static_assert(std::is_copy_constructible_v<T_> ||
std::is_move_constructible_v<T_>,
"type must be either copyable or moveable");
(init<Ns + 1, std::decay_t<T> >(
decltype(get(detail::mappings<I, Ns + 1>{})){}),
...);
}
template <std::size_t N, class T, class TExpr, class... TArgs>
constexpr void init(detail::type_list<TExpr, TArgs...>) noexcept {
vptr[N - 1] = reinterpret_cast<void *>(+[](void *self, TArgs... args) {
return detail::expr_wrapper<TExpr>{}(*static_cast<T *>(self), args...);
});
}
void* ptr() const noexcept
{
if constexpr(std::is_same_v<TStorage, shared_storage>)
return storage.ptr.get();
else
return storage.ptr;
}
TStorage storage;
TVtable vtable;
};
namespace detail {
template <
class I,
std::size_t N,
class R,
class TExpr,
class... Ts
>
constexpr auto call_impl(
const poly_base &self,
std::integral_constant<std::size_t, N>,
type_list<R>,
const TExpr,
Ts &&... args
)
{
void(typename mappings<I, N>::template set<type_list<TExpr, Ts...> >{});
return reinterpret_cast<R (*)(void *, Ts...)>(self.vptr[N - 1])(
self.ptr(), std::forward<Ts>(args)...);
}
template <class I, class T, std::size_t... Ns>
constexpr auto extends_impl(std::index_sequence<Ns...>) noexcept {
(void(typename mappings<T, Ns + 1>::template set<decltype(
get(mappings<I, Ns + 1>{}))>{}),
...);
}
template <class T, class TExpr, class... Ts>
constexpr auto requires_impl(type_list<TExpr, Ts...>)
-> decltype(&TExpr::template operator()<T, Ts...>);
template <class I, class T, std::size_t... Ns>
constexpr auto requires_impl(std::index_sequence<Ns...>) -> type_list<
decltype(requires_impl<I>(decltype(get(mappings<T, Ns + 1>{})){}))...>;
} // namespace detail
template <
class R = void,
std::size_t N = 0,
class TExpr,
class I,
class... Ts
>
constexpr auto call(
const TExpr expr,
const I &interface,
Ts &&... args)
{
static_assert(std::is_empty<TExpr>{});
return detail::call_impl<I>(
reinterpret_cast<const detail::poly_base &>(interface),
std::integral_constant<std::size_t, detail::mappings_size<I, class call>() + 1>{},
detail::type_list<R>{},
expr,
std::forward<Ts>(args)...
);
}
template <class I, class T>
constexpr auto extends(const T &) noexcept {
detail::extends_impl<I, T>(
std::make_index_sequence<detail::mappings_size<I, T>()>{});
}
#if defined(__cpp_concepts)
template <class I, class T>
concept var = requires {
detail::requires_impl<I, T>(
std::make_index_sequence<detail::mappings_size<T, I>()>{});
};
template <class I, class T>
concept conceptify = requires {
detail::requires_impl<I, T>(
std::make_index_sequence<detail::mappings_size<T, I>()>{});
};
#endif
} // namespace v1
} // namespace te
} // namespace ext
} // namespace boost
#if !defined(REQUIRES)
#define REQUIRES(R, name, ...) \
R { \
return ::te::call<R>( \
[](auto&& self, auto&&... args) -> decltype( \
self.name(std::forward<decltype(args)>(args)...)) { \
return self.name(std::forward<decltype(args)>(args)...); \
}, \
*this, ##__VA_ARGS__); \
}
#endif
#endif