1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___MEMORY_SHARED_PTR_H 11 #define _LIBCPP___MEMORY_SHARED_PTR_H 12 13 #include <__availability> 14 #include <__config> 15 #include <__functional_base> 16 #include <__functional/binary_function.h> 17 #include <__functional/operations.h> 18 #include <__functional/reference_wrapper.h> 19 #include <__memory/addressof.h> 20 #include <__memory/allocation_guard.h> 21 #include <__memory/allocator_traits.h> 22 #include <__memory/allocator.h> 23 #include <__memory/compressed_pair.h> 24 #include <__memory/pointer_traits.h> 25 #include <__memory/unique_ptr.h> 26 #include <__utility/forward.h> 27 #include <cstddef> 28 #include <cstdlib> // abort 29 #include <iosfwd> 30 #include <stdexcept> 31 #include <typeinfo> 32 #include <type_traits> 33 #include <utility> 34 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) 35 # include <atomic> 36 #endif 37 38 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 39 # include <__memory/auto_ptr.h> 40 #endif 41 42 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 43 #pragma GCC system_header 44 #endif 45 46 _LIBCPP_PUSH_MACROS 47 #include <__undef_macros> 48 49 _LIBCPP_BEGIN_NAMESPACE_STD 50 51 template <class _Alloc> 52 class __allocator_destructor 53 { 54 typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits; 55 public: 56 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer; 57 typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type; 58 private: 59 _Alloc& __alloc_; 60 size_type __s_; 61 public: 62 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s) 63 _NOEXCEPT 64 : __alloc_(__a), __s_(__s) {} 65 _LIBCPP_INLINE_VISIBILITY 66 void operator()(pointer __p) _NOEXCEPT 67 {__alloc_traits::deallocate(__alloc_, __p, __s_);} 68 }; 69 70 // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively) 71 // should be sufficient for thread safety. 72 // See https://llvm.org/PR22803 73 #if defined(__clang__) && __has_builtin(__atomic_add_fetch) \ 74 && defined(__ATOMIC_RELAXED) \ 75 && defined(__ATOMIC_ACQ_REL) 76 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 77 #elif defined(_LIBCPP_COMPILER_GCC) 78 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 79 #endif 80 81 template <class _ValueType> 82 inline _LIBCPP_INLINE_VISIBILITY 83 _ValueType __libcpp_relaxed_load(_ValueType const* __value) { 84 #if !defined(_LIBCPP_HAS_NO_THREADS) && \ 85 defined(__ATOMIC_RELAXED) && \ 86 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) 87 return __atomic_load_n(__value, __ATOMIC_RELAXED); 88 #else 89 return *__value; 90 #endif 91 } 92 93 template <class _ValueType> 94 inline _LIBCPP_INLINE_VISIBILITY 95 _ValueType __libcpp_acquire_load(_ValueType const* __value) { 96 #if !defined(_LIBCPP_HAS_NO_THREADS) && \ 97 defined(__ATOMIC_ACQUIRE) && \ 98 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) 99 return __atomic_load_n(__value, __ATOMIC_ACQUIRE); 100 #else 101 return *__value; 102 #endif 103 } 104 105 template <class _Tp> 106 inline _LIBCPP_INLINE_VISIBILITY _Tp 107 __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT 108 { 109 #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) 110 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED); 111 #else 112 return __t += 1; 113 #endif 114 } 115 116 template <class _Tp> 117 inline _LIBCPP_INLINE_VISIBILITY _Tp 118 __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT 119 { 120 #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) 121 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL); 122 #else 123 return __t -= 1; 124 #endif 125 } 126 127 class _LIBCPP_EXCEPTION_ABI bad_weak_ptr 128 : public std::exception 129 { 130 public: 131 bad_weak_ptr() _NOEXCEPT = default; 132 bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default; 133 virtual ~bad_weak_ptr() _NOEXCEPT; 134 virtual const char* what() const _NOEXCEPT; 135 }; 136 137 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 138 void __throw_bad_weak_ptr() 139 { 140 #ifndef _LIBCPP_NO_EXCEPTIONS 141 throw bad_weak_ptr(); 142 #else 143 _VSTD::abort(); 144 #endif 145 } 146 147 template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr; 148 149 class _LIBCPP_TYPE_VIS __shared_count 150 { 151 __shared_count(const __shared_count&); 152 __shared_count& operator=(const __shared_count&); 153 154 protected: 155 long __shared_owners_; 156 virtual ~__shared_count(); 157 private: 158 virtual void __on_zero_shared() _NOEXCEPT = 0; 159 160 public: 161 _LIBCPP_INLINE_VISIBILITY 162 explicit __shared_count(long __refs = 0) _NOEXCEPT 163 : __shared_owners_(__refs) {} 164 165 #if defined(_LIBCPP_BUILDING_LIBRARY) && \ 166 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) 167 void __add_shared() _NOEXCEPT; 168 bool __release_shared() _NOEXCEPT; 169 #else 170 _LIBCPP_INLINE_VISIBILITY 171 void __add_shared() _NOEXCEPT { 172 __libcpp_atomic_refcount_increment(__shared_owners_); 173 } 174 _LIBCPP_INLINE_VISIBILITY 175 bool __release_shared() _NOEXCEPT { 176 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) { 177 __on_zero_shared(); 178 return true; 179 } 180 return false; 181 } 182 #endif 183 _LIBCPP_INLINE_VISIBILITY 184 long use_count() const _NOEXCEPT { 185 return __libcpp_relaxed_load(&__shared_owners_) + 1; 186 } 187 }; 188 189 class _LIBCPP_TYPE_VIS __shared_weak_count 190 : private __shared_count 191 { 192 long __shared_weak_owners_; 193 194 public: 195 _LIBCPP_INLINE_VISIBILITY 196 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT 197 : __shared_count(__refs), 198 __shared_weak_owners_(__refs) {} 199 protected: 200 virtual ~__shared_weak_count(); 201 202 public: 203 #if defined(_LIBCPP_BUILDING_LIBRARY) && \ 204 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) 205 void __add_shared() _NOEXCEPT; 206 void __add_weak() _NOEXCEPT; 207 void __release_shared() _NOEXCEPT; 208 #else 209 _LIBCPP_INLINE_VISIBILITY 210 void __add_shared() _NOEXCEPT { 211 __shared_count::__add_shared(); 212 } 213 _LIBCPP_INLINE_VISIBILITY 214 void __add_weak() _NOEXCEPT { 215 __libcpp_atomic_refcount_increment(__shared_weak_owners_); 216 } 217 _LIBCPP_INLINE_VISIBILITY 218 void __release_shared() _NOEXCEPT { 219 if (__shared_count::__release_shared()) 220 __release_weak(); 221 } 222 #endif 223 void __release_weak() _NOEXCEPT; 224 _LIBCPP_INLINE_VISIBILITY 225 long use_count() const _NOEXCEPT {return __shared_count::use_count();} 226 __shared_weak_count* lock() _NOEXCEPT; 227 228 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; 229 private: 230 virtual void __on_zero_shared_weak() _NOEXCEPT = 0; 231 }; 232 233 template <class _Tp, class _Dp, class _Alloc> 234 class __shared_ptr_pointer 235 : public __shared_weak_count 236 { 237 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_; 238 public: 239 _LIBCPP_INLINE_VISIBILITY 240 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a) 241 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {} 242 243 #ifndef _LIBCPP_NO_RTTI 244 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; 245 #endif 246 247 private: 248 virtual void __on_zero_shared() _NOEXCEPT; 249 virtual void __on_zero_shared_weak() _NOEXCEPT; 250 }; 251 252 #ifndef _LIBCPP_NO_RTTI 253 254 template <class _Tp, class _Dp, class _Alloc> 255 const void* 256 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT 257 { 258 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr; 259 } 260 261 #endif // _LIBCPP_NO_RTTI 262 263 template <class _Tp, class _Dp, class _Alloc> 264 void 265 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT 266 { 267 __data_.first().second()(__data_.first().first()); 268 __data_.first().second().~_Dp(); 269 } 270 271 template <class _Tp, class _Dp, class _Alloc> 272 void 273 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT 274 { 275 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al; 276 typedef allocator_traits<_Al> _ATraits; 277 typedef pointer_traits<typename _ATraits::pointer> _PTraits; 278 279 _Al __a(__data_.second()); 280 __data_.second().~_Alloc(); 281 __a.deallocate(_PTraits::pointer_to(*this), 1); 282 } 283 284 template <class _Tp, class _Alloc> 285 struct __shared_ptr_emplace 286 : __shared_weak_count 287 { 288 template<class ..._Args> 289 _LIBCPP_HIDE_FROM_ABI 290 explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args) 291 : __storage_(_VSTD::move(__a)) 292 { 293 #if _LIBCPP_STD_VER > 17 294 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; 295 _TpAlloc __tmp(*__get_alloc()); 296 allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...); 297 #else 298 ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...); 299 #endif 300 } 301 302 _LIBCPP_HIDE_FROM_ABI 303 _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); } 304 305 _LIBCPP_HIDE_FROM_ABI 306 _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); } 307 308 private: 309 virtual void __on_zero_shared() _NOEXCEPT { 310 #if _LIBCPP_STD_VER > 17 311 using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; 312 _TpAlloc __tmp(*__get_alloc()); 313 allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem()); 314 #else 315 __get_elem()->~_Tp(); 316 #endif 317 } 318 319 virtual void __on_zero_shared_weak() _NOEXCEPT { 320 using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type; 321 using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer; 322 _ControlBlockAlloc __tmp(*__get_alloc()); 323 __storage_.~_Storage(); 324 allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, 325 pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1); 326 } 327 328 // This class implements the control block for non-array shared pointers created 329 // through `std::allocate_shared` and `std::make_shared`. 330 // 331 // In previous versions of the library, we used a compressed pair to store 332 // both the _Alloc and the _Tp. This implies using EBO, which is incompatible 333 // with Allocator construction for _Tp. To allow implementing P0674 in C++20, 334 // we now use a properly aligned char buffer while making sure that we maintain 335 // the same layout that we had when we used a compressed pair. 336 using _CompressedPair = __compressed_pair<_Alloc, _Tp>; 337 struct _ALIGNAS_TYPE(_CompressedPair) _Storage { 338 char __blob_[sizeof(_CompressedPair)]; 339 340 _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { 341 ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a)); 342 } 343 _LIBCPP_HIDE_FROM_ABI ~_Storage() { 344 __get_alloc()->~_Alloc(); 345 } 346 _Alloc* __get_alloc() _NOEXCEPT { 347 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_); 348 typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair); 349 _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first); 350 return __alloc; 351 } 352 _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT { 353 _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_); 354 typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair); 355 _Tp *__elem = reinterpret_cast<_Tp*>(__second); 356 return __elem; 357 } 358 }; 359 360 static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), ""); 361 static_assert(sizeof(_Storage) == sizeof(_CompressedPair), ""); 362 _Storage __storage_; 363 }; 364 365 struct __shared_ptr_dummy_rebind_allocator_type; 366 template <> 367 class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> 368 { 369 public: 370 template <class _Other> 371 struct rebind 372 { 373 typedef allocator<_Other> other; 374 }; 375 }; 376 377 template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this; 378 379 template<class _Tp, class _Up> 380 struct __compatible_with 381 #if _LIBCPP_STD_VER > 14 382 : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {}; 383 #else 384 : is_convertible<_Tp*, _Up*> {}; 385 #endif // _LIBCPP_STD_VER > 14 386 387 template <class _Ptr, class = void> 388 struct __is_deletable : false_type { }; 389 template <class _Ptr> 390 struct __is_deletable<_Ptr, decltype(delete declval<_Ptr>())> : true_type { }; 391 392 template <class _Ptr, class = void> 393 struct __is_array_deletable : false_type { }; 394 template <class _Ptr> 395 struct __is_array_deletable<_Ptr, decltype(delete[] declval<_Ptr>())> : true_type { }; 396 397 template <class _Dp, class _Pt, 398 class = decltype(declval<_Dp>()(declval<_Pt>()))> 399 static true_type __well_formed_deleter_test(int); 400 401 template <class, class> 402 static false_type __well_formed_deleter_test(...); 403 404 template <class _Dp, class _Pt> 405 struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {}; 406 407 template<class _Dp, class _Tp, class _Yp> 408 struct __shared_ptr_deleter_ctor_reqs 409 { 410 static const bool value = __compatible_with<_Tp, _Yp>::value && 411 is_move_constructible<_Dp>::value && 412 __well_formed_deleter<_Dp, _Tp*>::value; 413 }; 414 415 #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI) 416 # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi)) 417 #else 418 # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI 419 #endif 420 421 template<class _Tp> 422 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr 423 { 424 public: 425 #if _LIBCPP_STD_VER > 14 426 typedef weak_ptr<_Tp> weak_type; 427 typedef remove_extent_t<_Tp> element_type; 428 #else 429 typedef _Tp element_type; 430 #endif 431 432 private: 433 element_type* __ptr_; 434 __shared_weak_count* __cntrl_; 435 436 struct __nat {int __for_bool_;}; 437 public: 438 _LIBCPP_INLINE_VISIBILITY 439 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT; 440 _LIBCPP_INLINE_VISIBILITY 441 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT; 442 443 template<class _Yp, class = _EnableIf< 444 _And< 445 __compatible_with<_Yp, _Tp> 446 // In C++03 we get errors when trying to do SFINAE with the 447 // delete operator, so we always pretend that it's deletable. 448 // The same happens on GCC. 449 #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC) 450 , _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> > 451 #endif 452 >::value 453 > > 454 explicit shared_ptr(_Yp* __p) : __ptr_(__p) { 455 unique_ptr<_Yp> __hold(__p); 456 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 457 typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk; 458 __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT()); 459 __hold.release(); 460 __enable_weak_this(__p, __p); 461 } 462 463 template<class _Yp, class _Dp> 464 shared_ptr(_Yp* __p, _Dp __d, 465 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat()); 466 template<class _Yp, class _Dp, class _Alloc> 467 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a, 468 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat()); 469 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d); 470 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a); 471 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT; 472 _LIBCPP_INLINE_VISIBILITY 473 shared_ptr(const shared_ptr& __r) _NOEXCEPT; 474 template<class _Yp> 475 _LIBCPP_INLINE_VISIBILITY 476 shared_ptr(const shared_ptr<_Yp>& __r, 477 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()) 478 _NOEXCEPT; 479 _LIBCPP_INLINE_VISIBILITY 480 shared_ptr(shared_ptr&& __r) _NOEXCEPT; 481 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r, 482 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()) 483 _NOEXCEPT; 484 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r, 485 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat()); 486 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 487 template<class _Yp> 488 shared_ptr(auto_ptr<_Yp>&& __r, 489 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat()); 490 #endif 491 template <class _Yp, class _Dp> 492 shared_ptr(unique_ptr<_Yp, _Dp>&&, 493 typename enable_if 494 < 495 !is_lvalue_reference<_Dp>::value && 496 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, 497 __nat 498 >::type = __nat()); 499 template <class _Yp, class _Dp> 500 shared_ptr(unique_ptr<_Yp, _Dp>&&, 501 typename enable_if 502 < 503 is_lvalue_reference<_Dp>::value && 504 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, 505 __nat 506 >::type = __nat()); 507 508 ~shared_ptr(); 509 510 _LIBCPP_INLINE_VISIBILITY 511 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT; 512 template<class _Yp> 513 typename enable_if 514 < 515 __compatible_with<_Yp, element_type>::value, 516 shared_ptr& 517 >::type 518 _LIBCPP_INLINE_VISIBILITY 519 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT; 520 _LIBCPP_INLINE_VISIBILITY 521 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT; 522 template<class _Yp> 523 typename enable_if 524 < 525 __compatible_with<_Yp, element_type>::value, 526 shared_ptr& 527 >::type 528 _LIBCPP_INLINE_VISIBILITY 529 operator=(shared_ptr<_Yp>&& __r); 530 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 531 template<class _Yp> 532 _LIBCPP_INLINE_VISIBILITY 533 typename enable_if 534 < 535 !is_array<_Yp>::value && 536 is_convertible<_Yp*, element_type*>::value, 537 shared_ptr 538 >::type& 539 operator=(auto_ptr<_Yp>&& __r); 540 #endif 541 template <class _Yp, class _Dp> 542 typename enable_if 543 < 544 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, 545 shared_ptr& 546 >::type 547 _LIBCPP_INLINE_VISIBILITY 548 operator=(unique_ptr<_Yp, _Dp>&& __r); 549 550 _LIBCPP_INLINE_VISIBILITY 551 void swap(shared_ptr& __r) _NOEXCEPT; 552 _LIBCPP_INLINE_VISIBILITY 553 void reset() _NOEXCEPT; 554 template<class _Yp> 555 typename enable_if 556 < 557 __compatible_with<_Yp, element_type>::value, 558 void 559 >::type 560 _LIBCPP_INLINE_VISIBILITY 561 reset(_Yp* __p); 562 template<class _Yp, class _Dp> 563 typename enable_if 564 < 565 __compatible_with<_Yp, element_type>::value, 566 void 567 >::type 568 _LIBCPP_INLINE_VISIBILITY 569 reset(_Yp* __p, _Dp __d); 570 template<class _Yp, class _Dp, class _Alloc> 571 typename enable_if 572 < 573 __compatible_with<_Yp, element_type>::value, 574 void 575 >::type 576 _LIBCPP_INLINE_VISIBILITY 577 reset(_Yp* __p, _Dp __d, _Alloc __a); 578 579 _LIBCPP_INLINE_VISIBILITY 580 element_type* get() const _NOEXCEPT {return __ptr_;} 581 _LIBCPP_INLINE_VISIBILITY 582 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT 583 {return *__ptr_;} 584 _LIBCPP_INLINE_VISIBILITY 585 element_type* operator->() const _NOEXCEPT 586 { 587 static_assert(!is_array<_Tp>::value, 588 "std::shared_ptr<T>::operator-> is only valid when T is not an array type."); 589 return __ptr_; 590 } 591 _LIBCPP_INLINE_VISIBILITY 592 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;} 593 _LIBCPP_INLINE_VISIBILITY 594 bool unique() const _NOEXCEPT {return use_count() == 1;} 595 _LIBCPP_INLINE_VISIBILITY 596 explicit operator bool() const _NOEXCEPT {return get() != nullptr;} 597 template <class _Up> 598 _LIBCPP_INLINE_VISIBILITY 599 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT 600 {return __cntrl_ < __p.__cntrl_;} 601 template <class _Up> 602 _LIBCPP_INLINE_VISIBILITY 603 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT 604 {return __cntrl_ < __p.__cntrl_;} 605 _LIBCPP_INLINE_VISIBILITY 606 bool 607 __owner_equivalent(const shared_ptr& __p) const 608 {return __cntrl_ == __p.__cntrl_;} 609 610 #if _LIBCPP_STD_VER > 14 611 typename add_lvalue_reference<element_type>::type 612 _LIBCPP_INLINE_VISIBILITY 613 operator[](ptrdiff_t __i) const 614 { 615 static_assert(is_array<_Tp>::value, 616 "std::shared_ptr<T>::operator[] is only valid when T is an array type."); 617 return __ptr_[__i]; 618 } 619 #endif 620 621 #ifndef _LIBCPP_NO_RTTI 622 template <class _Dp> 623 _LIBCPP_INLINE_VISIBILITY 624 _Dp* __get_deleter() const _NOEXCEPT 625 {return static_cast<_Dp*>(__cntrl_ 626 ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp))) 627 : nullptr);} 628 #endif // _LIBCPP_NO_RTTI 629 630 template<class _Yp, class _CntrlBlk> 631 static shared_ptr<_Tp> 632 __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT 633 { 634 shared_ptr<_Tp> __r; 635 __r.__ptr_ = __p; 636 __r.__cntrl_ = __cntrl; 637 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_); 638 return __r; 639 } 640 641 private: 642 template <class _Yp, bool = is_function<_Yp>::value> 643 struct __shared_ptr_default_allocator 644 { 645 typedef allocator<_Yp> type; 646 }; 647 648 template <class _Yp> 649 struct __shared_ptr_default_allocator<_Yp, true> 650 { 651 typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type; 652 }; 653 654 template <class _Yp, class _OrigPtr> 655 _LIBCPP_INLINE_VISIBILITY 656 typename enable_if<is_convertible<_OrigPtr*, 657 const enable_shared_from_this<_Yp>* 658 >::value, 659 void>::type 660 __enable_weak_this(const enable_shared_from_this<_Yp>* __e, 661 _OrigPtr* __ptr) _NOEXCEPT 662 { 663 typedef typename remove_cv<_Yp>::type _RawYp; 664 if (__e && __e->__weak_this_.expired()) 665 { 666 __e->__weak_this_ = shared_ptr<_RawYp>(*this, 667 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr))); 668 } 669 } 670 671 _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {} 672 673 template <class, class _Yp> 674 struct __shared_ptr_default_delete 675 : default_delete<_Yp> {}; 676 677 template <class _Yp, class _Un, size_t _Sz> 678 struct __shared_ptr_default_delete<_Yp[_Sz], _Un> 679 : default_delete<_Yp[]> {}; 680 681 template <class _Yp, class _Un> 682 struct __shared_ptr_default_delete<_Yp[], _Un> 683 : default_delete<_Yp[]> {}; 684 685 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; 686 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; 687 }; 688 689 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES 690 template<class _Tp> 691 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; 692 template<class _Tp, class _Dp> 693 shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>; 694 #endif 695 696 template<class _Tp> 697 inline 698 _LIBCPP_CONSTEXPR 699 shared_ptr<_Tp>::shared_ptr() _NOEXCEPT 700 : __ptr_(nullptr), 701 __cntrl_(nullptr) 702 { 703 } 704 705 template<class _Tp> 706 inline 707 _LIBCPP_CONSTEXPR 708 shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT 709 : __ptr_(nullptr), 710 __cntrl_(nullptr) 711 { 712 } 713 714 template<class _Tp> 715 template<class _Yp, class _Dp> 716 shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, 717 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type) 718 : __ptr_(__p) 719 { 720 #ifndef _LIBCPP_NO_EXCEPTIONS 721 try 722 { 723 #endif // _LIBCPP_NO_EXCEPTIONS 724 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 725 typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk; 726 #ifndef _LIBCPP_CXX03_LANG 727 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT()); 728 #else 729 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); 730 #endif // not _LIBCPP_CXX03_LANG 731 __enable_weak_this(__p, __p); 732 #ifndef _LIBCPP_NO_EXCEPTIONS 733 } 734 catch (...) 735 { 736 __d(__p); 737 throw; 738 } 739 #endif // _LIBCPP_NO_EXCEPTIONS 740 } 741 742 template<class _Tp> 743 template<class _Dp> 744 shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d) 745 : __ptr_(nullptr) 746 { 747 #ifndef _LIBCPP_NO_EXCEPTIONS 748 try 749 { 750 #endif // _LIBCPP_NO_EXCEPTIONS 751 typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; 752 typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk; 753 #ifndef _LIBCPP_CXX03_LANG 754 __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT()); 755 #else 756 __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); 757 #endif // not _LIBCPP_CXX03_LANG 758 #ifndef _LIBCPP_NO_EXCEPTIONS 759 } 760 catch (...) 761 { 762 __d(__p); 763 throw; 764 } 765 #endif // _LIBCPP_NO_EXCEPTIONS 766 } 767 768 template<class _Tp> 769 template<class _Yp, class _Dp, class _Alloc> 770 shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a, 771 typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type) 772 : __ptr_(__p) 773 { 774 #ifndef _LIBCPP_NO_EXCEPTIONS 775 try 776 { 777 #endif // _LIBCPP_NO_EXCEPTIONS 778 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; 779 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; 780 typedef __allocator_destructor<_A2> _D2; 781 _A2 __a2(__a); 782 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); 783 ::new ((void*)_VSTD::addressof(*__hold2.get())) 784 #ifndef _LIBCPP_CXX03_LANG 785 _CntrlBlk(__p, _VSTD::move(__d), __a); 786 #else 787 _CntrlBlk(__p, __d, __a); 788 #endif // not _LIBCPP_CXX03_LANG 789 __cntrl_ = _VSTD::addressof(*__hold2.release()); 790 __enable_weak_this(__p, __p); 791 #ifndef _LIBCPP_NO_EXCEPTIONS 792 } 793 catch (...) 794 { 795 __d(__p); 796 throw; 797 } 798 #endif // _LIBCPP_NO_EXCEPTIONS 799 } 800 801 template<class _Tp> 802 template<class _Dp, class _Alloc> 803 shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a) 804 : __ptr_(nullptr) 805 { 806 #ifndef _LIBCPP_NO_EXCEPTIONS 807 try 808 { 809 #endif // _LIBCPP_NO_EXCEPTIONS 810 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk; 811 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; 812 typedef __allocator_destructor<_A2> _D2; 813 _A2 __a2(__a); 814 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); 815 ::new ((void*)_VSTD::addressof(*__hold2.get())) 816 #ifndef _LIBCPP_CXX03_LANG 817 _CntrlBlk(__p, _VSTD::move(__d), __a); 818 #else 819 _CntrlBlk(__p, __d, __a); 820 #endif // not _LIBCPP_CXX03_LANG 821 __cntrl_ = _VSTD::addressof(*__hold2.release()); 822 #ifndef _LIBCPP_NO_EXCEPTIONS 823 } 824 catch (...) 825 { 826 __d(__p); 827 throw; 828 } 829 #endif // _LIBCPP_NO_EXCEPTIONS 830 } 831 832 template<class _Tp> 833 template<class _Yp> 834 inline 835 shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT 836 : __ptr_(__p), 837 __cntrl_(__r.__cntrl_) 838 { 839 if (__cntrl_) 840 __cntrl_->__add_shared(); 841 } 842 843 template<class _Tp> 844 inline 845 shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT 846 : __ptr_(__r.__ptr_), 847 __cntrl_(__r.__cntrl_) 848 { 849 if (__cntrl_) 850 __cntrl_->__add_shared(); 851 } 852 853 template<class _Tp> 854 template<class _Yp> 855 inline 856 shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, 857 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) 858 _NOEXCEPT 859 : __ptr_(__r.__ptr_), 860 __cntrl_(__r.__cntrl_) 861 { 862 if (__cntrl_) 863 __cntrl_->__add_shared(); 864 } 865 866 template<class _Tp> 867 inline 868 shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT 869 : __ptr_(__r.__ptr_), 870 __cntrl_(__r.__cntrl_) 871 { 872 __r.__ptr_ = nullptr; 873 __r.__cntrl_ = nullptr; 874 } 875 876 template<class _Tp> 877 template<class _Yp> 878 inline 879 shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r, 880 typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) 881 _NOEXCEPT 882 : __ptr_(__r.__ptr_), 883 __cntrl_(__r.__cntrl_) 884 { 885 __r.__ptr_ = nullptr; 886 __r.__cntrl_ = nullptr; 887 } 888 889 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 890 template<class _Tp> 891 template<class _Yp> 892 shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r, 893 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type) 894 : __ptr_(__r.get()) 895 { 896 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk; 897 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>()); 898 __enable_weak_this(__r.get(), __r.get()); 899 __r.release(); 900 } 901 #endif 902 903 template<class _Tp> 904 template <class _Yp, class _Dp> 905 shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r, 906 typename enable_if 907 < 908 !is_lvalue_reference<_Dp>::value && 909 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, 910 __nat 911 >::type) 912 : __ptr_(__r.get()) 913 { 914 #if _LIBCPP_STD_VER > 11 915 if (__ptr_ == nullptr) 916 __cntrl_ = nullptr; 917 else 918 #endif 919 { 920 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 921 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk; 922 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT()); 923 __enable_weak_this(__r.get(), __r.get()); 924 } 925 __r.release(); 926 } 927 928 template<class _Tp> 929 template <class _Yp, class _Dp> 930 shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r, 931 typename enable_if 932 < 933 is_lvalue_reference<_Dp>::value && 934 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, 935 __nat 936 >::type) 937 : __ptr_(__r.get()) 938 { 939 #if _LIBCPP_STD_VER > 11 940 if (__ptr_ == nullptr) 941 __cntrl_ = nullptr; 942 else 943 #endif 944 { 945 typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; 946 typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, 947 reference_wrapper<typename remove_reference<_Dp>::type>, 948 _AllocT > _CntrlBlk; 949 __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT()); 950 __enable_weak_this(__r.get(), __r.get()); 951 } 952 __r.release(); 953 } 954 955 template<class _Tp> 956 shared_ptr<_Tp>::~shared_ptr() 957 { 958 if (__cntrl_) 959 __cntrl_->__release_shared(); 960 } 961 962 template<class _Tp> 963 inline 964 shared_ptr<_Tp>& 965 shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT 966 { 967 shared_ptr(__r).swap(*this); 968 return *this; 969 } 970 971 template<class _Tp> 972 template<class _Yp> 973 inline 974 typename enable_if 975 < 976 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, 977 shared_ptr<_Tp>& 978 >::type 979 shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT 980 { 981 shared_ptr(__r).swap(*this); 982 return *this; 983 } 984 985 template<class _Tp> 986 inline 987 shared_ptr<_Tp>& 988 shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT 989 { 990 shared_ptr(_VSTD::move(__r)).swap(*this); 991 return *this; 992 } 993 994 template<class _Tp> 995 template<class _Yp> 996 inline 997 typename enable_if 998 < 999 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, 1000 shared_ptr<_Tp>& 1001 >::type 1002 shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r) 1003 { 1004 shared_ptr(_VSTD::move(__r)).swap(*this); 1005 return *this; 1006 } 1007 1008 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 1009 template<class _Tp> 1010 template<class _Yp> 1011 inline 1012 typename enable_if 1013 < 1014 !is_array<_Yp>::value && 1015 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value, 1016 shared_ptr<_Tp> 1017 >::type& 1018 shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r) 1019 { 1020 shared_ptr(_VSTD::move(__r)).swap(*this); 1021 return *this; 1022 } 1023 #endif 1024 1025 template<class _Tp> 1026 template <class _Yp, class _Dp> 1027 inline 1028 typename enable_if 1029 < 1030 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, 1031 typename shared_ptr<_Tp>::element_type*>::value, 1032 shared_ptr<_Tp>& 1033 >::type 1034 shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r) 1035 { 1036 shared_ptr(_VSTD::move(__r)).swap(*this); 1037 return *this; 1038 } 1039 1040 template<class _Tp> 1041 inline 1042 void 1043 shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT 1044 { 1045 _VSTD::swap(__ptr_, __r.__ptr_); 1046 _VSTD::swap(__cntrl_, __r.__cntrl_); 1047 } 1048 1049 template<class _Tp> 1050 inline 1051 void 1052 shared_ptr<_Tp>::reset() _NOEXCEPT 1053 { 1054 shared_ptr().swap(*this); 1055 } 1056 1057 template<class _Tp> 1058 template<class _Yp> 1059 inline 1060 typename enable_if 1061 < 1062 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, 1063 void 1064 >::type 1065 shared_ptr<_Tp>::reset(_Yp* __p) 1066 { 1067 shared_ptr(__p).swap(*this); 1068 } 1069 1070 template<class _Tp> 1071 template<class _Yp, class _Dp> 1072 inline 1073 typename enable_if 1074 < 1075 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, 1076 void 1077 >::type 1078 shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d) 1079 { 1080 shared_ptr(__p, __d).swap(*this); 1081 } 1082 1083 template<class _Tp> 1084 template<class _Yp, class _Dp, class _Alloc> 1085 inline 1086 typename enable_if 1087 < 1088 __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, 1089 void 1090 >::type 1091 shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a) 1092 { 1093 shared_ptr(__p, __d, __a).swap(*this); 1094 } 1095 1096 // 1097 // std::allocate_shared and std::make_shared 1098 // 1099 template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> > 1100 _LIBCPP_HIDE_FROM_ABI 1101 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args) 1102 { 1103 using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>; 1104 using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type; 1105 __allocation_guard<_ControlBlockAllocator> __guard(__a, 1); 1106 ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...); 1107 auto __control_block = __guard.__release_ptr(); 1108 return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block)); 1109 } 1110 1111 template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> > 1112 _LIBCPP_HIDE_FROM_ABI 1113 shared_ptr<_Tp> make_shared(_Args&& ...__args) 1114 { 1115 return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...); 1116 } 1117 1118 template<class _Tp, class _Up> 1119 inline _LIBCPP_INLINE_VISIBILITY 1120 bool 1121 operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1122 { 1123 return __x.get() == __y.get(); 1124 } 1125 1126 template<class _Tp, class _Up> 1127 inline _LIBCPP_INLINE_VISIBILITY 1128 bool 1129 operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1130 { 1131 return !(__x == __y); 1132 } 1133 1134 template<class _Tp, class _Up> 1135 inline _LIBCPP_INLINE_VISIBILITY 1136 bool 1137 operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1138 { 1139 #if _LIBCPP_STD_VER <= 11 1140 typedef typename common_type<_Tp*, _Up*>::type _Vp; 1141 return less<_Vp>()(__x.get(), __y.get()); 1142 #else 1143 return less<>()(__x.get(), __y.get()); 1144 #endif 1145 1146 } 1147 1148 template<class _Tp, class _Up> 1149 inline _LIBCPP_INLINE_VISIBILITY 1150 bool 1151 operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1152 { 1153 return __y < __x; 1154 } 1155 1156 template<class _Tp, class _Up> 1157 inline _LIBCPP_INLINE_VISIBILITY 1158 bool 1159 operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1160 { 1161 return !(__y < __x); 1162 } 1163 1164 template<class _Tp, class _Up> 1165 inline _LIBCPP_INLINE_VISIBILITY 1166 bool 1167 operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT 1168 { 1169 return !(__x < __y); 1170 } 1171 1172 template<class _Tp> 1173 inline _LIBCPP_INLINE_VISIBILITY 1174 bool 1175 operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1176 { 1177 return !__x; 1178 } 1179 1180 template<class _Tp> 1181 inline _LIBCPP_INLINE_VISIBILITY 1182 bool 1183 operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1184 { 1185 return !__x; 1186 } 1187 1188 template<class _Tp> 1189 inline _LIBCPP_INLINE_VISIBILITY 1190 bool 1191 operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1192 { 1193 return static_cast<bool>(__x); 1194 } 1195 1196 template<class _Tp> 1197 inline _LIBCPP_INLINE_VISIBILITY 1198 bool 1199 operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1200 { 1201 return static_cast<bool>(__x); 1202 } 1203 1204 template<class _Tp> 1205 inline _LIBCPP_INLINE_VISIBILITY 1206 bool 1207 operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1208 { 1209 return less<_Tp*>()(__x.get(), nullptr); 1210 } 1211 1212 template<class _Tp> 1213 inline _LIBCPP_INLINE_VISIBILITY 1214 bool 1215 operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1216 { 1217 return less<_Tp*>()(nullptr, __x.get()); 1218 } 1219 1220 template<class _Tp> 1221 inline _LIBCPP_INLINE_VISIBILITY 1222 bool 1223 operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1224 { 1225 return nullptr < __x; 1226 } 1227 1228 template<class _Tp> 1229 inline _LIBCPP_INLINE_VISIBILITY 1230 bool 1231 operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1232 { 1233 return __x < nullptr; 1234 } 1235 1236 template<class _Tp> 1237 inline _LIBCPP_INLINE_VISIBILITY 1238 bool 1239 operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1240 { 1241 return !(nullptr < __x); 1242 } 1243 1244 template<class _Tp> 1245 inline _LIBCPP_INLINE_VISIBILITY 1246 bool 1247 operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1248 { 1249 return !(__x < nullptr); 1250 } 1251 1252 template<class _Tp> 1253 inline _LIBCPP_INLINE_VISIBILITY 1254 bool 1255 operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT 1256 { 1257 return !(__x < nullptr); 1258 } 1259 1260 template<class _Tp> 1261 inline _LIBCPP_INLINE_VISIBILITY 1262 bool 1263 operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT 1264 { 1265 return !(nullptr < __x); 1266 } 1267 1268 template<class _Tp> 1269 inline _LIBCPP_INLINE_VISIBILITY 1270 void 1271 swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT 1272 { 1273 __x.swap(__y); 1274 } 1275 1276 template<class _Tp, class _Up> 1277 inline _LIBCPP_INLINE_VISIBILITY 1278 shared_ptr<_Tp> 1279 static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1280 { 1281 return shared_ptr<_Tp>(__r, 1282 static_cast< 1283 typename shared_ptr<_Tp>::element_type*>(__r.get())); 1284 } 1285 1286 template<class _Tp, class _Up> 1287 inline _LIBCPP_INLINE_VISIBILITY 1288 shared_ptr<_Tp> 1289 dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1290 { 1291 typedef typename shared_ptr<_Tp>::element_type _ET; 1292 _ET* __p = dynamic_cast<_ET*>(__r.get()); 1293 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>(); 1294 } 1295 1296 template<class _Tp, class _Up> 1297 shared_ptr<_Tp> 1298 const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1299 { 1300 typedef typename shared_ptr<_Tp>::element_type _RTp; 1301 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get())); 1302 } 1303 1304 template<class _Tp, class _Up> 1305 shared_ptr<_Tp> 1306 reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT 1307 { 1308 return shared_ptr<_Tp>(__r, 1309 reinterpret_cast< 1310 typename shared_ptr<_Tp>::element_type*>(__r.get())); 1311 } 1312 1313 #ifndef _LIBCPP_NO_RTTI 1314 1315 template<class _Dp, class _Tp> 1316 inline _LIBCPP_INLINE_VISIBILITY 1317 _Dp* 1318 get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT 1319 { 1320 return __p.template __get_deleter<_Dp>(); 1321 } 1322 1323 #endif // _LIBCPP_NO_RTTI 1324 1325 template<class _Tp> 1326 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr 1327 { 1328 public: 1329 typedef _Tp element_type; 1330 private: 1331 element_type* __ptr_; 1332 __shared_weak_count* __cntrl_; 1333 1334 public: 1335 _LIBCPP_INLINE_VISIBILITY 1336 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT; 1337 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r, 1338 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) 1339 _NOEXCEPT; 1340 _LIBCPP_INLINE_VISIBILITY 1341 weak_ptr(weak_ptr const& __r) _NOEXCEPT; 1342 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r, 1343 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) 1344 _NOEXCEPT; 1345 1346 _LIBCPP_INLINE_VISIBILITY 1347 weak_ptr(weak_ptr&& __r) _NOEXCEPT; 1348 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r, 1349 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) 1350 _NOEXCEPT; 1351 ~weak_ptr(); 1352 1353 _LIBCPP_INLINE_VISIBILITY 1354 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT; 1355 template<class _Yp> 1356 typename enable_if 1357 < 1358 is_convertible<_Yp*, element_type*>::value, 1359 weak_ptr& 1360 >::type 1361 _LIBCPP_INLINE_VISIBILITY 1362 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT; 1363 1364 _LIBCPP_INLINE_VISIBILITY 1365 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT; 1366 template<class _Yp> 1367 typename enable_if 1368 < 1369 is_convertible<_Yp*, element_type*>::value, 1370 weak_ptr& 1371 >::type 1372 _LIBCPP_INLINE_VISIBILITY 1373 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT; 1374 1375 template<class _Yp> 1376 typename enable_if 1377 < 1378 is_convertible<_Yp*, element_type*>::value, 1379 weak_ptr& 1380 >::type 1381 _LIBCPP_INLINE_VISIBILITY 1382 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT; 1383 1384 _LIBCPP_INLINE_VISIBILITY 1385 void swap(weak_ptr& __r) _NOEXCEPT; 1386 _LIBCPP_INLINE_VISIBILITY 1387 void reset() _NOEXCEPT; 1388 1389 _LIBCPP_INLINE_VISIBILITY 1390 long use_count() const _NOEXCEPT 1391 {return __cntrl_ ? __cntrl_->use_count() : 0;} 1392 _LIBCPP_INLINE_VISIBILITY 1393 bool expired() const _NOEXCEPT 1394 {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;} 1395 shared_ptr<_Tp> lock() const _NOEXCEPT; 1396 template<class _Up> 1397 _LIBCPP_INLINE_VISIBILITY 1398 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT 1399 {return __cntrl_ < __r.__cntrl_;} 1400 template<class _Up> 1401 _LIBCPP_INLINE_VISIBILITY 1402 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT 1403 {return __cntrl_ < __r.__cntrl_;} 1404 1405 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; 1406 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; 1407 }; 1408 1409 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES 1410 template<class _Tp> 1411 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; 1412 #endif 1413 1414 template<class _Tp> 1415 inline 1416 _LIBCPP_CONSTEXPR 1417 weak_ptr<_Tp>::weak_ptr() _NOEXCEPT 1418 : __ptr_(nullptr), 1419 __cntrl_(nullptr) 1420 { 1421 } 1422 1423 template<class _Tp> 1424 inline 1425 weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT 1426 : __ptr_(__r.__ptr_), 1427 __cntrl_(__r.__cntrl_) 1428 { 1429 if (__cntrl_) 1430 __cntrl_->__add_weak(); 1431 } 1432 1433 template<class _Tp> 1434 template<class _Yp> 1435 inline 1436 weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r, 1437 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) 1438 _NOEXCEPT 1439 : __ptr_(__r.__ptr_), 1440 __cntrl_(__r.__cntrl_) 1441 { 1442 if (__cntrl_) 1443 __cntrl_->__add_weak(); 1444 } 1445 1446 template<class _Tp> 1447 template<class _Yp> 1448 inline 1449 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r, 1450 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) 1451 _NOEXCEPT 1452 : __ptr_(__r.__ptr_), 1453 __cntrl_(__r.__cntrl_) 1454 { 1455 if (__cntrl_) 1456 __cntrl_->__add_weak(); 1457 } 1458 1459 template<class _Tp> 1460 inline 1461 weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT 1462 : __ptr_(__r.__ptr_), 1463 __cntrl_(__r.__cntrl_) 1464 { 1465 __r.__ptr_ = nullptr; 1466 __r.__cntrl_ = nullptr; 1467 } 1468 1469 template<class _Tp> 1470 template<class _Yp> 1471 inline 1472 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r, 1473 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) 1474 _NOEXCEPT 1475 : __ptr_(__r.__ptr_), 1476 __cntrl_(__r.__cntrl_) 1477 { 1478 __r.__ptr_ = nullptr; 1479 __r.__cntrl_ = nullptr; 1480 } 1481 1482 template<class _Tp> 1483 weak_ptr<_Tp>::~weak_ptr() 1484 { 1485 if (__cntrl_) 1486 __cntrl_->__release_weak(); 1487 } 1488 1489 template<class _Tp> 1490 inline 1491 weak_ptr<_Tp>& 1492 weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT 1493 { 1494 weak_ptr(__r).swap(*this); 1495 return *this; 1496 } 1497 1498 template<class _Tp> 1499 template<class _Yp> 1500 inline 1501 typename enable_if 1502 < 1503 is_convertible<_Yp*, _Tp*>::value, 1504 weak_ptr<_Tp>& 1505 >::type 1506 weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT 1507 { 1508 weak_ptr(__r).swap(*this); 1509 return *this; 1510 } 1511 1512 template<class _Tp> 1513 inline 1514 weak_ptr<_Tp>& 1515 weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT 1516 { 1517 weak_ptr(_VSTD::move(__r)).swap(*this); 1518 return *this; 1519 } 1520 1521 template<class _Tp> 1522 template<class _Yp> 1523 inline 1524 typename enable_if 1525 < 1526 is_convertible<_Yp*, _Tp*>::value, 1527 weak_ptr<_Tp>& 1528 >::type 1529 weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT 1530 { 1531 weak_ptr(_VSTD::move(__r)).swap(*this); 1532 return *this; 1533 } 1534 1535 template<class _Tp> 1536 template<class _Yp> 1537 inline 1538 typename enable_if 1539 < 1540 is_convertible<_Yp*, _Tp*>::value, 1541 weak_ptr<_Tp>& 1542 >::type 1543 weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT 1544 { 1545 weak_ptr(__r).swap(*this); 1546 return *this; 1547 } 1548 1549 template<class _Tp> 1550 inline 1551 void 1552 weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT 1553 { 1554 _VSTD::swap(__ptr_, __r.__ptr_); 1555 _VSTD::swap(__cntrl_, __r.__cntrl_); 1556 } 1557 1558 template<class _Tp> 1559 inline _LIBCPP_INLINE_VISIBILITY 1560 void 1561 swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT 1562 { 1563 __x.swap(__y); 1564 } 1565 1566 template<class _Tp> 1567 inline 1568 void 1569 weak_ptr<_Tp>::reset() _NOEXCEPT 1570 { 1571 weak_ptr().swap(*this); 1572 } 1573 1574 template<class _Tp> 1575 template<class _Yp> 1576 shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r, 1577 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type) 1578 : __ptr_(__r.__ptr_), 1579 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) 1580 { 1581 if (__cntrl_ == nullptr) 1582 __throw_bad_weak_ptr(); 1583 } 1584 1585 template<class _Tp> 1586 shared_ptr<_Tp> 1587 weak_ptr<_Tp>::lock() const _NOEXCEPT 1588 { 1589 shared_ptr<_Tp> __r; 1590 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_; 1591 if (__r.__cntrl_) 1592 __r.__ptr_ = __ptr_; 1593 return __r; 1594 } 1595 1596 #if _LIBCPP_STD_VER > 14 1597 template <class _Tp = void> struct owner_less; 1598 #else 1599 template <class _Tp> struct owner_less; 1600 #endif 1601 1602 1603 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 1604 template <class _Tp> 1605 struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > 1606 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES) 1607 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> 1608 #endif 1609 { 1610 _LIBCPP_SUPPRESS_DEPRECATED_POP 1611 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1612 _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type; 1613 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> first_argument_type; 1614 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> second_argument_type; 1615 #endif 1616 _LIBCPP_INLINE_VISIBILITY 1617 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT 1618 {return __x.owner_before(__y);} 1619 _LIBCPP_INLINE_VISIBILITY 1620 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT 1621 {return __x.owner_before(__y);} 1622 _LIBCPP_INLINE_VISIBILITY 1623 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT 1624 {return __x.owner_before(__y);} 1625 }; 1626 1627 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 1628 template <class _Tp> 1629 struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > 1630 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES) 1631 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> 1632 #endif 1633 { 1634 _LIBCPP_SUPPRESS_DEPRECATED_POP 1635 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1636 _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type; 1637 _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> first_argument_type; 1638 _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> second_argument_type; 1639 #endif 1640 _LIBCPP_INLINE_VISIBILITY 1641 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT 1642 {return __x.owner_before(__y);} 1643 _LIBCPP_INLINE_VISIBILITY 1644 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT 1645 {return __x.owner_before(__y);} 1646 _LIBCPP_INLINE_VISIBILITY 1647 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT 1648 {return __x.owner_before(__y);} 1649 }; 1650 1651 #if _LIBCPP_STD_VER > 14 1652 template <> 1653 struct _LIBCPP_TEMPLATE_VIS owner_less<void> 1654 { 1655 template <class _Tp, class _Up> 1656 _LIBCPP_INLINE_VISIBILITY 1657 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT 1658 {return __x.owner_before(__y);} 1659 template <class _Tp, class _Up> 1660 _LIBCPP_INLINE_VISIBILITY 1661 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT 1662 {return __x.owner_before(__y);} 1663 template <class _Tp, class _Up> 1664 _LIBCPP_INLINE_VISIBILITY 1665 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT 1666 {return __x.owner_before(__y);} 1667 template <class _Tp, class _Up> 1668 _LIBCPP_INLINE_VISIBILITY 1669 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT 1670 {return __x.owner_before(__y);} 1671 typedef void is_transparent; 1672 }; 1673 #endif 1674 1675 template<class _Tp> 1676 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this 1677 { 1678 mutable weak_ptr<_Tp> __weak_this_; 1679 protected: 1680 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 1681 enable_shared_from_this() _NOEXCEPT {} 1682 _LIBCPP_INLINE_VISIBILITY 1683 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {} 1684 _LIBCPP_INLINE_VISIBILITY 1685 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT 1686 {return *this;} 1687 _LIBCPP_INLINE_VISIBILITY 1688 ~enable_shared_from_this() {} 1689 public: 1690 _LIBCPP_INLINE_VISIBILITY 1691 shared_ptr<_Tp> shared_from_this() 1692 {return shared_ptr<_Tp>(__weak_this_);} 1693 _LIBCPP_INLINE_VISIBILITY 1694 shared_ptr<_Tp const> shared_from_this() const 1695 {return shared_ptr<const _Tp>(__weak_this_);} 1696 1697 #if _LIBCPP_STD_VER > 14 1698 _LIBCPP_INLINE_VISIBILITY 1699 weak_ptr<_Tp> weak_from_this() _NOEXCEPT 1700 { return __weak_this_; } 1701 1702 _LIBCPP_INLINE_VISIBILITY 1703 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT 1704 { return __weak_this_; } 1705 #endif // _LIBCPP_STD_VER > 14 1706 1707 template <class _Up> friend class shared_ptr; 1708 }; 1709 1710 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash; 1711 1712 template <class _Tp> 1713 struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > 1714 { 1715 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) 1716 _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type; 1717 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; 1718 #endif 1719 1720 _LIBCPP_INLINE_VISIBILITY 1721 size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT 1722 { 1723 return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get()); 1724 } 1725 }; 1726 1727 template<class _CharT, class _Traits, class _Yp> 1728 inline _LIBCPP_INLINE_VISIBILITY 1729 basic_ostream<_CharT, _Traits>& 1730 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p); 1731 1732 1733 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) 1734 1735 class _LIBCPP_TYPE_VIS __sp_mut 1736 { 1737 void* __lx; 1738 public: 1739 void lock() _NOEXCEPT; 1740 void unlock() _NOEXCEPT; 1741 1742 private: 1743 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT; 1744 __sp_mut(const __sp_mut&); 1745 __sp_mut& operator=(const __sp_mut&); 1746 1747 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*); 1748 }; 1749 1750 _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1751 __sp_mut& __get_sp_mut(const void*); 1752 1753 template <class _Tp> 1754 inline _LIBCPP_INLINE_VISIBILITY 1755 bool 1756 atomic_is_lock_free(const shared_ptr<_Tp>*) 1757 { 1758 return false; 1759 } 1760 1761 template <class _Tp> 1762 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1763 shared_ptr<_Tp> 1764 atomic_load(const shared_ptr<_Tp>* __p) 1765 { 1766 __sp_mut& __m = __get_sp_mut(__p); 1767 __m.lock(); 1768 shared_ptr<_Tp> __q = *__p; 1769 __m.unlock(); 1770 return __q; 1771 } 1772 1773 template <class _Tp> 1774 inline _LIBCPP_INLINE_VISIBILITY 1775 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1776 shared_ptr<_Tp> 1777 atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) 1778 { 1779 return atomic_load(__p); 1780 } 1781 1782 template <class _Tp> 1783 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1784 void 1785 atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) 1786 { 1787 __sp_mut& __m = __get_sp_mut(__p); 1788 __m.lock(); 1789 __p->swap(__r); 1790 __m.unlock(); 1791 } 1792 1793 template <class _Tp> 1794 inline _LIBCPP_INLINE_VISIBILITY 1795 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1796 void 1797 atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) 1798 { 1799 atomic_store(__p, __r); 1800 } 1801 1802 template <class _Tp> 1803 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1804 shared_ptr<_Tp> 1805 atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) 1806 { 1807 __sp_mut& __m = __get_sp_mut(__p); 1808 __m.lock(); 1809 __p->swap(__r); 1810 __m.unlock(); 1811 return __r; 1812 } 1813 1814 template <class _Tp> 1815 inline _LIBCPP_INLINE_VISIBILITY 1816 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1817 shared_ptr<_Tp> 1818 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) 1819 { 1820 return atomic_exchange(__p, __r); 1821 } 1822 1823 template <class _Tp> 1824 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1825 bool 1826 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) 1827 { 1828 shared_ptr<_Tp> __temp; 1829 __sp_mut& __m = __get_sp_mut(__p); 1830 __m.lock(); 1831 if (__p->__owner_equivalent(*__v)) 1832 { 1833 _VSTD::swap(__temp, *__p); 1834 *__p = __w; 1835 __m.unlock(); 1836 return true; 1837 } 1838 _VSTD::swap(__temp, *__v); 1839 *__v = *__p; 1840 __m.unlock(); 1841 return false; 1842 } 1843 1844 template <class _Tp> 1845 inline _LIBCPP_INLINE_VISIBILITY 1846 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1847 bool 1848 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) 1849 { 1850 return atomic_compare_exchange_strong(__p, __v, __w); 1851 } 1852 1853 template <class _Tp> 1854 inline _LIBCPP_INLINE_VISIBILITY 1855 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1856 bool 1857 atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, 1858 shared_ptr<_Tp> __w, memory_order, memory_order) 1859 { 1860 return atomic_compare_exchange_strong(__p, __v, __w); 1861 } 1862 1863 template <class _Tp> 1864 inline _LIBCPP_INLINE_VISIBILITY 1865 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR 1866 bool 1867 atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, 1868 shared_ptr<_Tp> __w, memory_order, memory_order) 1869 { 1870 return atomic_compare_exchange_weak(__p, __v, __w); 1871 } 1872 1873 #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) 1874 1875 _LIBCPP_END_NAMESPACE_STD 1876 1877 _LIBCPP_POP_MACROS 1878 1879 #endif // _LIBCPP___MEMORY_SHARED_PTR_H 1880