| 1 | // shared_ptr and weak_ptr implementation details -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2007-2018 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
| 26 | |
| 27 | // shared_count.hpp |
| 28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
| 29 | |
| 30 | // shared_ptr.hpp |
| 31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
| 32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
| 33 | |
| 34 | // weak_ptr.hpp |
| 35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
| 36 | |
| 37 | // enable_shared_from_this.hpp |
| 38 | // Copyright (C) 2002 Peter Dimov |
| 39 | |
| 40 | // Distributed under the Boost Software License, Version 1.0. (See |
| 41 | // accompanying file LICENSE_1_0.txt or copy at |
| 42 | // http://www.boost.org/LICENSE_1_0.txt) |
| 43 | |
| 44 | /** @file bits/shared_ptr_base.h |
| 45 | * This is an internal header file, included by other library headers. |
| 46 | * Do not attempt to use it directly. @headername{memory} |
| 47 | */ |
| 48 | |
| 49 | #ifndef _SHARED_PTR_BASE_H |
| 50 | #define _SHARED_PTR_BASE_H 1 |
| 51 | |
| 52 | #include <typeinfo> |
| 53 | #include <bits/allocated_ptr.h> |
| 54 | #include <bits/refwrap.h> |
| 55 | #include <bits/stl_function.h> |
| 56 | #include <ext/aligned_buffer.h> |
| 57 | |
| 58 | namespace std _GLIBCXX_VISIBILITY(default) |
| 59 | { |
| 60 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 61 | |
| 62 | #if _GLIBCXX_USE_DEPRECATED |
| 63 | #pragma GCC diagnostic push |
| 64 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 65 | template<typename> class auto_ptr; |
| 66 | #pragma GCC diagnostic pop |
| 67 | #endif |
| 68 | |
| 69 | /** |
| 70 | * @brief Exception possibly thrown by @c shared_ptr. |
| 71 | * @ingroup exceptions |
| 72 | */ |
| 73 | class bad_weak_ptr : public std::exception |
| 74 | { |
| 75 | public: |
| 76 | virtual char const* what() const noexcept; |
| 77 | |
| 78 | virtual ~bad_weak_ptr() noexcept; |
| 79 | }; |
| 80 | |
| 81 | // Substitute for bad_weak_ptr object in the case of -fno-exceptions. |
| 82 | inline void |
| 83 | __throw_bad_weak_ptr() |
| 84 | { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); } |
| 85 | |
| 86 | using __gnu_cxx::_Lock_policy; |
| 87 | using __gnu_cxx::__default_lock_policy; |
| 88 | using __gnu_cxx::_S_single; |
| 89 | using __gnu_cxx::_S_mutex; |
| 90 | using __gnu_cxx::_S_atomic; |
| 91 | |
| 92 | // Empty helper class except when the template argument is _S_mutex. |
| 93 | template<_Lock_policy _Lp> |
| 94 | class _Mutex_base |
| 95 | { |
| 96 | protected: |
| 97 | // The atomic policy uses fully-fenced builtins, single doesn't care. |
| 98 | enum { _S_need_barriers = 0 }; |
| 99 | }; |
| 100 | |
| 101 | template<> |
| 102 | class _Mutex_base<_S_mutex> |
| 103 | : public __gnu_cxx::__mutex |
| 104 | { |
| 105 | protected: |
| 106 | // This policy is used when atomic builtins are not available. |
| 107 | // The replacement atomic operations might not have the necessary |
| 108 | // memory barriers. |
| 109 | enum { _S_need_barriers = 1 }; |
| 110 | }; |
| 111 | |
| 112 | template<_Lock_policy _Lp = __default_lock_policy> |
| 113 | class _Sp_counted_base |
| 114 | : public _Mutex_base<_Lp> |
| 115 | { |
| 116 | public: |
| 117 | _Sp_counted_base() noexcept |
| 118 | : _M_use_count(1), _M_weak_count(1) { } |
| 119 | |
| 120 | virtual |
| 121 | ~_Sp_counted_base() noexcept |
| 122 | { } |
| 123 | |
| 124 | // Called when _M_use_count drops to zero, to release the resources |
| 125 | // managed by *this. |
| 126 | virtual void |
| 127 | _M_dispose() noexcept = 0; |
| 128 | |
| 129 | // Called when _M_weak_count drops to zero. |
| 130 | virtual void |
| 131 | _M_destroy() noexcept |
| 132 | { delete this; } |
| 133 | |
| 134 | virtual void* |
| 135 | _M_get_deleter(const std::type_info&) noexcept = 0; |
| 136 | |
| 137 | void |
| 138 | _M_add_ref_copy() |
| 139 | { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } |
| 140 | |
| 141 | void |
| 142 | _M_add_ref_lock(); |
| 143 | |
| 144 | bool |
| 145 | _M_add_ref_lock_nothrow(); |
| 146 | |
| 147 | void |
| 148 | _M_release() noexcept |
| 149 | { |
| 150 | // Be race-detector-friendly. For more info see bits/c++config. |
| 151 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count); |
| 152 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) |
| 153 | { |
| 154 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count); |
| 155 | _M_dispose(); |
| 156 | // There must be a memory barrier between dispose() and destroy() |
| 157 | // to ensure that the effects of dispose() are observed in the |
| 158 | // thread that runs destroy(). |
| 159 | // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html |
| 160 | if (_Mutex_base<_Lp>::_S_need_barriers) |
| 161 | { |
| 162 | __atomic_thread_fence (__ATOMIC_ACQ_REL); |
| 163 | } |
| 164 | |
| 165 | // Be race-detector-friendly. For more info see bits/c++config. |
| 166 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
| 167 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, |
| 168 | -1) == 1) |
| 169 | { |
| 170 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
| 171 | _M_destroy(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | _M_weak_add_ref() noexcept |
| 178 | { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } |
| 179 | |
| 180 | void |
| 181 | _M_weak_release() noexcept |
| 182 | { |
| 183 | // Be race-detector-friendly. For more info see bits/c++config. |
| 184 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
| 185 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
| 186 | { |
| 187 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
| 188 | if (_Mutex_base<_Lp>::_S_need_barriers) |
| 189 | { |
| 190 | // See _M_release(), |
| 191 | // destroy() must observe results of dispose() |
| 192 | __atomic_thread_fence (__ATOMIC_ACQ_REL); |
| 193 | } |
| 194 | _M_destroy(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | long |
| 199 | _M_get_use_count() const noexcept |
| 200 | { |
| 201 | // No memory barrier is used here so there is no synchronization |
| 202 | // with other threads. |
| 203 | return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED); |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | _Sp_counted_base(_Sp_counted_base const&) = delete; |
| 208 | _Sp_counted_base& operator=(_Sp_counted_base const&) = delete; |
| 209 | |
| 210 | _Atomic_word _M_use_count; // #shared |
| 211 | _Atomic_word _M_weak_count; // #weak + (#shared != 0) |
| 212 | }; |
| 213 | |
| 214 | template<> |
| 215 | inline void |
| 216 | _Sp_counted_base<_S_single>:: |
| 217 | _M_add_ref_lock() |
| 218 | { |
| 219 | if (_M_use_count == 0) |
| 220 | __throw_bad_weak_ptr(); |
| 221 | ++_M_use_count; |
| 222 | } |
| 223 | |
| 224 | template<> |
| 225 | inline void |
| 226 | _Sp_counted_base<_S_mutex>:: |
| 227 | _M_add_ref_lock() |
| 228 | { |
| 229 | __gnu_cxx::__scoped_lock sentry(*this); |
| 230 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
| 231 | { |
| 232 | _M_use_count = 0; |
| 233 | __throw_bad_weak_ptr(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | template<> |
| 238 | inline void |
| 239 | _Sp_counted_base<_S_atomic>:: |
| 240 | _M_add_ref_lock() |
| 241 | { |
| 242 | // Perform lock-free add-if-not-zero operation. |
| 243 | _Atomic_word __count = _M_get_use_count(); |
| 244 | do |
| 245 | { |
| 246 | if (__count == 0) |
| 247 | __throw_bad_weak_ptr(); |
| 248 | // Replace the current counter value with the old value + 1, as |
| 249 | // long as it's not changed meanwhile. |
| 250 | } |
| 251 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
| 252 | true, __ATOMIC_ACQ_REL, |
| 253 | __ATOMIC_RELAXED)); |
| 254 | } |
| 255 | |
| 256 | template<> |
| 257 | inline bool |
| 258 | _Sp_counted_base<_S_single>:: |
| 259 | _M_add_ref_lock_nothrow() |
| 260 | { |
| 261 | if (_M_use_count == 0) |
| 262 | return false; |
| 263 | ++_M_use_count; |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | template<> |
| 268 | inline bool |
| 269 | _Sp_counted_base<_S_mutex>:: |
| 270 | _M_add_ref_lock_nothrow() |
| 271 | { |
| 272 | __gnu_cxx::__scoped_lock sentry(*this); |
| 273 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
| 274 | { |
| 275 | _M_use_count = 0; |
| 276 | return false; |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | template<> |
| 282 | inline bool |
| 283 | _Sp_counted_base<_S_atomic>:: |
| 284 | _M_add_ref_lock_nothrow() |
| 285 | { |
| 286 | // Perform lock-free add-if-not-zero operation. |
| 287 | _Atomic_word __count = _M_get_use_count(); |
| 288 | do |
| 289 | { |
| 290 | if (__count == 0) |
| 291 | return false; |
| 292 | // Replace the current counter value with the old value + 1, as |
| 293 | // long as it's not changed meanwhile. |
| 294 | } |
| 295 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
| 296 | true, __ATOMIC_ACQ_REL, |
| 297 | __ATOMIC_RELAXED)); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | template<> |
| 302 | inline void |
| 303 | _Sp_counted_base<_S_single>::_M_add_ref_copy() |
| 304 | { ++_M_use_count; } |
| 305 | |
| 306 | template<> |
| 307 | inline void |
| 308 | _Sp_counted_base<_S_single>::_M_release() noexcept |
| 309 | { |
| 310 | if (--_M_use_count == 0) |
| 311 | { |
| 312 | _M_dispose(); |
| 313 | if (--_M_weak_count == 0) |
| 314 | _M_destroy(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | template<> |
| 319 | inline void |
| 320 | _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept |
| 321 | { ++_M_weak_count; } |
| 322 | |
| 323 | template<> |
| 324 | inline void |
| 325 | _Sp_counted_base<_S_single>::_M_weak_release() noexcept |
| 326 | { |
| 327 | if (--_M_weak_count == 0) |
| 328 | _M_destroy(); |
| 329 | } |
| 330 | |
| 331 | template<> |
| 332 | inline long |
| 333 | _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept |
| 334 | { return _M_use_count; } |
| 335 | |
| 336 | |
| 337 | // Forward declarations. |
| 338 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
| 339 | class __shared_ptr; |
| 340 | |
| 341 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
| 342 | class __weak_ptr; |
| 343 | |
| 344 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
| 345 | class __enable_shared_from_this; |
| 346 | |
| 347 | template<typename _Tp> |
| 348 | class shared_ptr; |
| 349 | |
| 350 | template<typename _Tp> |
| 351 | class weak_ptr; |
| 352 | |
| 353 | template<typename _Tp> |
| 354 | struct owner_less; |
| 355 | |
| 356 | template<typename _Tp> |
| 357 | class enable_shared_from_this; |
| 358 | |
| 359 | template<_Lock_policy _Lp = __default_lock_policy> |
| 360 | class __weak_count; |
| 361 | |
| 362 | template<_Lock_policy _Lp = __default_lock_policy> |
| 363 | class __shared_count; |
| 364 | |
| 365 | |
| 366 | // Counted ptr with no deleter or allocator support |
| 367 | template<typename _Ptr, _Lock_policy _Lp> |
| 368 | class _Sp_counted_ptr final : public _Sp_counted_base<_Lp> |
| 369 | { |
| 370 | public: |
| 371 | explicit |
| 372 | _Sp_counted_ptr(_Ptr __p) noexcept |
| 373 | : _M_ptr(__p) { } |
| 374 | |
| 375 | virtual void |
| 376 | _M_dispose() noexcept |
| 377 | { delete _M_ptr; } |
| 378 | |
| 379 | virtual void |
| 380 | _M_destroy() noexcept |
| 381 | { delete this; } |
| 382 | |
| 383 | virtual void* |
| 384 | _M_get_deleter(const std::type_info&) noexcept |
| 385 | { return nullptr; } |
| 386 | |
| 387 | _Sp_counted_ptr(const _Sp_counted_ptr&) = delete; |
| 388 | _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete; |
| 389 | |
| 390 | private: |
| 391 | _Ptr _M_ptr; |
| 392 | }; |
| 393 | |
| 394 | template<> |
| 395 | inline void |
| 396 | _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { } |
| 397 | |
| 398 | template<> |
| 399 | inline void |
| 400 | _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { } |
| 401 | |
| 402 | template<> |
| 403 | inline void |
| 404 | _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { } |
| 405 | |
| 406 | template<int _Nm, typename _Tp, |
| 407 | bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)> |
| 408 | struct _Sp_ebo_helper; |
| 409 | |
| 410 | /// Specialization using EBO. |
| 411 | template<int _Nm, typename _Tp> |
| 412 | struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp |
| 413 | { |
| 414 | explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { } |
| 415 | explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { } |
| 416 | |
| 417 | static _Tp& |
| 418 | _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); } |
| 419 | }; |
| 420 | |
| 421 | /// Specialization not using EBO. |
| 422 | template<int _Nm, typename _Tp> |
| 423 | struct _Sp_ebo_helper<_Nm, _Tp, false> |
| 424 | { |
| 425 | explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { } |
| 426 | explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { } |
| 427 | |
| 428 | static _Tp& |
| 429 | _S_get(_Sp_ebo_helper& __eboh) |
| 430 | { return __eboh._M_tp; } |
| 431 | |
| 432 | private: |
| 433 | _Tp _M_tp; |
| 434 | }; |
| 435 | |
| 436 | // Support for custom deleter and/or allocator |
| 437 | template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp> |
| 438 | class _Sp_counted_deleter final : public _Sp_counted_base<_Lp> |
| 439 | { |
| 440 | class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc> |
| 441 | { |
| 442 | typedef _Sp_ebo_helper<0, _Deleter> _Del_base; |
| 443 | typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base; |
| 444 | |
| 445 | public: |
| 446 | _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
| 447 | : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a) |
| 448 | { } |
| 449 | |
| 450 | _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); } |
| 451 | _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); } |
| 452 | |
| 453 | _Ptr _M_ptr; |
| 454 | }; |
| 455 | |
| 456 | public: |
| 457 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>; |
| 458 | |
| 459 | // __d(__p) must not throw. |
| 460 | _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept |
| 461 | : _M_impl(__p, std::move(__d), _Alloc()) { } |
| 462 | |
| 463 | // __d(__p) must not throw. |
| 464 | _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
| 465 | : _M_impl(__p, std::move(__d), __a) { } |
| 466 | |
| 467 | ~_Sp_counted_deleter() noexcept { } |
| 468 | |
| 469 | virtual void |
| 470 | _M_dispose() noexcept |
| 471 | { _M_impl._M_del()(_M_impl._M_ptr); } |
| 472 | |
| 473 | virtual void |
| 474 | _M_destroy() noexcept |
| 475 | { |
| 476 | __allocator_type __a(_M_impl._M_alloc()); |
| 477 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
| 478 | this->~_Sp_counted_deleter(); |
| 479 | } |
| 480 | |
| 481 | virtual void* |
| 482 | _M_get_deleter(const std::type_info& __ti) noexcept |
| 483 | { |
| 484 | #if __cpp_rtti |
| 485 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 486 | // 2400. shared_ptr's get_deleter() should use addressof() |
| 487 | return __ti == typeid(_Deleter) |
| 488 | ? std::__addressof(_M_impl._M_del()) |
| 489 | : nullptr; |
| 490 | #else |
| 491 | return nullptr; |
| 492 | #endif |
| 493 | } |
| 494 | |
| 495 | private: |
| 496 | _Impl _M_impl; |
| 497 | }; |
| 498 | |
| 499 | // helpers for make_shared / allocate_shared |
| 500 | |
| 501 | struct _Sp_make_shared_tag |
| 502 | { |
| 503 | #if !__cpp_rtti |
| 504 | private: |
| 505 | template<typename _Tp, _Lock_policy _Lp> |
| 506 | friend class __shared_ptr; |
| 507 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
| 508 | friend class _Sp_counted_ptr_inplace; |
| 509 | |
| 510 | static const type_info& |
| 511 | _S_ti() noexcept |
| 512 | { |
| 513 | alignas(type_info) static constexpr _Sp_make_shared_tag __tag; |
| 514 | return reinterpret_cast<const type_info&>(__tag); |
| 515 | } |
| 516 | #endif |
| 517 | }; |
| 518 | |
| 519 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
| 520 | class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp> |
| 521 | { |
| 522 | class _Impl : _Sp_ebo_helper<0, _Alloc> |
| 523 | { |
| 524 | typedef _Sp_ebo_helper<0, _Alloc> _A_base; |
| 525 | |
| 526 | public: |
| 527 | explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { } |
| 528 | |
| 529 | _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } |
| 530 | |
| 531 | __gnu_cxx::__aligned_buffer<_Tp> _M_storage; |
| 532 | }; |
| 533 | |
| 534 | public: |
| 535 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; |
| 536 | |
| 537 | template<typename... _Args> |
| 538 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args) |
| 539 | : _M_impl(__a) |
| 540 | { |
| 541 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 542 | // 2070. allocate_shared should use allocator_traits<A>::construct |
| 543 | allocator_traits<_Alloc>::construct(__a, _M_ptr(), |
| 544 | std::forward<_Args>(__args)...); // might throw |
| 545 | } |
| 546 | |
| 547 | ~_Sp_counted_ptr_inplace() noexcept { } |
| 548 | |
| 549 | virtual void |
| 550 | _M_dispose() noexcept |
| 551 | { |
| 552 | allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr()); |
| 553 | } |
| 554 | |
| 555 | // Override because the allocator needs to know the dynamic type |
| 556 | virtual void |
| 557 | _M_destroy() noexcept |
| 558 | { |
| 559 | __allocator_type __a(_M_impl._M_alloc()); |
| 560 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
| 561 | this->~_Sp_counted_ptr_inplace(); |
| 562 | } |
| 563 | |
| 564 | // Sneaky trick so __shared_ptr can get the managed pointer |
| 565 | virtual void* |
| 566 | _M_get_deleter(const std::type_info& __ti) noexcept |
| 567 | { |
| 568 | #if __cpp_rtti |
| 569 | if (__ti == typeid(_Sp_make_shared_tag)) |
| 570 | #else |
| 571 | if (&__ti == &_Sp_make_shared_tag::_S_ti()) |
| 572 | #endif |
| 573 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
| 574 | return nullptr; |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } |
| 579 | |
| 580 | _Impl _M_impl; |
| 581 | }; |
| 582 | |
| 583 | // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>. |
| 584 | struct __sp_array_delete |
| 585 | { |
| 586 | template<typename _Yp> |
| 587 | void operator()(_Yp* __p) const { delete[] __p; } |
| 588 | }; |
| 589 | |
| 590 | template<_Lock_policy _Lp> |
| 591 | class __shared_count |
| 592 | { |
| 593 | public: |
| 594 | constexpr __shared_count() noexcept : _M_pi(0) |
| 595 | { } |
| 596 | |
| 597 | template<typename _Ptr> |
| 598 | explicit |
| 599 | __shared_count(_Ptr __p) : _M_pi(0) |
| 600 | { |
| 601 | __try |
| 602 | { |
| 603 | _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p); |
| 604 | } |
| 605 | __catch(...) |
| 606 | { |
| 607 | delete __p; |
| 608 | __throw_exception_again; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | template<typename _Ptr> |
| 613 | __shared_count(_Ptr __p, /* is_array = */ false_type) |
| 614 | : __shared_count(__p) |
| 615 | { } |
| 616 | |
| 617 | template<typename _Ptr> |
| 618 | __shared_count(_Ptr __p, /* is_array = */ true_type) |
| 619 | : __shared_count(__p, __sp_array_delete{}, allocator<void>()) |
| 620 | { } |
| 621 | |
| 622 | template<typename _Ptr, typename _Deleter> |
| 623 | __shared_count(_Ptr __p, _Deleter __d) |
| 624 | : __shared_count(__p, std::move(__d), allocator<void>()) |
| 625 | { } |
| 626 | |
| 627 | template<typename _Ptr, typename _Deleter, typename _Alloc> |
| 628 | __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0) |
| 629 | { |
| 630 | typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type; |
| 631 | __try |
| 632 | { |
| 633 | typename _Sp_cd_type::__allocator_type __a2(__a); |
| 634 | auto __guard = std::__allocate_guarded(__a2); |
| 635 | _Sp_cd_type* __mem = __guard.get(); |
| 636 | ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a)); |
| 637 | _M_pi = __mem; |
| 638 | __guard = nullptr; |
| 639 | } |
| 640 | __catch(...) |
| 641 | { |
| 642 | __d(__p); // Call _Deleter on __p. |
| 643 | __throw_exception_again; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | template<typename _Tp, typename _Alloc, typename... _Args> |
| 648 | __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a, |
| 649 | _Args&&... __args) |
| 650 | : _M_pi(0) |
| 651 | { |
| 652 | typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; |
| 653 | typename _Sp_cp_type::__allocator_type __a2(__a); |
| 654 | auto __guard = std::__allocate_guarded(__a2); |
| 655 | _Sp_cp_type* __mem = __guard.get(); |
| 656 | ::new (__mem) _Sp_cp_type(std::move(__a), |
| 657 | std::forward<_Args>(__args)...); |
| 658 | _M_pi = __mem; |
| 659 | __guard = nullptr; |
| 660 | } |
| 661 | |
| 662 | #if _GLIBCXX_USE_DEPRECATED |
| 663 | #pragma GCC diagnostic push |
| 664 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 665 | // Special case for auto_ptr<_Tp> to provide the strong guarantee. |
| 666 | template<typename _Tp> |
| 667 | explicit |
| 668 | __shared_count(std::auto_ptr<_Tp>&& __r); |
| 669 | #pragma GCC diagnostic pop |
| 670 | #endif |
| 671 | |
| 672 | // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee. |
| 673 | template<typename _Tp, typename _Del> |
| 674 | explicit |
| 675 | __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0) |
| 676 | { |
| 677 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 678 | // 2415. Inconsistency between unique_ptr and shared_ptr |
| 679 | if (__r.get() == nullptr) |
| 680 | return; |
| 681 | |
| 682 | using _Ptr = typename unique_ptr<_Tp, _Del>::pointer; |
| 683 | using _Del2 = typename conditional<is_reference<_Del>::value, |
| 684 | reference_wrapper<typename remove_reference<_Del>::type>, |
| 685 | _Del>::type; |
| 686 | using _Sp_cd_type |
| 687 | = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>; |
| 688 | using _Alloc = allocator<_Sp_cd_type>; |
| 689 | using _Alloc_traits = allocator_traits<_Alloc>; |
| 690 | _Alloc __a; |
| 691 | _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1); |
| 692 | _Alloc_traits::construct(__a, __mem, __r.release(), |
| 693 | __r.get_deleter()); // non-throwing |
| 694 | _M_pi = __mem; |
| 695 | } |
| 696 | |
| 697 | // Throw bad_weak_ptr when __r._M_get_use_count() == 0. |
| 698 | explicit __shared_count(const __weak_count<_Lp>& __r); |
| 699 | |
| 700 | // Does not throw if __r._M_get_use_count() == 0, caller must check. |
| 701 | explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t); |
| 702 | |
| 703 | ~__shared_count() noexcept |
| 704 | { |
| 705 | if (_M_pi != nullptr) |
| 706 | _M_pi->_M_release(); |
| 707 | } |
| 708 | |
| 709 | __shared_count(const __shared_count& __r) noexcept |
| 710 | : _M_pi(__r._M_pi) |
| 711 | { |
| 712 | if (_M_pi != 0) |
| 713 | _M_pi->_M_add_ref_copy(); |
| 714 | } |
| 715 | |
| 716 | __shared_count& |
| 717 | operator=(const __shared_count& __r) noexcept |
| 718 | { |
| 719 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 720 | if (__tmp != _M_pi) |
| 721 | { |
| 722 | if (__tmp != 0) |
| 723 | __tmp->_M_add_ref_copy(); |
| 724 | if (_M_pi != 0) |
| 725 | _M_pi->_M_release(); |
| 726 | _M_pi = __tmp; |
| 727 | } |
| 728 | return *this; |
| 729 | } |
| 730 | |
| 731 | void |
| 732 | _M_swap(__shared_count& __r) noexcept |
| 733 | { |
| 734 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 735 | __r._M_pi = _M_pi; |
| 736 | _M_pi = __tmp; |
| 737 | } |
| 738 | |
| 739 | long |
| 740 | _M_get_use_count() const noexcept |
| 741 | { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; } |
| 742 | |
| 743 | bool |
| 744 | _M_unique() const noexcept |
| 745 | { return this->_M_get_use_count() == 1; } |
| 746 | |
| 747 | void* |
| 748 | _M_get_deleter(const std::type_info& __ti) const noexcept |
| 749 | { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; } |
| 750 | |
| 751 | bool |
| 752 | _M_less(const __shared_count& __rhs) const noexcept |
| 753 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 754 | |
| 755 | bool |
| 756 | _M_less(const __weak_count<_Lp>& __rhs) const noexcept |
| 757 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 758 | |
| 759 | // Friend function injected into enclosing namespace and found by ADL |
| 760 | friend inline bool |
| 761 | operator==(const __shared_count& __a, const __shared_count& __b) noexcept |
| 762 | { return __a._M_pi == __b._M_pi; } |
| 763 | |
| 764 | private: |
| 765 | friend class __weak_count<_Lp>; |
| 766 | |
| 767 | _Sp_counted_base<_Lp>* _M_pi; |
| 768 | }; |
| 769 | |
| 770 | |
| 771 | template<_Lock_policy _Lp> |
| 772 | class __weak_count |
| 773 | { |
| 774 | public: |
| 775 | constexpr __weak_count() noexcept : _M_pi(nullptr) |
| 776 | { } |
| 777 | |
| 778 | __weak_count(const __shared_count<_Lp>& __r) noexcept |
| 779 | : _M_pi(__r._M_pi) |
| 780 | { |
| 781 | if (_M_pi != nullptr) |
| 782 | _M_pi->_M_weak_add_ref(); |
| 783 | } |
| 784 | |
| 785 | __weak_count(const __weak_count& __r) noexcept |
| 786 | : _M_pi(__r._M_pi) |
| 787 | { |
| 788 | if (_M_pi != nullptr) |
| 789 | _M_pi->_M_weak_add_ref(); |
| 790 | } |
| 791 | |
| 792 | __weak_count(__weak_count&& __r) noexcept |
| 793 | : _M_pi(__r._M_pi) |
| 794 | { __r._M_pi = nullptr; } |
| 795 | |
| 796 | ~__weak_count() noexcept |
| 797 | { |
| 798 | if (_M_pi != nullptr) |
| 799 | _M_pi->_M_weak_release(); |
| 800 | } |
| 801 | |
| 802 | __weak_count& |
| 803 | operator=(const __shared_count<_Lp>& __r) noexcept |
| 804 | { |
| 805 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 806 | if (__tmp != nullptr) |
| 807 | __tmp->_M_weak_add_ref(); |
| 808 | if (_M_pi != nullptr) |
| 809 | _M_pi->_M_weak_release(); |
| 810 | _M_pi = __tmp; |
| 811 | return *this; |
| 812 | } |
| 813 | |
| 814 | __weak_count& |
| 815 | operator=(const __weak_count& __r) noexcept |
| 816 | { |
| 817 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 818 | if (__tmp != nullptr) |
| 819 | __tmp->_M_weak_add_ref(); |
| 820 | if (_M_pi != nullptr) |
| 821 | _M_pi->_M_weak_release(); |
| 822 | _M_pi = __tmp; |
| 823 | return *this; |
| 824 | } |
| 825 | |
| 826 | __weak_count& |
| 827 | operator=(__weak_count&& __r) noexcept |
| 828 | { |
| 829 | if (_M_pi != nullptr) |
| 830 | _M_pi->_M_weak_release(); |
| 831 | _M_pi = __r._M_pi; |
| 832 | __r._M_pi = nullptr; |
| 833 | return *this; |
| 834 | } |
| 835 | |
| 836 | void |
| 837 | _M_swap(__weak_count& __r) noexcept |
| 838 | { |
| 839 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 840 | __r._M_pi = _M_pi; |
| 841 | _M_pi = __tmp; |
| 842 | } |
| 843 | |
| 844 | long |
| 845 | _M_get_use_count() const noexcept |
| 846 | { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; } |
| 847 | |
| 848 | bool |
| 849 | _M_less(const __weak_count& __rhs) const noexcept |
| 850 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 851 | |
| 852 | bool |
| 853 | _M_less(const __shared_count<_Lp>& __rhs) const noexcept |
| 854 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 855 | |
| 856 | // Friend function injected into enclosing namespace and found by ADL |
| 857 | friend inline bool |
| 858 | operator==(const __weak_count& __a, const __weak_count& __b) noexcept |
| 859 | { return __a._M_pi == __b._M_pi; } |
| 860 | |
| 861 | private: |
| 862 | friend class __shared_count<_Lp>; |
| 863 | |
| 864 | _Sp_counted_base<_Lp>* _M_pi; |
| 865 | }; |
| 866 | |
| 867 | // Now that __weak_count is defined we can define this constructor: |
| 868 | template<_Lock_policy _Lp> |
| 869 | inline |
| 870 | __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r) |
| 871 | : _M_pi(__r._M_pi) |
| 872 | { |
| 873 | if (_M_pi != nullptr) |
| 874 | _M_pi->_M_add_ref_lock(); |
| 875 | else |
| 876 | __throw_bad_weak_ptr(); |
| 877 | } |
| 878 | |
| 879 | // Now that __weak_count is defined we can define this constructor: |
| 880 | template<_Lock_policy _Lp> |
| 881 | inline |
| 882 | __shared_count<_Lp>:: |
| 883 | __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) |
| 884 | : _M_pi(__r._M_pi) |
| 885 | { |
| 886 | if (_M_pi != nullptr) |
| 887 | if (!_M_pi->_M_add_ref_lock_nothrow()) |
| 888 | _M_pi = nullptr; |
| 889 | } |
| 890 | |
| 891 | #define __cpp_lib_shared_ptr_arrays 201603 |
| 892 | |
| 893 | // Helper traits for shared_ptr of array: |
| 894 | |
| 895 | // A pointer type Y* is said to be compatible with a pointer type T* when |
| 896 | // either Y* is convertible to T* or Y is U[N] and T is U cv []. |
| 897 | template<typename _Yp_ptr, typename _Tp_ptr> |
| 898 | struct __sp_compatible_with |
| 899 | : false_type |
| 900 | { }; |
| 901 | |
| 902 | template<typename _Yp, typename _Tp> |
| 903 | struct __sp_compatible_with<_Yp*, _Tp*> |
| 904 | : is_convertible<_Yp*, _Tp*>::type |
| 905 | { }; |
| 906 | |
| 907 | template<typename _Up, size_t _Nm> |
| 908 | struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]> |
| 909 | : true_type |
| 910 | { }; |
| 911 | |
| 912 | template<typename _Up, size_t _Nm> |
| 913 | struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]> |
| 914 | : true_type |
| 915 | { }; |
| 916 | |
| 917 | template<typename _Up, size_t _Nm> |
| 918 | struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]> |
| 919 | : true_type |
| 920 | { }; |
| 921 | |
| 922 | template<typename _Up, size_t _Nm> |
| 923 | struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]> |
| 924 | : true_type |
| 925 | { }; |
| 926 | |
| 927 | // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N]. |
| 928 | template<typename _Up, size_t _Nm, typename _Yp, typename = void> |
| 929 | struct __sp_is_constructible_arrN |
| 930 | : false_type |
| 931 | { }; |
| 932 | |
| 933 | template<typename _Up, size_t _Nm, typename _Yp> |
| 934 | struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>> |
| 935 | : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type |
| 936 | { }; |
| 937 | |
| 938 | // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[]. |
| 939 | template<typename _Up, typename _Yp, typename = void> |
| 940 | struct __sp_is_constructible_arr |
| 941 | : false_type |
| 942 | { }; |
| 943 | |
| 944 | template<typename _Up, typename _Yp> |
| 945 | struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>> |
| 946 | : is_convertible<_Yp(*)[], _Up(*)[]>::type |
| 947 | { }; |
| 948 | |
| 949 | // Trait to check if shared_ptr<T> can be constructed from Y*. |
| 950 | template<typename _Tp, typename _Yp> |
| 951 | struct __sp_is_constructible; |
| 952 | |
| 953 | // When T is U[N], Y(*)[N] shall be convertible to T*; |
| 954 | template<typename _Up, size_t _Nm, typename _Yp> |
| 955 | struct __sp_is_constructible<_Up[_Nm], _Yp> |
| 956 | : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type |
| 957 | { }; |
| 958 | |
| 959 | // when T is U[], Y(*)[] shall be convertible to T*; |
| 960 | template<typename _Up, typename _Yp> |
| 961 | struct __sp_is_constructible<_Up[], _Yp> |
| 962 | : __sp_is_constructible_arr<_Up, _Yp>::type |
| 963 | { }; |
| 964 | |
| 965 | // otherwise, Y* shall be convertible to T*. |
| 966 | template<typename _Tp, typename _Yp> |
| 967 | struct __sp_is_constructible |
| 968 | : is_convertible<_Yp*, _Tp*>::type |
| 969 | { }; |
| 970 | |
| 971 | |
| 972 | // Define operator* and operator-> for shared_ptr<T>. |
| 973 | template<typename _Tp, _Lock_policy _Lp, |
| 974 | bool = is_array<_Tp>::value, bool = is_void<_Tp>::value> |
| 975 | class __shared_ptr_access |
| 976 | { |
| 977 | public: |
| 978 | using element_type = _Tp; |
| 979 | |
| 980 | element_type& |
| 981 | operator*() const noexcept |
| 982 | { |
| 983 | __glibcxx_assert(_M_get() != nullptr); |
| 984 | return *_M_get(); |
| 985 | } |
| 986 | |
| 987 | element_type* |
| 988 | operator->() const noexcept |
| 989 | { |
| 990 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
| 991 | return _M_get(); |
| 992 | } |
| 993 | |
| 994 | private: |
| 995 | element_type* |
| 996 | _M_get() const noexcept |
| 997 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
| 998 | }; |
| 999 | |
| 1000 | // Define operator-> for shared_ptr<cv void>. |
| 1001 | template<typename _Tp, _Lock_policy _Lp> |
| 1002 | class __shared_ptr_access<_Tp, _Lp, false, true> |
| 1003 | { |
| 1004 | public: |
| 1005 | using element_type = _Tp; |
| 1006 | |
| 1007 | element_type* |
| 1008 | operator->() const noexcept |
| 1009 | { |
| 1010 | auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); |
| 1011 | _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr); |
| 1012 | return __ptr; |
| 1013 | } |
| 1014 | }; |
| 1015 | |
| 1016 | // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>. |
| 1017 | template<typename _Tp, _Lock_policy _Lp> |
| 1018 | class __shared_ptr_access<_Tp, _Lp, true, false> |
| 1019 | { |
| 1020 | public: |
| 1021 | using element_type = typename remove_extent<_Tp>::type; |
| 1022 | |
| 1023 | #if __cplusplus <= 201402L |
| 1024 | [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17" )]] |
| 1025 | element_type& |
| 1026 | operator*() const noexcept |
| 1027 | { |
| 1028 | __glibcxx_assert(_M_get() != nullptr); |
| 1029 | return *_M_get(); |
| 1030 | } |
| 1031 | |
| 1032 | [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17" )]] |
| 1033 | element_type* |
| 1034 | operator->() const noexcept |
| 1035 | { |
| 1036 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
| 1037 | return _M_get(); |
| 1038 | } |
| 1039 | #endif |
| 1040 | |
| 1041 | element_type& |
| 1042 | operator[](ptrdiff_t __i) const |
| 1043 | { |
| 1044 | __glibcxx_assert(_M_get() != nullptr); |
| 1045 | __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value); |
| 1046 | return _M_get()[__i]; |
| 1047 | } |
| 1048 | |
| 1049 | private: |
| 1050 | element_type* |
| 1051 | _M_get() const noexcept |
| 1052 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
| 1053 | }; |
| 1054 | |
| 1055 | template<typename _Tp, _Lock_policy _Lp> |
| 1056 | class __shared_ptr |
| 1057 | : public __shared_ptr_access<_Tp, _Lp> |
| 1058 | { |
| 1059 | public: |
| 1060 | using element_type = typename remove_extent<_Tp>::type; |
| 1061 | |
| 1062 | private: |
| 1063 | // Constraint for taking ownership of a pointer of type _Yp*: |
| 1064 | template<typename _Yp> |
| 1065 | using _SafeConv |
| 1066 | = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type; |
| 1067 | |
| 1068 | // Constraint for construction from shared_ptr and weak_ptr: |
| 1069 | template<typename _Yp, typename _Res = void> |
| 1070 | using _Compatible = typename |
| 1071 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
| 1072 | |
| 1073 | // Constraint for assignment from shared_ptr and weak_ptr: |
| 1074 | template<typename _Yp> |
| 1075 | using _Assignable = _Compatible<_Yp, __shared_ptr&>; |
| 1076 | |
| 1077 | // Constraint for construction from unique_ptr: |
| 1078 | template<typename _Yp, typename _Del, typename _Res = void, |
| 1079 | typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer> |
| 1080 | using _UniqCompatible = typename enable_if<__and_< |
| 1081 | __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*> |
| 1082 | >::value, _Res>::type; |
| 1083 | |
| 1084 | // Constraint for assignment from unique_ptr: |
| 1085 | template<typename _Yp, typename _Del> |
| 1086 | using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>; |
| 1087 | |
| 1088 | public: |
| 1089 | |
| 1090 | #if __cplusplus > 201402L |
| 1091 | using weak_type = __weak_ptr<_Tp, _Lp>; |
| 1092 | #endif |
| 1093 | |
| 1094 | constexpr __shared_ptr() noexcept |
| 1095 | : _M_ptr(0), _M_refcount() |
| 1096 | { } |
| 1097 | |
| 1098 | template<typename _Yp, typename = _SafeConv<_Yp>> |
| 1099 | explicit |
| 1100 | __shared_ptr(_Yp* __p) |
| 1101 | : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type()) |
| 1102 | { |
| 1103 | static_assert( !is_void<_Yp>::value, "incomplete type" ); |
| 1104 | static_assert( sizeof(_Yp) > 0, "incomplete type" ); |
| 1105 | _M_enable_shared_from_this_with(__p); |
| 1106 | } |
| 1107 | |
| 1108 | template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>> |
| 1109 | __shared_ptr(_Yp* __p, _Deleter __d) |
| 1110 | : _M_ptr(__p), _M_refcount(__p, std::move(__d)) |
| 1111 | { |
| 1112 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
| 1113 | "deleter expression d(p) is well-formed" ); |
| 1114 | _M_enable_shared_from_this_with(__p); |
| 1115 | } |
| 1116 | |
| 1117 | template<typename _Yp, typename _Deleter, typename _Alloc, |
| 1118 | typename = _SafeConv<_Yp>> |
| 1119 | __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) |
| 1120 | : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a)) |
| 1121 | { |
| 1122 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
| 1123 | "deleter expression d(p) is well-formed" ); |
| 1124 | _M_enable_shared_from_this_with(__p); |
| 1125 | } |
| 1126 | |
| 1127 | template<typename _Deleter> |
| 1128 | __shared_ptr(nullptr_t __p, _Deleter __d) |
| 1129 | : _M_ptr(0), _M_refcount(__p, std::move(__d)) |
| 1130 | { } |
| 1131 | |
| 1132 | template<typename _Deleter, typename _Alloc> |
| 1133 | __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
| 1134 | : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a)) |
| 1135 | { } |
| 1136 | |
| 1137 | template<typename _Yp> |
| 1138 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r, |
| 1139 | element_type* __p) noexcept |
| 1140 | : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws |
| 1141 | { } |
| 1142 | |
| 1143 | __shared_ptr(const __shared_ptr&) noexcept = default; |
| 1144 | __shared_ptr& operator=(const __shared_ptr&) noexcept = default; |
| 1145 | ~__shared_ptr() = default; |
| 1146 | |
| 1147 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1148 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1149 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
| 1150 | { } |
| 1151 | |
| 1152 | __shared_ptr(__shared_ptr&& __r) noexcept |
| 1153 | : _M_ptr(__r._M_ptr), _M_refcount() |
| 1154 | { |
| 1155 | _M_refcount._M_swap(__r._M_refcount); |
| 1156 | __r._M_ptr = 0; |
| 1157 | } |
| 1158 | |
| 1159 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1160 | __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
| 1161 | : _M_ptr(__r._M_ptr), _M_refcount() |
| 1162 | { |
| 1163 | _M_refcount._M_swap(__r._M_refcount); |
| 1164 | __r._M_ptr = 0; |
| 1165 | } |
| 1166 | |
| 1167 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1168 | explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r) |
| 1169 | : _M_refcount(__r._M_refcount) // may throw |
| 1170 | { |
| 1171 | // It is now safe to copy __r._M_ptr, as |
| 1172 | // _M_refcount(__r._M_refcount) did not throw. |
| 1173 | _M_ptr = __r._M_ptr; |
| 1174 | } |
| 1175 | |
| 1176 | // If an exception is thrown this constructor has no effect. |
| 1177 | template<typename _Yp, typename _Del, |
| 1178 | typename = _UniqCompatible<_Yp, _Del>> |
| 1179 | __shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
| 1180 | : _M_ptr(__r.get()), _M_refcount() |
| 1181 | { |
| 1182 | auto __raw = __to_address(__r.get()); |
| 1183 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
| 1184 | _M_enable_shared_from_this_with(__raw); |
| 1185 | } |
| 1186 | |
| 1187 | #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED |
| 1188 | protected: |
| 1189 | // If an exception is thrown this constructor has no effect. |
| 1190 | template<typename _Tp1, typename _Del, |
| 1191 | typename enable_if<__and_< |
| 1192 | __not_<is_array<_Tp>>, is_array<_Tp1>, |
| 1193 | is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*> |
| 1194 | >::value, bool>::type = true> |
| 1195 | __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete) |
| 1196 | : _M_ptr(__r.get()), _M_refcount() |
| 1197 | { |
| 1198 | auto __raw = __to_address(__r.get()); |
| 1199 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
| 1200 | _M_enable_shared_from_this_with(__raw); |
| 1201 | } |
| 1202 | public: |
| 1203 | #endif |
| 1204 | |
| 1205 | #if _GLIBCXX_USE_DEPRECATED |
| 1206 | #pragma GCC diagnostic push |
| 1207 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1208 | // Postcondition: use_count() == 1 and __r.get() == 0 |
| 1209 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1210 | __shared_ptr(auto_ptr<_Yp>&& __r); |
| 1211 | #pragma GCC diagnostic pop |
| 1212 | #endif |
| 1213 | |
| 1214 | constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } |
| 1215 | |
| 1216 | template<typename _Yp> |
| 1217 | _Assignable<_Yp> |
| 1218 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1219 | { |
| 1220 | _M_ptr = __r._M_ptr; |
| 1221 | _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw |
| 1222 | return *this; |
| 1223 | } |
| 1224 | |
| 1225 | #if _GLIBCXX_USE_DEPRECATED |
| 1226 | #pragma GCC diagnostic push |
| 1227 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1228 | template<typename _Yp> |
| 1229 | _Assignable<_Yp> |
| 1230 | operator=(auto_ptr<_Yp>&& __r) |
| 1231 | { |
| 1232 | __shared_ptr(std::move(__r)).swap(*this); |
| 1233 | return *this; |
| 1234 | } |
| 1235 | #pragma GCC diagnostic pop |
| 1236 | #endif |
| 1237 | |
| 1238 | __shared_ptr& |
| 1239 | operator=(__shared_ptr&& __r) noexcept |
| 1240 | { |
| 1241 | __shared_ptr(std::move(__r)).swap(*this); |
| 1242 | return *this; |
| 1243 | } |
| 1244 | |
| 1245 | template<class _Yp> |
| 1246 | _Assignable<_Yp> |
| 1247 | operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
| 1248 | { |
| 1249 | __shared_ptr(std::move(__r)).swap(*this); |
| 1250 | return *this; |
| 1251 | } |
| 1252 | |
| 1253 | template<typename _Yp, typename _Del> |
| 1254 | _UniqAssignable<_Yp, _Del> |
| 1255 | operator=(unique_ptr<_Yp, _Del>&& __r) |
| 1256 | { |
| 1257 | __shared_ptr(std::move(__r)).swap(*this); |
| 1258 | return *this; |
| 1259 | } |
| 1260 | |
| 1261 | void |
| 1262 | reset() noexcept |
| 1263 | { __shared_ptr().swap(*this); } |
| 1264 | |
| 1265 | template<typename _Yp> |
| 1266 | _SafeConv<_Yp> |
| 1267 | reset(_Yp* __p) // _Yp must be complete. |
| 1268 | { |
| 1269 | // Catch self-reset errors. |
| 1270 | __glibcxx_assert(__p == 0 || __p != _M_ptr); |
| 1271 | __shared_ptr(__p).swap(*this); |
| 1272 | } |
| 1273 | |
| 1274 | template<typename _Yp, typename _Deleter> |
| 1275 | _SafeConv<_Yp> |
| 1276 | reset(_Yp* __p, _Deleter __d) |
| 1277 | { __shared_ptr(__p, std::move(__d)).swap(*this); } |
| 1278 | |
| 1279 | template<typename _Yp, typename _Deleter, typename _Alloc> |
| 1280 | _SafeConv<_Yp> |
| 1281 | reset(_Yp* __p, _Deleter __d, _Alloc __a) |
| 1282 | { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); } |
| 1283 | |
| 1284 | element_type* |
| 1285 | get() const noexcept |
| 1286 | { return _M_ptr; } |
| 1287 | |
| 1288 | explicit operator bool() const // never throws |
| 1289 | { return _M_ptr == 0 ? false : true; } |
| 1290 | |
| 1291 | bool |
| 1292 | unique() const noexcept |
| 1293 | { return _M_refcount._M_unique(); } |
| 1294 | |
| 1295 | long |
| 1296 | use_count() const noexcept |
| 1297 | { return _M_refcount._M_get_use_count(); } |
| 1298 | |
| 1299 | void |
| 1300 | swap(__shared_ptr<_Tp, _Lp>& __other) noexcept |
| 1301 | { |
| 1302 | std::swap(_M_ptr, __other._M_ptr); |
| 1303 | _M_refcount._M_swap(__other._M_refcount); |
| 1304 | } |
| 1305 | |
| 1306 | template<typename _Tp1> |
| 1307 | bool |
| 1308 | owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
| 1309 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1310 | |
| 1311 | template<typename _Tp1> |
| 1312 | bool |
| 1313 | owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
| 1314 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1315 | |
| 1316 | protected: |
| 1317 | // This constructor is non-standard, it is used by allocate_shared. |
| 1318 | template<typename _Alloc, typename... _Args> |
| 1319 | __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
| 1320 | _Args&&... __args) |
| 1321 | : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a, |
| 1322 | std::forward<_Args>(__args)...) |
| 1323 | { |
| 1324 | // _M_ptr needs to point to the newly constructed object. |
| 1325 | // This relies on _Sp_counted_ptr_inplace::_M_get_deleter. |
| 1326 | #if __cpp_rtti |
| 1327 | void* __p = _M_refcount._M_get_deleter(typeid(__tag)); |
| 1328 | #else |
| 1329 | void* __p = _M_refcount._M_get_deleter(_Sp_make_shared_tag::_S_ti()); |
| 1330 | #endif |
| 1331 | _M_ptr = static_cast<_Tp*>(__p); |
| 1332 | _M_enable_shared_from_this_with(_M_ptr); |
| 1333 | } |
| 1334 | |
| 1335 | template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc, |
| 1336 | typename... _Args> |
| 1337 | friend __shared_ptr<_Tp1, _Lp1> |
| 1338 | __allocate_shared(const _Alloc& __a, _Args&&... __args); |
| 1339 | |
| 1340 | // This constructor is used by __weak_ptr::lock() and |
| 1341 | // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t). |
| 1342 | __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) |
| 1343 | : _M_refcount(__r._M_refcount, std::nothrow) |
| 1344 | { |
| 1345 | _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr; |
| 1346 | } |
| 1347 | |
| 1348 | friend class __weak_ptr<_Tp, _Lp>; |
| 1349 | |
| 1350 | private: |
| 1351 | |
| 1352 | template<typename _Yp> |
| 1353 | using __esft_base_t = decltype(__enable_shared_from_this_base( |
| 1354 | std::declval<const __shared_count<_Lp>&>(), |
| 1355 | std::declval<_Yp*>())); |
| 1356 | |
| 1357 | // Detect an accessible and unambiguous enable_shared_from_this base. |
| 1358 | template<typename _Yp, typename = void> |
| 1359 | struct __has_esft_base |
| 1360 | : false_type { }; |
| 1361 | |
| 1362 | template<typename _Yp> |
| 1363 | struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>> |
| 1364 | : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays |
| 1365 | |
| 1366 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
| 1367 | typename enable_if<__has_esft_base<_Yp2>::value>::type |
| 1368 | _M_enable_shared_from_this_with(_Yp* __p) noexcept |
| 1369 | { |
| 1370 | if (auto __base = __enable_shared_from_this_base(_M_refcount, __p)) |
| 1371 | __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount); |
| 1372 | } |
| 1373 | |
| 1374 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
| 1375 | typename enable_if<!__has_esft_base<_Yp2>::value>::type |
| 1376 | _M_enable_shared_from_this_with(_Yp*) noexcept |
| 1377 | { } |
| 1378 | |
| 1379 | void* |
| 1380 | _M_get_deleter(const std::type_info& __ti) const noexcept |
| 1381 | { return _M_refcount._M_get_deleter(__ti); } |
| 1382 | |
| 1383 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
| 1384 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
| 1385 | |
| 1386 | template<typename _Del, typename _Tp1, _Lock_policy _Lp1> |
| 1387 | friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; |
| 1388 | |
| 1389 | template<typename _Del, typename _Tp1> |
| 1390 | friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept; |
| 1391 | |
| 1392 | element_type* _M_ptr; // Contained pointer. |
| 1393 | __shared_count<_Lp> _M_refcount; // Reference counter. |
| 1394 | }; |
| 1395 | |
| 1396 | |
| 1397 | // 20.7.2.2.7 shared_ptr comparisons |
| 1398 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1399 | inline bool |
| 1400 | operator==(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1401 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1402 | { return __a.get() == __b.get(); } |
| 1403 | |
| 1404 | template<typename _Tp, _Lock_policy _Lp> |
| 1405 | inline bool |
| 1406 | operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1407 | { return !__a; } |
| 1408 | |
| 1409 | template<typename _Tp, _Lock_policy _Lp> |
| 1410 | inline bool |
| 1411 | operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1412 | { return !__a; } |
| 1413 | |
| 1414 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1415 | inline bool |
| 1416 | operator!=(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1417 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1418 | { return __a.get() != __b.get(); } |
| 1419 | |
| 1420 | template<typename _Tp, _Lock_policy _Lp> |
| 1421 | inline bool |
| 1422 | operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1423 | { return (bool)__a; } |
| 1424 | |
| 1425 | template<typename _Tp, _Lock_policy _Lp> |
| 1426 | inline bool |
| 1427 | operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1428 | { return (bool)__a; } |
| 1429 | |
| 1430 | template<typename _Tp, typename _Up, _Lock_policy _Lp> |
| 1431 | inline bool |
| 1432 | operator<(const __shared_ptr<_Tp, _Lp>& __a, |
| 1433 | const __shared_ptr<_Up, _Lp>& __b) noexcept |
| 1434 | { |
| 1435 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
| 1436 | using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type; |
| 1437 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; |
| 1438 | return less<_Vp>()(__a.get(), __b.get()); |
| 1439 | } |
| 1440 | |
| 1441 | template<typename _Tp, _Lock_policy _Lp> |
| 1442 | inline bool |
| 1443 | operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1444 | { |
| 1445 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
| 1446 | return less<_Tp_elt*>()(__a.get(), nullptr); |
| 1447 | } |
| 1448 | |
| 1449 | template<typename _Tp, _Lock_policy _Lp> |
| 1450 | inline bool |
| 1451 | operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1452 | { |
| 1453 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
| 1454 | return less<_Tp_elt*>()(nullptr, __a.get()); |
| 1455 | } |
| 1456 | |
| 1457 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1458 | inline bool |
| 1459 | operator<=(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1460 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1461 | { return !(__b < __a); } |
| 1462 | |
| 1463 | template<typename _Tp, _Lock_policy _Lp> |
| 1464 | inline bool |
| 1465 | operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1466 | { return !(nullptr < __a); } |
| 1467 | |
| 1468 | template<typename _Tp, _Lock_policy _Lp> |
| 1469 | inline bool |
| 1470 | operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1471 | { return !(__a < nullptr); } |
| 1472 | |
| 1473 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1474 | inline bool |
| 1475 | operator>(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1476 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1477 | { return (__b < __a); } |
| 1478 | |
| 1479 | template<typename _Tp, _Lock_policy _Lp> |
| 1480 | inline bool |
| 1481 | operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1482 | { return nullptr < __a; } |
| 1483 | |
| 1484 | template<typename _Tp, _Lock_policy _Lp> |
| 1485 | inline bool |
| 1486 | operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1487 | { return __a < nullptr; } |
| 1488 | |
| 1489 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1490 | inline bool |
| 1491 | operator>=(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1492 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1493 | { return !(__a < __b); } |
| 1494 | |
| 1495 | template<typename _Tp, _Lock_policy _Lp> |
| 1496 | inline bool |
| 1497 | operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1498 | { return !(__a < nullptr); } |
| 1499 | |
| 1500 | template<typename _Tp, _Lock_policy _Lp> |
| 1501 | inline bool |
| 1502 | operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1503 | { return !(nullptr < __a); } |
| 1504 | |
| 1505 | template<typename _Sp> |
| 1506 | struct _Sp_less : public binary_function<_Sp, _Sp, bool> |
| 1507 | { |
| 1508 | bool |
| 1509 | operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept |
| 1510 | { |
| 1511 | typedef typename _Sp::element_type element_type; |
| 1512 | return std::less<element_type*>()(__lhs.get(), __rhs.get()); |
| 1513 | } |
| 1514 | }; |
| 1515 | |
| 1516 | template<typename _Tp, _Lock_policy _Lp> |
| 1517 | struct less<__shared_ptr<_Tp, _Lp>> |
| 1518 | : public _Sp_less<__shared_ptr<_Tp, _Lp>> |
| 1519 | { }; |
| 1520 | |
| 1521 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
| 1522 | template<typename _Tp, _Lock_policy _Lp> |
| 1523 | inline void |
| 1524 | swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept |
| 1525 | { __a.swap(__b); } |
| 1526 | |
| 1527 | // 20.7.2.2.9 shared_ptr casts |
| 1528 | |
| 1529 | // The seemingly equivalent code: |
| 1530 | // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get())) |
| 1531 | // will eventually result in undefined behaviour, attempting to |
| 1532 | // delete the same object twice. |
| 1533 | /// static_pointer_cast |
| 1534 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1535 | inline __shared_ptr<_Tp, _Lp> |
| 1536 | static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1537 | { |
| 1538 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1539 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); |
| 1540 | } |
| 1541 | |
| 1542 | // The seemingly equivalent code: |
| 1543 | // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get())) |
| 1544 | // will eventually result in undefined behaviour, attempting to |
| 1545 | // delete the same object twice. |
| 1546 | /// const_pointer_cast |
| 1547 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1548 | inline __shared_ptr<_Tp, _Lp> |
| 1549 | const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1550 | { |
| 1551 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1552 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); |
| 1553 | } |
| 1554 | |
| 1555 | // The seemingly equivalent code: |
| 1556 | // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get())) |
| 1557 | // will eventually result in undefined behaviour, attempting to |
| 1558 | // delete the same object twice. |
| 1559 | /// dynamic_pointer_cast |
| 1560 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1561 | inline __shared_ptr<_Tp, _Lp> |
| 1562 | dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1563 | { |
| 1564 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1565 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) |
| 1566 | return _Sp(__r, __p); |
| 1567 | return _Sp(); |
| 1568 | } |
| 1569 | |
| 1570 | #if __cplusplus > 201402L |
| 1571 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1572 | inline __shared_ptr<_Tp, _Lp> |
| 1573 | reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1574 | { |
| 1575 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1576 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); |
| 1577 | } |
| 1578 | #endif |
| 1579 | |
| 1580 | template<typename _Tp, _Lock_policy _Lp> |
| 1581 | class __weak_ptr |
| 1582 | { |
| 1583 | template<typename _Yp, typename _Res = void> |
| 1584 | using _Compatible = typename |
| 1585 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
| 1586 | |
| 1587 | // Constraint for assignment from shared_ptr and weak_ptr: |
| 1588 | template<typename _Yp> |
| 1589 | using _Assignable = _Compatible<_Yp, __weak_ptr&>; |
| 1590 | |
| 1591 | public: |
| 1592 | using element_type = typename remove_extent<_Tp>::type; |
| 1593 | |
| 1594 | constexpr __weak_ptr() noexcept |
| 1595 | : _M_ptr(nullptr), _M_refcount() |
| 1596 | { } |
| 1597 | |
| 1598 | __weak_ptr(const __weak_ptr&) noexcept = default; |
| 1599 | |
| 1600 | ~__weak_ptr() = default; |
| 1601 | |
| 1602 | // The "obvious" converting constructor implementation: |
| 1603 | // |
| 1604 | // template<typename _Tp1> |
| 1605 | // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) |
| 1606 | // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws |
| 1607 | // { } |
| 1608 | // |
| 1609 | // has a serious problem. |
| 1610 | // |
| 1611 | // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr) |
| 1612 | // conversion may require access to *__r._M_ptr (virtual inheritance). |
| 1613 | // |
| 1614 | // It is not possible to avoid spurious access violations since |
| 1615 | // in multithreaded programs __r._M_ptr may be invalidated at any point. |
| 1616 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1617 | __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
| 1618 | : _M_refcount(__r._M_refcount) |
| 1619 | { _M_ptr = __r.lock().get(); } |
| 1620 | |
| 1621 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1622 | __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1623 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
| 1624 | { } |
| 1625 | |
| 1626 | __weak_ptr(__weak_ptr&& __r) noexcept |
| 1627 | : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount)) |
| 1628 | { __r._M_ptr = nullptr; } |
| 1629 | |
| 1630 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1631 | __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
| 1632 | : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount)) |
| 1633 | { __r._M_ptr = nullptr; } |
| 1634 | |
| 1635 | __weak_ptr& |
| 1636 | operator=(const __weak_ptr& __r) noexcept = default; |
| 1637 | |
| 1638 | template<typename _Yp> |
| 1639 | _Assignable<_Yp> |
| 1640 | operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
| 1641 | { |
| 1642 | _M_ptr = __r.lock().get(); |
| 1643 | _M_refcount = __r._M_refcount; |
| 1644 | return *this; |
| 1645 | } |
| 1646 | |
| 1647 | template<typename _Yp> |
| 1648 | _Assignable<_Yp> |
| 1649 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1650 | { |
| 1651 | _M_ptr = __r._M_ptr; |
| 1652 | _M_refcount = __r._M_refcount; |
| 1653 | return *this; |
| 1654 | } |
| 1655 | |
| 1656 | __weak_ptr& |
| 1657 | operator=(__weak_ptr&& __r) noexcept |
| 1658 | { |
| 1659 | _M_ptr = __r._M_ptr; |
| 1660 | _M_refcount = std::move(__r._M_refcount); |
| 1661 | __r._M_ptr = nullptr; |
| 1662 | return *this; |
| 1663 | } |
| 1664 | |
| 1665 | template<typename _Yp> |
| 1666 | _Assignable<_Yp> |
| 1667 | operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
| 1668 | { |
| 1669 | _M_ptr = __r.lock().get(); |
| 1670 | _M_refcount = std::move(__r._M_refcount); |
| 1671 | __r._M_ptr = nullptr; |
| 1672 | return *this; |
| 1673 | } |
| 1674 | |
| 1675 | __shared_ptr<_Tp, _Lp> |
| 1676 | lock() const noexcept |
| 1677 | { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); } |
| 1678 | |
| 1679 | long |
| 1680 | use_count() const noexcept |
| 1681 | { return _M_refcount._M_get_use_count(); } |
| 1682 | |
| 1683 | bool |
| 1684 | expired() const noexcept |
| 1685 | { return _M_refcount._M_get_use_count() == 0; } |
| 1686 | |
| 1687 | template<typename _Tp1> |
| 1688 | bool |
| 1689 | owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept |
| 1690 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1691 | |
| 1692 | template<typename _Tp1> |
| 1693 | bool |
| 1694 | owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept |
| 1695 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1696 | |
| 1697 | void |
| 1698 | reset() noexcept |
| 1699 | { __weak_ptr().swap(*this); } |
| 1700 | |
| 1701 | void |
| 1702 | swap(__weak_ptr& __s) noexcept |
| 1703 | { |
| 1704 | std::swap(_M_ptr, __s._M_ptr); |
| 1705 | _M_refcount._M_swap(__s._M_refcount); |
| 1706 | } |
| 1707 | |
| 1708 | private: |
| 1709 | // Used by __enable_shared_from_this. |
| 1710 | void |
| 1711 | _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept |
| 1712 | { |
| 1713 | if (use_count() == 0) |
| 1714 | { |
| 1715 | _M_ptr = __ptr; |
| 1716 | _M_refcount = __refcount; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
| 1721 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
| 1722 | friend class __enable_shared_from_this<_Tp, _Lp>; |
| 1723 | friend class enable_shared_from_this<_Tp>; |
| 1724 | |
| 1725 | element_type* _M_ptr; // Contained pointer. |
| 1726 | __weak_count<_Lp> _M_refcount; // Reference counter. |
| 1727 | }; |
| 1728 | |
| 1729 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
| 1730 | template<typename _Tp, _Lock_policy _Lp> |
| 1731 | inline void |
| 1732 | swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept |
| 1733 | { __a.swap(__b); } |
| 1734 | |
| 1735 | template<typename _Tp, typename _Tp1> |
| 1736 | struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool> |
| 1737 | { |
| 1738 | bool |
| 1739 | operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept |
| 1740 | { return __lhs.owner_before(__rhs); } |
| 1741 | |
| 1742 | bool |
| 1743 | operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept |
| 1744 | { return __lhs.owner_before(__rhs); } |
| 1745 | |
| 1746 | bool |
| 1747 | operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept |
| 1748 | { return __lhs.owner_before(__rhs); } |
| 1749 | }; |
| 1750 | |
| 1751 | template<> |
| 1752 | struct _Sp_owner_less<void, void> |
| 1753 | { |
| 1754 | template<typename _Tp, typename _Up> |
| 1755 | auto |
| 1756 | operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept |
| 1757 | -> decltype(__lhs.owner_before(__rhs)) |
| 1758 | { return __lhs.owner_before(__rhs); } |
| 1759 | |
| 1760 | using is_transparent = void; |
| 1761 | }; |
| 1762 | |
| 1763 | template<typename _Tp, _Lock_policy _Lp> |
| 1764 | struct owner_less<__shared_ptr<_Tp, _Lp>> |
| 1765 | : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>> |
| 1766 | { }; |
| 1767 | |
| 1768 | template<typename _Tp, _Lock_policy _Lp> |
| 1769 | struct owner_less<__weak_ptr<_Tp, _Lp>> |
| 1770 | : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>> |
| 1771 | { }; |
| 1772 | |
| 1773 | |
| 1774 | template<typename _Tp, _Lock_policy _Lp> |
| 1775 | class __enable_shared_from_this |
| 1776 | { |
| 1777 | protected: |
| 1778 | constexpr __enable_shared_from_this() noexcept { } |
| 1779 | |
| 1780 | __enable_shared_from_this(const __enable_shared_from_this&) noexcept { } |
| 1781 | |
| 1782 | __enable_shared_from_this& |
| 1783 | operator=(const __enable_shared_from_this&) noexcept |
| 1784 | { return *this; } |
| 1785 | |
| 1786 | ~__enable_shared_from_this() { } |
| 1787 | |
| 1788 | public: |
| 1789 | __shared_ptr<_Tp, _Lp> |
| 1790 | shared_from_this() |
| 1791 | { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); } |
| 1792 | |
| 1793 | __shared_ptr<const _Tp, _Lp> |
| 1794 | shared_from_this() const |
| 1795 | { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); } |
| 1796 | |
| 1797 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
| 1798 | __weak_ptr<_Tp, _Lp> |
| 1799 | weak_from_this() noexcept |
| 1800 | { return this->_M_weak_this; } |
| 1801 | |
| 1802 | __weak_ptr<const _Tp, _Lp> |
| 1803 | weak_from_this() const noexcept |
| 1804 | { return this->_M_weak_this; } |
| 1805 | #endif |
| 1806 | |
| 1807 | private: |
| 1808 | template<typename _Tp1> |
| 1809 | void |
| 1810 | _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept |
| 1811 | { _M_weak_this._M_assign(__p, __n); } |
| 1812 | |
| 1813 | friend const __enable_shared_from_this* |
| 1814 | __enable_shared_from_this_base(const __shared_count<_Lp>&, |
| 1815 | const __enable_shared_from_this* __p) |
| 1816 | { return __p; } |
| 1817 | |
| 1818 | template<typename, _Lock_policy> |
| 1819 | friend class __shared_ptr; |
| 1820 | |
| 1821 | mutable __weak_ptr<_Tp, _Lp> _M_weak_this; |
| 1822 | }; |
| 1823 | |
| 1824 | template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args> |
| 1825 | inline __shared_ptr<_Tp, _Lp> |
| 1826 | __allocate_shared(const _Alloc& __a, _Args&&... __args) |
| 1827 | { |
| 1828 | return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a, |
| 1829 | std::forward<_Args>(__args)...); |
| 1830 | } |
| 1831 | |
| 1832 | template<typename _Tp, _Lock_policy _Lp, typename... _Args> |
| 1833 | inline __shared_ptr<_Tp, _Lp> |
| 1834 | __make_shared(_Args&&... __args) |
| 1835 | { |
| 1836 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
| 1837 | return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(), |
| 1838 | std::forward<_Args>(__args)...); |
| 1839 | } |
| 1840 | |
| 1841 | /// std::hash specialization for __shared_ptr. |
| 1842 | template<typename _Tp, _Lock_policy _Lp> |
| 1843 | struct hash<__shared_ptr<_Tp, _Lp>> |
| 1844 | : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>> |
| 1845 | { |
| 1846 | size_t |
| 1847 | operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept |
| 1848 | { |
| 1849 | return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()( |
| 1850 | __s.get()); |
| 1851 | } |
| 1852 | }; |
| 1853 | |
| 1854 | _GLIBCXX_END_NAMESPACE_VERSION |
| 1855 | } // namespace |
| 1856 | |
| 1857 | #endif // _SHARED_PTR_BASE_H |
| 1858 | |