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