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