| 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 <__atomic/memory_order.h> |
| 14 | #include <__compare/compare_three_way.h> |
| 15 | #include <__compare/ordering.h> |
| 16 | #include <__config> |
| 17 | #include <__cstddef/nullptr_t.h> |
| 18 | #include <__cstddef/ptrdiff_t.h> |
| 19 | #include <__exception/exception.h> |
| 20 | #include <__functional/binary_function.h> |
| 21 | #include <__functional/operations.h> |
| 22 | #include <__functional/reference_wrapper.h> |
| 23 | #include <__fwd/ostream.h> |
| 24 | #include <__iterator/access.h> |
| 25 | #include <__memory/addressof.h> |
| 26 | #include <__memory/allocation_guard.h> |
| 27 | #include <__memory/allocator.h> |
| 28 | #include <__memory/allocator_destructor.h> |
| 29 | #include <__memory/allocator_traits.h> |
| 30 | #include <__memory/auto_ptr.h> |
| 31 | #include <__memory/construct_at.h> |
| 32 | #include <__memory/destroy.h> |
| 33 | #include <__memory/pointer_traits.h> |
| 34 | #include <__memory/shared_count.h> |
| 35 | #include <__memory/uninitialized_algorithms.h> |
| 36 | #include <__memory/uninitialized_multidimensional_algorithms.h> |
| 37 | #include <__memory/unique_ptr.h> |
| 38 | #include <__type_traits/add_reference.h> |
| 39 | #include <__type_traits/conditional.h> |
| 40 | #include <__type_traits/conjunction.h> |
| 41 | #include <__type_traits/enable_if.h> |
| 42 | #include <__type_traits/is_array.h> |
| 43 | #include <__type_traits/is_constructible.h> |
| 44 | #include <__type_traits/is_convertible.h> |
| 45 | #include <__type_traits/is_function.h> |
| 46 | #include <__type_traits/is_reference.h> |
| 47 | #include <__type_traits/is_same.h> |
| 48 | #include <__type_traits/nat.h> |
| 49 | #include <__type_traits/remove_cv.h> |
| 50 | #include <__type_traits/remove_extent.h> |
| 51 | #include <__type_traits/remove_pointer.h> |
| 52 | #include <__type_traits/remove_reference.h> |
| 53 | #include <__type_traits/void_t.h> |
| 54 | #include <__utility/declval.h> |
| 55 | #include <__utility/exception_guard.h> |
| 56 | #include <__utility/forward.h> |
| 57 | #include <__utility/move.h> |
| 58 | #include <__utility/swap.h> |
| 59 | #include <__verbose_abort> |
| 60 | #include <typeinfo> |
| 61 | |
| 62 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 63 | # pragma GCC system_header |
| 64 | #endif |
| 65 | |
| 66 | _LIBCPP_PUSH_MACROS |
| 67 | #include <__undef_macros> |
| 68 | |
| 69 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 70 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 71 | |
| 72 | class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception { |
| 73 | public: |
| 74 | _LIBCPP_HIDE_FROM_ABI bad_weak_ptr() _NOEXCEPT = default; |
| 75 | _LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default; |
| 76 | _LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default; |
| 77 | ~bad_weak_ptr() _NOEXCEPT override; |
| 78 | [[__nodiscard__]] const char* what() const _NOEXCEPT override; |
| 79 | }; |
| 80 | |
| 81 | [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_weak_ptr() { |
| 82 | #if _LIBCPP_HAS_EXCEPTIONS |
| 83 | throw bad_weak_ptr(); |
| 84 | #else |
| 85 | _LIBCPP_VERBOSE_ABORT("bad_weak_ptr was thrown in -fno-exceptions mode" ); |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | template <class _Tp> |
| 90 | class weak_ptr; |
| 91 | |
| 92 | template <class _Tp, class _Dp, class _Alloc> |
| 93 | class _LIBCPP_HIDE_STRUCT_FROM_ABI __shared_ptr_pointer final : public __shared_weak_count { |
| 94 | _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; |
| 95 | _LIBCPP_NO_UNIQUE_ADDRESS _Dp __deleter_; |
| 96 | _LIBCPP_NO_UNIQUE_ADDRESS _Tp __ptr_; |
| 97 | |
| 98 | public: |
| 99 | _LIBCPP_HIDE_FROM_ABI __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a) |
| 100 | : __alloc_(std::move(__a)), __deleter_(std::move(__d)), __ptr_(__p) {} |
| 101 | |
| 102 | #if _LIBCPP_HAS_RTTI |
| 103 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL const void* __get_deleter(const type_info& __t) const _NOEXCEPT override { |
| 104 | return __t == typeid(_Dp) ? std::addressof(__deleter_) : nullptr; |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | private: |
| 109 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { |
| 110 | __deleter_(__ptr_); |
| 111 | __ptr_.~_Tp(); |
| 112 | __deleter_.~_Dp(); |
| 113 | } |
| 114 | |
| 115 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { |
| 116 | using __alloc_t = __allocator_traits_rebind_t<_Alloc, __shared_ptr_pointer>; |
| 117 | |
| 118 | __alloc_t __a(__alloc_); |
| 119 | __alloc_.~_Alloc(); |
| 120 | __a.deallocate(pointer_traits<typename allocator_traits<__alloc_t>::pointer>::pointer_to(*this), 1); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | // This tag is used to instantiate an allocator type. The various shared_ptr control blocks |
| 125 | // detect that the allocator has been instantiated for this type and perform alternative |
| 126 | // initialization/destruction based on that. |
| 127 | struct __for_overwrite_tag {}; |
| 128 | |
| 129 | template <class _Tp, class _Alloc> |
| 130 | struct _LIBCPP_HIDE_STRUCT_FROM_ABI __shared_ptr_emplace : __shared_weak_count { |
| 131 | using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<_Alloc>; |
| 132 | using __value_type _LIBCPP_NODEBUG = __remove_cv_t<_Tp>; |
| 133 | |
| 134 | template <class... _Args> |
| 135 | _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&... __args) : __alloc_(std::move(__a)) { |
| 136 | allocator_traits<_Alloc>::construct(__alloc_, __get_elem(), std::forward<_Args>(__args)...); |
| 137 | } |
| 138 | |
| 139 | _LIBCPP_HIDE_FROM_ABI __value_type* __get_elem() _NOEXCEPT { return reinterpret_cast<__value_type*>(__buffer_); } |
| 140 | |
| 141 | private: |
| 142 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { |
| 143 | allocator_traits<_Alloc>::destroy(__alloc_, __get_elem()); |
| 144 | } |
| 145 | |
| 146 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { |
| 147 | using __alloc_t = __allocator_traits_rebind_t<_Alloc, __shared_ptr_emplace>; |
| 148 | __alloc_t __tmp(__alloc_); |
| 149 | __alloc_.~_Alloc(); |
| 150 | allocator_traits<__alloc_t>::deallocate( |
| 151 | __tmp, pointer_traits<typename allocator_traits<__alloc_t>::pointer>::pointer_to(*this), 1); |
| 152 | } |
| 153 | |
| 154 | _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; |
| 155 | _ALIGNAS_TYPE(__value_type) char __buffer_[sizeof(__value_type)]; |
| 156 | }; |
| 157 | |
| 158 | template <class _Tp, class _Alloc> |
| 159 | struct _LIBCPP_HIDE_STRUCT_FROM_ABI __shared_ptr_emplace_for_overwrite : __shared_weak_count { |
| 160 | using __value_type _LIBCPP_NODEBUG = __remove_cv_t<_Tp>; |
| 161 | |
| 162 | _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace_for_overwrite(_Alloc __a) : __alloc_(std::move(__a)) {} |
| 163 | |
| 164 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { __value_.~__value_type(); } |
| 165 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { |
| 166 | using __alloc_t = __allocator_traits_rebind_t<_Alloc, __shared_ptr_emplace_for_overwrite>; |
| 167 | using __alloc_traits = allocator_traits<__alloc_t>; |
| 168 | __alloc_t __tmp(__alloc_); |
| 169 | __alloc_.~_Alloc(); |
| 170 | __alloc_traits::deallocate(__tmp, pointer_traits<typename __alloc_traits::pointer>::pointer_to(*this), 1); |
| 171 | } |
| 172 | |
| 173 | _LIBCPP_HIDE_FROM_ABI __value_type* __get_elem() _NOEXCEPT { return std::addressof(__value_); } |
| 174 | |
| 175 | private: |
| 176 | _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; |
| 177 | _LIBCPP_NO_UNIQUE_ADDRESS __value_type __value_; |
| 178 | }; |
| 179 | |
| 180 | struct __shared_ptr_dummy_rebind_allocator_type; |
| 181 | template <> |
| 182 | class allocator<__shared_ptr_dummy_rebind_allocator_type> { |
| 183 | public: |
| 184 | template <class _Other> |
| 185 | struct rebind { |
| 186 | typedef allocator<_Other> other; |
| 187 | }; |
| 188 | }; |
| 189 | |
| 190 | template <class _Tp> |
| 191 | class enable_shared_from_this; |
| 192 | |
| 193 | // http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.general-6 |
| 194 | // A pointer type Y* is said to be compatible with a pointer type T* |
| 195 | // when either Y* is convertible to T* or Y is U[N] and T is cv U[]. |
| 196 | #if _LIBCPP_STD_VER >= 17 |
| 197 | template <class _Yp, class _Tp> |
| 198 | inline const bool __bounded_convertible_to_unbounded_v = false; |
| 199 | |
| 200 | template <class _Up, std::size_t _Np, class _Tp> |
| 201 | inline const bool __bounded_convertible_to_unbounded_v<_Up[_Np], _Tp> = is_same_v<remove_cv_t<_Tp>, _Up[]>; |
| 202 | |
| 203 | template <class _Yp, class _Tp> |
| 204 | inline const bool __compatible_with_v = is_convertible_v<_Yp*, _Tp*> || __bounded_convertible_to_unbounded_v<_Yp, _Tp>; |
| 205 | #else |
| 206 | template <class _Yp, class _Tp> |
| 207 | inline const bool __compatible_with_v = is_convertible<_Yp*, _Tp*>::value; |
| 208 | #endif // _LIBCPP_STD_VER >= 17 |
| 209 | |
| 210 | // Constructors that take raw pointers have a different set of "compatible" constraints |
| 211 | // http://eel.is/c++draft/util.sharedptr#util.smartptr.shared.const-9.1 |
| 212 | // - If T is an array type, then either T is U[N] and Y(*)[N] is convertible to T*, |
| 213 | // or T is U[] and Y(*)[] is convertible to T*. |
| 214 | // - If T is not an array type, then Y* is convertible to T*. |
| 215 | #if _LIBCPP_STD_VER >= 17 |
| 216 | template <class _Yp, class _Tp, class = void> |
| 217 | inline const bool __raw_pointer_compatible_with_v = !is_array_v<_Tp> && is_convertible_v<_Yp*, _Tp*>; |
| 218 | |
| 219 | template <class _Yp, class _Up, std::size_t _Np> |
| 220 | inline const bool |
| 221 | __raw_pointer_compatible_with_v<_Yp, _Up[_Np], enable_if_t<is_convertible_v<_Yp (*)[_Np], _Up (*)[_Np]>>> = true; |
| 222 | |
| 223 | template <class _Yp, class _Up> |
| 224 | inline const bool __raw_pointer_compatible_with_v<_Yp, _Up[], enable_if_t<is_convertible_v<_Yp (*)[], _Up (*)[]>>> = |
| 225 | true; |
| 226 | |
| 227 | #else |
| 228 | template <class _Yp, class _Tp> |
| 229 | inline const bool __raw_pointer_compatible_with_v = is_convertible<_Yp*, _Tp*>::value; |
| 230 | #endif // _LIBCPP_STD_VER >= 17 |
| 231 | |
| 232 | template <class _Ptr, class = void> |
| 233 | inline const bool __is_deletable_v = false; |
| 234 | template <class _Ptr> |
| 235 | inline const bool __is_deletable_v<_Ptr, decltype(delete std::declval<_Ptr>())> = true; |
| 236 | |
| 237 | template <class _Ptr, class = void> |
| 238 | inline const bool __is_array_deletable_v = false; |
| 239 | template <class _Ptr> |
| 240 | inline const bool __is_array_deletable_v<_Ptr, decltype(delete[] std::declval<_Ptr>())> = true; |
| 241 | |
| 242 | template <class _Dp, class _Pt, class = void> |
| 243 | inline const bool __well_formed_deleter_v = false; |
| 244 | |
| 245 | template <class _Dp, class _Pt> |
| 246 | inline const bool __well_formed_deleter_v<_Dp, _Pt, __void_t<decltype(std::declval<_Dp>()(std::declval<_Pt>()))> > = |
| 247 | true; |
| 248 | |
| 249 | template <class _Dp, class _Yp, class _Tp> |
| 250 | inline const bool __shared_ptr_deleter_ctor_reqs_v = |
| 251 | __raw_pointer_compatible_with_v<_Yp, _Tp> && is_move_constructible<_Dp>::value && |
| 252 | __well_formed_deleter_v<_Dp, _Yp*>; |
| 253 | |
| 254 | template <class _Dp> |
| 255 | inline const bool __shared_ptr_nullptr_deleter_ctor_reqs_v = |
| 256 | is_move_constructible<_Dp>::value && __well_formed_deleter_v<_Dp, nullptr_t>; |
| 257 | |
| 258 | #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI) |
| 259 | # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__)) |
| 260 | #else |
| 261 | # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI |
| 262 | #endif |
| 263 | |
| 264 | template <class _Tp> |
| 265 | class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr { |
| 266 | struct __nullptr_sfinae_tag {}; |
| 267 | |
| 268 | public: |
| 269 | #if _LIBCPP_STD_VER >= 17 |
| 270 | typedef weak_ptr<_Tp> weak_type; |
| 271 | typedef remove_extent_t<_Tp> element_type; |
| 272 | #else |
| 273 | typedef _Tp element_type; |
| 274 | #endif |
| 275 | |
| 276 | // A shared_ptr contains only two raw pointers which point to the heap and move constructing already doesn't require |
| 277 | // any bookkeeping, so it's always trivially relocatable. |
| 278 | using __trivially_relocatable _LIBCPP_NODEBUG = shared_ptr; |
| 279 | |
| 280 | private: |
| 281 | element_type* __ptr_; |
| 282 | __shared_weak_count* __cntrl_; |
| 283 | |
| 284 | public: |
| 285 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {} |
| 286 | |
| 287 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {} |
| 288 | |
| 289 | template <class _Yp, |
| 290 | __enable_if_t<__raw_pointer_compatible_with_v<_Yp, _Tp> |
| 291 | // In C++03 we get errors when trying to do SFINAE with the |
| 292 | // delete operator, so we always pretend that it's deletable. |
| 293 | // The same happens on GCC. |
| 294 | #if !defined(_LIBCPP_CXX03_LANG) |
| 295 | && (is_array<_Tp>::value ? __is_array_deletable_v<_Yp*> : __is_deletable_v<_Yp*>) |
| 296 | #endif |
| 297 | , |
| 298 | int> = 0> |
| 299 | _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(_Yp* __p) : __ptr_(__p) { |
| 300 | unique_ptr<_Yp> __hold(__p); |
| 301 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
| 302 | typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk; |
| 303 | __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT()); |
| 304 | __hold.release(); |
| 305 | __enable_weak_this(__p, __p); |
| 306 | } |
| 307 | |
| 308 | template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs_v<_Dp, _Yp, _Tp>, int> = 0> |
| 309 | _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d) : __ptr_(__p) { |
| 310 | auto __guard = std::__make_exception_guard([&] { __d(__p); }); |
| 311 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
| 312 | typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk; |
| 313 | #ifndef _LIBCPP_CXX03_LANG |
| 314 | __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); |
| 315 | #else |
| 316 | __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); |
| 317 | #endif // not _LIBCPP_CXX03_LANG |
| 318 | __enable_weak_this(__p, __p); |
| 319 | __guard.__complete(); |
| 320 | } |
| 321 | |
| 322 | template <class _Yp, class _Dp, class _Alloc, __enable_if_t<__shared_ptr_deleter_ctor_reqs_v<_Dp, _Yp, _Tp>, int> = 0> |
| 323 | _LIBCPP_HIDE_FROM_ABI shared_ptr(_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) { |
| 324 | auto __guard = std::__make_exception_guard([&] { __d(__p); }); |
| 325 | typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; |
| 326 | typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; |
| 327 | typedef __allocator_destructor<_A2> _D2; |
| 328 | _A2 __a2(__a); |
| 329 | unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); |
| 330 | ::new ((void*)std::addressof(*__hold2.get())) |
| 331 | #ifndef _LIBCPP_CXX03_LANG |
| 332 | _CntrlBlk(__p, std::move(__d), __a); |
| 333 | #else |
| 334 | _CntrlBlk(__p, __d, __a); |
| 335 | #endif // not _LIBCPP_CXX03_LANG |
| 336 | __cntrl_ = std::addressof(*__hold2.release()); |
| 337 | __enable_weak_this(__p, __p); |
| 338 | __guard.__complete(); |
| 339 | } |
| 340 | |
| 341 | template <class _Dp> |
| 342 | _LIBCPP_HIDE_FROM_ABI shared_ptr( |
| 343 | nullptr_t __p, |
| 344 | _Dp __d, |
| 345 | __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs_v<_Dp>, __nullptr_sfinae_tag> = __nullptr_sfinae_tag()) |
| 346 | : __ptr_(nullptr) { |
| 347 | auto __guard = std::__make_exception_guard([&] { __d(__p); }); |
| 348 | typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; |
| 349 | typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk; |
| 350 | #ifndef _LIBCPP_CXX03_LANG |
| 351 | __cntrl_ = new _CntrlBlk(__p, std::move(__d), _AllocT()); |
| 352 | #else |
| 353 | __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); |
| 354 | #endif // not _LIBCPP_CXX03_LANG |
| 355 | __guard.__complete(); |
| 356 | } |
| 357 | |
| 358 | template <class _Dp, class _Alloc> |
| 359 | _LIBCPP_HIDE_FROM_ABI shared_ptr( |
| 360 | nullptr_t __p, |
| 361 | _Dp __d, |
| 362 | _Alloc __a, |
| 363 | __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs_v<_Dp>, __nullptr_sfinae_tag> = __nullptr_sfinae_tag()) |
| 364 | : __ptr_(nullptr) { |
| 365 | auto __guard = std::__make_exception_guard([&] { __d(__p); }); |
| 366 | typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk; |
| 367 | typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; |
| 368 | typedef __allocator_destructor<_A2> _D2; |
| 369 | _A2 __a2(__a); |
| 370 | unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); |
| 371 | ::new ((void*)std::addressof(*__hold2.get())) |
| 372 | #ifndef _LIBCPP_CXX03_LANG |
| 373 | _CntrlBlk(__p, std::move(__d), __a); |
| 374 | #else |
| 375 | _CntrlBlk(__p, __d, __a); |
| 376 | #endif // not _LIBCPP_CXX03_LANG |
| 377 | __cntrl_ = std::addressof(*__hold2.release()); |
| 378 | __guard.__complete(); |
| 379 | } |
| 380 | |
| 381 | template <class _Yp> |
| 382 | _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT |
| 383 | : __ptr_(__p), |
| 384 | __cntrl_(__r.__cntrl_) { |
| 385 | if (__cntrl_) |
| 386 | __cntrl_->__add_shared(); |
| 387 | } |
| 388 | |
| 389 | // LWG-2996 |
| 390 | // We don't backport because it is an evolutionary change. |
| 391 | #if _LIBCPP_STD_VER >= 20 |
| 392 | template <class _Yp> |
| 393 | _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept |
| 394 | : __ptr_(__p), __cntrl_(__r.__cntrl_) { |
| 395 | __r.__ptr_ = nullptr; |
| 396 | __r.__cntrl_ = nullptr; |
| 397 | } |
| 398 | #endif |
| 399 | |
| 400 | _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 401 | if (__cntrl_) |
| 402 | __cntrl_->__add_shared(); |
| 403 | } |
| 404 | |
| 405 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 406 | _LIBCPP_HIDE_FROM_ABI shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 407 | if (__cntrl_) |
| 408 | __cntrl_->__add_shared(); |
| 409 | } |
| 410 | |
| 411 | _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 412 | __r.__ptr_ = nullptr; |
| 413 | __r.__cntrl_ = nullptr; |
| 414 | } |
| 415 | |
| 416 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 417 | _LIBCPP_HIDE_FROM_ABI shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 418 | __r.__ptr_ = nullptr; |
| 419 | __r.__cntrl_ = nullptr; |
| 420 | } |
| 421 | |
| 422 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 423 | _LIBCPP_HIDE_FROM_ABI explicit shared_ptr(const weak_ptr<_Yp>& __r) |
| 424 | : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) { |
| 425 | if (__cntrl_ == nullptr) |
| 426 | std::__throw_bad_weak_ptr(); |
| 427 | } |
| 428 | |
| 429 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 430 | template <class _Yp, __enable_if_t<is_convertible<_Yp*, element_type*>::value, int> = 0> |
| 431 | _LIBCPP_HIDE_FROM_ABI shared_ptr(auto_ptr<_Yp>&& __r) : __ptr_(__r.get()) { |
| 432 | typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<__remove_cv_t<_Yp> > > _CntrlBlk; |
| 433 | __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<__remove_cv_t<_Yp> >()); |
| 434 | __enable_weak_this(__r.get(), __r.get()); |
| 435 | __r.release(); |
| 436 | } |
| 437 | #endif |
| 438 | |
| 439 | template <class _Yp, |
| 440 | class _Dp, |
| 441 | __enable_if_t<__compatible_with_v<_Yp, _Tp> && |
| 442 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 443 | int> = 0> |
| 444 | _LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) { |
| 445 | using _AllocT = typename __shared_ptr_default_allocator<_Yp>::type; |
| 446 | using _Deleter = _If<is_lvalue_reference<_Dp>::value, reference_wrapper<__libcpp_remove_reference_t<_Dp> >, _Dp>; |
| 447 | using _CntrlBlk = __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Deleter, _AllocT>; |
| 448 | |
| 449 | __cntrl_ = __ptr_ ? new _CntrlBlk(__r.get(), std::forward<_Dp>(__r.get_deleter()), _AllocT()) : nullptr; |
| 450 | __enable_weak_this(__r.get(), __r.get()); |
| 451 | __r.release(); |
| 452 | } |
| 453 | |
| 454 | _LIBCPP_HIDE_FROM_ABI ~shared_ptr() { |
| 455 | if (__cntrl_) |
| 456 | __cntrl_->__release_shared(); |
| 457 | } |
| 458 | |
| 459 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT { |
| 460 | shared_ptr(__r).swap(r&: *this); |
| 461 | return *this; |
| 462 | } |
| 463 | |
| 464 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 465 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT { |
| 466 | shared_ptr(__r).swap(r&: *this); |
| 467 | return *this; |
| 468 | } |
| 469 | |
| 470 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT { |
| 471 | shared_ptr(std::move(__r)).swap(r&: *this); |
| 472 | return *this; |
| 473 | } |
| 474 | |
| 475 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 476 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r) { |
| 477 | shared_ptr(std::move(__r)).swap(r&: *this); |
| 478 | return *this; |
| 479 | } |
| 480 | |
| 481 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 482 | template <class _Yp, |
| 483 | __enable_if_t<!is_array<_Yp>::value && is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value, |
| 484 | int> = 0> |
| 485 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r) { |
| 486 | shared_ptr(std::move(__r)).swap(*this); |
| 487 | return *this; |
| 488 | } |
| 489 | #endif |
| 490 | |
| 491 | template <class _Yp, |
| 492 | class _Dp, |
| 493 | __enable_if_t<__compatible_with_v<_Yp, _Tp> && |
| 494 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 495 | int> = 0> |
| 496 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r) { |
| 497 | shared_ptr(std::move(__r)).swap(r&: *this); |
| 498 | return *this; |
| 499 | } |
| 500 | |
| 501 | _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr& __r) _NOEXCEPT { |
| 502 | std::swap(__ptr_, __r.__ptr_); |
| 503 | std::swap(x&: __cntrl_, y&: __r.__cntrl_); |
| 504 | } |
| 505 | |
| 506 | _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { shared_ptr().swap(r&: *this); } |
| 507 | |
| 508 | template <class _Yp, __enable_if_t<__raw_pointer_compatible_with_v<_Yp, _Tp>, int> = 0> |
| 509 | _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p) { |
| 510 | shared_ptr(__p).swap(r&: *this); |
| 511 | } |
| 512 | |
| 513 | template <class _Yp, class _Dp, __enable_if_t<__shared_ptr_deleter_ctor_reqs_v<_Dp, _Yp, _Tp>, int> = 0> |
| 514 | _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d) { |
| 515 | shared_ptr(__p, __d).swap(r&: *this); |
| 516 | } |
| 517 | |
| 518 | template <class _Yp, class _Dp, class _Alloc, __enable_if_t<__shared_ptr_deleter_ctor_reqs_v<_Dp, _Yp, _Tp>, int> = 0> |
| 519 | _LIBCPP_HIDE_FROM_ABI void reset(_Yp* __p, _Dp __d, _Alloc __a) { |
| 520 | shared_ptr(__p, __d, __a).swap(r&: *this); |
| 521 | } |
| 522 | |
| 523 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; } |
| 524 | |
| 525 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT { |
| 526 | return *__ptr_; |
| 527 | } |
| 528 | |
| 529 | _LIBCPP_HIDE_FROM_ABI element_type* operator->() const _NOEXCEPT { |
| 530 | static_assert(!is_array<_Tp>::value, "std::shared_ptr<T>::operator-> is only valid when T is not an array type." ); |
| 531 | return __ptr_; |
| 532 | } |
| 533 | |
| 534 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { |
| 535 | return __cntrl_ ? __cntrl_->use_count() : 0; |
| 536 | } |
| 537 | |
| 538 | #if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE) |
| 539 | [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { |
| 540 | return use_count() == 1; |
| 541 | } |
| 542 | #endif |
| 543 | |
| 544 | _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; } |
| 545 | |
| 546 | template <class _Up> |
| 547 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT { |
| 548 | return __cntrl_ < __p.__cntrl_; |
| 549 | } |
| 550 | |
| 551 | template <class _Up> |
| 552 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT { |
| 553 | return __cntrl_ < __p.__cntrl_; |
| 554 | } |
| 555 | |
| 556 | _LIBCPP_HIDE_FROM_ABI bool __owner_equivalent(const shared_ptr& __p) const { return __cntrl_ == __p.__cntrl_; } |
| 557 | |
| 558 | #if _LIBCPP_STD_VER >= 17 |
| 559 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const { |
| 560 | static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type." ); |
| 561 | return __ptr_[__i]; |
| 562 | } |
| 563 | #endif |
| 564 | |
| 565 | #if _LIBCPP_HAS_RTTI |
| 566 | template <class _Dp> |
| 567 | _LIBCPP_HIDE_FROM_ABI _Dp* __get_deleter() const _NOEXCEPT { |
| 568 | return static_cast<_Dp*>(__cntrl_ ? const_cast<void*>(__cntrl_->__get_deleter(typeid(_Dp))) : nullptr); |
| 569 | } |
| 570 | #endif // _LIBCPP_HAS_RTTI |
| 571 | |
| 572 | template <class _Yp, class _CntrlBlk> |
| 573 | _LIBCPP_HIDE_FROM_ABI static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT { |
| 574 | shared_ptr<_Tp> __r; |
| 575 | __r.__ptr_ = __p; |
| 576 | __r.__cntrl_ = __cntrl; |
| 577 | __r.__enable_weak_this(__r.__ptr_, __r.__ptr_); |
| 578 | return __r; |
| 579 | } |
| 580 | |
| 581 | private: |
| 582 | template <class _Yp, bool = is_function<_Yp>::value> |
| 583 | struct __shared_ptr_default_allocator { |
| 584 | typedef allocator<__remove_cv_t<_Yp> > type; |
| 585 | }; |
| 586 | |
| 587 | template <class _Yp> |
| 588 | struct __shared_ptr_default_allocator<_Yp, true> { |
| 589 | typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type; |
| 590 | }; |
| 591 | |
| 592 | template <class _Yp, |
| 593 | class _OrigPtr, |
| 594 | __enable_if_t<is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value, int> = 0> |
| 595 | _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT { |
| 596 | typedef __remove_cv_t<_Yp> _RawYp; |
| 597 | if (__e && __e->__weak_this_.expired()) { |
| 598 | __e->__weak_this_ = shared_ptr<_RawYp>(*this, const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr))); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | template <class _Vp> |
| 603 | static __type_identity<_Vp> __get_shared_from_this_impl(const enable_shared_from_this<_Vp>*); |
| 604 | |
| 605 | template <class _Vp, class = void> |
| 606 | struct __get_shared_from_this { |
| 607 | using type _LIBCPP_NODEBUG = __nat; |
| 608 | }; |
| 609 | |
| 610 | template <class _Vp> |
| 611 | struct __get_shared_from_this<_Vp, __void_t<decltype(__get_shared_from_this_impl(static_cast<_Vp*>(nullptr)))> > { |
| 612 | using type _LIBCPP_NODEBUG = const enable_shared_from_this<typename decltype(__get_shared_from_this_impl( |
| 613 | static_cast<_Vp*>(nullptr)))::type>*; |
| 614 | }; |
| 615 | |
| 616 | template < |
| 617 | class _Yp, |
| 618 | class _OrigPtr, |
| 619 | __enable_if_t<!is_convertible<_OrigPtr, typename __get_shared_from_this<__remove_pointer_t<_Yp> >::type>::value, |
| 620 | int> = 0> |
| 621 | _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(_Yp, _OrigPtr) {} |
| 622 | |
| 623 | template <class, class _Yp> |
| 624 | struct __shared_ptr_default_delete : default_delete<_Yp> {}; |
| 625 | |
| 626 | template <class _Yp, class _Un, size_t _Sz> |
| 627 | struct __shared_ptr_default_delete<_Yp[_Sz], _Un> : default_delete<_Yp[]> {}; |
| 628 | |
| 629 | template <class _Yp, class _Un> |
| 630 | struct __shared_ptr_default_delete<_Yp[], _Un> : default_delete<_Yp[]> {}; |
| 631 | |
| 632 | template <class _Up> |
| 633 | friend class shared_ptr; |
| 634 | template <class _Up> |
| 635 | friend class weak_ptr; |
| 636 | }; |
| 637 | |
| 638 | #if _LIBCPP_STD_VER >= 17 |
| 639 | template <class _Tp> |
| 640 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; |
| 641 | template <class _Tp, class _Dp> |
| 642 | shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>; |
| 643 | #endif |
| 644 | |
| 645 | // |
| 646 | // std::allocate_shared and std::make_shared |
| 647 | // |
| 648 | |
| 649 | template <class _ControlBlock, class _Tp, class _Alloc, class... _Args> |
| 650 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> __allocate_shared_impl(const _Alloc& __alloc, _Args&&... __args) { |
| 651 | using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type; |
| 652 | __allocation_guard<_ControlBlockAllocator> __guard(__alloc, 1); |
| 653 | ::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__alloc, std::forward<_Args>(__args)...); |
| 654 | auto __control_block = __guard.__release_ptr(); |
| 655 | return shared_ptr<_Tp>::__create_with_control_block( |
| 656 | __control_block->__get_elem(), std::__to_address(__control_block)); |
| 657 | } |
| 658 | |
| 659 | template <class _Tp, class _Alloc, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0> |
| 660 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) { |
| 661 | using _CanonicalAlloc = __allocator_traits_rebind_t<_Alloc, __remove_cv_t<_Tp> >; |
| 662 | using _ControlBlock = __shared_ptr_emplace<_Tp, _CanonicalAlloc>; |
| 663 | return std::__allocate_shared_impl<_ControlBlock, _Tp>(_CanonicalAlloc(__a), std::forward<_Args>(__args)...); |
| 664 | } |
| 665 | |
| 666 | template <class _Tp, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0> |
| 667 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) { |
| 668 | return std::allocate_shared<_Tp>(allocator<__remove_cv_t<_Tp> >(), std::forward<_Args>(__args)...); |
| 669 | } |
| 670 | |
| 671 | #if _LIBCPP_STD_VER >= 20 |
| 672 | |
| 673 | template <class _Tp, class _Alloc, __enable_if_t<!is_array<_Tp>::value, int> = 0> |
| 674 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) { |
| 675 | using _CanonicalAlloc = __allocator_traits_rebind_t<_Alloc, remove_cv_t<_Tp>>; |
| 676 | using _ControlBlock = __shared_ptr_emplace_for_overwrite<_Tp, _CanonicalAlloc>; |
| 677 | return std::__allocate_shared_impl<_ControlBlock, _Tp>(_CanonicalAlloc(__a)); |
| 678 | } |
| 679 | |
| 680 | template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0> |
| 681 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() { |
| 682 | return std::allocate_shared_for_overwrite<_Tp>(allocator<__remove_cv_t<_Tp>>()); |
| 683 | } |
| 684 | |
| 685 | #endif // _LIBCPP_STD_VER >= 20 |
| 686 | |
| 687 | #if _LIBCPP_STD_VER >= 17 |
| 688 | |
| 689 | template <size_t _Alignment> |
| 690 | struct __sp_aligned_storage { |
| 691 | alignas(_Alignment) char __storage[_Alignment]; |
| 692 | }; |
| 693 | |
| 694 | template <class _Tp, class _Alloc> |
| 695 | struct __unbounded_array_control_block; |
| 696 | |
| 697 | template <class _Tp, class _Alloc> |
| 698 | struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count { |
| 699 | _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; } |
| 700 | |
| 701 | _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block( |
| 702 | _Alloc const& __alloc, size_t __count, _Tp const& __arg) |
| 703 | : __alloc_(__alloc), __count_(__count) { |
| 704 | std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::begin(__data_), __count_, __arg); |
| 705 | } |
| 706 | |
| 707 | _LIBCPP_HIDE_FROM_ABI explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count) |
| 708 | : __alloc_(__alloc), __count_(__count) { |
| 709 | # if _LIBCPP_STD_VER >= 20 |
| 710 | if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { |
| 711 | // We are purposefully not using an allocator-aware default construction because the spec says so. |
| 712 | // There's currently no way of expressing default initialization in an allocator-aware manner anyway. |
| 713 | std::uninitialized_default_construct_n(std::begin(__data_), __count_); |
| 714 | } else { |
| 715 | std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_); |
| 716 | } |
| 717 | # else |
| 718 | std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::begin(__data_), __count_); |
| 719 | # endif |
| 720 | } |
| 721 | |
| 722 | // Returns the number of bytes required to store a control block followed by the given number |
| 723 | // of elements of _Tp, with the whole storage being aligned to a multiple of _Tp's alignment. |
| 724 | _LIBCPP_HIDE_FROM_ABI static constexpr size_t __bytes_for(size_t __elements) { |
| 725 | // When there's 0 elements, the control block alone is enough since it holds one element. |
| 726 | // Otherwise, we allocate one fewer element than requested because the control block already |
| 727 | // holds one. Also, we use the bitwise formula below to ensure that we allocate enough bytes |
| 728 | // for the whole allocation to be a multiple of _Tp's alignment. That formula is taken from [1]. |
| 729 | // |
| 730 | // [1]: https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding |
| 731 | size_t __bytes = __elements == 0 ? sizeof(__unbounded_array_control_block) |
| 732 | : (__elements - 1) * sizeof(_Tp) + sizeof(__unbounded_array_control_block); |
| 733 | constexpr size_t __align = alignof(__unbounded_array_control_block); |
| 734 | return (__bytes + __align - 1) & ~(__align - 1); |
| 735 | } |
| 736 | |
| 737 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL |
| 738 | ~__unbounded_array_control_block() override { |
| 739 | } // can't be `= default` because of the sometimes-non-trivial union member __data_ |
| 740 | |
| 741 | private: |
| 742 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { |
| 743 | # if _LIBCPP_STD_VER >= 20 |
| 744 | if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { |
| 745 | std::__reverse_destroy(__data_, __data_ + __count_); |
| 746 | } else { |
| 747 | __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); |
| 748 | std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_); |
| 749 | } |
| 750 | # else |
| 751 | __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); |
| 752 | std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_); |
| 753 | # endif |
| 754 | } |
| 755 | |
| 756 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { |
| 757 | using _AlignedStorage = __sp_aligned_storage<alignof(__unbounded_array_control_block)>; |
| 758 | using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>; |
| 759 | using _PointerTraits = pointer_traits<typename allocator_traits<_StorageAlloc>::pointer>; |
| 760 | |
| 761 | _StorageAlloc __tmp(__alloc_); |
| 762 | __alloc_.~_Alloc(); |
| 763 | size_t __size = __unbounded_array_control_block::__bytes_for(elements: __count_); |
| 764 | _AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this); |
| 765 | allocator_traits<_StorageAlloc>::deallocate( |
| 766 | __tmp, _PointerTraits::pointer_to(*__storage), __size / sizeof(_AlignedStorage)); |
| 767 | } |
| 768 | |
| 769 | _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; |
| 770 | size_t __count_; |
| 771 | union { |
| 772 | _Tp __data_[1]; |
| 773 | }; |
| 774 | }; |
| 775 | |
| 776 | template <class _Array, class _Alloc, class... _Arg> |
| 777 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> |
| 778 | __allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&&... __arg) { |
| 779 | static_assert(__is_unbounded_array_v<_Array>); |
| 780 | // We compute the number of bytes necessary to hold the control block and the |
| 781 | // array elements. Then, we allocate an array of properly-aligned dummy structs |
| 782 | // large enough to hold the control block and array. This allows shifting the |
| 783 | // burden of aligning memory properly from us to the allocator. |
| 784 | using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>; |
| 785 | using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>; |
| 786 | using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>; |
| 787 | __allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage)); |
| 788 | _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get())); |
| 789 | std::__construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...); |
| 790 | __guard.__release_ptr(); |
| 791 | return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block); |
| 792 | } |
| 793 | |
| 794 | template <class _Tp, class _Alloc> |
| 795 | struct __bounded_array_control_block; |
| 796 | |
| 797 | template <class _Tp, size_t _Count, class _Alloc> |
| 798 | struct __bounded_array_control_block<_Tp[_Count], _Alloc> : __shared_weak_count { |
| 799 | _LIBCPP_HIDE_FROM_ABI constexpr _Tp* __get_data() noexcept { return __data_; } |
| 800 | |
| 801 | _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg) |
| 802 | : __alloc_(__alloc) { |
| 803 | std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count, __arg); |
| 804 | } |
| 805 | |
| 806 | _LIBCPP_HIDE_FROM_ABI explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) { |
| 807 | # if _LIBCPP_STD_VER >= 20 |
| 808 | if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { |
| 809 | // We are purposefully not using an allocator-aware default construction because the spec says so. |
| 810 | // There's currently no way of expressing default initialization in an allocator-aware manner anyway. |
| 811 | std::uninitialized_default_construct_n(std::addressof(__data_[0]), _Count); |
| 812 | } else { |
| 813 | std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count); |
| 814 | } |
| 815 | # else |
| 816 | std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count); |
| 817 | # endif |
| 818 | } |
| 819 | |
| 820 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL |
| 821 | ~__bounded_array_control_block() override { |
| 822 | } // can't be `= default` because of the sometimes-non-trivial union member __data_ |
| 823 | |
| 824 | private: |
| 825 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override { |
| 826 | # if _LIBCPP_STD_VER >= 20 |
| 827 | if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) { |
| 828 | std::__reverse_destroy(__data_, __data_ + _Count); |
| 829 | } else { |
| 830 | __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); |
| 831 | std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count); |
| 832 | } |
| 833 | # else |
| 834 | __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_); |
| 835 | std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count); |
| 836 | # endif |
| 837 | } |
| 838 | |
| 839 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override { |
| 840 | using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>; |
| 841 | using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>; |
| 842 | |
| 843 | _ControlBlockAlloc __tmp(__alloc_); |
| 844 | __alloc_.~_Alloc(); |
| 845 | allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), 1); |
| 846 | } |
| 847 | |
| 848 | _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_; |
| 849 | union { |
| 850 | _Tp __data_[_Count]; |
| 851 | }; |
| 852 | }; |
| 853 | |
| 854 | template <class _Array, class _Alloc, class... _Arg> |
| 855 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&&... __arg) { |
| 856 | static_assert(__is_bounded_array_v<_Array>); |
| 857 | using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>; |
| 858 | using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>; |
| 859 | |
| 860 | __allocation_guard<_ControlBlockAlloc> __guard(__a, 1); |
| 861 | _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get())); |
| 862 | std::__construct_at(__control_block, __a, std::forward<_Arg>(__arg)...); |
| 863 | __guard.__release_ptr(); |
| 864 | return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block); |
| 865 | } |
| 866 | |
| 867 | #endif // _LIBCPP_STD_VER >= 17 |
| 868 | |
| 869 | #if _LIBCPP_STD_VER >= 20 |
| 870 | |
| 871 | // bounded array variants |
| 872 | template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> |
| 873 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) { |
| 874 | return std::__allocate_shared_bounded_array<_Tp>(__a); |
| 875 | } |
| 876 | |
| 877 | template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> |
| 878 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> |
| 879 | allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) { |
| 880 | return std::__allocate_shared_bounded_array<_Tp>(__a, __u); |
| 881 | } |
| 882 | |
| 883 | template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> |
| 884 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) { |
| 885 | using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>; |
| 886 | _ForOverwriteAllocator __alloc(__a); |
| 887 | return std::__allocate_shared_bounded_array<_Tp>(__alloc); |
| 888 | } |
| 889 | |
| 890 | template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> |
| 891 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() { |
| 892 | return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>()); |
| 893 | } |
| 894 | |
| 895 | template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> |
| 896 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) { |
| 897 | return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u); |
| 898 | } |
| 899 | |
| 900 | template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0> |
| 901 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() { |
| 902 | return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>()); |
| 903 | } |
| 904 | |
| 905 | // unbounded array variants |
| 906 | template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> |
| 907 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) { |
| 908 | return std::__allocate_shared_unbounded_array<_Tp>(__a, __n); |
| 909 | } |
| 910 | |
| 911 | template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> |
| 912 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> |
| 913 | allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) { |
| 914 | return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u); |
| 915 | } |
| 916 | |
| 917 | template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> |
| 918 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) { |
| 919 | using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>; |
| 920 | _ForOverwriteAllocator __alloc(__a); |
| 921 | return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n); |
| 922 | } |
| 923 | |
| 924 | template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> |
| 925 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) { |
| 926 | return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n); |
| 927 | } |
| 928 | |
| 929 | template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> |
| 930 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) { |
| 931 | return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u); |
| 932 | } |
| 933 | |
| 934 | template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0> |
| 935 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) { |
| 936 | return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n); |
| 937 | } |
| 938 | |
| 939 | #endif // _LIBCPP_STD_VER >= 20 |
| 940 | |
| 941 | template <class _Tp, class _Up> |
| 942 | inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { |
| 943 | return __x.get() == __y.get(); |
| 944 | } |
| 945 | |
| 946 | #if _LIBCPP_STD_VER <= 17 |
| 947 | |
| 948 | template <class _Tp, class _Up> |
| 949 | inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { |
| 950 | return !(__x == __y); |
| 951 | } |
| 952 | |
| 953 | template <class _Tp, class _Up> |
| 954 | inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { |
| 955 | # if _LIBCPP_STD_VER <= 11 |
| 956 | typedef typename common_type<_Tp*, _Up*>::type _Vp; |
| 957 | return less<_Vp>()(__x.get(), __y.get()); |
| 958 | # else |
| 959 | return less<>()(__x.get(), __y.get()); |
| 960 | # endif |
| 961 | } |
| 962 | |
| 963 | template <class _Tp, class _Up> |
| 964 | inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { |
| 965 | return __y < __x; |
| 966 | } |
| 967 | |
| 968 | template <class _Tp, class _Up> |
| 969 | inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { |
| 970 | return !(__y < __x); |
| 971 | } |
| 972 | |
| 973 | template <class _Tp, class _Up> |
| 974 | inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT { |
| 975 | return !(__x < __y); |
| 976 | } |
| 977 | |
| 978 | #endif // _LIBCPP_STD_VER <= 17 |
| 979 | |
| 980 | #if _LIBCPP_STD_VER >= 20 |
| 981 | template <class _Tp, class _Up> |
| 982 | _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) noexcept { |
| 983 | return compare_three_way()(__x.get(), __y.get()); |
| 984 | } |
| 985 | #endif |
| 986 | |
| 987 | template <class _Tp> |
| 988 | inline _LIBCPP_HIDE_FROM_ABI bool operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { |
| 989 | return !__x; |
| 990 | } |
| 991 | |
| 992 | #if _LIBCPP_STD_VER <= 17 |
| 993 | |
| 994 | template <class _Tp> |
| 995 | inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { |
| 996 | return !__x; |
| 997 | } |
| 998 | |
| 999 | template <class _Tp> |
| 1000 | inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { |
| 1001 | return static_cast<bool>(__x); |
| 1002 | } |
| 1003 | |
| 1004 | template <class _Tp> |
| 1005 | inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { |
| 1006 | return static_cast<bool>(__x); |
| 1007 | } |
| 1008 | |
| 1009 | template <class _Tp> |
| 1010 | inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { |
| 1011 | return less<typename shared_ptr<_Tp>::element_type*>()(__x.get(), nullptr); |
| 1012 | } |
| 1013 | |
| 1014 | template <class _Tp> |
| 1015 | inline _LIBCPP_HIDE_FROM_ABI bool operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { |
| 1016 | return less<typename shared_ptr<_Tp>::element_type*>()(nullptr, __x.get()); |
| 1017 | } |
| 1018 | |
| 1019 | template <class _Tp> |
| 1020 | inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { |
| 1021 | return nullptr < __x; |
| 1022 | } |
| 1023 | |
| 1024 | template <class _Tp> |
| 1025 | inline _LIBCPP_HIDE_FROM_ABI bool operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { |
| 1026 | return __x < nullptr; |
| 1027 | } |
| 1028 | |
| 1029 | template <class _Tp> |
| 1030 | inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { |
| 1031 | return !(nullptr < __x); |
| 1032 | } |
| 1033 | |
| 1034 | template <class _Tp> |
| 1035 | inline _LIBCPP_HIDE_FROM_ABI bool operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { |
| 1036 | return !(__x < nullptr); |
| 1037 | } |
| 1038 | |
| 1039 | template <class _Tp> |
| 1040 | inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT { |
| 1041 | return !(__x < nullptr); |
| 1042 | } |
| 1043 | |
| 1044 | template <class _Tp> |
| 1045 | inline _LIBCPP_HIDE_FROM_ABI bool operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT { |
| 1046 | return !(nullptr < __x); |
| 1047 | } |
| 1048 | |
| 1049 | #endif // _LIBCPP_STD_VER <= 17 |
| 1050 | |
| 1051 | #if _LIBCPP_STD_VER >= 20 |
| 1052 | template <class _Tp> |
| 1053 | _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(shared_ptr<_Tp> const& __x, nullptr_t) noexcept { |
| 1054 | return compare_three_way()(__x.get(), static_cast<typename shared_ptr<_Tp>::element_type*>(nullptr)); |
| 1055 | } |
| 1056 | #endif |
| 1057 | |
| 1058 | template <class _Tp> |
| 1059 | inline _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT { |
| 1060 | __x.swap(__y); |
| 1061 | } |
| 1062 | |
| 1063 | template <class _Tp, class _Up> |
| 1064 | [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> |
| 1065 | static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { |
| 1066 | return shared_ptr<_Tp>(__r, static_cast< typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 1067 | } |
| 1068 | |
| 1069 | // LWG-2996 |
| 1070 | // We don't backport because it is an evolutionary change. |
| 1071 | #if _LIBCPP_STD_VER >= 20 |
| 1072 | template <class _Tp, class _Up> |
| 1073 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept { |
| 1074 | return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 1075 | } |
| 1076 | #endif |
| 1077 | |
| 1078 | template <class _Tp, class _Up> |
| 1079 | [[__nodiscard__]] inline |
| 1080 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { |
| 1081 | typedef typename shared_ptr<_Tp>::element_type _ET; |
| 1082 | _ET* __p = dynamic_cast<_ET*>(__r.get()); |
| 1083 | return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>(); |
| 1084 | } |
| 1085 | |
| 1086 | // LWG-2996 |
| 1087 | // We don't backport because it is an evolutionary change. |
| 1088 | #if _LIBCPP_STD_VER >= 20 |
| 1089 | template <class _Tp, class _Up> |
| 1090 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept { |
| 1091 | auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()); |
| 1092 | return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>(); |
| 1093 | } |
| 1094 | #endif |
| 1095 | |
| 1096 | template <class _Tp, class _Up> |
| 1097 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { |
| 1098 | typedef typename shared_ptr<_Tp>::element_type _RTp; |
| 1099 | return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get())); |
| 1100 | } |
| 1101 | |
| 1102 | // LWG-2996 |
| 1103 | // We don't backport because it is an evolutionary change. |
| 1104 | #if _LIBCPP_STD_VER >= 20 |
| 1105 | template <class _Tp, class _Up> |
| 1106 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept { |
| 1107 | return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 1108 | } |
| 1109 | #endif |
| 1110 | |
| 1111 | template <class _Tp, class _Up> |
| 1112 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT { |
| 1113 | return shared_ptr<_Tp>(__r, reinterpret_cast< typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 1114 | } |
| 1115 | |
| 1116 | // LWG-2996 |
| 1117 | // We don't backport because it is an evolutionary change. |
| 1118 | #if _LIBCPP_STD_VER >= 20 |
| 1119 | template <class _Tp, class _Up> |
| 1120 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept { |
| 1121 | return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 1122 | } |
| 1123 | #endif |
| 1124 | |
| 1125 | #if _LIBCPP_HAS_RTTI |
| 1126 | |
| 1127 | template <class _Dp, class _Tp> |
| 1128 | [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT { |
| 1129 | return __p.template __get_deleter<_Dp>(); |
| 1130 | } |
| 1131 | |
| 1132 | #endif // _LIBCPP_HAS_RTTI |
| 1133 | |
| 1134 | template <class _Tp> |
| 1135 | class _LIBCPP_SHARED_PTR_TRIVIAL_ABI weak_ptr { |
| 1136 | public: |
| 1137 | #if _LIBCPP_STD_VER >= 17 |
| 1138 | typedef remove_extent_t<_Tp> element_type; |
| 1139 | #else |
| 1140 | typedef _Tp element_type; |
| 1141 | #endif |
| 1142 | |
| 1143 | // A weak_ptr contains only two raw pointers which point to the heap and move constructing already doesn't require |
| 1144 | // any bookkeeping, so it's always trivially relocatable. |
| 1145 | using __trivially_relocatable _LIBCPP_NODEBUG = weak_ptr; |
| 1146 | |
| 1147 | private: |
| 1148 | element_type* __ptr_; |
| 1149 | __shared_weak_count* __cntrl_; |
| 1150 | |
| 1151 | public: |
| 1152 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {} |
| 1153 | |
| 1154 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 1155 | _LIBCPP_HIDE_FROM_ABI weak_ptr(shared_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 1156 | if (__cntrl_) |
| 1157 | __cntrl_->__add_weak(); |
| 1158 | } |
| 1159 | |
| 1160 | _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr const& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 1161 | if (__cntrl_) |
| 1162 | __cntrl_->__add_weak(); |
| 1163 | } |
| 1164 | |
| 1165 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 1166 | _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp> const& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) { |
| 1167 | shared_ptr<_Yp> __s = __r.lock(); |
| 1168 | *this = weak_ptr<_Tp>(__s); |
| 1169 | } |
| 1170 | |
| 1171 | _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr&& __r) _NOEXCEPT : __ptr_(__r.__ptr_), __cntrl_(__r.__cntrl_) { |
| 1172 | __r.__ptr_ = nullptr; |
| 1173 | __r.__cntrl_ = nullptr; |
| 1174 | } |
| 1175 | |
| 1176 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 1177 | _LIBCPP_HIDE_FROM_ABI weak_ptr(weak_ptr<_Yp>&& __r) _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) { |
| 1178 | shared_ptr<_Yp> __s = __r.lock(); |
| 1179 | *this = weak_ptr<_Tp>(__s); |
| 1180 | __r.reset(); |
| 1181 | } |
| 1182 | |
| 1183 | _LIBCPP_HIDE_FROM_ABI ~weak_ptr() { |
| 1184 | if (__cntrl_) |
| 1185 | __cntrl_->__release_weak(); |
| 1186 | } |
| 1187 | |
| 1188 | _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT { |
| 1189 | weak_ptr(__r).swap(r&: *this); |
| 1190 | return *this; |
| 1191 | } |
| 1192 | |
| 1193 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 1194 | _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT { |
| 1195 | weak_ptr(__r).swap(r&: *this); |
| 1196 | return *this; |
| 1197 | } |
| 1198 | |
| 1199 | _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT { |
| 1200 | weak_ptr(std::move(__r)).swap(r&: *this); |
| 1201 | return *this; |
| 1202 | } |
| 1203 | |
| 1204 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 1205 | _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT { |
| 1206 | weak_ptr(std::move(__r)).swap(r&: *this); |
| 1207 | return *this; |
| 1208 | } |
| 1209 | |
| 1210 | template <class _Yp, __enable_if_t<__compatible_with_v<_Yp, _Tp>, int> = 0> |
| 1211 | _LIBCPP_HIDE_FROM_ABI weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT { |
| 1212 | weak_ptr(__r).swap(r&: *this); |
| 1213 | return *this; |
| 1214 | } |
| 1215 | |
| 1216 | _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr& __r) _NOEXCEPT { |
| 1217 | std::swap(__ptr_, __r.__ptr_); |
| 1218 | std::swap(x&: __cntrl_, y&: __r.__cntrl_); |
| 1219 | } |
| 1220 | |
| 1221 | _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { weak_ptr().swap(r&: *this); } |
| 1222 | |
| 1223 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { |
| 1224 | return __cntrl_ ? __cntrl_->use_count() : 0; |
| 1225 | } |
| 1226 | |
| 1227 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT { |
| 1228 | return __cntrl_ == nullptr || __cntrl_->use_count() == 0; |
| 1229 | } |
| 1230 | |
| 1231 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT { |
| 1232 | shared_ptr<_Tp> __r; |
| 1233 | __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_; |
| 1234 | if (__r.__cntrl_) |
| 1235 | __r.__ptr_ = __ptr_; |
| 1236 | return __r; |
| 1237 | } |
| 1238 | |
| 1239 | template <class _Up> |
| 1240 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT { |
| 1241 | return __cntrl_ < __r.__cntrl_; |
| 1242 | } |
| 1243 | template <class _Up> |
| 1244 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT { |
| 1245 | return __cntrl_ < __r.__cntrl_; |
| 1246 | } |
| 1247 | |
| 1248 | template <class _Up> |
| 1249 | friend class weak_ptr; |
| 1250 | template <class _Up> |
| 1251 | friend class shared_ptr; |
| 1252 | }; |
| 1253 | |
| 1254 | #if _LIBCPP_STD_VER >= 17 |
| 1255 | template <class _Tp> |
| 1256 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; |
| 1257 | #endif |
| 1258 | |
| 1259 | template <class _Tp> |
| 1260 | inline _LIBCPP_HIDE_FROM_ABI void swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT { |
| 1261 | __x.swap(__y); |
| 1262 | } |
| 1263 | |
| 1264 | #if _LIBCPP_STD_VER >= 17 |
| 1265 | template <class _Tp = void> |
| 1266 | struct owner_less; |
| 1267 | #else |
| 1268 | template <class _Tp> |
| 1269 | struct owner_less; |
| 1270 | #endif |
| 1271 | |
| 1272 | template <class _Tp> |
| 1273 | struct owner_less<shared_ptr<_Tp> > : __binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> { |
| 1274 | _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT { |
| 1275 | return __x.owner_before(__y); |
| 1276 | } |
| 1277 | _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT { |
| 1278 | return __x.owner_before(__y); |
| 1279 | } |
| 1280 | _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT { |
| 1281 | return __x.owner_before(__y); |
| 1282 | } |
| 1283 | }; |
| 1284 | |
| 1285 | template <class _Tp> |
| 1286 | struct owner_less<weak_ptr<_Tp> > : __binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> { |
| 1287 | _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT { |
| 1288 | return __x.owner_before(__y); |
| 1289 | } |
| 1290 | _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT { |
| 1291 | return __x.owner_before(__y); |
| 1292 | } |
| 1293 | _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT { |
| 1294 | return __x.owner_before(__y); |
| 1295 | } |
| 1296 | }; |
| 1297 | |
| 1298 | #if _LIBCPP_STD_VER >= 17 |
| 1299 | template <> |
| 1300 | struct owner_less<void> { |
| 1301 | template <class _Tp, class _Up> |
| 1302 | _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT { |
| 1303 | return __x.owner_before(__y); |
| 1304 | } |
| 1305 | template <class _Tp, class _Up> |
| 1306 | _LIBCPP_HIDE_FROM_ABI bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT { |
| 1307 | return __x.owner_before(__y); |
| 1308 | } |
| 1309 | template <class _Tp, class _Up> |
| 1310 | _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT { |
| 1311 | return __x.owner_before(__y); |
| 1312 | } |
| 1313 | template <class _Tp, class _Up> |
| 1314 | _LIBCPP_HIDE_FROM_ABI bool operator()(weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT { |
| 1315 | return __x.owner_before(__y); |
| 1316 | } |
| 1317 | typedef void is_transparent; |
| 1318 | }; |
| 1319 | #endif |
| 1320 | |
| 1321 | template <class _Tp> |
| 1322 | class enable_shared_from_this { |
| 1323 | mutable weak_ptr<_Tp> __weak_this_; |
| 1324 | |
| 1325 | protected: |
| 1326 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR enable_shared_from_this() _NOEXCEPT {} |
| 1327 | _LIBCPP_HIDE_FROM_ABI enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {} |
| 1328 | _LIBCPP_HIDE_FROM_ABI enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT { return *this; } |
| 1329 | _LIBCPP_HIDE_FROM_ABI ~enable_shared_from_this() {} |
| 1330 | |
| 1331 | public: |
| 1332 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); } |
| 1333 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const { |
| 1334 | return shared_ptr<const _Tp>(__weak_this_); |
| 1335 | } |
| 1336 | |
| 1337 | #if _LIBCPP_STD_VER >= 17 |
| 1338 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; } |
| 1339 | |
| 1340 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; } |
| 1341 | #endif // _LIBCPP_STD_VER >= 17 |
| 1342 | |
| 1343 | template <class _Up> |
| 1344 | friend class shared_ptr; |
| 1345 | }; |
| 1346 | |
| 1347 | template <class _Tp> |
| 1348 | struct hash; |
| 1349 | |
| 1350 | template <class _Tp> |
| 1351 | struct hash<shared_ptr<_Tp> > { |
| 1352 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS) |
| 1353 | _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type; |
| 1354 | _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type; |
| 1355 | #endif |
| 1356 | |
| 1357 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT { |
| 1358 | return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get()); |
| 1359 | } |
| 1360 | }; |
| 1361 | |
| 1362 | template <class _CharT, class _Traits, class _Yp> |
| 1363 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 1364 | operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p); |
| 1365 | |
| 1366 | #if _LIBCPP_HAS_THREADS |
| 1367 | |
| 1368 | class _LIBCPP_EXPORTED_FROM_ABI __sp_mut { |
| 1369 | void* __lx_; |
| 1370 | |
| 1371 | public: |
| 1372 | void lock() _NOEXCEPT; |
| 1373 | void unlock() _NOEXCEPT; |
| 1374 | |
| 1375 | private: |
| 1376 | _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT; |
| 1377 | __sp_mut(const __sp_mut&); |
| 1378 | __sp_mut& operator=(const __sp_mut&); |
| 1379 | |
| 1380 | friend _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*); |
| 1381 | }; |
| 1382 | |
| 1383 | _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*); |
| 1384 | |
| 1385 | template <class _Tp> |
| 1386 | inline _LIBCPP_HIDE_FROM_ABI bool atomic_is_lock_free(const shared_ptr<_Tp>*) { |
| 1387 | return false; |
| 1388 | } |
| 1389 | |
| 1390 | template <class _Tp> |
| 1391 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p) { |
| 1392 | __sp_mut& __m = std::__get_sp_mut(__p); |
| 1393 | __m.lock(); |
| 1394 | shared_ptr<_Tp> __q = *__p; |
| 1395 | __m.unlock(); |
| 1396 | return __q; |
| 1397 | } |
| 1398 | |
| 1399 | template <class _Tp> |
| 1400 | inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) { |
| 1401 | return std::atomic_load(__p); |
| 1402 | } |
| 1403 | |
| 1404 | template <class _Tp> |
| 1405 | _LIBCPP_HIDE_FROM_ABI void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) { |
| 1406 | __sp_mut& __m = std::__get_sp_mut(__p); |
| 1407 | __m.lock(); |
| 1408 | __p->swap(__r); |
| 1409 | __m.unlock(); |
| 1410 | } |
| 1411 | |
| 1412 | template <class _Tp> |
| 1413 | inline _LIBCPP_HIDE_FROM_ABI void atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) { |
| 1414 | std::atomic_store(__p, __r); |
| 1415 | } |
| 1416 | |
| 1417 | template <class _Tp> |
| 1418 | _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) { |
| 1419 | __sp_mut& __m = std::__get_sp_mut(__p); |
| 1420 | __m.lock(); |
| 1421 | __p->swap(__r); |
| 1422 | __m.unlock(); |
| 1423 | return __r; |
| 1424 | } |
| 1425 | |
| 1426 | template <class _Tp> |
| 1427 | inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> |
| 1428 | atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) { |
| 1429 | return std::atomic_exchange(__p, __r); |
| 1430 | } |
| 1431 | |
| 1432 | template <class _Tp> |
| 1433 | _LIBCPP_HIDE_FROM_ABI bool |
| 1434 | atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) { |
| 1435 | shared_ptr<_Tp> __temp; |
| 1436 | __sp_mut& __m = std::__get_sp_mut(__p); |
| 1437 | __m.lock(); |
| 1438 | if (__p->__owner_equivalent(*__v)) { |
| 1439 | std::swap(__temp, *__p); |
| 1440 | *__p = __w; |
| 1441 | __m.unlock(); |
| 1442 | return true; |
| 1443 | } |
| 1444 | std::swap(__temp, *__v); |
| 1445 | *__v = *__p; |
| 1446 | __m.unlock(); |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
| 1450 | template <class _Tp> |
| 1451 | inline _LIBCPP_HIDE_FROM_ABI bool |
| 1452 | atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) { |
| 1453 | return std::atomic_compare_exchange_strong(__p, __v, __w); |
| 1454 | } |
| 1455 | |
| 1456 | template <class _Tp> |
| 1457 | inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_strong_explicit( |
| 1458 | shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) { |
| 1459 | return std::atomic_compare_exchange_strong(__p, __v, __w); |
| 1460 | } |
| 1461 | |
| 1462 | template <class _Tp> |
| 1463 | inline _LIBCPP_HIDE_FROM_ABI bool atomic_compare_exchange_weak_explicit( |
| 1464 | shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w, memory_order, memory_order) { |
| 1465 | return std::atomic_compare_exchange_weak(__p, __v, __w); |
| 1466 | } |
| 1467 | |
| 1468 | #endif // _LIBCPP_HAS_THREADS |
| 1469 | |
| 1470 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 1471 | _LIBCPP_END_NAMESPACE_STD |
| 1472 | |
| 1473 | _LIBCPP_POP_MACROS |
| 1474 | |
| 1475 | #endif // _LIBCPP___MEMORY_SHARED_PTR_H |
| 1476 | |