-
Notifications
You must be signed in to change notification settings - Fork 78
/
proxy.h
673 lines (615 loc) · 26 KB
/
proxy.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#ifndef _MSFT_PROXY_
#define _MSFT_PROXY_
#include <bit>
#include <concepts>
#include <initializer_list>
#include <memory>
#include <new>
#include <tuple>
#include <type_traits>
#include <utility>
namespace pro {
enum class constraint_level { none, nontrivial, nothrow, trivial };
struct proxiable_ptr_constraints {
std::size_t max_size;
std::size_t max_align;
constraint_level copyability;
constraint_level relocatability;
constraint_level destructibility;
};
constexpr proxiable_ptr_constraints relocatable_ptr_constraints{
.max_size = sizeof(void*) * 2u,
.max_align = alignof(void*),
.copyability = constraint_level::none,
.relocatability = constraint_level::nothrow,
.destructibility = constraint_level::nothrow,
};
constexpr proxiable_ptr_constraints copyable_ptr_constraints{
.max_size = sizeof(void*) * 2u,
.max_align = alignof(void*),
.copyability = constraint_level::nontrivial,
.relocatability = constraint_level::nothrow,
.destructibility = constraint_level::nothrow,
};
constexpr proxiable_ptr_constraints trivial_ptr_constraints{
.max_size = sizeof(void*),
.max_align = alignof(void*),
.copyability = constraint_level::trivial,
.relocatability = constraint_level::trivial,
.destructibility = constraint_level::trivial,
};
namespace details {
struct applicable_traits { static constexpr bool applicable = true; };
struct inapplicable_traits { static constexpr bool applicable = false; };
template <class T>
consteval bool has_copyability(constraint_level level) {
switch (level) {
case constraint_level::trivial:
return std::is_trivially_copy_constructible_v<T>;
case constraint_level::nothrow:
return std::is_nothrow_copy_constructible_v<T>;
case constraint_level::nontrivial: return std::is_copy_constructible_v<T>;
case constraint_level::none: return true;
default: return false;
}
}
template <class T>
consteval bool has_relocatability(constraint_level level) {
switch (level) {
case constraint_level::trivial:
return std::is_trivially_move_constructible_v<T> &&
std::is_trivially_destructible_v<T>;
case constraint_level::nothrow:
return std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_destructible_v<T>;
case constraint_level::nontrivial:
return std::is_move_constructible_v<T> && std::is_destructible_v<T>;
case constraint_level::none: return true;
default: return false;
}
}
template <class T>
consteval bool has_destructibility(constraint_level level) {
switch (level) {
case constraint_level::trivial: return std::is_trivially_destructible_v<T>;
case constraint_level::nothrow: return std::is_nothrow_destructible_v<T>;
case constraint_level::nontrivial: return std::is_destructible_v<T>;
case constraint_level::none: return true;
default: return false;
}
}
// As per std::to_address() wording in [pointer.conversion]
template <class P>
concept is_address_deducible = std::is_pointer_v<P> ||
requires(const P p) { std::pointer_traits<P>::to_address(p); } ||
requires(const P p) { p.operator->(); };
// Bypass function pointer restriction of std::to_address()
template <class P>
auto deduce_address(const P& p) {
if constexpr (std::is_pointer_v<P>) {
return p;
} else {
return std::to_address(p);
}
}
template <class T, class... Us> struct contains_traits : inapplicable_traits {};
template <class T, class... Us>
struct contains_traits<T, T, Us...> : applicable_traits {};
template <class T, class U, class... Us>
struct contains_traits<T, U, Us...> : contains_traits<T, Us...> {};
template <class... Ts> struct default_traits { using type = void; };
template <class T> struct default_traits<T> { using type = T; };
template <class O> struct overload_traits : inapplicable_traits {};
template <class R, class... Args>
struct overload_traits<R(Args...)> : applicable_traits {
using dispatcher_type = R (*)(const char*, Args...);
template <class T> struct resolver { T operator()(Args...); };
template <class D, class P>
static constexpr bool applicable_ptr = requires(const P& p, Args... args)
{ D{}(*deduce_address(p), std::forward<Args>(args)...); };
template <class D, class P>
static R dispatcher(const char* erased, Args... args) {
const P& p = *reinterpret_cast<const P*>(erased);
if constexpr (std::is_void_v<R>) {
D{}(*deduce_address(p), std::forward<Args>(args)...);
} else {
return D{}(*deduce_address(p), std::forward<Args>(args)...);
}
}
};
template <class Os, class Is> struct dispatch_traits_overload_resolution_impl;
template <class Os, std::size_t... Is>
struct dispatch_traits_overload_resolution_impl<
Os, std::index_sequence<Is...>> {
private:
template <std::size_t I>
using single_resolver = typename overload_traits<std::tuple_element_t<I, Os>>
::template resolver<std::integral_constant<std::size_t, I>>;
struct resolver : single_resolver<Is>...
{ using single_resolver<Is>::operator()...; };
public:
template <class... Args>
static constexpr bool has_overload = std::is_invocable_v<resolver, Args...>;
template <class... Args>
static constexpr std::size_t overload_index =
std::invoke_result_t<resolver, Args...>::value;
};
template <class D, class Os>
struct dispatch_traits_impl : inapplicable_traits {};
template <class D, class... Os>
requires(sizeof...(Os) > 0u && (overload_traits<Os>::applicable && ...))
struct dispatch_traits_impl<D, std::tuple<Os...>> : applicable_traits,
dispatch_traits_overload_resolution_impl<std::tuple<Os...>,
std::make_index_sequence<sizeof...(Os)>> {
using dispatcher_types =
std::tuple<typename overload_traits<Os>::dispatcher_type...>;
template <class P>
static constexpr bool applicable_ptr =
(overload_traits<Os>::template applicable_ptr<D, P> && ...);
template <class P>
static constexpr dispatcher_types dispatchers{
overload_traits<Os>::template dispatcher<D, P>...};
};
template <class D> struct dispatch_traits : inapplicable_traits {};
template <class D>
requires(requires { typename D::overload_types; } &&
std::is_trivially_default_constructible_v<D>)
struct dispatch_traits<D>
: dispatch_traits_impl<D, typename D::overload_types> {};
template <class D>
struct dispatch_meta {
template <class P>
constexpr explicit dispatch_meta(std::in_place_type_t<P>)
: dispatchers(dispatch_traits<D>::template dispatchers<P>) {}
typename dispatch_traits<D>::dispatcher_types dispatchers;
};
struct copy_meta {
template <class P>
constexpr explicit copy_meta(std::in_place_type_t<P>)
: clone([](char* self, const char* rhs)
{ new(self) P(*reinterpret_cast<const P*>(rhs)); }) {}
void (*clone)(char*, const char*);
};
struct relocation_meta {
template <class P>
constexpr explicit relocation_meta(std::in_place_type_t<P>)
: relocate([](char* self, char* rhs) {
new(self) P(std::move(*reinterpret_cast<P*>(rhs)));
reinterpret_cast<P*>(rhs)->~P();
}) {}
void (*relocate)(char*, char*);
};
struct destruction_meta {
template <class P>
constexpr explicit destruction_meta(std::in_place_type_t<P>)
: destroy([](char* self) { reinterpret_cast<P*>(self)->~P(); }) {}
void (*destroy)(char*);
};
template <class... Ms>
struct facade_meta : Ms... {
template <class P>
constexpr explicit facade_meta(std::in_place_type_t<P>)
: Ms(std::in_place_type<P>)... {}
};
template <constraint_level C, class M> struct conditional_meta_tag {};
template <class M, class Ms> struct facade_meta_traits_impl;
template <class M, class... Ms>
struct facade_meta_traits_impl<M, facade_meta<Ms...>>
{ using type = facade_meta<M, Ms...>; };
template <constraint_level C, class M, class... Ms>
requires(C > constraint_level::none && C < constraint_level::trivial)
struct facade_meta_traits_impl<conditional_meta_tag<C, M>, facade_meta<Ms...>>
{ using type = facade_meta<M, Ms...>; };
template <constraint_level C, class M, class... Ms>
requires(C < constraint_level::nontrivial || C > constraint_level::nothrow)
struct facade_meta_traits_impl<conditional_meta_tag<C, M>, facade_meta<Ms...>>
{ using type = facade_meta<Ms...>; };
template <class... Ms> struct facade_meta_traits;
template <class M, class... Ms>
struct facade_meta_traits<M, Ms...> : facade_meta_traits_impl<
M, typename facade_meta_traits<Ms...>::type> {};
template <> struct facade_meta_traits<> { using type = facade_meta<>; };
template <class F, class Ds>
struct basic_facade_traits_impl : inapplicable_traits {};
template <class F, class... Ds>
struct basic_facade_traits_impl<F, std::tuple<Ds...>> : applicable_traits {
using meta_type = typename facade_meta_traits<
conditional_meta_tag<F::constraints.copyability, copy_meta>,
conditional_meta_tag<F::constraints.relocatability, relocation_meta>,
conditional_meta_tag<F::constraints.destructibility, destruction_meta>,
conditional_meta_tag<std::is_void_v<typename F::reflection_type> ?
constraint_level::none : constraint_level::nothrow,
typename F::reflection_type>>::type;
using default_dispatch = typename default_traits<Ds...>::type;
template <class D>
static constexpr bool has_dispatch = contains_traits<D, Ds...>::applicable;
};
template <class F> struct basic_facade_traits : inapplicable_traits {};
template <class F>
requires(
requires {
typename F::dispatch_types;
F::constraints;
typename F::reflection_type;
} &&
std::is_same_v<decltype(F::constraints),
const proxiable_ptr_constraints> &&
std::popcount(F::constraints.max_align) == 1u &&
F::constraints.max_size % F::constraints.max_align == 0u &&
(std::is_void_v<typename F::reflection_type> ||
std::is_trivially_copyable_v<typename F::reflection_type>))
struct basic_facade_traits<F>
: basic_facade_traits_impl<F, typename F::dispatch_types> {};
template <class F, class Ds>
struct facade_traits_impl : inapplicable_traits {};
template <class F, class... Ds> requires(dispatch_traits<Ds>::applicable && ...)
struct facade_traits_impl<F, std::tuple<Ds...>> : applicable_traits {
using meta_type = facade_meta<
typename basic_facade_traits<F>::meta_type, dispatch_meta<Ds>...>;
template <class P>
static constexpr bool applicable_ptr =
sizeof(P) <= F::constraints.max_size &&
alignof(P) <= F::constraints.max_align &&
has_copyability<P>(F::constraints.copyability) &&
has_relocatability<P>(F::constraints.relocatability) &&
has_destructibility<P>(F::constraints.destructibility) &&
(dispatch_traits<Ds>::template applicable_ptr<P> && ...) &&
(std::is_void_v<typename F::reflection_type> || std::is_constructible_v<
typename F::reflection_type, std::in_place_type_t<P>>);
template <class P> static constexpr meta_type meta{std::in_place_type<P>};
};
template <class F>
struct facade_traits : facade_traits_impl<F, typename F::dispatch_types> {};
template <class T, class...> struct dependent_traits { using type = T; };
template <class T, class... Us>
using dependent_t = typename dependent_traits<T, Us...>::type;
} // namespace details
template <class F>
concept basic_facade = details::basic_facade_traits<F>::applicable;
template <class F>
concept facade = basic_facade<F> && details::facade_traits<F>::applicable;
template <class P, class F>
concept proxiable = facade<F> && details::is_address_deducible<P> &&
details::facade_traits<F>::template applicable_ptr<P>;
template <basic_facade F>
class proxy {
using BasicTraits = details::basic_facade_traits<F>;
using Traits = details::facade_traits<F>;
using DefaultDispatch = typename BasicTraits::default_dispatch;
template <class P, class... Args>
static constexpr bool HasNothrowPolyConstructor = std::conditional_t<
proxiable<P, F>, std::is_nothrow_constructible<P, Args...>,
std::false_type>::value;
template <class P, class... Args>
static constexpr bool HasPolyConstructor = std::conditional_t<
proxiable<P, F>, std::is_constructible<P, Args...>,
std::false_type>::value;
static constexpr bool HasTrivialCopyConstructor =
F::constraints.copyability == constraint_level::trivial;
static constexpr bool HasNothrowCopyConstructor =
F::constraints.copyability >= constraint_level::nothrow;
static constexpr bool HasCopyConstructor =
F::constraints.copyability >= constraint_level::nontrivial;
static constexpr bool HasNothrowMoveConstructor =
F::constraints.relocatability >= constraint_level::nothrow;
static constexpr bool HasMoveConstructor =
F::constraints.relocatability >= constraint_level::nontrivial;
static constexpr bool HasTrivialDestructor =
F::constraints.destructibility == constraint_level::trivial;
static constexpr bool HasNothrowDestructor =
F::constraints.destructibility >= constraint_level::nothrow;
static constexpr bool HasDestructor =
F::constraints.destructibility >= constraint_level::nontrivial;
template <class P, class... Args>
static constexpr bool HasNothrowPolyAssignment =
HasNothrowPolyConstructor<P, Args...> && HasNothrowDestructor;
template <class P, class... Args>
static constexpr bool HasPolyAssignment = HasPolyConstructor<P, Args...> &&
HasDestructor;
static constexpr bool HasTrivialCopyAssignment = HasTrivialCopyConstructor &&
HasTrivialDestructor;
static constexpr bool HasNothrowCopyAssignment = HasNothrowCopyConstructor &&
HasNothrowDestructor;
static constexpr bool HasCopyAssignment = HasNothrowCopyAssignment ||
(HasCopyConstructor && HasMoveConstructor && HasDestructor);
static constexpr bool HasNothrowMoveAssignment = HasNothrowMoveConstructor &&
HasNothrowDestructor;
static constexpr bool HasMoveAssignment = HasMoveConstructor && HasDestructor;
public:
proxy() noexcept { meta_ = nullptr; }
proxy(std::nullptr_t) noexcept : proxy() {}
proxy(const proxy& rhs) noexcept(HasNothrowCopyConstructor)
requires(!HasTrivialCopyConstructor && HasCopyConstructor) {
if (rhs.meta_ != nullptr) {
rhs.meta_->clone(ptr_, rhs.ptr_);
meta_ = rhs.meta_;
} else {
meta_ = nullptr;
}
}
proxy(const proxy&) noexcept requires(HasTrivialCopyConstructor) = default;
proxy(const proxy&) requires(!HasCopyConstructor) = delete;
proxy(proxy&& rhs) noexcept(HasNothrowMoveConstructor)
requires(HasMoveConstructor) {
if (rhs.meta_ != nullptr) {
if constexpr (F::constraints.relocatability ==
constraint_level::trivial) {
memcpy(ptr_, rhs.ptr_, F::constraints.max_size);
} else {
rhs.meta_->relocate(ptr_, rhs.ptr_);
}
meta_ = rhs.meta_;
rhs.meta_ = nullptr;
} else {
meta_ = nullptr;
}
}
proxy(proxy&&) requires(!HasMoveConstructor) = delete;
template <class P>
proxy(P&& ptr) noexcept(HasNothrowPolyConstructor<std::decay_t<P>, P>)
requires(HasPolyConstructor<std::decay_t<P>, P>)
{ initialize<std::decay_t<P>>(std::forward<P>(ptr)); }
template <class P, class... Args>
explicit proxy(std::in_place_type_t<P>, Args&&... args)
noexcept(HasNothrowPolyConstructor<P, Args...>)
requires(HasPolyConstructor<P, Args...>)
{ initialize<P>(std::forward<Args>(args)...); }
template <class P, class U, class... Args>
explicit proxy(std::in_place_type_t<P>, std::initializer_list<U> il,
Args&&... args)
noexcept(HasNothrowPolyConstructor<P, std::initializer_list<U>&, Args...>)
requires(HasPolyConstructor<P, std::initializer_list<U>&, Args...>)
{ initialize<P>(il, std::forward<Args>(args)...); }
proxy& operator=(std::nullptr_t) noexcept(HasNothrowDestructor)
requires(HasDestructor) {
this->~proxy();
new(this) proxy();
return *this;
}
proxy& operator=(const proxy& rhs)
requires(!HasNothrowCopyAssignment && HasCopyAssignment)
{ return *this = proxy{rhs}; }
proxy& operator=(const proxy& rhs) noexcept
requires(!HasTrivialCopyAssignment && HasNothrowCopyAssignment) {
if (this != &rhs) {
this->~proxy();
new(this) proxy(rhs);
}
return *this;
}
proxy& operator=(const proxy&) noexcept requires(HasTrivialCopyAssignment) =
default;
proxy& operator=(const proxy&) requires(!HasCopyAssignment) = delete;
proxy& operator=(proxy&& rhs) noexcept(HasNothrowMoveAssignment)
requires(HasMoveAssignment) {
if constexpr (HasNothrowMoveAssignment) {
this->~proxy();
} else {
reset(); // For weak exception safety
}
new(this) proxy(std::move(rhs));
return *this;
}
proxy& operator=(proxy&&) requires(!HasMoveAssignment) = delete;
template <class P>
proxy& operator=(P&& ptr) noexcept
requires(HasNothrowPolyAssignment<std::decay_t<P>, P>) {
this->~proxy();
initialize<std::decay_t<P>>(std::forward<P>(ptr));
return *this;
}
template <class P>
proxy& operator=(P&& ptr)
requires(!HasNothrowPolyAssignment<std::decay_t<P>, P> &&
HasPolyAssignment<std::decay_t<P>, P>)
{ return *this = proxy{std::forward<P>(ptr)}; }
~proxy() noexcept(HasNothrowDestructor)
requires(!HasTrivialDestructor && HasDestructor) {
if (meta_ != nullptr) {
meta_->destroy(ptr_);
}
}
~proxy() requires(HasTrivialDestructor) = default;
~proxy() requires(!HasDestructor) = delete;
bool has_value() const noexcept { return meta_ != nullptr; }
decltype(auto) reflect() const noexcept
requires(!std::is_void_v<typename F::reflection_type>)
{ return static_cast<const typename F::reflection_type&>(*meta_); }
void reset() noexcept(HasNothrowDestructor) requires(HasDestructor)
{ this->~proxy(); meta_ = nullptr; }
void swap(proxy& rhs) noexcept(HasNothrowMoveConstructor)
requires(HasMoveConstructor) {
if constexpr (F::constraints.relocatability == constraint_level::trivial) {
std::swap(meta_, rhs.meta_);
std::swap(ptr_, rhs.ptr);
} else {
if (meta_ != nullptr) {
if (rhs.meta_ != nullptr) {
proxy temp = std::move(*this);
new(this) proxy(std::move(rhs));
new(&rhs) proxy(std::move(temp));
} else {
new(&rhs) proxy(std::move(*this));
}
} else if (rhs.meta_ != nullptr) {
new(this) proxy(std::move(rhs));
}
}
}
friend void swap(proxy& a, proxy& b) noexcept(HasNothrowMoveConstructor)
{ a.swap(b); }
template <class P, class... Args>
P& emplace(Args&&... args) noexcept(HasNothrowPolyAssignment<P, Args...>)
requires(HasPolyAssignment<P, Args...>) {
reset();
initialize<P>(std::forward<Args>(args)...);
return *reinterpret_cast<P*>(ptr_);
}
template <class P, class U, class... Args>
P& emplace(std::initializer_list<U> il, Args&&... args)
noexcept(HasNothrowPolyAssignment<P, std::initializer_list<U>&, Args...>)
requires(HasPolyAssignment<P, std::initializer_list<U>&, Args...>) {
reset();
initialize<P>(il, std::forward<Args>(args)...);
return *reinterpret_cast<P*>(ptr_);
}
template <class D = DefaultDispatch, class... Args>
decltype(auto) invoke(Args&&... args) const
requires(facade<details::dependent_t<F, Args...>> &&
BasicTraits::template has_dispatch<D> &&
details::dispatch_traits<D>::template has_overload<Args...>) {
constexpr std::size_t OverloadIndex =
details::dispatch_traits<D>::template overload_index<Args...>;
const auto& dispatchers = static_cast<const typename Traits::meta_type*>(
meta_)->template dispatch_meta<D>::dispatchers;
const auto& dispatcher = std::get<OverloadIndex>(dispatchers);
return dispatcher(ptr_, std::forward<Args>(args)...);
}
template <class... Args>
decltype(auto) operator()(Args&&... args) const
requires(facade<details::dependent_t<F, Args...>> &&
!std::is_void_v<DefaultDispatch> &&
details::dependent_t<details::dispatch_traits<DefaultDispatch>,
Args...>::template has_overload<Args...>)
{ return invoke(std::forward<Args>(args)...); }
private:
template <class P, class... Args>
void initialize(Args&&... args) {
new(ptr_) P(std::forward<Args>(args)...);
meta_ = &Traits::template meta<P>;
}
const typename BasicTraits::meta_type* meta_;
alignas(F::constraints.max_align) char ptr_[F::constraints.max_size];
};
namespace details {
template <class T>
class sbo_ptr {
public:
template <class... Args>
sbo_ptr(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args...>)
requires(std::is_constructible_v<T, Args...>)
: value_(std::forward<Args>(args)...) {}
sbo_ptr(const sbo_ptr&) noexcept(std::is_nothrow_copy_constructible_v<T>)
= default;
sbo_ptr(sbo_ptr&&) noexcept(std::is_nothrow_move_constructible_v<T>)
= default;
T* operator->() const noexcept { return &value_; }
private:
mutable T value_;
};
template <class T>
class deep_ptr {
public:
template <class... Args>
deep_ptr(Args&&... args) requires(std::is_constructible_v<T, Args...>)
: ptr_(new T(std::forward<Args>(args)...)) {}
deep_ptr(const deep_ptr& rhs) requires(std::is_copy_constructible_v<T>)
: ptr_(rhs.ptr_ == nullptr ? nullptr : new T(*rhs.ptr_)) {}
deep_ptr(deep_ptr&& rhs) noexcept : ptr_(rhs.ptr_) { rhs.ptr_ = nullptr; }
~deep_ptr() noexcept { delete ptr_; }
T* operator->() const noexcept { return ptr_; }
private:
T* ptr_;
};
template <class F, class T, class... Args>
proxy<F> make_proxy_impl(Args&&... args) {
return proxy<F>{std::in_place_type<
std::conditional_t<proxiable<sbo_ptr<T>, F>, sbo_ptr<T>, deep_ptr<T>>>,
std::forward<Args>(args)...};
}
} // namespace details
template <class F, class T, class... Args>
proxy<F> make_proxy(Args&&... args)
{ return details::make_proxy_impl<F, T>(std::forward<Args>(args)...); }
template <class F, class T, class U, class... Args>
proxy<F> make_proxy(std::initializer_list<U> il, Args&&... args)
{ return details::make_proxy_impl<F, T>(il, std::forward<Args>(args)...); }
template <class F, class T>
proxy<F> make_proxy(T&& value) {
return details::make_proxy_impl<F, std::decay_t<T>>(std::forward<T>(value));
}
// The following types and macros aim to simplify definition of dispatch and
// facade types prior to C++26
namespace details {
template <class O> struct overload_args_traits;
template <class R, class... Args>
struct overload_args_traits<R(Args...)> { using type = std::tuple<Args&&...>; };
template <class Args, class Os>
struct overloads_matching_traits : inapplicable_traits {};
template <class... Args, class... Os>
struct overloads_matching_traits<std::tuple<Args...>, std::tuple<Os...>>
: contains_traits<std::tuple<Args&&...>,
typename overload_args_traits<Os>::type...> {};
template <class Args, class Os>
concept matches_overloads = overloads_matching_traits<Args, Os>::applicable;
template <class T, class U> struct flattening_traits_impl;
template <class T>
struct flattening_traits_impl<std::tuple<>, T> { using type = T; };
template <class T, class... Ts, class U>
struct flattening_traits_impl<std::tuple<T, Ts...>, U>
: flattening_traits_impl<std::tuple<Ts...>, U> {};
template <class T, class... Ts, class... Us>
requires(!contains_traits<T, Us...>::applicable)
struct flattening_traits_impl<std::tuple<T, Ts...>, std::tuple<Us...>>
: flattening_traits_impl<std::tuple<Ts...>, std::tuple<Us..., T>> {};
template <class T> struct flattening_traits { using type = std::tuple<T>; };
template <>
struct flattening_traits<std::tuple<>> { using type = std::tuple<>; };
template <class T, class... Ts>
struct flattening_traits<std::tuple<T, Ts...>> : flattening_traits_impl<
typename flattening_traits<T>::type,
typename flattening_traits<std::tuple<Ts...>>::type> {};
template <class... Ds>
struct overloads_combination_traits { using type = std::tuple<>; };
template <class D, class... Ds>
struct overloads_combination_traits<D, Ds...>
: overloads_combination_traits<Ds...> {};
template <class D, class... Ds>
requires(requires { typename D::overload_types; })
struct overloads_combination_traits<D, Ds...>
: flattening_traits<std::tuple<typename D::overload_types,
typename overloads_combination_traits<Ds...>::type>> {};
template <class... Os> requires(sizeof...(Os) > 0u)
struct dispatch_prototype { using overload_types = std::tuple<Os...>; };
template <class... Ds> requires(sizeof...(Ds) > 0u)
struct combined_dispatch_prototype : Ds... {
using overload_types = typename overloads_combination_traits<Ds...>::type;
using Ds::operator()...;
};
template <class Ds = std::tuple<>, proxiable_ptr_constraints C =
relocatable_ptr_constraints, class R = void>
struct facade_prototype {
using dispatch_types = typename flattening_traits<Ds>::type;
static constexpr proxiable_ptr_constraints constraints = C;
using reflection_type = R;
};
} // namespace details
} // namespace pro
#define PRO_DEF_MEMBER_DISPATCH(NAME, ...) \
struct NAME : ::pro::details::dispatch_prototype<__VA_ARGS__> { \
template <class __T, class... __Args> \
decltype(auto) operator()(__T& __self, __Args&&... __args) \
requires(::pro::details::matches_overloads<std::tuple<__Args...>, \
std::tuple<__VA_ARGS__>> && \
requires{ __self.NAME(std::forward<__Args>(__args)...); }) { \
return __self.NAME(std::forward<__Args>(__args)...); \
} \
}
#define PRO_DEF_FREE_DISPATCH(NAME, FUNC, ...) \
struct NAME : ::pro::details::dispatch_prototype<__VA_ARGS__> { \
template <class __T, class... __Args> \
decltype(auto) operator()(__T& __self, __Args&&... __args) \
requires(::pro::details::matches_overloads<std::tuple<__Args...>, \
std::tuple<__VA_ARGS__>> && \
requires{ FUNC(__self, std::forward<__Args>(__args)...); }) { \
return FUNC(__self, std::forward<__Args>(__args)...); \
} \
}
#define PRO_DEF_COMBINED_DISPATCH(NAME, ...) \
struct NAME : ::pro::details::combined_dispatch_prototype<__VA_ARGS__> {}
#define PRO_MAKE_DISPATCH_PACK(...) std::tuple<__VA_ARGS__>
#define PRO_DEF_FACADE(NAME, ...) \
struct NAME : ::pro::details::facade_prototype<__VA_ARGS__> {}
#endif // _MSFT_PROXY_