File indexing completed on 2026-05-03 08:13:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___MEMORY_SHARED_PTR_H
0011 #define _LIBCPP___MEMORY_SHARED_PTR_H
0012
0013 #include <__compare/compare_three_way.h>
0014 #include <__compare/ordering.h>
0015 #include <__config>
0016 #include <__cstddef/nullptr_t.h>
0017 #include <__cstddef/ptrdiff_t.h>
0018 #include <__exception/exception.h>
0019 #include <__functional/binary_function.h>
0020 #include <__functional/operations.h>
0021 #include <__functional/reference_wrapper.h>
0022 #include <__fwd/ostream.h>
0023 #include <__iterator/access.h>
0024 #include <__memory/addressof.h>
0025 #include <__memory/allocation_guard.h>
0026 #include <__memory/allocator.h>
0027 #include <__memory/allocator_destructor.h>
0028 #include <__memory/allocator_traits.h>
0029 #include <__memory/auto_ptr.h>
0030 #include <__memory/compressed_pair.h>
0031 #include <__memory/construct_at.h>
0032 #include <__memory/pointer_traits.h>
0033 #include <__memory/shared_count.h>
0034 #include <__memory/uninitialized_algorithms.h>
0035 #include <__memory/unique_ptr.h>
0036 #include <__type_traits/add_lvalue_reference.h>
0037 #include <__type_traits/conditional.h>
0038 #include <__type_traits/conjunction.h>
0039 #include <__type_traits/disjunction.h>
0040 #include <__type_traits/enable_if.h>
0041 #include <__type_traits/integral_constant.h>
0042 #include <__type_traits/is_array.h>
0043 #include <__type_traits/is_bounded_array.h>
0044 #include <__type_traits/is_constructible.h>
0045 #include <__type_traits/is_convertible.h>
0046 #include <__type_traits/is_function.h>
0047 #include <__type_traits/is_reference.h>
0048 #include <__type_traits/is_same.h>
0049 #include <__type_traits/is_unbounded_array.h>
0050 #include <__type_traits/nat.h>
0051 #include <__type_traits/negation.h>
0052 #include <__type_traits/remove_cv.h>
0053 #include <__type_traits/remove_extent.h>
0054 #include <__type_traits/remove_reference.h>
0055 #include <__utility/declval.h>
0056 #include <__utility/forward.h>
0057 #include <__utility/move.h>
0058 #include <__utility/swap.h>
0059 #include <__verbose_abort>
0060 #include <typeinfo>
0061 #if _LIBCPP_HAS_ATOMIC_HEADER
0062 # include <__atomic/memory_order.h>
0063 #endif
0064
0065 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0066 # pragma GCC system_header
0067 #endif
0068
0069 _LIBCPP_PUSH_MACROS
0070 #include <__undef_macros>
0071
0072 _LIBCPP_BEGIN_NAMESPACE_STD
0073
0074 class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception {
0075 public:
0076 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr() _NOEXCEPT = default;
0077 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
0078 _LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default;
0079 ~bad_weak_ptr() _NOEXCEPT override;
0080 const char* what() const _NOEXCEPT override;
0081 };
0082
0083 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_weak_ptr() {
0084 #if _LIBCPP_HAS_EXCEPTIONS
0085 throw bad_weak_ptr();
0086 #else
0087 _LIBCPP_VERBOSE_ABORT("bad_weak_ptr was thrown in -fno-exceptions mode");
0088 #endif
0089 }
0090
0091 template <class _Tp>
0092 class _LIBCPP_TEMPLATE_VIS weak_ptr;
0093
0094 template <class _Tp, class _Dp, class _Alloc>
0095 class __shared_ptr_pointer : public __shared_weak_count {
0096 _LIBCPP_COMPRESSED_TRIPLE(_Tp, __ptr_, _Dp, __deleter_, _Alloc, __alloc_);
0097
0098 public:
0099 _LIBCPP_HIDE_FROM_ABI __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
0100 : __ptr_(__p), __deleter_(std::move(__d)), __alloc_(std::move(__a)) {}
0101
0102 #if _LIBCPP_HAS_RTTI
0103 _LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* __get_deleter(const type_info&) const _NOEXCEPT override;
0104 #endif
0105
0106 private:
0107 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
0108 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override;
0109 };
0110
0111 #if _LIBCPP_HAS_RTTI
0112
0113 template <class _Tp, class _Dp, class _Alloc>
0114 const void* __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT {
0115 return __t == typeid(_Dp) ? std::addressof(__deleter_) : nullptr;
0116 }
0117
0118 #endif
0119
0120 template <class _Tp, class _Dp, class _Alloc>
0121 void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT {
0122 __deleter_(__ptr_);
0123 __deleter_.~_Dp();
0124 }
0125
0126 template <class _Tp, class _Dp, class _Alloc>
0127 void __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT {
0128 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
0129 typedef allocator_traits<_Al> _ATraits;
0130 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
0131
0132 _Al __a(__alloc_);
0133 __alloc_.~_Alloc();
0134 __a.deallocate(_PTraits::pointer_to(*this), 1);
0135 }
0136
0137
0138
0139
0140 struct __for_overwrite_tag {};
0141
0142 template <class _Tp, class _Alloc>
0143 struct __shared_ptr_emplace : __shared_weak_count {
0144 using __value_type _LIBCPP_NODEBUG = __remove_cv_t<_Tp>;
0145
0146 template <class... _Args,
0147 class _Allocator = _Alloc,
0148 __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
0149 _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&...) : __storage_(std::move(__a)) {
0150 static_assert(
0151 sizeof...(_Args) == 0, "No argument should be provided to the control block when using _for_overwrite");
0152 ::new (static_cast<void*>(__get_elem())) __value_type;
0153 }
0154
0155 template <class... _Args,
0156 class _Allocator = _Alloc,
0157 __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
0158 _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&... __args) : __storage_(std::move(__a)) {
0159 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, __value_type>::type;
0160 _TpAlloc __tmp(*__get_alloc());
0161 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), std::forward<_Args>(__args)...);
0162 }
0163
0164 _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
0165
0166 _LIBCPP_HIDE_FROM_ABI __value_type* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
0167
0168 private:
0169 template <class _Allocator = _Alloc,
0170 __enable_if_t<is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
0171 _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {
0172 __get_elem()->~__value_type();
0173 }
0174
0175 template <class _Allocator = _Alloc,
0176 __enable_if_t<!is_same<typename _Allocator::value_type, __for_overwrite_tag>::value, int> = 0>
0177 _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT {
0178 using _TpAlloc = typename __allocator_traits_rebind<_Allocator, __remove_cv_t<_Tp> >::type;
0179 _TpAlloc __tmp(*__get_alloc());
0180 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
0181 }
0182
0183 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { __on_zero_shared_impl(); }
0184
0185 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
0186 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
0187 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
0188 _ControlBlockAlloc __tmp(*__get_alloc());
0189 __storage_.~_Storage();
0190 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
0191 }
0192
0193
0194
0195
0196 struct _Storage {
0197 struct _Data {
0198 _LIBCPP_COMPRESSED_PAIR(_Alloc, __alloc_, __value_type, __elem_);
0199 };
0200
0201 _ALIGNAS_TYPE(_Data) char __buffer_[sizeof(_Data)];
0202
0203 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { ::new ((void*)__get_alloc()) _Alloc(std::move(__a)); }
0204 _LIBCPP_HIDE_FROM_ABI ~_Storage() { __get_alloc()->~_Alloc(); }
0205
0206 _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT {
0207 return std::addressof(reinterpret_cast<_Data*>(__buffer_)->__alloc_);
0208 }
0209
0210 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __value_type* __get_elem() _NOEXCEPT {
0211 return std::addressof(reinterpret_cast<_Data*>(__buffer_)->__elem_);
0212 }
0213 };
0214
0215 _Storage __storage_;
0216 };
0217
0218 struct __shared_ptr_dummy_rebind_allocator_type;
0219 template <>
0220 class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> {
0221 public:
0222 template <class _Other>
0223 struct rebind {
0224 typedef allocator<_Other> other;
0225 };
0226 };
0227
0228 template <class _Tp>
0229 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
0230
0231
0232
0233
0234 #if _LIBCPP_STD_VER >= 17
0235 template <class _Yp, class _Tp>
0236 struct __bounded_convertible_to_unbounded : false_type {};
0237
0238 template <class _Up, std::size_t _Np, class _Tp>
0239 struct __bounded_convertible_to_unbounded<_Up[_Np], _Tp> : is_same<__remove_cv_t<_Tp>, _Up[]> {};
0240
0241 template <class _Yp, class _Tp>
0242 struct __compatible_with : _Or< is_convertible<_Yp*, _Tp*>, __bounded_convertible_to_unbounded<_Yp, _Tp> > {};
0243 #else
0244 template <class _Yp, class _Tp>
0245 struct __compatible_with : is_convertible<_Yp*, _Tp*> {};
0246 #endif
0247
0248
0249
0250
0251
0252
0253 #if _LIBCPP_STD_VER >= 17
0254 template <class _Yp, class _Tp, class = void>
0255 struct __raw_pointer_compatible_with : _And< _Not<is_array<_Tp>>, is_convertible<_Yp*, _Tp*> > {};
0256
0257 template <class _Yp, class _Up, std::size_t _Np>
0258 struct __raw_pointer_compatible_with<_Yp, _Up[_Np], __enable_if_t< is_convertible<_Yp (*)[_Np], _Up (*)[_Np]>::value> >
0259 : true_type {};
0260
0261 template <class _Yp, class _Up>
0262 struct __raw_pointer_compatible_with<_Yp, _Up[], __enable_if_t< is_convertible<_Yp (*)[], _Up (*)[]>::value> >
0263 : true_type {};
0264
0265 #else
0266 template <class _Yp, class _Tp>
0267 struct __raw_pointer_compatible_with : is_convertible<_Yp*, _Tp*> {};
0268 #endif
0269
0270 template <class _Ptr, class = void>
0271 struct __is_deletable : false_type {};
0272 template <class _Ptr>
0273 struct __is_deletable<_Ptr, decltype(delete std::declval<_Ptr>())> : true_type {};
0274
0275 template <class _Ptr, class = void>
0276 struct __is_array_deletable : false_type {};
0277 template <class _Ptr>
0278 struct __is_array_deletable<_Ptr, decltype(delete[] std::declval<_Ptr>())> : true_type {};
0279
0280 template <class _Dp, class _Pt, class = decltype(std::declval<_Dp>()(std::declval<_Pt>()))>
0281 true_type __well_formed_deleter_test(int);
0282
0283 template <class, class>
0284 false_type __well_formed_deleter_test(...);
0285
0286 template <class _Dp, class _Pt>
0287 struct __well_formed_deleter : decltype(std::__well_formed_deleter_test<_Dp, _Pt>(0)) {};
0288
0289 template <class _Dp, class _Yp, class _Tp>
0290 struct __shared_ptr_deleter_ctor_reqs {
0291 static const bool value = __raw_pointer_compatible_with<_Yp, _Tp>::value && is_move_constructible<_Dp>::value &&
0292 __well_formed_deleter<_Dp, _Yp*>::value;
0293 };
0294
0295 template <class _Dp>
0296 using __shared_ptr_nullptr_deleter_ctor_reqs _LIBCPP_NODEBUG =
0297 _And<is_move_constructible<_Dp>, __well_formed_deleter<_Dp, nullptr_t> >;
0298
0299 #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
0300 # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))
0301 #else
0302 # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
0303 #endif
0304
0305 template <class _Tp>
0306 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
0307 struct __nullptr_sfinae_tag {};
0308
0309 public:
0310 #if _LIBCPP_STD_VER >= 17
0311 typedef weak_ptr<_Tp> weak_type;
0312 typedef remove_extent_t<_Tp> element_type;
0313 #else
0314 typedef _Tp element_type;
0315 #endif
0316
0317
0318
0319 using __trivially_relocatable _LIBCPP_NODEBUG = shared_ptr;
0320
0321 private:
0322 element_type* __ptr_;
0323 __shared_weak_count* __cntrl_;
0324
0325 public:
0326 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
0327
0328 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
0329
0330 template <class _Yp,
0331 __enable_if_t< _And< __raw_pointer_compatible_with<_Yp, _Tp>
0332
0333
0334
0335 #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)
0336 ,
0337 _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >
0338 #endif
0339 >::value,
0340 int> = 0>
0341 _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
0342 unique_ptr<_Yp> __hold(__p);
0343 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
0344 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk;
0345 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
0346 __hold.release();
0347 __enable_weak_this(__p, __p);
0348 }
0349
0350 template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
0351 _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) {
0352 #if _LIBCPP_HAS_EXCEPTIONS
0353 try {
0354 #endif
0355 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
0356 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
0357 #ifndef _LIBCPP_CXX03_LANG
0358 __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
0359 #else
0360 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
0361 #endif
0362 __enable_weak_this(__p, __p);
0363 #if _LIBCPP_HAS_EXCEPTIONS
0364 } catch (...) {
0365 __d(__p);
0366 throw;
0367 }
0368 #endif
0369 }
0370
0371 template <class _Yp,
0372 class _Dp,
0373 class _Alloc,
0374 __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
0375 _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {
0376 #if _LIBCPP_HAS_EXCEPTIONS
0377 try {
0378 #endif
0379 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
0380 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
0381 typedef __allocator_destructor<_A2> _D2;
0382 _A2 __a2(__a);
0383 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
0384 ::new ((void*)std::addressof(*__hold2.get()))
0385 #ifndef _LIBCPP_CXX03_LANG
0386 _CntrlBlk(__p, std::move(__d), __a);
0387 #else
0388 _CntrlBlk(__p, __d, __a);
0389 #endif
0390 __cntrl_ = std::addressof(*__hold2.release());
0391 __enable_weak_this(__p, __p);
0392 #if _LIBCPP_HAS_EXCEPTIONS
0393 } catch (...) {
0394 __d(__p);
0395 throw;
0396 }
0397 #endif
0398 }
0399
0400 template <class _Dp>
0401 _LIBCPP_HIDE_FROM_ABI shared_ptr(
0402 nullptr_t __p,
0403 _Dp __d,
0404 __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
0405 : __ptr_(nullptr) {
0406 #if _LIBCPP_HAS_EXCEPTIONS
0407 try {
0408 #endif
0409 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
0410 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
0411 #ifndef _LIBCPP_CXX03_LANG
0412 __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT());
0413 #else
0414 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
0415 #endif
0416 #if _LIBCPP_HAS_EXCEPTIONS
0417 } catch (...) {
0418 __d(__p);
0419 throw;
0420 }
0421 #endif
0422 }
0423
0424 template <class _Dp, class _Alloc>
0425 _LIBCPP_HIDE_FROM_ABI shared_ptr(
0426 nullptr_t __p,
0427 _Dp __d,
0428 _Alloc __a,
0429 __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
0430 : __ptr_(nullptr) {
0431 #if _LIBCPP_HAS_EXCEPTIONS
0432 try {
0433 #endif
0434 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
0435 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
0436 typedef __allocator_destructor<_A2> _D2;
0437 _A2 __a2(__a);
0438 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
0439 ::new ((void*)std::addressof(*__hold2.get()))
0440 #ifndef _LIBCPP_CXX03_LANG
0441 _CntrlBlk(__p, std::move(__d), __a);
0442 #else
0443 _CntrlBlk(__p, __d, __a);
0444 #endif
0445 __cntrl_ = std::addressof(*__hold2.release());
0446 #if _LIBCPP_HAS_EXCEPTIONS
0447 } catch (...) {
0448 __d(__p);
0449 throw;
0450 }
0451 #endif
0452 }
0453
0454 template <class _Yp>
0455 _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT
0456 : __ptr_(__p),
0457 __cntrl_(__r.__cntrl_) {
0458 if (__cntrl_)
0459 __cntrl_->__add_shared();
0460 }
0461
0462
0463
0464 #if _LIBCPP_STD_VER >= 20
0465 template <class _Yp>
0466 _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept
0467 : __ptr_(__p), __cntrl_(__r.__cntrl_) {
0468 __r.__ptr_ = nullptr;
0469 __r.__cntrl_ = nullptr;
0470 }
0471 #endif
0472
0473 _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
0474 if (__cntrl_)
0475 __cntrl_->__add_shared();
0476 }
0477
0478 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
0479 _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
0480 if (__cntrl_)
0481 __cntrl_->__add_shared();
0482 }
0483
0484 _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
0485 __r.__ptr_ = nullptr;
0486 __r.__cntrl_ = nullptr;
0487 }
0488
0489 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
0490 _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
0491 __r.__ptr_ = nullptr;
0492 __r.__cntrl_ = nullptr;
0493 }
0494
0495 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
0496 _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(const weak_ptr<_Yp>& __r)
0497 : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) {
0498 if (__cntrl_ == nullptr)
0499 __throw_bad_weak_ptr();
0500 }
0501
0502 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
0503 template <class _Yp, __enable_if_t<is_convertible<_Yp*, element_type*>::value, int> = 0>
0504 _LIBCPP_HIDE_FROM_ABI shared_ptr(auto_ptr<_Yp>&& __r) : __ptr_(__r.get()) {
0505 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<__remove_cv_t<_Yp> > > _CntrlBlk;
0506 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<__remove_cv_t<_Yp> >());
0507 __enable_weak_this(__r.get(), __r.get());
0508 __r.release();
0509 }
0510 #endif
0511
0512 template <class _Yp,
0513 class _Dp,
0514 __enable_if_t<!is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&
0515 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
0516 int> = 0>
0517 _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {
0518 #if _LIBCPP_STD_VER >= 14
0519 if (__ptr_ == nullptr)
0520 __cntrl_ = nullptr;
0521 else
0522 #endif
0523 {
0524 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
0525 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk;
0526 __cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());
0527 __enable_weak_this(__r.get(), __r.get());
0528 }
0529 __r.release();
0530 }
0531
0532 template <class _Yp,
0533 class _Dp,
0534 class = void,
0535 __enable_if_t<is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&
0536 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
0537 int> = 0>
0538 _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {
0539 #if _LIBCPP_STD_VER >= 14
0540 if (__ptr_ == nullptr)
0541 __cntrl_ = nullptr;
0542 else
0543 #endif
0544 {
0545 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
0546 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
0547 reference_wrapper<__libcpp_remove_reference_t<_Dp> >,
0548 _AllocT>
0549 _CntrlBlk;
0550 __cntrl_ = new _CntrlBlk(__r.get(), std::ref(__r.get_deleter()), _AllocT());
0551 __enable_weak_this(__r.get(), __r.get());
0552 }
0553 __r.release();
0554 }
0555
0556 _LIBCPP_HIDE_FROM_ABI ~shared_ptr() {
0557 if (__cntrl_)
0558 __cntrl_->__release_shared();
0559 }
0560
0561 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT {
0562 shared_ptr(__r).swap(*this);
0563 return *this;
0564 }
0565
0566 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
0567 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT {
0568 shared_ptr(__r).swap(*this);
0569 return *this;
0570 }
0571
0572 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT {
0573 shared_ptr(std::move(__r)).swap(*this);
0574 return *this;
0575 }
0576
0577 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
0578 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r) {
0579 shared_ptr(std::move(__r)).swap(*this);
0580 return *this;
0581 }
0582
0583 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
0584 template <class _Yp,
0585 __enable_if_t<!is_array<_Yp>::value && is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
0586 int> = 0>
0587 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r) {
0588 shared_ptr(std::move(__r)).swap(*this);
0589 return *this;
0590 }
0591 #endif
0592
0593 template <class _Yp,
0594 class _Dp,
0595 __enable_if_t<_And< __compatible_with<_Yp, _Tp>,
0596 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*> >::value,
0597 int> = 0>
0598 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r) {
0599 shared_ptr(std::move(__r)).swap(*this);
0600 return *this;
0601 }
0602
0603 _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr& __r) _NOEXCEPT {
0604 std::swap(__ptr_, __r.__ptr_);
0605 std::swap(__cntrl_, __r.__cntrl_);
0606 }
0607
0608 _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { shared_ptr().swap(*this); }
0609
0610 template <class _Yp, __enable_if_t<__raw_pointer_compatible_with<_Yp, _Tp>::value, int> = 0>
0611 _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p) {
0612 shared_ptr(__p).swap(*this);
0613 }
0614
0615 template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
0616 _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d) {
0617 shared_ptr(__p, __d).swap(*this);
0618 }
0619
0620 template <class _Yp,
0621 class _Dp,
0622 class _Alloc,
0623 __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int> = 0>
0624 _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d, _Alloc __a) {
0625 shared_ptr(__p, __d, __a).swap(*this);
0626 }
0627
0628 _LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; }
0629
0630 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT { return *__ptr_; }
0631
0632 _LIBCPP_HIDE_FROM_ABI element_type* operator->() const _NOEXCEPT {
0633 static_assert(!is_array<_Tp>::value, "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
0634 return __ptr_;
0635 }
0636
0637 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
0638
0639 #if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE)
0640 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }
0641 #endif
0642
0643 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; }
0644
0645 template <class _Up>
0646 _LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {
0647 return __cntrl_ < __p.__cntrl_;
0648 }
0649
0650 template <class _Up>
0651 _LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {
0652 return __cntrl_ < __p.__cntrl_;
0653 }
0654
0655 _LIBCPP_HIDE_FROM_ABI bool __owner_equivalent(const shared_ptr& __p) const { return __cntrl_ == __p.__cntrl_; }
0656
0657 #if _LIBCPP_STD_VER >= 17
0658 _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {
0659 static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
0660 return __ptr_[__i];
0661 }
0662 #endif
0663
0664 #if _LIBCPP_HAS_RTTI
0665 template <class _Dp>
0666 _LIBCPP_HIDE_FROM_ABI _Dp* __get_deleter() const _NOEXCEPT {
0667 return static_cast<_Dp*>(__cntrl_ ? const_cast<void*>(__cntrl_->__get_deleter(typeid(_Dp))) : nullptr);
0668 }
0669 #endif
0670
0671 template <class _Yp, class _CntrlBlk>
0672 _LIBCPP_HIDE_FROM_ABI static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT {
0673 shared_ptr<_Tp> __r;
0674 __r.__ptr_ = __p;
0675 __r.__cntrl_ = __cntrl;
0676 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
0677 return __r;
0678 }
0679
0680 private:
0681 template <class _Yp, bool = is_function<_Yp>::value>
0682 struct __shared_ptr_default_allocator {
0683 typedef allocator<__remove_cv_t<_Yp> > type;
0684 };
0685
0686 template <class _Yp>
0687 struct __shared_ptr_default_allocator<_Yp, true> {
0688 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
0689 };
0690
0691 template <class _Yp,
0692 class _OrigPtr,
0693 __enable_if_t<is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value, int> = 0>
0694 _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT {
0695 typedef __remove_cv_t<_Yp> _RawYp;
0696 if (__e && __e->__weak_this_.expired()) {
0697 __e->__weak_this_ = shared_ptr<_RawYp>(*this, const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
0698 }
0699 }
0700
0701 _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT {}
0702
0703 template <class, class _Yp>
0704 struct __shared_ptr_default_delete : default_delete<_Yp> {};
0705
0706 template <class _Yp, class _Un, size_t _Sz>
0707 struct __shared_ptr_default_delete<_Yp[_Sz], _Un> : default_delete<_Yp[]> {};
0708
0709 template <class _Yp, class _Un>
0710 struct __shared_ptr_default_delete<_Yp[], _Un> : default_delete<_Yp[]> {};
0711
0712 template <class _Up>
0713 friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
0714 template <class _Up>
0715 friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
0716 };
0717
0718 #if _LIBCPP_STD_VER >= 17
0719 template <class _Tp>
0720 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
0721 template <class _Tp, class _Dp>
0722 shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
0723 #endif
0724
0725
0726
0727
0728 template <class _Tp, class _Alloc, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>
0729 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) {
0730 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
0731 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
0732 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
0733 ::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__a, std::forward<_Args>(__args)...);
0734 auto __control_block = __guard.__release_ptr();
0735 return shared_ptr<_Tp>::__create_with_control_block(
0736 (*__control_block).__get_elem(), std::addressof(*__control_block));
0737 }
0738
0739 template <class _Tp, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>
0740 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) {
0741 return std::allocate_shared<_Tp>(allocator<__remove_cv_t<_Tp> >(), std::forward<_Args>(__args)...);
0742 }
0743
0744 #if _LIBCPP_STD_VER >= 20
0745
0746 template <class _Tp, class _Alloc, __enable_if_t<!is_array<_Tp>::value, int> = 0>
0747 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
0748 using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
0749 _ForOverwriteAllocator __alloc(__a);
0750 return std::allocate_shared<_Tp>(__alloc);
0751 }
0752
0753 template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
0754 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
0755 return std::allocate_shared_for_overwrite<_Tp>(allocator<__remove_cv_t<_Tp>>());
0756 }
0757
0758 #endif
0759
0760 #if _LIBCPP_STD_VER >= 17
0761
0762 template <size_t _Alignment>
0763 struct __sp_aligned_storage {
0764 alignas(_Alignment) char __storage[_Alignment];
0765 };
0766
0767 template <class _Tp, class _Alloc>
0768 struct __unbounded_array_control_block;
0769
0770 template <class _Tp, class _Alloc>
0771 struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count {
0772 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; }
0773
0774 _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(
0775 _Alloc const& __alloc, size_t __count, _Tp const& __arg)
0776 : __alloc_(__alloc), __count_(__count) {
0777 std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::begin(__data_), __count_, __arg);
0778 }
0779
0780 _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count)
0781 : __alloc_(__alloc), __count_(__count) {
0782 # if _LIBCPP_STD_VER >= 20
0783 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
0784
0785
0786 std::uninitialized_default_construct_n(std::begin(__data_), __count_);
0787 } else {
0788 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_);
0789 }
0790 # else
0791 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_);
0792 # endif
0793 }
0794
0795
0796
0797 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __bytes_for(size_t __elements) {
0798
0799
0800
0801
0802
0803
0804 size_t __bytes = __elements == 0 ? sizeof(__unbounded_array_control_block)
0805 : (__elements - 1) * sizeof(_Tp) + sizeof(__unbounded_array_control_block);
0806 constexpr size_t __align = alignof(_Tp);
0807 return (__bytes + __align - 1) & ~(__align - 1);
0808 }
0809
0810 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
0811 ~__unbounded_array_control_block() override {
0812 }
0813
0814 private:
0815 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {
0816 # if _LIBCPP_STD_VER >= 20
0817 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
0818 std::__reverse_destroy(__data_, __data_ + __count_);
0819 } else {
0820 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
0821 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);
0822 }
0823 # else
0824 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
0825 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);
0826 # endif
0827 }
0828
0829 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
0830 using _AlignedStorage = __sp_aligned_storage<alignof(__unbounded_array_control_block)>;
0831 using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
0832 using _PointerTraits = pointer_traits<typename allocator_traits<_StorageAlloc>::pointer>;
0833
0834 _StorageAlloc __tmp(__alloc_);
0835 __alloc_.~_Alloc();
0836 size_t __size = __unbounded_array_control_block::__bytes_for(__count_);
0837 _AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this);
0838 allocator_traits<_StorageAlloc>::deallocate(
0839 __tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage));
0840 }
0841
0842 _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
0843 size_t __count_;
0844 union {
0845 _Tp __data_[1];
0846 };
0847 };
0848
0849 template <class _Array, class _Alloc, class... _Arg>
0850 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array>
0851 __allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&&... __arg) {
0852 static_assert(__is_unbounded_array_v<_Array>);
0853
0854
0855
0856
0857 using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>;
0858 using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>;
0859 using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
0860 __allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage));
0861 _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
0862 std::__construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...);
0863 __guard.__release_ptr();
0864 return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
0865 }
0866
0867 template <class _Tp, class _Alloc>
0868 struct __bounded_array_control_block;
0869
0870 template <class _Tp, size_t _Count, class _Alloc>
0871 struct __bounded_array_control_block<_Tp[_Count], _Alloc> : __shared_weak_count {
0872 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; }
0873
0874 _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg)
0875 : __alloc_(__alloc) {
0876 std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count, __arg);
0877 }
0878
0879 _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) {
0880 # if _LIBCPP_STD_VER >= 20
0881 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
0882
0883
0884 std::uninitialized_default_construct_n(std::addressof(__data_[0]), _Count);
0885 } else {
0886 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);
0887 }
0888 # else
0889 std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);
0890 # endif
0891 }
0892
0893 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
0894 ~__bounded_array_control_block() override {
0895 }
0896
0897 private:
0898 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {
0899 # if _LIBCPP_STD_VER >= 20
0900 if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
0901 std::__reverse_destroy(__data_, __data_ + _Count);
0902 } else {
0903 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
0904 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
0905 }
0906 # else
0907 __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
0908 std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
0909 # endif
0910 }
0911
0912 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
0913 using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>;
0914 using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>;
0915
0916 _ControlBlockAlloc __tmp(__alloc_);
0917 __alloc_.~_Alloc();
0918 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1);
0919 }
0920
0921 _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
0922 union {
0923 _Tp __data_[_Count];
0924 };
0925 };
0926
0927 template <class _Array, class _Alloc, class... _Arg>
0928 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&&... __arg) {
0929 static_assert(__is_bounded_array_v<_Array>);
0930 using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>;
0931 using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>;
0932
0933 __allocation_guard<_ControlBlockAlloc> __guard(__a, 1);
0934 _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
0935 std::__construct_at(__control_block, __a, std::forward<_Arg>(__arg)...);
0936 __guard.__release_ptr();
0937 return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
0938 }
0939
0940 #endif
0941
0942 #if _LIBCPP_STD_VER >= 20
0943
0944
0945 template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
0946 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) {
0947 return std::__allocate_shared_bounded_array<_Tp>(__a);
0948 }
0949
0950 template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
0951 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) {
0952 return std::__allocate_shared_bounded_array<_Tp>(__a, __u);
0953 }
0954
0955 template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
0956 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
0957 using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
0958 _ForOverwriteAllocator __alloc(__a);
0959 return std::__allocate_shared_bounded_array<_Tp>(__alloc);
0960 }
0961
0962 template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
0963 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {
0964 return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());
0965 }
0966
0967 template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
0968 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) {
0969 return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);
0970 }
0971
0972 template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
0973 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
0974 return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>());
0975 }
0976
0977
0978 template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
0979 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) {
0980 return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);
0981 }
0982
0983 template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
0984 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) {
0985 return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);
0986 }
0987
0988 template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
0989 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) {
0990 using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
0991 _ForOverwriteAllocator __alloc(__a);
0992 return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n);
0993 }
0994
0995 template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
0996 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) {
0997 return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);
0998 }
0999
1000 template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
1001 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) {
1002 return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);
1003 }
1004
1005 template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
1006 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) {
1007 return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n);
1008 }
1009
1010 #endif
1011
1012 template <class _Tp, class _Up>
1013 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
1014 return __x.get() == __y.get();
1015 }
1016
1017 #if _LIBCPP_STD_VER <= 17
1018
1019 template <class _Tp, class _Up>
1020 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
1021 return !(__x == __y);
1022 }
1023
1024 template <class _Tp, class _Up>
1025 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
1026 # if _LIBCPP_STD_VER <= 11
1027 typedef typename common_type<_Tp*, _Up*>::type _Vp;
1028 return less<_Vp>()(__x.get(), __y.get());
1029 # else
1030 return less<>()(__x.get(), __y.get());
1031 # endif
1032 }
1033
1034 template <class _Tp, class _Up>
1035 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
1036 return __y < __x;
1037 }
1038
1039 template <class _Tp, class _Up>
1040 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
1041 return !(__y < __x);
1042 }
1043
1044 template <class _Tp, class _Up>
1045 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
1046 return !(__x < __y);
1047 }
1048
1049 #endif
1050
1051 #if _LIBCPP_STD_VER >= 20
1052 template <class _Tp, class _Up>
1053 _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) noexcept {
1054 return compare_three_way()(__x.get(), __y.get());
1055 }
1056 #endif
1057
1058 template <class _Tp>
1059 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
1060 return !__x;
1061 }
1062
1063 #if _LIBCPP_STD_VER <= 17
1064
1065 template <class _Tp>
1066 inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
1067 return !__x;
1068 }
1069
1070 template <class _Tp>
1071 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
1072 return static_cast<bool>(__x);
1073 }
1074
1075 template <class _Tp>
1076 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
1077 return static_cast<bool>(__x);
1078 }
1079
1080 template <class _Tp>
1081 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
1082 return less<typename shared_ptr<_Tp>::element_type*>()(__x.get(), nullptr);
1083 }
1084
1085 template <class _Tp>
1086 inline _LIBCPP_HIDE_FROM_ABI bool operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
1087 return less<typename shared_ptr<_Tp>::element_type*>()(nullptr, __x.get());
1088 }
1089
1090 template <class _Tp>
1091 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
1092 return nullptr < __x;
1093 }
1094
1095 template <class _Tp>
1096 inline _LIBCPP_HIDE_FROM_ABI bool operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
1097 return __x < nullptr;
1098 }
1099
1100 template <class _Tp>
1101 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
1102 return !(nullptr < __x);
1103 }
1104
1105 template <class _Tp>
1106 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
1107 return !(__x < nullptr);
1108 }
1109
1110 template <class _Tp>
1111 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
1112 return !(__x < nullptr);
1113 }
1114
1115 template <class _Tp>
1116 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
1117 return !(nullptr < __x);
1118 }
1119
1120 #endif
1121
1122 #if _LIBCPP_STD_VER >= 20
1123 template <class _Tp>
1124 _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, nullptr_t) noexcept {
1125 return compare_three_way()(__x.get(), static_cast<typename shared_ptr<_Tp>::element_type*>(nullptr));
1126 }
1127 #endif
1128
1129 template <class _Tp>
1130 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT {
1131 __x.swap(__y);
1132 }
1133
1134 template <class _Tp, class _Up>
1135 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1136 return shared_ptr<_Tp>(__r, static_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));
1137 }
1138
1139
1140
1141 #if _LIBCPP_STD_VER >= 20
1142 template <class _Tp, class _Up>
1143 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1144 return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
1145 }
1146 #endif
1147
1148 template <class _Tp, class _Up>
1149 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1150 typedef typename shared_ptr<_Tp>::element_type _ET;
1151 _ET* __p = dynamic_cast<_ET*>(__r.get());
1152 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
1153 }
1154
1155
1156
1157 #if _LIBCPP_STD_VER >= 20
1158 template <class _Tp, class _Up>
1159 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1160 auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get());
1161 return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>();
1162 }
1163 #endif
1164
1165 template <class _Tp, class _Up>
1166 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1167 typedef typename shared_ptr<_Tp>::element_type _RTp;
1168 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
1169 }
1170
1171
1172
1173 #if _LIBCPP_STD_VER >= 20
1174 template <class _Tp, class _Up>
1175 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1176 return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
1177 }
1178 #endif
1179
1180 template <class _Tp, class _Up>
1181 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1182 return shared_ptr<_Tp>(__r, reinterpret_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));
1183 }
1184
1185
1186
1187 #if _LIBCPP_STD_VER >= 20
1188 template <class _Tp, class _Up>
1189 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1190 return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
1191 }
1192 #endif
1193
1194 #if _LIBCPP_HAS_RTTI
1195
1196 template <class _Dp, class _Tp>
1197 inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT {
1198 return __p.template __get_deleter<_Dp>();
1199 }
1200
1201 #endif
1202
1203 template <class _Tp>
1204 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr {
1205 public:
1206 #if _LIBCPP_STD_VER >= 17
1207 typedef remove_extent_t<_Tp> element_type;
1208 #else
1209 typedef _Tp element_type;
1210 #endif
1211
1212
1213
1214 using __trivially_relocatable _LIBCPP_NODEBUG = weak_ptr;
1215
1216 private:
1217 element_type* __ptr_;
1218 __shared_weak_count* __cntrl_;
1219
1220 public:
1221 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
1222
1223 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
1224 _LIBCPP_HIDE_FROM_ABI weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT;
1225
1226 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr const& __r) _NOEXCEPT;
1227
1228 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
1229 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT;
1230
1231 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr&& __r) _NOEXCEPT;
1232
1233 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
1234 _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT;
1235
1236 _LIBCPP_HIDE_FROM_ABI ~weak_ptr();
1237
1238 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
1239 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
1240 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
1241
1242 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
1243 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
1244 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
1245
1246 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> = 0>
1247 _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
1248
1249 _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr& __r) _NOEXCEPT;
1250 _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT;
1251
1252 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
1253 _LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT { return __cntrl_ == nullptr || __cntrl_->use_count() == 0; }
1254 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT;
1255 template <class _Up>
1256 _LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT {
1257 return __cntrl_ < __r.__cntrl_;
1258 }
1259 template <class _Up>
1260 _LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT {
1261 return __cntrl_ < __r.__cntrl_;
1262 }
1263
1264 template <class _Up>
1265 friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
1266 template <class _Up>
1267 friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
1268 };
1269
1270 #if _LIBCPP_STD_VER >= 17
1271 template <class _Tp>
1272 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
1273 #endif
1274
1275 template <class _Tp>
1276 inline _LIBCPP_CONSTEXPR weak_ptr<_Tp>::weak_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
1277
1278 template <class _Tp>
1279 inline weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
1280 if (__cntrl_)
1281 __cntrl_->__add_weak();
1282 }
1283
1284 template <class _Tp>
1285 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
1286 inline weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
1287 if (__cntrl_)
1288 __cntrl_->__add_weak();
1289 }
1290
1291 template <class _Tp>
1292 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
1293 inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {
1294 shared_ptr<_Yp> __s = __r.lock();
1295 *this = weak_ptr<_Tp>(__s);
1296 }
1297
1298 template <class _Tp>
1299 inline weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) {
1300 __r.__ptr_ = nullptr;
1301 __r.__cntrl_ = nullptr;
1302 }
1303
1304 template <class _Tp>
1305 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
1306 inline weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {
1307 shared_ptr<_Yp> __s = __r.lock();
1308 *this = weak_ptr<_Tp>(__s);
1309 __r.reset();
1310 }
1311
1312 template <class _Tp>
1313 weak_ptr<_Tp>::~weak_ptr() {
1314 if (__cntrl_)
1315 __cntrl_->__release_weak();
1316 }
1317
1318 template <class _Tp>
1319 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT {
1320 weak_ptr(__r).swap(*this);
1321 return *this;
1322 }
1323
1324 template <class _Tp>
1325 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
1326 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT {
1327 weak_ptr(__r).swap(*this);
1328 return *this;
1329 }
1330
1331 template <class _Tp>
1332 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT {
1333 weak_ptr(std::move(__r)).swap(*this);
1334 return *this;
1335 }
1336
1337 template <class _Tp>
1338 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
1339 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT {
1340 weak_ptr(std::move(__r)).swap(*this);
1341 return *this;
1342 }
1343
1344 template <class _Tp>
1345 template <class _Yp, __enable_if_t<__compatible_with<_Yp, _Tp>::value, int> >
1346 inline weak_ptr<_Tp>& weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT {
1347 weak_ptr(__r).swap(*this);
1348 return *this;
1349 }
1350
1351 template <class _Tp>
1352 inline void weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT {
1353 std::swap(__ptr_, __r.__ptr_);
1354 std::swap(__cntrl_, __r.__cntrl_);
1355 }
1356
1357 template <class _Tp>
1358 inline _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT {
1359 __x.swap(__y);
1360 }
1361
1362 template <class _Tp>
1363 inline void weak_ptr<_Tp>::reset() _NOEXCEPT {
1364 weak_ptr().swap(*this);
1365 }
1366
1367 template <class _Tp>
1368 shared_ptr<_Tp> weak_ptr<_Tp>::lock() const _NOEXCEPT {
1369 shared_ptr<_Tp> __r;
1370 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
1371 if (__r.__cntrl_)
1372 __r.__ptr_ = __ptr_;
1373 return __r;
1374 }
1375
1376 #if _LIBCPP_STD_VER >= 17
1377 template <class _Tp = void>
1378 struct owner_less;
1379 #else
1380 template <class _Tp>
1381 struct owner_less;
1382 #endif
1383
1384 template <class _Tp>
1385 struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > : __binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> {
1386 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {
1387 return __x.owner_before(__y);
1388 }
1389 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {
1390 return __x.owner_before(__y);
1391 }
1392 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {
1393 return __x.owner_before(__y);
1394 }
1395 };
1396
1397 template <class _Tp>
1398 struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > : __binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> {
1399 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {
1400 return __x.owner_before(__y);
1401 }
1402 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT {
1403 return __x.owner_before(__y);
1404 }
1405 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT {
1406 return __x.owner_before(__y);
1407 }
1408 };
1409
1410 #if _LIBCPP_STD_VER >= 17
1411 template <>
1412 struct _LIBCPP_TEMPLATE_VIS owner_less<void> {
1413 template <class _Tp, class _Up>
1414 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT {
1415 return __x.owner_before(__y);
1416 }
1417 template <class _Tp, class _Up>
1418 _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT {
1419 return __x.owner_before(__y);
1420 }
1421 template <class _Tp, class _Up>
1422 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT {
1423 return __x.owner_before(__y);
1424 }
1425 template <class _Tp, class _Up>
1426 _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT {
1427 return __x.owner_before(__y);
1428 }
1429 typedef void is_transparent;
1430 };
1431 #endif
1432
1433 template <class _Tp>
1434 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this {
1435 mutable weak_ptr<_Tp> __weak_this_;
1436
1437 protected:
1438 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR enable_shared_from_this() _NOEXCEPT {}
1439 _LIBCPP_HIDE_FROM_ABI enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
1440 _LIBCPP_HIDE_FROM_ABI enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT { return *this; }
1441 _LIBCPP_HIDE_FROM_ABI ~enable_shared_from_this() {}
1442
1443 public:
1444 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); }
1445 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const { return shared_ptr<const _Tp>(__weak_this_); }
1446
1447 #if _LIBCPP_STD_VER >= 17
1448 _LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; }
1449
1450 _LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; }
1451 #endif
1452
1453 template <class _Up>
1454 friend class shared_ptr;
1455 };
1456
1457 template <class _Tp>
1458 struct _LIBCPP_TEMPLATE_VIS hash;
1459
1460 template <class _Tp>
1461 struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > {
1462 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1463 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;
1464 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
1465 #endif
1466
1467 _LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT {
1468 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
1469 }
1470 };
1471
1472 template <class _CharT, class _Traits, class _Yp>
1473 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1474 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
1475
1476 #if _LIBCPP_HAS_THREADS
1477
1478 class _LIBCPP_EXPORTED_FROM_ABI __sp_mut {
1479 void* __lx_;
1480
1481 public:
1482 void lock() _NOEXCEPT;
1483 void unlock() _NOEXCEPT;
1484
1485 private:
1486 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
1487 __sp_mut(const __sp_mut&);
1488 __sp_mut& operator=(const __sp_mut&);
1489
1490 friend _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);
1491 };
1492
1493 _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);
1494
1495 template <class _Tp>
1496 inline _LIBCPP_HIDE_FROM_ABI bool atomic_is_lock_free(const shared_ptr<_Tp>*) {
1497 return false;
1498 }
1499
1500 template <class _Tp>
1501 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p) {
1502 __sp_mut& __m = std::__get_sp_mut(__p);
1503 __m.lock();
1504 shared_ptr<_Tp> __q = *__p;
1505 __m.unlock();
1506 return __q;
1507 }
1508
1509 template <class _Tp>
1510 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) {
1511 return std::atomic_load(__p);
1512 }
1513
1514 template <class _Tp>
1515 _LIBCPP_HIDE_FROM_ABI void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) {
1516 __sp_mut& __m = std::__get_sp_mut(__p);
1517 __m.lock();
1518 __p->swap(__r);
1519 __m.unlock();
1520 }
1521
1522 template <class _Tp>
1523 inline _LIBCPP_HIDE_FROM_ABI void atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) {
1524 std::atomic_store(__p, __r);
1525 }
1526
1527 template <class _Tp>
1528 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) {
1529 __sp_mut& __m = std::__get_sp_mut(__p);
1530 __m.lock();
1531 __p->swap(__r);
1532 __m.unlock();
1533 return __r;
1534 }
1535
1536 template <class _Tp>
1537 inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
1538 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) {
1539 return std::atomic_exchange(__p, __r);
1540 }
1541
1542 template <class _Tp>
1543 _LIBCPP_HIDE_FROM_ABI bool
1544 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) {
1545 shared_ptr<_Tp> __temp;
1546 __sp_mut& __m = std::__get_sp_mut(__p);
1547 __m.lock();
1548 if (__p->__owner_equivalent(*__v)) {
1549 std::swap(__temp, *__p);
1550 *__p = __w;
1551 __m.unlock();
1552 return true;
1553 }
1554 std::swap(__temp, *__v);
1555 *__v = *__p;
1556 __m.unlock();
1557 return false;
1558 }
1559
1560 template <class _Tp>
1561 inline _LIBCPP_HIDE_FROM_ABI bool
1562 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) {
1563 return std::atomic_compare_exchange_strong(__p, __v, __w);
1564 }
1565
1566 template <class _Tp>
1567 inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_strong_explicit(
1568 shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) {
1569 return std::atomic_compare_exchange_strong(__p, __v, __w);
1570 }
1571
1572 template <class _Tp>
1573 inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_weak_explicit(
1574 shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) {
1575 return std::atomic_compare_exchange_weak(__p, __v, __w);
1576 }
1577
1578 #endif
1579
1580 _LIBCPP_END_NAMESPACE_STD
1581
1582 _LIBCPP_POP_MACROS
1583
1584 #endif