1 // shared_ptr and weak_ptr implementation -*- C++ -*- 2 3 // Copyright (C) 2007, 2008, 2009, 2010 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.h 45 * This is an internal header file, included by other library headers. 46 * You should not attempt to use it directly. 47 */ 48 49 #ifndef _SHARED_PTR_H 50 #define _SHARED_PTR_H 1 51 52 #include <bits/shared_ptr_base.h> 53 54 _GLIBCXX_BEGIN_NAMESPACE(std) 55 56 /** 57 * @addtogroup pointer_abstractions 58 * @{ 59 */ 60 61 /// 2.2.3.7 shared_ptr I/O 62 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp> 63 inline std::basic_ostream<_Ch, _Tr>& 64 operator<<(std::basic_ostream<_Ch, _Tr>& __os, 65 const __shared_ptr<_Tp, _Lp>& __p) 66 { 67 __os << __p.get(); 68 return __os; 69 } 70 71 /// 2.2.3.10 shared_ptr get_deleter (experimental) 72 template<typename _Del, typename _Tp, _Lock_policy _Lp> 73 inline _Del* 74 get_deleter(const __shared_ptr<_Tp, _Lp>& __p) 75 { 76 #ifdef __GXX_RTTI 77 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); 78 #else 79 return 0; 80 #endif 81 } 82 83 84 /** 85 * @brief A smart pointer with reference-counted copy semantics. 86 * 87 * The object pointed to is deleted when the last shared_ptr pointing to 88 * it is destroyed or reset. 89 */ 90 template<typename _Tp> 91 class shared_ptr : public __shared_ptr<_Tp> 92 { 93 public: 94 /** 95 * @brief Construct an empty %shared_ptr. 96 * @post use_count()==0 && get()==0 97 */ 98 shared_ptr() : __shared_ptr<_Tp>() { } 99 100 /** 101 * @brief Construct a %shared_ptr that owns the pointer @a __p. 102 * @param __p A pointer that is convertible to element_type*. 103 * @post use_count() == 1 && get() == __p 104 * @throw std::bad_alloc, in which case @c delete @a __p is called. 105 */ 106 template<typename _Tp1> 107 explicit shared_ptr(_Tp1* __p) : __shared_ptr<_Tp>(__p) { } 108 109 /** 110 * @brief Construct a %shared_ptr that owns the pointer @a __p 111 * and the deleter @a __d. 112 * @param __p A pointer. 113 * @param __d A deleter. 114 * @post use_count() == 1 && get() == __p 115 * @throw std::bad_alloc, in which case @a __d(__p) is called. 116 * 117 * Requirements: _Deleter's copy constructor and destructor must 118 * not throw 119 * 120 * __shared_ptr will release __p by calling __d(__p) 121 */ 122 template<typename _Tp1, typename _Deleter> 123 shared_ptr(_Tp1* __p, _Deleter __d) : __shared_ptr<_Tp>(__p, __d) { } 124 125 /** 126 * @brief Construct a %shared_ptr that owns the pointer @a __p 127 * and the deleter @a __d. 128 * @param __p A pointer. 129 * @param __d A deleter. 130 * @param __a An allocator. 131 * @post use_count() == 1 && get() == __p 132 * @throw std::bad_alloc, in which case @a __d(__p) is called. 133 * 134 * Requirements: _Deleter's copy constructor and destructor must 135 * not throw _Alloc's copy constructor and destructor must not 136 * throw. 137 * 138 * __shared_ptr will release __p by calling __d(__p) 139 */ 140 template<typename _Tp1, typename _Deleter, typename _Alloc> 141 shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a) 142 : __shared_ptr<_Tp>(__p, __d, __a) { } 143 144 // Aliasing constructor 145 146 /** 147 * @brief Constructs a %shared_ptr instance that stores @a __p 148 * and shares ownership with @a __r. 149 * @param __r A %shared_ptr. 150 * @param __p A pointer that will remain valid while @a *__r is valid. 151 * @post get() == __p && use_count() == __r.use_count() 152 * 153 * This can be used to construct a @c shared_ptr to a sub-object 154 * of an object managed by an existing @c shared_ptr. 155 * 156 * @code 157 * shared_ptr< pair<int,int> > pii(new pair<int,int>()); 158 * shared_ptr<int> pi(pii, &pii->first); 159 * assert(pii.use_count() == 2); 160 * @endcode 161 */ 162 template<typename _Tp1> 163 shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p) 164 : __shared_ptr<_Tp>(__r, __p) { } 165 166 /** 167 * @brief If @a __r is empty, constructs an empty %shared_ptr; 168 * otherwise construct a %shared_ptr that shares ownership 169 * with @a __r. 170 * @param __r A %shared_ptr. 171 * @post get() == __r.get() && use_count() == __r.use_count() 172 */ 173 template<typename _Tp1> 174 shared_ptr(const shared_ptr<_Tp1>& __r) : __shared_ptr<_Tp>(__r) { } 175 176 /** 177 * @brief Move-constructs a %shared_ptr instance from @a __r. 178 * @param __r A %shared_ptr rvalue. 179 * @post *this contains the old value of @a __r, @a __r is empty. 180 */ 181 shared_ptr(shared_ptr&& __r) 182 : __shared_ptr<_Tp>(std::move(__r)) { } 183 184 /** 185 * @brief Move-constructs a %shared_ptr instance from @a __r. 186 * @param __r A %shared_ptr rvalue. 187 * @post *this contains the old value of @a __r, @a __r is empty. 188 */ 189 template<typename _Tp1> 190 shared_ptr(shared_ptr<_Tp1>&& __r) 191 : __shared_ptr<_Tp>(std::move(__r)) { } 192 193 /** 194 * @brief Constructs a %shared_ptr that shares ownership with @a __r 195 * and stores a copy of the pointer stored in @a __r. 196 * @param __r A weak_ptr. 197 * @post use_count() == __r.use_count() 198 * @throw bad_weak_ptr when __r.expired(), 199 * in which case the constructor has no effect. 200 */ 201 template<typename _Tp1> 202 explicit shared_ptr(const weak_ptr<_Tp1>& __r) 203 : __shared_ptr<_Tp>(__r) { } 204 205 #if _GLIBCXX_DEPRECATED 206 template<typename _Tp1> 207 shared_ptr(std::auto_ptr<_Tp1>&& __r) 208 : __shared_ptr<_Tp>(std::move(__r)) { } 209 #endif 210 211 template<typename _Tp1, typename _Del> 212 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r) 213 : __shared_ptr<_Tp>(std::move(__r)) { } 214 215 template<typename _Tp1> 216 shared_ptr& 217 operator=(const shared_ptr<_Tp1>& __r) // never throws 218 { 219 this->__shared_ptr<_Tp>::operator=(__r); 220 return *this; 221 } 222 223 #if _GLIBCXX_DEPRECATED 224 template<typename _Tp1> 225 shared_ptr& 226 operator=(std::auto_ptr<_Tp1>&& __r) 227 { 228 this->__shared_ptr<_Tp>::operator=(std::move(__r)); 229 return *this; 230 } 231 #endif 232 233 shared_ptr& 234 operator=(shared_ptr&& __r) 235 { 236 this->__shared_ptr<_Tp>::operator=(std::move(__r)); 237 return *this; 238 } 239 240 template<class _Tp1> 241 shared_ptr& 242 operator=(shared_ptr<_Tp1>&& __r) 243 { 244 this->__shared_ptr<_Tp>::operator=(std::move(__r)); 245 return *this; 246 } 247 248 template<typename _Tp1, typename _Del> 249 shared_ptr& 250 operator=(std::unique_ptr<_Tp1, _Del>&& __r) 251 { 252 this->__shared_ptr<_Tp>::operator=(std::move(__r)); 253 return *this; 254 } 255 256 private: 257 // This constructor is non-standard, it is used by allocate_shared. 258 template<typename _Alloc, typename... _Args> 259 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args) 260 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...) 261 { } 262 263 template<typename _Tp1, typename _Alloc, typename... _Args> 264 friend shared_ptr<_Tp1> 265 allocate_shared(_Alloc __a, _Args&&... __args); 266 }; 267 268 // 20.8.13.2.7 shared_ptr comparisons 269 template<typename _Tp1, typename _Tp2> 270 inline bool 271 operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b) 272 { return __a.get() == __b.get(); } 273 274 template<typename _Tp1, typename _Tp2> 275 inline bool 276 operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b) 277 { return __a.get() != __b.get(); } 278 279 template<typename _Tp1, typename _Tp2> 280 inline bool 281 operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b) 282 { return __a.get() < __b.get(); } 283 284 template<typename _Tp> 285 struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>> 286 { }; 287 288 // 20.8.13.2.9 shared_ptr specialized algorithms. 289 template<typename _Tp> 290 inline void 291 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) 292 { __a.swap(__b); } 293 294 // 20.8.13.2.10 shared_ptr casts. 295 template<typename _Tp, typename _Tp1> 296 inline shared_ptr<_Tp> 297 static_pointer_cast(const shared_ptr<_Tp1>& __r) 298 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); } 299 300 template<typename _Tp, typename _Tp1> 301 inline shared_ptr<_Tp> 302 const_pointer_cast(const shared_ptr<_Tp1>& __r) 303 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); } 304 305 template<typename _Tp, typename _Tp1> 306 inline shared_ptr<_Tp> 307 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) 308 { 309 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get())) 310 return shared_ptr<_Tp>(__r, __p); 311 return shared_ptr<_Tp>(); 312 } 313 314 315 /** 316 * @brief A smart pointer with weak semantics. 317 * 318 * With forwarding constructors and assignment operators. 319 */ 320 template<typename _Tp> 321 class weak_ptr : public __weak_ptr<_Tp> 322 { 323 public: 324 weak_ptr() : __weak_ptr<_Tp>() { } 325 326 template<typename _Tp1> 327 weak_ptr(const weak_ptr<_Tp1>& __r) 328 : __weak_ptr<_Tp>(__r) { } 329 330 template<typename _Tp1> 331 weak_ptr(const shared_ptr<_Tp1>& __r) 332 : __weak_ptr<_Tp>(__r) { } 333 334 template<typename _Tp1> 335 weak_ptr& 336 operator=(const weak_ptr<_Tp1>& __r) // never throws 337 { 338 this->__weak_ptr<_Tp>::operator=(__r); 339 return *this; 340 } 341 342 template<typename _Tp1> 343 weak_ptr& 344 operator=(const shared_ptr<_Tp1>& __r) // never throws 345 { 346 this->__weak_ptr<_Tp>::operator=(__r); 347 return *this; 348 } 349 350 shared_ptr<_Tp> 351 lock() const // never throws 352 { 353 #ifdef __GTHREADS 354 if (this->expired()) 355 return shared_ptr<_Tp>(); 356 357 __try 358 { 359 return shared_ptr<_Tp>(*this); 360 } 361 __catch(const bad_weak_ptr&) 362 { 363 return shared_ptr<_Tp>(); 364 } 365 #else 366 return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this); 367 #endif 368 } 369 }; 370 371 // 20.8.13.3.7 weak_ptr specialized algorithms. 372 template<typename _Tp> 373 inline void 374 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) 375 { __a.swap(__b); } 376 377 378 /// Primary template owner_less 379 template<typename _Tp> 380 struct owner_less; 381 382 /// Partial specialization of owner_less for shared_ptr. 383 template<typename _Tp> 384 struct owner_less<shared_ptr<_Tp>> 385 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>> 386 { }; 387 388 /// Partial specialization of owner_less for weak_ptr. 389 template<typename _Tp> 390 struct owner_less<weak_ptr<_Tp>> 391 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>> 392 { }; 393 394 /** 395 * @brief Base class allowing use of member function shared_from_this. 396 */ 397 template<typename _Tp> 398 class enable_shared_from_this 399 { 400 protected: 401 enable_shared_from_this() { } 402 403 enable_shared_from_this(const enable_shared_from_this&) { } 404 405 enable_shared_from_this& 406 operator=(const enable_shared_from_this&) 407 { return *this; } 408 409 ~enable_shared_from_this() { } 410 411 public: 412 shared_ptr<_Tp> 413 shared_from_this() 414 { return shared_ptr<_Tp>(this->_M_weak_this); } 415 416 shared_ptr<const _Tp> 417 shared_from_this() const 418 { return shared_ptr<const _Tp>(this->_M_weak_this); } 419 420 private: 421 template<typename _Tp1> 422 void 423 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const 424 { _M_weak_this._M_assign(__p, __n); } 425 426 template<typename _Tp1> 427 friend void 428 __enable_shared_from_this_helper(const __shared_count<>& __pn, 429 const enable_shared_from_this* __pe, 430 const _Tp1* __px) 431 { 432 if (__pe != 0) 433 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); 434 } 435 436 mutable weak_ptr<_Tp> _M_weak_this; 437 }; 438 439 /** 440 * @brief Create an object that is owned by a shared_ptr. 441 * @param __a An allocator. 442 * @param __args Arguments for the @a _Tp object's constructor. 443 * @return A shared_ptr that owns the newly created object. 444 * @throw An exception thrown from @a _Alloc::allocate or from the 445 * constructor of @a _Tp. 446 * 447 * A copy of @a __a will be used to allocate memory for the shared_ptr 448 * and the new object. 449 */ 450 template<typename _Tp, typename _Alloc, typename... _Args> 451 inline shared_ptr<_Tp> 452 allocate_shared(_Alloc __a, _Args&&... __args) 453 { 454 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a), 455 std::forward<_Args>(__args)...); 456 } 457 458 /** 459 * @brief Create an object that is owned by a shared_ptr. 460 * @param __args Arguments for the @a _Tp object's constructor. 461 * @return A shared_ptr that owns the newly created object. 462 * @throw std::bad_alloc, or an exception thrown from the 463 * constructor of @a _Tp. 464 */ 465 template<typename _Tp, typename... _Args> 466 inline shared_ptr<_Tp> 467 make_shared(_Args&&... __args) 468 { 469 typedef typename std::remove_const<_Tp>::type _Tp_nc; 470 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(), 471 std::forward<_Args>(__args)...); 472 } 473 474 // @} group pointer_abstractions 475 476 _GLIBCXX_END_NAMESPACE 477 478 #endif // _SHARED_PTR_H 479