-
Notifications
You must be signed in to change notification settings - Fork 136
/
proxy.h
1679 lines (1567 loc) · 68.1 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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#ifndef _MSFT_PROXY_
#define _MSFT_PROXY_
#include <cassert>
#include <cstddef>
#include <bit>
#include <concepts>
#include <initializer_list>
#include <limits>
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
#if __has_cpp_attribute(msvc::no_unique_address)
#define ___PRO_NO_UNIQUE_ADDRESS_ATTRIBUTE msvc::no_unique_address
#elif __has_cpp_attribute(no_unique_address)
#define ___PRO_NO_UNIQUE_ADDRESS_ATTRIBUTE no_unique_address
#else
#error "Proxy requires C++20 attribute no_unique_address"
#endif
#ifdef _MSC_VER
#define ___PRO_ENFORCE_EBO __declspec(empty_bases)
#else
#define ___PRO_ENFORCE_EBO
#endif // _MSC_VER
#define __msft_lib_proxy 202410L
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;
};
template <class F> class proxy;
namespace details {
struct applicable_traits { static constexpr bool applicable = true; };
struct inapplicable_traits { static constexpr bool applicable = false; };
enum class qualifier_type { lv, const_lv, rv, const_rv };
template <class T, qualifier_type Q> struct add_qualifier_traits;
template <class T>
struct add_qualifier_traits<T, qualifier_type::lv> : std::type_identity<T&> {};
template <class T>
struct add_qualifier_traits<T, qualifier_type::const_lv>
: std::type_identity<const T&> {};
template <class T>
struct add_qualifier_traits<T, qualifier_type::rv> : std::type_identity<T&&> {};
template <class T>
struct add_qualifier_traits<T, qualifier_type::const_rv>
: std::type_identity<const T&&> {};
template <class T, qualifier_type Q>
using add_qualifier_t = typename add_qualifier_traits<T, Q>::type;
template <class T, qualifier_type Q>
using add_qualifier_ptr_t = std::remove_reference_t<add_qualifier_t<T, Q>>*;
template <template <class, class> class R, class O, class... Is>
struct recursive_reduction : std::type_identity<O> {};
template <template <class, class> class R, class O, class... Is>
using recursive_reduction_t = typename recursive_reduction<R, O, Is...>::type;
template <template <class, class> class R, class O, class I, class... Is>
struct recursive_reduction<R, O, I, Is...>
{ using type = recursive_reduction_t<R, R<O, I>, Is...>; };
template <class Expr>
consteval bool is_consteval(Expr)
{ return requires { typename std::bool_constant<(Expr{}(), false)>; }; }
template <class T, std::size_t I>
concept has_tuple_element = requires { typename std::tuple_element_t<I, T>; };
template <class T>
consteval bool is_tuple_like_well_formed() {
if constexpr (requires { { std::tuple_size<T>::value } ->
std::same_as<const std::size_t&>; }) {
if constexpr (is_consteval([] { return std::tuple_size<T>::value; })) {
return []<std::size_t... I>(std::index_sequence<I...>) {
return (has_tuple_element<T, I> && ...);
}(std::make_index_sequence<std::tuple_size_v<T>>{});
}
}
return false;
}
template <template <class...> class T, class TL, class Is, class... Args>
struct instantiated_traits;
template <template <class...> class T, class TL, std::size_t... Is,
class... Args>
struct instantiated_traits<T, TL, std::index_sequence<Is...>, Args...>
{ using type = T<Args..., std::tuple_element_t<Is, TL>...>; };
template <template <class...> class T, class TL, class... Args>
using instantiated_t = typename instantiated_traits<
T, TL, std::make_index_sequence<std::tuple_size_v<TL>>, Args...>::type;
template <class T>
consteval bool has_copyability(constraint_level level) {
switch (level) {
case constraint_level::none: return true;
case constraint_level::nontrivial: return std::is_copy_constructible_v<T>;
case constraint_level::nothrow:
return std::is_nothrow_copy_constructible_v<T>;
case constraint_level::trivial:
return std::is_trivially_copy_constructible_v<T> &&
std::is_trivially_destructible_v<T>;
default: return false;
}
}
template <class T>
consteval bool has_relocatability(constraint_level level) {
switch (level) {
case constraint_level::none: return true;
case constraint_level::nontrivial:
return std::is_move_constructible_v<T> && std::is_destructible_v<T>;
case constraint_level::nothrow:
return std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_destructible_v<T>;
case constraint_level::trivial:
return std::is_trivially_move_constructible_v<T> &&
std::is_trivially_destructible_v<T>;
default: return false;
}
}
template <class T>
consteval bool has_destructibility(constraint_level level) {
switch (level) {
case constraint_level::none: return true;
case constraint_level::nontrivial: return std::is_destructible_v<T>;
case constraint_level::nothrow: return std::is_nothrow_destructible_v<T>;
case constraint_level::trivial: return std::is_trivially_destructible_v<T>;
default: return false;
}
}
template <class T>
class destruction_guard {
public:
explicit destruction_guard(T* p) noexcept : p_(p) {}
destruction_guard(const destruction_guard&) = delete;
~destruction_guard() noexcept(std::is_nothrow_destructible_v<T>)
{ std::destroy_at(p_); }
private:
T* p_;
};
template <class P, qualifier_type Q, bool NE>
struct ptr_traits : inapplicable_traits {};
template <class P, qualifier_type Q, bool NE>
requires(requires { *std::declval<add_qualifier_t<P, Q>>(); } &&
(!NE || noexcept(*std::declval<add_qualifier_t<P, Q>>())))
struct ptr_traits<P, Q, NE> : applicable_traits
{ using target_type = decltype(*std::declval<add_qualifier_t<P, Q>>()); };
template <class D, bool NE, class R, class... Args>
concept invocable_dispatch = (NE && std::is_nothrow_invocable_r_v<
R, D, Args...>) || (!NE && std::is_invocable_r_v<R, D, Args...>);
template <class D, class P, qualifier_type Q, bool NE, class R, class... Args>
concept invocable_dispatch_ptr_indirect = ptr_traits<P, Q, NE>::applicable &&
invocable_dispatch<
D, NE, R, typename ptr_traits<P, Q, NE>::target_type, Args...>;
template <class D, class P, qualifier_type Q, bool NE, class R, class... Args>
concept invocable_dispatch_ptr_direct = invocable_dispatch<
D, NE, R, add_qualifier_t<P, Q>, Args...> && (Q != qualifier_type::rv ||
(NE && std::is_nothrow_destructible_v<P>) ||
(!NE && std::is_destructible_v<P>));
template <bool NE, class R, class... Args>
using func_ptr_t = std::conditional_t<
NE, R (*)(Args...) noexcept, R (*)(Args...)>;
template <class D, class R, class... Args>
R invoke_dispatch(Args&&... args) {
if constexpr (std::is_void_v<R>) {
D{}(std::forward<Args>(args)...);
} else {
return D{}(std::forward<Args>(args)...);
}
}
template <class D, class P, qualifier_type Q, class R, class... Args>
R indirect_conv_dispatcher(add_qualifier_t<std::byte, Q> self, Args... args)
noexcept(invocable_dispatch_ptr_indirect<D, P, Q, true, R, Args...>) {
return invoke_dispatch<D, R>(*std::forward<add_qualifier_t<P, Q>>(
*std::launder(reinterpret_cast<add_qualifier_ptr_t<P, Q>>(&self))),
std::forward<Args>(args)...);
}
template <class D, class P, qualifier_type Q, class R, class... Args>
R direct_conv_dispatcher(add_qualifier_t<std::byte, Q> self, Args... args)
noexcept(invocable_dispatch_ptr_direct<D, P, Q, true, R, Args...>) {
auto& qp = *std::launder(
reinterpret_cast<add_qualifier_ptr_t<P, Q>>(&self));
if constexpr (Q == qualifier_type::rv) {
destruction_guard guard{&qp};
return invoke_dispatch<D, R>(
std::forward<add_qualifier_t<P, Q>>(qp), std::forward<Args>(args)...);
} else {
return invoke_dispatch<D, R>(
std::forward<add_qualifier_t<P, Q>>(qp), std::forward<Args>(args)...);
}
}
template <class D, qualifier_type Q, class R, class... Args>
R default_conv_dispatcher(add_qualifier_t<std::byte, Q>, Args... args)
noexcept(invocable_dispatch<D, true, R, std::nullptr_t, Args...>)
{ return invoke_dispatch<D, R>(nullptr, std::forward<Args>(args)...); }
template <class P>
void copying_dispatcher(std::byte& self, const std::byte& rhs)
noexcept(has_copyability<P>(constraint_level::nothrow)) {
std::construct_at(reinterpret_cast<P*>(&self),
*std::launder(reinterpret_cast<const P*>(&rhs)));
}
template <std::size_t Len, std::size_t Align>
void copying_default_dispatcher(std::byte& self, const std::byte& rhs)
noexcept {
std::uninitialized_copy_n(
std::assume_aligned<Align>(&rhs), Len, std::assume_aligned<Align>(&self));
}
template <class P>
void relocation_dispatcher(std::byte& self, const std::byte& rhs)
noexcept(has_relocatability<P>(constraint_level::nothrow)) {
P* other = std::launder(reinterpret_cast<P*>(const_cast<std::byte*>(&rhs)));
destruction_guard guard{other};
std::construct_at(reinterpret_cast<P*>(&self), std::move(*other));
}
template <class P>
void destruction_dispatcher(std::byte& self)
noexcept(has_destructibility<P>(constraint_level::nothrow))
{ std::destroy_at(std::launder(reinterpret_cast<P*>(&self))); }
inline void destruction_default_dispatcher(std::byte&) noexcept {}
template <class O> struct overload_traits : inapplicable_traits {};
template <qualifier_type Q, bool NE, class R, class... Args>
struct overload_traits_impl : applicable_traits {
template <bool IS_DIRECT, class D>
struct meta_provider {
template <class P>
static constexpr auto get()
-> func_ptr_t<NE, R, add_qualifier_t<std::byte, Q>, Args...> {
if constexpr (!IS_DIRECT &&
invocable_dispatch_ptr_indirect<D, P, Q, NE, R, Args...>) {
return &indirect_conv_dispatcher<D, P, Q, R, Args...>;
} else if constexpr (IS_DIRECT &&
invocable_dispatch_ptr_direct<D, P, Q, NE, R, Args...>) {
return &direct_conv_dispatcher<D, P, Q, R, Args...>;
} else if constexpr (invocable_dispatch<
D, NE, R, std::nullptr_t, Args...>) {
return &default_conv_dispatcher<D, Q, R, Args...>;
} else {
return nullptr;
}
}
};
template <bool IS_DIRECT, class D, class P>
static constexpr bool applicable_ptr =
meta_provider<IS_DIRECT, D>::template get<P>() != nullptr;
static constexpr qualifier_type qualifier = Q;
};
template <class R, class... Args>
struct overload_traits<R(Args...)>
: overload_traits_impl<qualifier_type::lv, false, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) noexcept>
: overload_traits_impl<qualifier_type::lv, true, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) &>
: overload_traits_impl<qualifier_type::lv, false, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) & noexcept>
: overload_traits_impl<qualifier_type::lv, true, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) &&>
: overload_traits_impl<qualifier_type::rv, false, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) && noexcept>
: overload_traits_impl<qualifier_type::rv, true, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) const>
: overload_traits_impl<qualifier_type::const_lv, false, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) const noexcept>
: overload_traits_impl<qualifier_type::const_lv, true, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) const&>
: overload_traits_impl<qualifier_type::const_lv, false, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) const& noexcept>
: overload_traits_impl<qualifier_type::const_lv, true, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) const&&>
: overload_traits_impl<qualifier_type::const_rv, false, R, Args...> {};
template <class R, class... Args>
struct overload_traits<R(Args...) const&& noexcept>
: overload_traits_impl<qualifier_type::const_rv, true, R, Args...> {};
template <class MP>
struct dispatcher_meta {
constexpr dispatcher_meta() noexcept : dispatcher(nullptr) {}
template <class P>
constexpr explicit dispatcher_meta(std::in_place_type_t<P>) noexcept
: dispatcher(MP::template get<P>()) {}
decltype(MP::template get<void>()) dispatcher;
};
template <class... Ms>
struct composite_meta_impl : Ms... {
constexpr composite_meta_impl() noexcept = default;
template <class P>
constexpr explicit composite_meta_impl(std::in_place_type_t<P>) noexcept
: Ms(std::in_place_type<P>)... {}
};
template <class O, class I> struct meta_reduction : std::type_identity<O> {};
template <class... Ms, class I> requires(!std::is_void_v<I>)
struct meta_reduction<composite_meta_impl<Ms...>, I>
: std::type_identity<composite_meta_impl<Ms..., I>> {};
template <class... Ms1, class... Ms2>
struct meta_reduction<composite_meta_impl<Ms1...>, composite_meta_impl<Ms2...>>
: std::type_identity<composite_meta_impl<Ms1..., Ms2...>> {};
template <class O, class I>
using meta_reduction_t = typename meta_reduction<O, I>::type;
template <class... Ms>
using composite_meta =
recursive_reduction_t<meta_reduction_t, composite_meta_impl<>, Ms...>;
template <class T>
consteval bool is_is_direct_well_formed() {
if constexpr (requires { { T::is_direct } -> std::same_as<const bool&>; }) {
if constexpr (is_consteval([] { return T::is_direct; })) {
return true;
}
}
return false;
}
template <class C, class... Os>
struct conv_traits_impl : inapplicable_traits {};
template <class C, class... Os>
requires(sizeof...(Os) > 0u && (overload_traits<Os>::applicable && ...))
struct conv_traits_impl<C, Os...> : applicable_traits {
using meta = composite_meta_impl<dispatcher_meta<typename overload_traits<Os>
::template meta_provider<C::is_direct, typename C::dispatch_type>>...>;
template <class P>
static constexpr bool applicable_ptr =
(overload_traits<Os>::template applicable_ptr<
C::is_direct, typename C::dispatch_type, P> && ...);
};
template <class C> struct conv_traits : inapplicable_traits {};
template <class C>
requires(
requires {
typename C::dispatch_type;
typename C::overload_types;
} &&
is_is_direct_well_formed<C>() &&
std::is_trivial_v<typename C::dispatch_type> &&
is_tuple_like_well_formed<typename C::overload_types>())
struct conv_traits<C>
: instantiated_t<conv_traits_impl, typename C::overload_types, C> {};
template <class P>
using ptr_element_t = typename std::pointer_traits<P>::element_type;
template <class R>
struct refl_meta {
template <class P> requires(R::is_direct)
constexpr explicit refl_meta(std::in_place_type_t<P>)
: reflector(std::in_place_type<P>) {}
template <class P> requires(!R::is_direct)
constexpr explicit refl_meta(std::in_place_type_t<P>)
: reflector(std::in_place_type<ptr_element_t<P>>) {}
typename R::reflector_type reflector;
};
template <class R, class T, bool IS_DIRECT>
consteval bool is_reflector_well_formed() {
if constexpr (IS_DIRECT) {
if constexpr (std::is_constructible_v<R, std::in_place_type_t<T>>) {
if constexpr (is_consteval([] { return R{std::in_place_type<T>}; })) {
return true;
}
}
} else if constexpr (requires { typename ptr_element_t<T>; }) {
return is_reflector_well_formed<R, ptr_element_t<T>, true>();
}
return false;
}
template <class R> struct refl_traits : inapplicable_traits {};
template <class R>
requires(requires { typename R::reflector_type; } &&
is_is_direct_well_formed<R>())
struct refl_traits<R> : applicable_traits {
template <class P>
static constexpr bool applicable_ptr =
is_reflector_well_formed<typename R::reflector_type, P, R::is_direct>();
};
template <bool NE>
struct copyability_meta_provider {
template <class P>
static constexpr func_ptr_t<NE, void, std::byte&, const std::byte&> get() {
if constexpr (has_copyability<P>(constraint_level::trivial)) {
return ©ing_default_dispatcher<sizeof(P), alignof(P)>;
} else {
return ©ing_dispatcher<P>;
}
}
};
template <bool NE>
struct relocatability_meta_provider {
template <class P>
static constexpr func_ptr_t<NE, void, std::byte&, const std::byte&> get() {
if constexpr (has_relocatability<P>(constraint_level::trivial)) {
return ©ing_default_dispatcher<sizeof(P), alignof(P)>;
} else {
return &relocation_dispatcher<P>;
}
}
};
template <bool NE>
struct destructibility_meta_provider {
template <class P>
static constexpr func_ptr_t<NE, void, std::byte&> get() {
if constexpr (has_destructibility<P>(constraint_level::trivial)) {
return &destruction_default_dispatcher;
} else {
return &destruction_dispatcher<P>;
}
}
};
template <template <bool> class MP, constraint_level C>
struct lifetime_meta_traits : std::type_identity<void> {};
template <template <bool> class MP>
struct lifetime_meta_traits<MP, constraint_level::nothrow>
: std::type_identity<dispatcher_meta<MP<true>>> {};
template <template <bool> class MP>
struct lifetime_meta_traits<MP, constraint_level::nontrivial>
: std::type_identity<dispatcher_meta<MP<false>>> {};
template <template <bool> class MP, constraint_level C>
using lifetime_meta_t = typename lifetime_meta_traits<MP, C>::type;
template <class... As>
class ___PRO_ENFORCE_EBO composite_accessor_impl : public As... {
template <class> friend class pro::proxy;
composite_accessor_impl() noexcept = default;
composite_accessor_impl(const composite_accessor_impl&) noexcept = default;
composite_accessor_impl& operator=(const composite_accessor_impl&) noexcept
= default;
};
template <class SFINAE, class T, class... Args>
struct sfinae_accessor_traits : std::type_identity<void> {};
template <class T, class... Args>
struct sfinae_accessor_traits<
std::void_t<typename T::template accessor<Args...>>, T, Args...>
: std::type_identity<typename T::template accessor<Args...>> {};
template <class T, class... Args>
using accessor_t = typename sfinae_accessor_traits<void, T, Args...>::type;
template <bool IS_DIRECT, class F, class O, class I>
struct composite_accessor_reduction : std::type_identity<O> {};
template <bool IS_DIRECT, class F, class... As, class I>
requires(IS_DIRECT == I::is_direct && std::is_trivial_v<accessor_t<I, F>> &&
!std::is_final_v<accessor_t<I, F>>)
struct composite_accessor_reduction<
IS_DIRECT, F, composite_accessor_impl<As...>, I>
{ using type = composite_accessor_impl<As..., accessor_t<I, F>>; };
template <bool IS_DIRECT, class F>
struct composite_accessor_helper {
template <class O, class I>
using reduction_t =
typename composite_accessor_reduction<IS_DIRECT, F, O, I>::type;
};
template <bool IS_DIRECT, class F, class... Ts>
using composite_accessor = recursive_reduction_t<
composite_accessor_helper<IS_DIRECT, F>::template reduction_t,
composite_accessor_impl<>, Ts...>;
template <class A1, class A2> struct composite_accessor_merge_traits;
template <class... A1, class... A2>
struct composite_accessor_merge_traits<
composite_accessor_impl<A1...>, composite_accessor_impl<A2...>>
: std::type_identity<composite_accessor_impl<A1..., A2...>> {};
template <class A1, class A2>
using merged_composite_accessor =
typename composite_accessor_merge_traits<A1, A2>::type;
template <class F>
consteval bool is_facade_constraints_well_formed() {
if constexpr (requires {
{ F::constraints } -> std::same_as<const proxiable_ptr_constraints&>; }) {
if constexpr (is_consteval([] { return F::constraints; })) {
return std::has_single_bit(F::constraints.max_align) &&
F::constraints.max_size % F::constraints.max_align == 0u;
}
}
return false;
}
struct empty_proxy_base {};
template <class F, class... Cs>
struct facade_conv_traits_impl : inapplicable_traits {};
template <class F, class... Cs> requires(conv_traits<Cs>::applicable && ...)
struct facade_conv_traits_impl<F, Cs...> : applicable_traits {
using conv_meta = composite_meta<typename conv_traits<Cs>::meta...>;
using conv_indirect_accessor = composite_accessor<false, F, Cs...>;
using conv_direct_accessor = composite_accessor<true, F, Cs...>;
template <class P>
static constexpr bool conv_applicable_ptr =
(conv_traits<Cs>::template applicable_ptr<P> && ...);
};
template <class F, class... Rs>
struct facade_refl_traits_impl {
using refl_meta = composite_meta<details::refl_meta<Rs>...>;
using refl_indirect_accessor = composite_accessor<false, F, Rs...>;
using refl_direct_accessor = composite_accessor<true, F, Rs...>;
template <class P>
static constexpr bool refl_applicable_ptr =
(refl_traits<Rs>::template applicable_ptr<P> && ...);
};
template <class F> struct facade_traits : inapplicable_traits {};
template <class F>
requires(
requires {
typename F::convention_types;
typename F::reflection_types;
} &&
is_facade_constraints_well_formed<F>() &&
is_tuple_like_well_formed<typename F::convention_types>() &&
instantiated_t<facade_conv_traits_impl, typename F::convention_types, F>
::applicable &&
is_tuple_like_well_formed<typename F::reflection_types>())
struct facade_traits<F>
: instantiated_t<facade_conv_traits_impl, typename F::convention_types, F>,
instantiated_t<facade_refl_traits_impl, typename F::reflection_types, F> {
using copyability_meta = lifetime_meta_t<
copyability_meta_provider, F::constraints.copyability>;
using relocatability_meta = lifetime_meta_t<
relocatability_meta_provider,
F::constraints.copyability == constraint_level::trivial ?
constraint_level::trivial : F::constraints.relocatability>;
using destructibility_meta = lifetime_meta_t<
destructibility_meta_provider, F::constraints.destructibility>;
using meta = composite_meta<copyability_meta, relocatability_meta,
destructibility_meta, typename facade_traits::conv_meta,
typename facade_traits::refl_meta>;
using indirect_accessor = merged_composite_accessor<
typename facade_traits::conv_indirect_accessor,
typename facade_traits::refl_indirect_accessor>;
using direct_accessor = merged_composite_accessor<
typename facade_traits::conv_direct_accessor,
typename facade_traits::refl_direct_accessor>;
using base = std::conditional_t<std::is_same_v<direct_accessor,
composite_accessor_impl<>>, empty_proxy_base, direct_accessor>;
static constexpr bool has_indirection = !std::is_same_v<
typename facade_traits::indirect_accessor, composite_accessor_impl<>>;
};
using ptr_prototype = void*[2];
template <class M>
struct meta_ptr_indirect_impl {
constexpr meta_ptr_indirect_impl() noexcept : ptr_(nullptr) {};
template <class P>
constexpr explicit meta_ptr_indirect_impl(std::in_place_type_t<P>) noexcept
: ptr_(&storage<P>) {}
bool has_value() const noexcept { return ptr_ != nullptr; }
void reset() noexcept { ptr_ = nullptr; }
const M* operator->() const noexcept { return ptr_; }
private:
const M* ptr_;
template <class P> static constexpr M storage{std::in_place_type<P>};
};
template <class M, class DM>
struct meta_ptr_direct_impl : private M {
using M::M;
bool has_value() const noexcept { return this->DM::dispatcher != nullptr; }
void reset() noexcept { this->DM::dispatcher = nullptr; }
const M* operator->() const noexcept { return this; }
};
template <class M>
struct meta_ptr_traits_impl : std::type_identity<meta_ptr_indirect_impl<M>> {};
template <class MP, class... Ms>
struct meta_ptr_traits_impl<composite_meta_impl<dispatcher_meta<MP>, Ms...>>
: std::type_identity<meta_ptr_direct_impl<composite_meta_impl<
dispatcher_meta<MP>, Ms...>, dispatcher_meta<MP>>> {};
template <class M>
struct meta_ptr_traits : std::type_identity<meta_ptr_indirect_impl<M>> {};
template <class M>
requires(sizeof(M) <= sizeof(ptr_prototype) &&
alignof(M) <= alignof(ptr_prototype) &&
std::is_nothrow_default_constructible_v<M> &&
std::is_trivially_copyable_v<M>)
struct meta_ptr_traits<M> : meta_ptr_traits_impl<M> {};
template <class M>
using meta_ptr = typename meta_ptr_traits<M>::type;
template <class MP>
struct meta_ptr_reset_guard {
public:
explicit meta_ptr_reset_guard(MP& meta) noexcept : meta_(meta) {}
meta_ptr_reset_guard(const meta_ptr_reset_guard&) = delete;
~meta_ptr_reset_guard() { meta_.reset(); }
private:
MP& meta_;
};
template <class F>
struct proxy_helper {
static inline const auto& get_meta(const proxy<F>& p) noexcept {
assert(p.has_value());
return *p.meta_.operator->();
}
template <class C, class O, qualifier_type Q, class... Args>
static decltype(auto) invoke(add_qualifier_t<proxy<F>, Q> p, Args&&... args) {
auto dispatcher = get_meta(p)
.template dispatcher_meta<typename overload_traits<O>
::template meta_provider<C::is_direct, typename C::dispatch_type>>
::dispatcher;
if constexpr (C::is_direct &&
overload_traits<O>::qualifier == qualifier_type::rv) {
meta_ptr_reset_guard guard{p.meta_};
return dispatcher(std::forward<add_qualifier_t<std::byte, Q>>(*p.ptr_),
std::forward<Args>(args)...);
} else {
return dispatcher(std::forward<add_qualifier_t<std::byte, Q>>(*p.ptr_),
std::forward<Args>(args)...);
}
}
template <class A, qualifier_type Q>
static add_qualifier_t<proxy<F>, Q> access(add_qualifier_t<A, Q> a) {
if constexpr (std::is_base_of_v<A, proxy<F>>) {
return static_cast<add_qualifier_t<proxy<F>, Q>>(
std::forward<add_qualifier_t<A, Q>>(a));
} else {
return reinterpret_cast<add_qualifier_t<proxy<F>, Q>>(
*(reinterpret_cast<add_qualifier_ptr_t<std::byte, Q>>(
static_cast<add_qualifier_ptr_t<
typename facade_traits<F>::indirect_accessor, Q>>(
std::addressof(a))) - offsetof(proxy<F>, ia_)));
}
}
};
} // namespace details
template <class F>
concept facade = details::facade_traits<F>::applicable;
template <class P, class F>
concept proxiable = facade<F> && sizeof(P) <= F::constraints.max_size &&
alignof(P) <= F::constraints.max_align &&
details::has_copyability<P>(F::constraints.copyability) &&
details::has_relocatability<P>(F::constraints.relocatability) &&
details::has_destructibility<P>(F::constraints.destructibility) &&
details::facade_traits<F>::template conv_applicable_ptr<P> &&
details::facade_traits<F>::template refl_applicable_ptr<P>;
template <class F>
class proxy : public details::facade_traits<F>::base {
static_assert(facade<F>);
friend struct details::proxy_helper<F>;
using _Traits = details::facade_traits<F>;
public:
proxy() noexcept = default;
proxy(std::nullptr_t) noexcept {}
proxy(const proxy&) noexcept requires(F::constraints.copyability ==
constraint_level::trivial) = default;
proxy(const proxy& rhs)
noexcept(F::constraints.copyability == constraint_level::nothrow)
requires(F::constraints.copyability == constraint_level::nontrivial ||
F::constraints.copyability == constraint_level::nothrow) {
if (rhs.meta_.has_value()) {
rhs.meta_->_Traits::copyability_meta::dispatcher(*ptr_, *rhs.ptr_);
meta_ = rhs.meta_;
}
}
proxy(proxy&& rhs)
noexcept(F::constraints.relocatability == constraint_level::nothrow)
requires(F::constraints.relocatability >= constraint_level::nontrivial &&
F::constraints.copyability != constraint_level::trivial) {
if (rhs.meta_.has_value()) {
details::meta_ptr_reset_guard guard{rhs.meta_};
if constexpr (F::constraints.relocatability ==
constraint_level::trivial) {
std::ranges::uninitialized_copy(rhs.ptr_, ptr_);
} else {
rhs.meta_->_Traits::relocatability_meta::dispatcher(*ptr_, *rhs.ptr_);
}
meta_ = rhs.meta_;
}
}
template <class P>
proxy(P&& ptr) noexcept(std::is_nothrow_constructible_v<std::decay_t<P>, P>)
requires(proxiable<std::decay_t<P>, F> &&
std::is_constructible_v<std::decay_t<P>, P>)
{ initialize<std::decay_t<P>>(std::forward<P>(ptr)); }
template <proxiable<F> P, class... Args>
explicit proxy(std::in_place_type_t<P>, Args&&... args)
noexcept(std::is_nothrow_constructible_v<P, Args...>)
requires(std::is_constructible_v<P, Args...>)
{ initialize<P>(std::forward<Args>(args)...); }
template <proxiable<F> P, class U, class... Args>
explicit proxy(std::in_place_type_t<P>, std::initializer_list<U> il,
Args&&... args)
noexcept(std::is_nothrow_constructible_v<
P, std::initializer_list<U>&, Args...>)
requires(std::is_constructible_v<P, std::initializer_list<U>&, Args...>)
{ initialize<P>(il, std::forward<Args>(args)...); }
proxy& operator=(std::nullptr_t)
noexcept(F::constraints.destructibility >= constraint_level::nothrow)
requires(F::constraints.destructibility >= constraint_level::nontrivial)
{ reset(); return *this; }
proxy& operator=(const proxy&) noexcept requires(F::constraints.copyability ==
constraint_level::trivial) = default;
proxy& operator=(const proxy& rhs)
noexcept(F::constraints.copyability >= constraint_level::nothrow &&
F::constraints.destructibility >= constraint_level::nothrow)
requires((F::constraints.copyability == constraint_level::nontrivial ||
F::constraints.copyability == constraint_level::nothrow) &&
F::constraints.destructibility >= constraint_level::nontrivial) {
if (this != &rhs) {
if constexpr (F::constraints.copyability == constraint_level::nothrow) {
std::destroy_at(this);
std::construct_at(this, rhs);
} else {
*this = proxy{rhs};
}
}
return *this;
}
proxy& operator=(proxy&& rhs)
noexcept(F::constraints.relocatability >= constraint_level::nothrow &&
F::constraints.destructibility >= constraint_level::nothrow)
requires(F::constraints.relocatability >= constraint_level::nontrivial &&
F::constraints.destructibility >= constraint_level::nontrivial &&
F::constraints.copyability != constraint_level::trivial) {
if (this != &rhs) {
reset();
std::construct_at(this, std::move(rhs));
}
return *this;
}
template <class P>
proxy& operator=(P&& ptr)
noexcept(std::is_nothrow_constructible_v<std::decay_t<P>, P> &&
F::constraints.destructibility >= constraint_level::nothrow)
requires(proxiable<std::decay_t<P>, F> &&
std::is_constructible_v<std::decay_t<P>, P> &&
F::constraints.destructibility >= constraint_level::nontrivial) {
if constexpr (std::is_nothrow_constructible_v<std::decay_t<P>, P>) {
std::destroy_at(this);
initialize<std::decay_t<P>>(std::forward<P>(ptr));
} else {
*this = proxy{std::forward<P>(ptr)};
}
return *this;
}
~proxy() requires(F::constraints.destructibility == constraint_level::trivial)
= default;
~proxy() noexcept(F::constraints.destructibility == constraint_level::nothrow)
requires(F::constraints.destructibility == constraint_level::nontrivial ||
F::constraints.destructibility == constraint_level::nothrow) {
if (meta_.has_value()) {
meta_->_Traits::destructibility_meta::dispatcher(*ptr_);
}
}
bool has_value() const noexcept { return meta_.has_value(); }
explicit operator bool() const noexcept { return meta_.has_value(); }
void reset()
noexcept(F::constraints.destructibility >= constraint_level::nothrow)
requires(F::constraints.destructibility >= constraint_level::nontrivial)
{ std::destroy_at(this); meta_.reset(); }
void swap(proxy& rhs)
noexcept(F::constraints.relocatability >= constraint_level::nothrow ||
F::constraints.copyability == constraint_level::trivial)
requires(F::constraints.relocatability >= constraint_level::nontrivial ||
F::constraints.copyability == constraint_level::trivial) {
if constexpr (F::constraints.relocatability == constraint_level::trivial ||
F::constraints.copyability == constraint_level::trivial) {
std::swap(meta_, rhs.meta_);
std::swap(ptr_, rhs.ptr);
} else {
if (meta_.has_value()) {
if (rhs.meta_.has_value()) {
proxy temp = std::move(*this);
std::construct_at(this, std::move(rhs));
std::construct_at(&rhs, std::move(temp));
} else {
std::construct_at(&rhs, std::move(*this));
}
} else if (rhs.meta_.has_value()) {
std::construct_at(this, std::move(rhs));
}
}
}
template <proxiable<F> P, class... Args>
P& emplace(Args&&... args)
noexcept(std::is_nothrow_constructible_v<P, Args...> &&
F::constraints.destructibility >= constraint_level::nothrow)
requires(std::is_constructible_v<P, Args...> &&
F::constraints.destructibility >= constraint_level::nontrivial)
{ reset(); return initialize<P>(std::forward<Args>(args)...); }
template <proxiable<F> P, class U, class... Args>
P& emplace(std::initializer_list<U> il, Args&&... args)
noexcept(std::is_nothrow_constructible_v<
P, std::initializer_list<U>&, Args...> &&
F::constraints.destructibility >= constraint_level::nothrow)
requires(std::is_constructible_v<P, std::initializer_list<U>&, Args...> &&
F::constraints.destructibility >= constraint_level::nontrivial)
{ reset(); return initialize<P>(il, std::forward<Args>(args)...); }
auto operator->() noexcept requires(_Traits::has_indirection)
{ return std::addressof(ia_); }
auto operator->() const noexcept requires(_Traits::has_indirection)
{ return std::addressof(ia_); }
auto& operator*() & noexcept requires(_Traits::has_indirection)
{ return ia_; }
auto& operator*() const& noexcept requires(_Traits::has_indirection)
{ return ia_; }
auto&& operator*() && noexcept requires(_Traits::has_indirection)
{ return std::forward<typename _Traits::indirect_accessor>(ia_); }
auto&& operator*() const&& noexcept requires(_Traits::has_indirection)
{ return std::forward<const typename _Traits::indirect_accessor>(ia_); }
friend void swap(proxy& lhs, proxy& rhs) noexcept(noexcept(lhs.swap(rhs)))
{ lhs.swap(rhs); }
friend bool operator==(const proxy& lhs, std::nullptr_t) noexcept
{ return !lhs.has_value(); }
private:
template <class P, class... Args>
P& initialize(Args&&... args) {
P& result = *std::construct_at(
reinterpret_cast<P*>(ptr_), std::forward<Args>(args)...);
if constexpr (requires { (bool)result; })
{ assert((bool)result); }
meta_ = details::meta_ptr<typename _Traits::meta>{std::in_place_type<P>};
return result;
}
[[___PRO_NO_UNIQUE_ADDRESS_ATTRIBUTE]]
typename _Traits::indirect_accessor ia_;
details::meta_ptr<typename _Traits::meta> meta_;
alignas(F::constraints.max_align) std::byte ptr_[F::constraints.max_size];
};
template <class C, class O, class F, class... Args>
decltype(auto) proxy_invoke(proxy<F>& p, Args&&... args) {
return details::proxy_helper<F>::template invoke<
C, O, details::qualifier_type::lv>(p, std::forward<Args>(args)...);
}
template <class C, class O, class F, class... Args>
decltype(auto) proxy_invoke(const proxy<F>& p, Args&&... args) {
return details::proxy_helper<F>::template invoke<
C, O, details::qualifier_type::const_lv>(p, std::forward<Args>(args)...);
}
template <class C, class O, class F, class... Args>
decltype(auto) proxy_invoke(proxy<F>&& p, Args&&... args) {
return details::proxy_helper<F>::template invoke<
C, O, details::qualifier_type::rv>(
std::forward<proxy<F>>(p), std::forward<Args>(args)...);
}
template <class C, class O, class F, class... Args>
decltype(auto) proxy_invoke(const proxy<F>&& p, Args&&... args) {
return details::proxy_helper<F>::template invoke<
C, O, details::qualifier_type::const_rv>(
std::forward<const proxy<F>>(p), std::forward<Args>(args)...);
}
template <class R, class F>
const auto& proxy_reflect(const proxy<F>& p) noexcept {
return static_cast<const details::refl_meta<R>&>(
details::proxy_helper<F>::get_meta(p)).reflector;
}
template <class F, class A>
proxy<F>& access_proxy(A& a) noexcept {
return details::proxy_helper<F>::template access<
A, details::qualifier_type::lv>(a);
}
template <class F, class A>
const proxy<F>& access_proxy(const A& a) noexcept {
return details::proxy_helper<F>::template access<
A, details::qualifier_type::const_lv>(a);
}
template <class F, class A>
proxy<F>&& access_proxy(A&& a) noexcept {
return details::proxy_helper<F>::template access<
A, details::qualifier_type::rv>(std::forward<A>(a));
}
template <class F, class A>
const proxy<F>&& access_proxy(const A&& a) noexcept {
return details::proxy_helper<F>::template access<
A, details::qualifier_type::const_rv>(std::forward<const A>(a));
}
namespace details {
template <class T>
class inplace_ptr {
public:
template <class... Args>
inplace_ptr(Args&&... args)
noexcept(std::is_nothrow_constructible_v<T, Args...>)
requires(std::is_constructible_v<T, Args...>)
: value_(std::forward<Args>(args)...) {}
inplace_ptr(const inplace_ptr&)
noexcept(std::is_nothrow_copy_constructible_v<T>) = default;
inplace_ptr(inplace_ptr&&)
noexcept(std::is_nothrow_move_constructible_v<T>) = default;
T* operator->() noexcept { return &value_; }
const T* operator->() const noexcept { return &value_; }
T& operator*() & noexcept { return value_; }
const T& operator*() const& noexcept { return value_; }
T&& operator*() && noexcept { return std::forward<T>(value_); }
const T&& operator*() const&& noexcept
{ return std::forward<const T>(value_); }
private:
T value_;
};
#if __STDC_HOSTED__
template <class T, class Alloc>
static auto rebind_allocator(const Alloc& alloc) {
return typename std::allocator_traits<Alloc>::template rebind_alloc<T>(alloc);
}
template <class T, class Alloc, class... Args>
static T* allocate(const Alloc& alloc, Args&&... args) {
auto al = rebind_allocator<T>(alloc);
auto deleter = [&](T* ptr) { al.deallocate(ptr, 1); };
std::unique_ptr<T, decltype(deleter)> result{al.allocate(1), deleter};
std::construct_at(result.get(), std::forward<Args>(args)...);
return result.release();
}
template <class Alloc, class T>
static void deallocate(const Alloc& alloc, T* ptr) {
auto al = rebind_allocator<T>(alloc);
std::destroy_at(ptr);
al.deallocate(ptr, 1);
}
template <class T, class Alloc>
class allocated_ptr {
public:
template <class... Args>
allocated_ptr(const Alloc& alloc, Args&&... args)
requires(std::is_constructible_v<T, Args...>)
: alloc_(alloc), ptr_(allocate<T>(alloc, std::forward<Args>(args)...)) {}
allocated_ptr(const allocated_ptr& rhs)
requires(std::is_copy_constructible_v<T>)
: alloc_(rhs.alloc_), ptr_(rhs.ptr_ == nullptr ? nullptr :
allocate<T>(alloc_, std::as_const(*rhs.ptr_))) {}
allocated_ptr(allocated_ptr&& rhs)
noexcept(std::is_nothrow_move_constructible_v<Alloc>)
: alloc_(std::move(rhs.alloc_)), ptr_(std::exchange(rhs.ptr_, nullptr)) {}
~allocated_ptr() { if (ptr_ != nullptr) { deallocate(alloc_, ptr_); } }
T* operator->() noexcept { return ptr_; }
const T* operator->() const noexcept { return ptr_; }
T& operator*() & noexcept { return *ptr_; }
const T& operator*() const& noexcept { return *ptr_; }
T&& operator*() && noexcept { return std::forward<T>(*ptr_); }
const T&& operator*() const&& noexcept
{ return std::forward<const T>(*ptr_); }
private:
[[___PRO_NO_UNIQUE_ADDRESS_ATTRIBUTE]]
Alloc alloc_;
T* ptr_;
};
template <class T, class Alloc>
class compact_ptr {
public:
template <class... Args>
compact_ptr(const Alloc& alloc, Args&&... args)
requires(std::is_constructible_v<T, Args...>)
: ptr_(allocate<storage>(alloc, alloc, std::forward<Args>(args)...)) {}
compact_ptr(const compact_ptr& rhs) requires(std::is_copy_constructible_v<T>)
: ptr_(rhs.ptr_ == nullptr ? nullptr : allocate<storage>(rhs.ptr_->alloc,
rhs.ptr_->alloc, std::as_const(rhs.ptr_->value))) {}
compact_ptr(compact_ptr&& rhs) noexcept