/
shared_ptr.h
2104 lines (1847 loc) · 65.3 KB
/
shared_ptr.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
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___MEMORY_SHARED_PTR_H
#define _LIBCPP___MEMORY_SHARED_PTR_H
#include <__availability>
#include <__compare/compare_three_way.h>
#include <__compare/ordering.h>
#include <__config>
#include <__exception/exception.h>
#include <__functional/binary_function.h>
#include <__functional/operations.h>
#include <__functional/reference_wrapper.h>
#include <__fwd/ostream.h>
#include <__iterator/access.h>
#include <__memory/addressof.h>
#include <__memory/allocation_guard.h>
#include <__memory/allocator.h>
#include <__memory/allocator_destructor.h>
#include <__memory/allocator_traits.h>
#include <__memory/auto_ptr.h>
#include <__memory/compressed_pair.h>
#include <__memory/construct_at.h>
#include <__memory/pointer_traits.h>
#include <__memory/uninitialized_algorithms.h>
#include <__memory/unique_ptr.h>
#include <__type_traits/add_lvalue_reference.h>
#include <__type_traits/conditional.h>
#include <__type_traits/conjunction.h>
#include <__type_traits/disjunction.h>
#include <__type_traits/is_array.h>
#include <__type_traits/is_bounded_array.h>
#include <__type_traits/is_convertible.h>
#include <__type_traits/is_move_constructible.h>
#include <__type_traits/is_reference.h>
#include <__type_traits/is_unbounded_array.h>
#include <__type_traits/nat.h>
#include <__type_traits/negation.h>
#include <__type_traits/remove_extent.h>
#include <__type_traits/remove_reference.h>
#include <__utility/declval.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
#include <__verbose_abort>
#include <cstddef>
#include <new>
#include <typeinfo>
#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
# include <__atomic/memory_order.h>
#endif
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
// should be sufficient for thread safety.
// See https://llvm.org/PR22803
#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
&& defined(__ATOMIC_RELAXED) \
&& defined(__ATOMIC_ACQ_REL)
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
#elif defined(_LIBCPP_COMPILER_GCC)
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
#endif
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
#if !defined(_LIBCPP_HAS_NO_THREADS) && \
defined(__ATOMIC_RELAXED) && \
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
return __atomic_load_n(__value, __ATOMIC_RELAXED);
#else
return *__value;
#endif
}
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_ValueType __libcpp_acquire_load(_ValueType const* __value) {
#if !defined(_LIBCPP_HAS_NO_THREADS) && \
defined(__ATOMIC_ACQUIRE) && \
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
#else
return *__value;
#endif
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _Tp
__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
{
#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
#else
return __t += 1;
#endif
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _Tp
__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
{
#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
#else
return __t -= 1;
#endif
}
class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr
: public std::exception
{
public:
_LIBCPP_HIDE_FROM_ABI bad_weak_ptr() _NOEXCEPT = default;
_LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
_LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default;
~bad_weak_ptr() _NOEXCEPT override;
const char* what() const _NOEXCEPT override;
};
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
void __throw_bad_weak_ptr()
{
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
throw bad_weak_ptr();
#else
_LIBCPP_VERBOSE_ABORT("bad_weak_ptr was thrown in -fno-exceptions mode");
#endif
}
template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
class _LIBCPP_EXPORTED_FROM_ABI __shared_count
{
__shared_count(const __shared_count&);
__shared_count& operator=(const __shared_count&);
protected:
long __shared_owners_;
virtual ~__shared_count();
private:
virtual void __on_zero_shared() _NOEXCEPT = 0;
public:
_LIBCPP_INLINE_VISIBILITY
explicit __shared_count(long __refs = 0) _NOEXCEPT
: __shared_owners_(__refs) {}
#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
void __add_shared() noexcept;
bool __release_shared() noexcept;
#else
_LIBCPP_INLINE_VISIBILITY
void __add_shared() _NOEXCEPT {
__libcpp_atomic_refcount_increment(__shared_owners_);
}
_LIBCPP_INLINE_VISIBILITY
bool __release_shared() _NOEXCEPT {
if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
__on_zero_shared();
return true;
}
return false;
}
#endif
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT {
return __libcpp_relaxed_load(&__shared_owners_) + 1;
}
};
class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count
: private __shared_count
{
long __shared_weak_owners_;
public:
_LIBCPP_INLINE_VISIBILITY
explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
: __shared_count(__refs),
__shared_weak_owners_(__refs) {}
protected:
~__shared_weak_count() override;
public:
#if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
void __add_shared() noexcept;
void __add_weak() noexcept;
void __release_shared() noexcept;
#else
_LIBCPP_INLINE_VISIBILITY
void __add_shared() _NOEXCEPT {
__shared_count::__add_shared();
}
_LIBCPP_INLINE_VISIBILITY
void __add_weak() _NOEXCEPT {
__libcpp_atomic_refcount_increment(__shared_weak_owners_);
}
_LIBCPP_INLINE_VISIBILITY
void __release_shared() _NOEXCEPT {
if (__shared_count::__release_shared())
__release_weak();
}
#endif
void __release_weak() _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT {return __shared_count::use_count();}
__shared_weak_count* lock() _NOEXCEPT;
virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
private:
virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
};
template <class _Tp, class _Dp, class _Alloc>
class __shared_ptr_pointer
: public __shared_weak_count
{
__compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
public:
_LIBCPP_INLINE_VISIBILITY
__shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
: __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
#ifndef _LIBCPP_HAS_NO_RTTI
_LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* __get_deleter(const type_info&) const _NOEXCEPT override;
#endif
private:
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override;
};
#ifndef _LIBCPP_HAS_NO_RTTI
template <class _Tp, class _Dp, class _Alloc>
const void*
__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
{
return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
}
#endif // _LIBCPP_HAS_NO_RTTI
template <class _Tp, class _Dp, class _Alloc>
void
__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
{
__data_.first().second()(__data_.first().first());
__data_.first().second().~_Dp();
}
template <class _Tp, class _Dp, class _Alloc>
void
__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
{
typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
typedef allocator_traits<_Al> _ATraits;
typedef pointer_traits<typename _ATraits::pointer> _PTraits;
_Al __a(__data_.second());
__data_.second().~_Alloc();
__a.deallocate(_PTraits::pointer_to(*this), 1);
}
// This tag is used to instantiate an allocator type. The various shared_ptr control blocks
// detect that the allocator has been instantiated for this type and perform alternative
// initialization/destruction based on that.
struct __for_overwrite_tag {};
template <class _Tp, class _Alloc>
struct __shared_ptr_emplace
: __shared_weak_count
{
template <class... _Args, class _Allocator = _Alloc, __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI
explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...)
: __storage_(_VSTD::move(__a))
{
static_assert(sizeof...(_Args) == 0, "No argument should be provided to the control block when using _for_overwrite");
::new ((void*)__get_elem()) _Tp;
}
template <class... _Args, class _Allocator = _Alloc, __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI
explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
: __storage_(_VSTD::move(__a))
{
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
_TpAlloc __tmp(*__get_alloc());
allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
}
_LIBCPP_HIDE_FROM_ABI
_Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
_LIBCPP_HIDE_FROM_ABI
_Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
private:
template <class _Allocator = _Alloc, __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {
__get_elem()->~_Tp();
}
template <class _Allocator = _Alloc, __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {
using _TpAlloc = typename __allocator_traits_rebind<_Allocator, _Tp>::type;
_TpAlloc __tmp(*__get_alloc());
allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
}
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {
__on_zero_shared_impl();
}
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
_ControlBlockAlloc __tmp(*__get_alloc());
__storage_.~_Storage();
allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
}
// This class implements the control block for non-array shared pointers created
// through `std::allocate_shared` and `std::make_shared`.
//
// In previous versions of the library, we used a compressed pair to store
// both the _Alloc and the _Tp. This implies using EBO, which is incompatible
// with Allocator construction for _Tp. To allow implementing P0674 in C++20,
// we now use a properly aligned char buffer while making sure that we maintain
// the same layout that we had when we used a compressed pair.
using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
char __blob_[sizeof(_CompressedPair)];
_LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
}
_LIBCPP_HIDE_FROM_ABI ~_Storage() {
__get_alloc()->~_Alloc();
}
_LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT {
_CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
_Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
return __alloc;
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
_CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
_Tp *__elem = reinterpret_cast<_Tp*>(__second);
return __elem;
}
};
static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
_Storage __storage_;
};
struct __shared_ptr_dummy_rebind_allocator_type;
template <>
class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
{
public:
template <class _Other>
struct rebind
{
typedef allocator<_Other> other;
};
};
template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
// http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.general-6
// A pointer type Y* is said to be compatible with a pointer type T*
// when either Y* is convertible to T* or Y is U[N] and T is cv U[].
#if _LIBCPP_STD_VER >= 17
template <class _Yp, class _Tp>
struct __bounded_convertible_to_unbounded : false_type {};
template <class _Up, std::size_t _Np, class _Tp>
struct __bounded_convertible_to_unbounded<_Up[_Np], _Tp>
: is_same<__remove_cv_t<_Tp>, _Up[]> {};
template <class _Yp, class _Tp>
struct __compatible_with
: _Or<
is_convertible<_Yp*, _Tp*>,
__bounded_convertible_to_unbounded<_Yp, _Tp>
> {};
#else
template <class _Yp, class _Tp>
struct __compatible_with
: is_convertible<_Yp*, _Tp*> {};
#endif // _LIBCPP_STD_VER >= 17
// Constructors that take raw pointers have a different set of "compatible" constraints
// http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.const-9.1
// - If T is an array type, then either T is U[N] and Y(*)[N] is convertible to T*,
// or T is U[] and Y(*)[] is convertible to T*.
// - If T is not an array type, then Y* is convertible to T*.
#if _LIBCPP_STD_VER >= 17
template <class _Yp, class _Tp, class = void>
struct __raw_pointer_compatible_with : _And<
_Not<is_array<_Tp>>,
is_convertible<_Yp*, _Tp*>
> {};
template <class _Yp, class _Up, std::size_t _Np>
struct __raw_pointer_compatible_with<_Yp, _Up[_Np], __enable_if_t<
is_convertible<_Yp(*)[_Np], _Up(*)[_Np]>::value> >
: true_type {};
template <class _Yp, class _Up>
struct __raw_pointer_compatible_with<_Yp, _Up[], __enable_if_t<
is_convertible<_Yp(*)[], _Up(*)[]>::value> >
: true_type {};
#else
template <class _Yp, class _Tp>
struct __raw_pointer_compatible_with
: is_convertible<_Yp*, _Tp*> {};
#endif // _LIBCPP_STD_VER >= 17
template <class _Ptr, class = void>
struct __is_deletable : false_type { };
template <class _Ptr>
struct __is_deletable<_Ptr, decltype(delete std::declval<_Ptr>())> : true_type { };
template <class _Ptr, class = void>
struct __is_array_deletable : false_type { };
template <class _Ptr>
struct __is_array_deletable<_Ptr, decltype(delete[] std::declval<_Ptr>())> : true_type { };
template <class _Dp, class _Pt,
class = decltype(std::declval<_Dp>()(std::declval<_Pt>()))>
true_type __well_formed_deleter_test(int);
template <class, class>
false_type __well_formed_deleter_test(...);
template <class _Dp, class _Pt>
struct __well_formed_deleter : decltype(std::__well_formed_deleter_test<_Dp, _Pt>(0)) {};
template<class _Dp, class _Yp, class _Tp>
struct __shared_ptr_deleter_ctor_reqs
{
static const bool value = __raw_pointer_compatible_with<_Yp, _Tp>::value &&
is_move_constructible<_Dp>::value &&
__well_formed_deleter<_Dp, _Yp*>::value;
};
#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))
#else
# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
#endif
template<class _Tp>
class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
{
public:
#if _LIBCPP_STD_VER >= 17
typedef weak_ptr<_Tp> weak_type;
typedef remove_extent_t<_Tp> element_type;
#else
typedef _Tp element_type;
#endif
private:
element_type* __ptr_;
__shared_weak_count* __cntrl_;
public:
_LIBCPP_HIDE_FROM_ABI
_LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT
: __ptr_(nullptr),
__cntrl_(nullptr)
{ }
_LIBCPP_HIDE_FROM_ABI
_LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT
: __ptr_(nullptr),
__cntrl_(nullptr)
{ }
template<class _Yp, class = __enable_if_t<
_And<
__raw_pointer_compatible_with<_Yp, _Tp>
// In C++03 we get errors when trying to do SFINAE with the
// delete operator, so we always pretend that it's deletable.
// The same happens on GCC.
#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)
, _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >
#endif
>::value
> >
_LIBCPP_HIDE_FROM_ABI explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
unique_ptr<_Yp> __hold(__p);
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk;
__cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
__hold.release();
__enable_weak_this(__p, __p);
}
template<class _Yp, class _Dp, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(_Yp* __p, _Dp __d)
: __ptr_(__p)
{
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
#ifndef _LIBCPP_CXX03_LANG
__cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
#else
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
#endif // not _LIBCPP_CXX03_LANG
__enable_weak_this(__p, __p);
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
}
template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
: __ptr_(__p)
{
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
typedef __allocator_destructor<_A2> _D2;
_A2 __a2(__a);
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
::new ((void*)_VSTD::addressof(*__hold2.get()))
#ifndef _LIBCPP_CXX03_LANG
_CntrlBlk(__p, _VSTD::move(__d), __a);
#else
_CntrlBlk(__p, __d, __a);
#endif // not _LIBCPP_CXX03_LANG
__cntrl_ = _VSTD::addressof(*__hold2.release());
__enable_weak_this(__p, __p);
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
}
template<class _Dp>
_LIBCPP_HIDE_FROM_ABI
shared_ptr(nullptr_t __p, _Dp __d)
: __ptr_(nullptr)
{
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
#ifndef _LIBCPP_CXX03_LANG
__cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
#else
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
#endif // not _LIBCPP_CXX03_LANG
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
}
template<class _Dp, class _Alloc>
_LIBCPP_HIDE_FROM_ABI
shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
: __ptr_(nullptr)
{
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
typedef __allocator_destructor<_A2> _D2;
_A2 __a2(__a);
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
::new ((void*)_VSTD::addressof(*__hold2.get()))
#ifndef _LIBCPP_CXX03_LANG
_CntrlBlk(__p, _VSTD::move(__d), __a);
#else
_CntrlBlk(__p, __d, __a);
#endif // not _LIBCPP_CXX03_LANG
__cntrl_ = _VSTD::addressof(*__hold2.release());
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
}
template<class _Yp>
_LIBCPP_HIDE_FROM_ABI
shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
: __ptr_(__p),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_shared();
}
// LWG-2996
// We don't backport because it is an evolutionary change.
#if _LIBCPP_STD_VER >= 20
template <class _Yp>
_LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept
: __ptr_(__p),
__cntrl_(__r.__cntrl_) {
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
}
#endif
_LIBCPP_HIDE_FROM_ABI
shared_ptr(const shared_ptr& __r) _NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_shared();
}
template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_shared();
}
_LIBCPP_HIDE_FROM_ABI
shared_ptr(shared_ptr&& __r) _NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
}
template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
}
template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
explicit shared_ptr(const weak_ptr<_Yp>& __r)
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
{
if (__cntrl_ == nullptr)
__throw_bad_weak_ptr();
}
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
template<class _Yp, class = __enable_if_t<is_convertible<_Yp*, element_type*>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(auto_ptr<_Yp>&& __r)
: __ptr_(__r.get())
{
typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
__enable_weak_this(__r.get(), __r.get());
__r.release();
}
#endif
template <class _Yp, class _Dp, class = __enable_if_t<
!is_lvalue_reference<_Dp>::value &&
__compatible_with<_Yp, _Tp>::value &&
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
: __ptr_(__r.get())
{
#if _LIBCPP_STD_VER >= 14
if (__ptr_ == nullptr)
__cntrl_ = nullptr;
else
#endif
{
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());
__enable_weak_this(__r.get(), __r.get());
}
__r.release();
}
template <class _Yp, class _Dp, class = void, class = __enable_if_t<
is_lvalue_reference<_Dp>::value &&
__compatible_with<_Yp, _Tp>::value &&
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
: __ptr_(__r.get())
{
#if _LIBCPP_STD_VER >= 14
if (__ptr_ == nullptr)
__cntrl_ = nullptr;
else
#endif
{
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
reference_wrapper<__libcpp_remove_reference_t<_Dp> >,
_AllocT> _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
__enable_weak_this(__r.get(), __r.get());
}
__r.release();
}
_LIBCPP_HIDE_FROM_ABI
~shared_ptr()
{
if (__cntrl_)
__cntrl_->__release_shared();
}
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT
{
shared_ptr(__r).swap(*this);
return *this;
}
template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
{
shared_ptr(__r).swap(*this);
return *this;
}
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT
{
shared_ptr(_VSTD::move(__r)).swap(*this);
return *this;
}
template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r)
{
shared_ptr(_VSTD::move(__r)).swap(*this);
return *this;
}
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
template<class _Yp, class = __enable_if_t<
!is_array<_Yp>::value &&
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value
> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r)
{
shared_ptr(_VSTD::move(__r)).swap(*this);
return *this;
}
#endif
template <class _Yp, class _Dp, class = __enable_if_t<_And<
__compatible_with<_Yp, _Tp>,
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>
>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r)
{
shared_ptr(_VSTD::move(__r)).swap(*this);
return *this;
}
_LIBCPP_HIDE_FROM_ABI
void swap(shared_ptr& __r) _NOEXCEPT
{
_VSTD::swap(__ptr_, __r.__ptr_);
_VSTD::swap(__cntrl_, __r.__cntrl_);
}
_LIBCPP_HIDE_FROM_ABI
void reset() _NOEXCEPT
{
shared_ptr().swap(*this);
}
template<class _Yp, class = __enable_if_t<
__raw_pointer_compatible_with<_Yp, _Tp>::value
> >
_LIBCPP_HIDE_FROM_ABI
void reset(_Yp* __p)
{
shared_ptr(__p).swap(*this);
}
template<class _Yp, class _Dp, class = __enable_if_t<
__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
void reset(_Yp* __p, _Dp __d)
{
shared_ptr(__p, __d).swap(*this);
}
template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<
__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
void reset(_Yp* __p, _Dp __d, _Alloc __a)
{
shared_ptr(__p, __d, __a).swap(*this);
}
_LIBCPP_HIDE_FROM_ABI
element_type* get() const _NOEXCEPT
{
return __ptr_;
}
_LIBCPP_HIDE_FROM_ABI
__add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT
{
return *__ptr_;
}
_LIBCPP_HIDE_FROM_ABI
element_type* operator->() const _NOEXCEPT
{
static_assert(!is_array<_Tp>::value,
"std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
return __ptr_;
}
_LIBCPP_HIDE_FROM_ABI
long use_count() const _NOEXCEPT
{
return __cntrl_ ? __cntrl_->use_count() : 0;
}
_LIBCPP_HIDE_FROM_ABI
bool unique() const _NOEXCEPT
{
return use_count() == 1;
}
_LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{
return get() != nullptr;
}
template <class _Up>
_LIBCPP_HIDE_FROM_ABI
bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
{
return __cntrl_ < __p.__cntrl_;
}
template <class _Up>
_LIBCPP_HIDE_FROM_ABI
bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
{
return __cntrl_ < __p.__cntrl_;
}
_LIBCPP_HIDE_FROM_ABI
bool __owner_equivalent(const shared_ptr& __p) const
{
return __cntrl_ == __p.__cntrl_;
}
#if _LIBCPP_STD_VER >= 17
_LIBCPP_HIDE_FROM_ABI
__add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const
{
static_assert(is_array<_Tp>::value,
"std::shared_ptr<T>::operator[] is only valid when T is an array type.");
return __ptr_[__i];
}
#endif
#ifndef _LIBCPP_HAS_NO_RTTI
template <class _Dp>
_LIBCPP_HIDE_FROM_ABI
_Dp* __get_deleter() const _NOEXCEPT
{
return static_cast<_Dp*>(__cntrl_
? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
: nullptr);
}
#endif // _LIBCPP_HAS_NO_RTTI
template<class _Yp, class _CntrlBlk>
_LIBCPP_HIDE_FROM_ABI
static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
{
shared_ptr<_Tp> __r;
__r.__ptr_ = __p;
__r.__cntrl_ = __cntrl;
__r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
return __r;
}
private:
template <class _Yp, bool = is_function<_Yp>::value>
struct __shared_ptr_default_allocator
{
typedef allocator<_Yp> type;
};
template <class _Yp>
struct __shared_ptr_default_allocator<_Yp, true>
{
typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
};
template <class _Yp, class _OrigPtr, class = __enable_if_t<
is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value
> >
_LIBCPP_HIDE_FROM_ABI
void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT
{
typedef __remove_cv_t<_Yp> _RawYp;
if (__e && __e->__weak_this_.expired())
{
__e->__weak_this_ = shared_ptr<_RawYp>(*this,
const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
}
}
_LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT { }
template <class, class _Yp>
struct __shared_ptr_default_delete
: default_delete<_Yp>
{ };
template <class _Yp, class _Un, size_t _Sz>
struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
: default_delete<_Yp[]>
{ };
template <class _Yp, class _Un>
struct __shared_ptr_default_delete<_Yp[], _Un>