1 // <bits/shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
38 // Copyright (C) 2001, 2002, 2003 Peter Dimov
40 // enable_shared_from_this.hpp
41 // Copyright (C) 2002 Peter Dimov
43 // Distributed under the Boost Software License, Version 1.0. (See
44 // accompanying file LICENSE_1_0.txt or copy at
45 // http://www.boost.org/LICENSE_1_0.txt)
47 // GCC Note: based on version 1.32.0 of the Boost library.
49 /** @file bits/shared_ptr.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
54 #ifndef __GXX_EXPERIMENTAL_CXX0X__
55 # include <c++0x_warning.h>
58 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
59 # error C++0x header cannot be included from TR1 header
64 // counted ptr with no deleter or allocator support
65 template<typename _Ptr
, _Lock_policy _Lp
>
67 : public _Sp_counted_base
<_Lp
>
70 _Sp_counted_ptr(_Ptr __p
)
74 _M_dispose() // nothrow
78 _M_destroy() // nothrow
82 _M_get_deleter(const std::type_info
& __ti
)
85 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
86 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
89 _Ptr _M_ptr
; // copy constructor must not throw
92 // support for custom deleter and/or allocator
93 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
94 class _Sp_counted_deleter
95 : public _Sp_counted_ptr
<_Ptr
, _Lp
>
97 typedef typename
_Alloc::template
98 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
100 // Helper class that stores the Deleter and also acts as an allocator.
101 // Used to dispose of the owned pointer and the internal refcount
102 // Requires that copies of _Alloc can free each other's memory.
104 : public _My_alloc_type
// copy constructor must not throw
106 _Deleter _M_del
; // copy constructor must not throw
107 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
108 : _My_alloc_type(__a
), _M_del(__d
) { }
112 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
117 * @pre __d(__p) must not throw.
119 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
120 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
124 * @pre __d(__p) must not throw.
126 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
127 : _Base_type(__p
), _M_del(__d
, __a
) { }
130 _M_dispose() // nothrow
131 { _M_del
._M_del(_Base_type::_M_ptr
); }
134 _M_destroy() // nothrow
136 _My_alloc_type
__a(_M_del
);
137 this->~_Sp_counted_deleter();
138 __a
.deallocate(this, 1);
142 _M_get_deleter(const std::type_info
& __ti
)
143 { return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0; }
146 _My_Deleter _M_del
; // copy constructor must not throw
149 // helpers for make_shared / allocate_shared
151 template<typename _Tp
>
152 struct _Sp_destroy_inplace
154 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
157 struct _Sp_make_shared_tag
{ };
159 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
160 class _Sp_counted_ptr_inplace
161 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
163 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
167 _Sp_counted_ptr_inplace(_Alloc __a
)
168 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
171 void* __p
= &_M_storage
;
172 ::new (__p
) _Tp(); // might throw
173 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
176 template<typename
... _Args
>
177 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
178 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
181 void* __p
= &_M_storage
;
182 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
183 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
186 // override because the allocator needs to know the dynamic type
188 _M_destroy() // nothrow
190 typedef typename
_Alloc::template
191 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
192 _My_alloc_type
__a(_Base_type::_M_del
);
193 this->~_Sp_counted_ptr_inplace();
194 __a
.deallocate(this, 1);
197 // sneaky trick so __shared_ptr can get the managed pointer
199 _M_get_deleter(const std::type_info
& __ti
)
201 return __ti
== typeid(_Sp_make_shared_tag
)
202 ? static_cast<void*>(&_M_storage
)
203 : _Base_type::_M_get_deleter(__ti
);
207 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
211 template<_Lock_policy _Lp
= __default_lock_policy
>
214 template<_Lock_policy _Lp
= __default_lock_policy
>
219 : _M_pi(0) // nothrow
222 template<typename _Ptr
>
223 __shared_count(_Ptr __p
) : _M_pi(0)
227 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
232 __throw_exception_again
;
236 template<typename _Ptr
, typename _Deleter
>
237 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
239 // allocator's value_type doesn't matter, will rebind it anyway
240 typedef std::allocator
<int> _Alloc
;
241 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
242 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
246 _M_pi
= __a2
.allocate(1);
247 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
251 __d(__p
); // Call _Deleter on __p.
253 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
254 __throw_exception_again
;
258 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
259 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
261 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
262 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
266 _M_pi
= __a2
.allocate(1);
267 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
271 __d(__p
); // Call _Deleter on __p.
273 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
274 __throw_exception_again
;
278 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
279 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
282 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
283 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
287 _M_pi
= __a2
.allocate(1);
288 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
289 std::forward
<_Args
>(__args
)...);
294 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
295 __throw_exception_again
;
299 #if _GLIBCXX_DEPRECATED
300 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
301 template<typename _Tp
>
303 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
304 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
308 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
309 template<typename _Tp
, typename _Del
>
311 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
312 : _M_pi(_S_create_from_up(std::move(__r
)))
315 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
317 __shared_count(const __weak_count
<_Lp
>& __r
);
319 ~__shared_count() // nothrow
325 __shared_count(const __shared_count
& __r
)
326 : _M_pi(__r
._M_pi
) // nothrow
329 _M_pi
->_M_add_ref_copy();
333 operator=(const __shared_count
& __r
) // nothrow
335 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
339 __tmp
->_M_add_ref_copy();
348 _M_swap(__shared_count
& __r
) // nothrow
350 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
356 _M_get_use_count() const // nothrow
357 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
360 _M_unique() const // nothrow
361 { return this->_M_get_use_count() == 1; }
364 _M_get_deleter(const std::type_info
& __ti
) const
365 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
368 _M_less(const __shared_count
& __rhs
) const
369 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
372 _M_less(const __weak_count
<_Lp
>& __rhs
) const
373 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
375 // friend function injected into enclosing namespace and found by ADL
377 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
378 { return __a
._M_pi
== __b
._M_pi
; }
381 friend class __weak_count
<_Lp
>;
383 template<typename _Tp
, typename _Del
>
384 static _Sp_counted_base
<_Lp
>*
385 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
386 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
388 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
389 _Lp
>(__r
.get(), __r
.get_deleter());
392 template<typename _Tp
, typename _Del
>
393 static _Sp_counted_base
<_Lp
>*
394 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
395 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
397 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
398 typedef std::reference_wrapper
<_Del1
> _Del2
;
399 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
400 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
403 _Sp_counted_base
<_Lp
>* _M_pi
;
407 template<_Lock_policy _Lp
>
412 : _M_pi(0) // nothrow
415 __weak_count(const __shared_count
<_Lp
>& __r
)
416 : _M_pi(__r
._M_pi
) // nothrow
419 _M_pi
->_M_weak_add_ref();
422 __weak_count(const __weak_count
<_Lp
>& __r
)
423 : _M_pi(__r
._M_pi
) // nothrow
426 _M_pi
->_M_weak_add_ref();
429 ~__weak_count() // nothrow
432 _M_pi
->_M_weak_release();
436 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
438 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
440 __tmp
->_M_weak_add_ref();
442 _M_pi
->_M_weak_release();
448 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
450 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
452 __tmp
->_M_weak_add_ref();
454 _M_pi
->_M_weak_release();
460 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
462 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
468 _M_get_use_count() const // nothrow
469 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
472 _M_less(const __weak_count
& __rhs
) const
473 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
476 _M_less(const __shared_count
<_Lp
>& __rhs
) const
477 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
479 // friend function injected into enclosing namespace and found by ADL
481 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
482 { return __a
._M_pi
== __b
._M_pi
; }
485 friend class __shared_count
<_Lp
>;
487 _Sp_counted_base
<_Lp
>* _M_pi
;
490 // now that __weak_count is defined we can define this constructor:
491 template<_Lock_policy _Lp
>
493 __shared_count
<_Lp
>::
494 __shared_count(const __weak_count
<_Lp
>& __r
)
498 _M_pi
->_M_add_ref_lock();
500 __throw_bad_weak_ptr();
503 // Forward declarations.
504 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
507 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
510 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
511 class __enable_shared_from_this
;
513 template<typename _Tp
>
516 template<typename _Tp
>
519 template<typename _Tp
>
520 class enable_shared_from_this
;
522 // Support for enable_shared_from_this.
524 // Friend of __enable_shared_from_this.
525 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
527 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
528 const __enable_shared_from_this
<_Tp1
,
531 // Friend of enable_shared_from_this.
532 template<typename _Tp1
, typename _Tp2
>
534 __enable_shared_from_this_helper(const __shared_count
<>&,
535 const enable_shared_from_this
<_Tp1
>*,
538 template<_Lock_policy _Lp
>
540 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
545 * @class __shared_ptr
547 * A smart pointer with reference-counted copy semantics.
548 * The object pointed to is deleted when the last shared_ptr pointing to
549 * it is destroyed or reset.
551 template<typename _Tp
, _Lock_policy _Lp
>
555 typedef _Tp element_type
;
557 /** @brief Construct an empty %__shared_ptr.
558 * @post use_count()==0 && get()==0
561 : _M_ptr(0), _M_refcount() // never throws
564 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
565 * @param __p A pointer that is convertible to element_type*.
566 * @post use_count() == 1 && get() == __p
567 * @throw std::bad_alloc, in which case @c delete @a __p is called.
569 template<typename _Tp1
>
571 __shared_ptr(_Tp1
* __p
)
572 : _M_ptr(__p
), _M_refcount(__p
)
574 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
575 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
576 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
580 // Requirements: _Deleter's copy constructor and destructor must
583 // __shared_ptr will release __p by calling __d(__p)
585 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
586 * and the deleter @a __d.
587 * @param __p A pointer.
588 * @param __d A deleter.
589 * @post use_count() == 1 && get() == __p
590 * @throw std::bad_alloc, in which case @a __d(__p) is called.
592 template<typename _Tp1
, typename _Deleter
>
593 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
594 : _M_ptr(__p
), _M_refcount(__p
, __d
)
596 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
597 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
598 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
602 // Requirements: _Deleter's copy constructor and destructor must
603 // not throw _Alloc's copy constructor and destructor must not
606 // __shared_ptr will release __p by calling __d(__p)
608 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
609 * and the deleter @a __d.
610 * @param __p A pointer.
611 * @param __d A deleter.
612 * @param __a An allocator.
613 * @post use_count() == 1 && get() == __p
614 * @throw std::bad_alloc, in which case @a __d(__p) is called.
616 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
617 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
618 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
620 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
621 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
622 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
625 /** @brief Constructs a %__shared_ptr instance that stores @a __p
626 * and shares ownership with @a __r.
627 * @param __r A %__shared_ptr.
628 * @param __p A pointer that will remain valid while @a *__r is valid.
629 * @post get() == __p && use_count() == __r.use_count()
631 * This can be used to construct a @c shared_ptr to a sub-object
632 * of an object managed by an existing @c shared_ptr.
635 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
636 * shared_ptr<int> pi(pii, &pii->first);
637 * assert(pii.use_count() == 2);
640 template<typename _Tp1
>
641 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
642 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
645 // generated copy constructor, assignment, destructor are fine.
647 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
648 * otherwise construct a %__shared_ptr that shares ownership
650 * @param __r A %__shared_ptr.
651 * @post get() == __r.get() && use_count() == __r.use_count()
653 template<typename _Tp1
>
654 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
655 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
656 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
658 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
659 * @param __r A %__shared_ptr rvalue.
660 * @post *this contains the old value of @a __r, @a __r is empty.
662 __shared_ptr(__shared_ptr
&& __r
)
663 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
665 _M_refcount
._M_swap(__r
._M_refcount
);
669 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
670 * @param __r A %__shared_ptr rvalue.
671 * @post *this contains the old value of @a __r, @a __r is empty.
673 template<typename _Tp1
>
674 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
675 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
677 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
678 _M_refcount
._M_swap(__r
._M_refcount
);
682 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
683 * and stores a copy of the pointer stored in @a __r.
684 * @param __r A weak_ptr.
685 * @post use_count() == __r.use_count()
686 * @throw bad_weak_ptr when __r.expired(),
687 * in which case the constructor has no effect.
689 template<typename _Tp1
>
691 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
692 : _M_refcount(__r
._M_refcount
) // may throw
694 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
695 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
700 template<typename _Tp1
, typename _Del
>
702 __shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
705 * If an exception is thrown this constructor has no effect.
707 template<typename _Tp1
, typename _Del
>
709 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
710 : _M_ptr(__r
.get()), _M_refcount()
712 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
713 _Tp1
* __tmp
= __r
.get();
714 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
715 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
718 #if _GLIBCXX_DEPRECATED
720 * @post use_count() == 1 and __r.get() == 0
722 template<typename _Tp1
>
724 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
725 : _M_ptr(__r
.get()), _M_refcount()
727 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
728 // TODO requires _Tp1 is complete, delete __r.release() well-formed
729 _Tp1
* __tmp
= __r
.get();
730 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
731 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
735 template<typename _Tp1
>
737 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
740 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
744 #if _GLIBCXX_DEPRECATED
745 template<typename _Tp1
>
747 operator=(std::auto_ptr
<_Tp1
>&& __r
)
749 __shared_ptr(std::move(__r
)).swap(*this);
755 operator=(__shared_ptr
&& __r
)
757 __shared_ptr(std::move(__r
)).swap(*this);
763 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
765 __shared_ptr(std::move(__r
)).swap(*this);
769 template<typename _Tp1
, typename _Del
>
771 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
773 template<typename _Tp1
, typename _Del
>
775 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
777 __shared_ptr(std::move(__r
)).swap(*this);
782 reset() // never throws
783 { __shared_ptr().swap(*this); }
785 template<typename _Tp1
>
787 reset(_Tp1
* __p
) // _Tp1 must be complete.
789 // Catch self-reset errors.
790 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
791 __shared_ptr(__p
).swap(*this);
794 template<typename _Tp1
, typename _Deleter
>
796 reset(_Tp1
* __p
, _Deleter __d
)
797 { __shared_ptr(__p
, __d
).swap(*this); }
799 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
801 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
802 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
804 // Allow class instantiation when _Tp is [cv-qual] void.
805 typename
std::add_lvalue_reference
<_Tp
>::type
806 operator*() const // never throws
808 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
813 operator->() const // never throws
815 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
820 get() const // never throws
823 // Implicit conversion to "bool"
825 typedef _Tp
* __shared_ptr::*__unspecified_bool_type
;
828 operator __unspecified_bool_type() const // never throws
829 { return _M_ptr
== 0 ? 0 : &__shared_ptr::_M_ptr
; }
832 unique() const // never throws
833 { return _M_refcount
._M_unique(); }
836 use_count() const // never throws
837 { return _M_refcount
._M_get_use_count(); }
840 swap(__shared_ptr
<_Tp
, _Lp
>&& __other
) // never throws
842 std::swap(_M_ptr
, __other
._M_ptr
);
843 _M_refcount
._M_swap(__other
._M_refcount
);
846 template<typename _Tp1
>
848 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
849 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
851 template<typename _Tp1
>
853 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
854 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
857 // This constructor is non-standard, it is used by allocate_shared.
858 template<typename _Alloc
, typename
... _Args
>
859 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
860 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
861 std::forward
<_Args
>(__args
)...)
863 // _M_ptr needs to point to the newly constructed object.
864 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
865 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
866 _M_ptr
= static_cast<_Tp
*>(__p
);
867 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
870 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
872 friend __shared_ptr
<_Tp1
, _Lp1
>
873 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
877 _M_get_deleter(const std::type_info
& __ti
) const
878 { return _M_refcount
._M_get_deleter(__ti
); }
880 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
881 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
883 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
884 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
886 _Tp
* _M_ptr
; // Contained pointer.
887 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
890 // 20.8.13.2.7 shared_ptr comparisons
891 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
893 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
894 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
895 { return __a
.get() == __b
.get(); }
897 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
899 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
900 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
901 { return __a
.get() != __b
.get(); }
903 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
905 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
906 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
907 { return __a
.get() < __b
.get(); }
909 template<typename _Sp
>
910 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
913 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
915 return std::less
<typename
_Sp::element_type
*>()(__lhs
.get(),
920 template<typename _Tp
, _Lock_policy _Lp
>
921 struct less
<__shared_ptr
<_Tp
, _Lp
>>
922 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
925 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
926 template<typename _Tp
, _Lock_policy _Lp
>
928 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
929 const __shared_ptr
<_Tp
, _Lp
>& __b
)
930 { return __a
.get() > __b
.get(); }
932 template<typename _Tp
, _Lock_policy _Lp
>
934 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
935 const __shared_ptr
<_Tp
, _Lp
>& __b
)
936 { return __a
.get() >= __b
.get(); }
938 template<typename _Tp
, _Lock_policy _Lp
>
940 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
941 const __shared_ptr
<_Tp
, _Lp
>& __b
)
942 { return __a
.get() <= __b
.get(); }
944 // 2.2.3.8 shared_ptr specialized algorithms.
945 template<typename _Tp
, _Lock_policy _Lp
>
947 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
950 template<typename _Tp
, _Lock_policy _Lp
>
952 swap(__shared_ptr
<_Tp
, _Lp
>&& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
955 template<typename _Tp
, _Lock_policy _Lp
>
957 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>&& __b
)
960 // 2.2.3.9 shared_ptr casts
961 /** @warning The seemingly equivalent
962 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
963 * will eventually result in undefined behaviour,
964 * attempting to delete the same object twice.
966 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
967 inline __shared_ptr
<_Tp
, _Lp
>
968 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
969 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
971 /** @warning The seemingly equivalent
972 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
973 * will eventually result in undefined behaviour,
974 * attempting to delete the same object twice.
976 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
977 inline __shared_ptr
<_Tp
, _Lp
>
978 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
979 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
981 /** @warning The seemingly equivalent
982 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
983 * will eventually result in undefined behaviour,
984 * attempting to delete the same object twice.
986 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
987 inline __shared_ptr
<_Tp
, _Lp
>
988 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
990 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
991 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
992 return __shared_ptr
<_Tp
, _Lp
>();
995 // 2.2.3.7 shared_ptr I/O
996 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
997 std::basic_ostream
<_Ch
, _Tr
>&
998 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
999 const __shared_ptr
<_Tp
, _Lp
>& __p
)
1005 // 2.2.3.10 shared_ptr get_deleter (experimental)
1006 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
1008 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
1009 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
1012 template<typename _Tp
, _Lock_policy _Lp
>
1016 typedef _Tp element_type
;
1019 : _M_ptr(0), _M_refcount() // never throws
1022 // Generated copy constructor, assignment, destructor are fine.
1024 // The "obvious" converting constructor implementation:
1026 // template<typename _Tp1>
1027 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1028 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1031 // has a serious problem.
1033 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1034 // conversion may require access to *__r._M_ptr (virtual inheritance).
1036 // It is not possible to avoid spurious access violations since
1037 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1038 template<typename _Tp1
>
1039 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
1040 : _M_refcount(__r
._M_refcount
) // never throws
1042 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
1043 _M_ptr
= __r
.lock().get();
1046 template<typename _Tp1
>
1047 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1048 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1049 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
1051 template<typename _Tp1
>
1053 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1055 _M_ptr
= __r
.lock().get();
1056 _M_refcount
= __r
._M_refcount
;
1060 template<typename _Tp1
>
1062 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1064 _M_ptr
= __r
._M_ptr
;
1065 _M_refcount
= __r
._M_refcount
;
1069 __shared_ptr
<_Tp
, _Lp
>
1070 lock() const // never throws
1073 // Optimization: avoid throw overhead.
1075 return __shared_ptr
<element_type
, _Lp
>();
1079 return __shared_ptr
<element_type
, _Lp
>(*this);
1081 catch(const bad_weak_ptr
&)
1083 // Q: How can we get here?
1084 // A: Another thread may have invalidated r after the
1085 // use_count test above.
1086 return __shared_ptr
<element_type
, _Lp
>();
1090 // Optimization: avoid try/catch overhead when single threaded.
1091 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1092 : __shared_ptr
<element_type
, _Lp
>(*this);
1098 use_count() const // never throws
1099 { return _M_refcount
._M_get_use_count(); }
1102 expired() const // never throws
1103 { return _M_refcount
._M_get_use_count() == 0; }
1105 template<typename _Tp1
>
1107 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1108 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1110 template<typename _Tp1
>
1112 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1113 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1116 reset() // never throws
1117 { __weak_ptr().swap(*this); }
1120 swap(__weak_ptr
& __s
) // never throws
1122 std::swap(_M_ptr
, __s
._M_ptr
);
1123 _M_refcount
._M_swap(__s
._M_refcount
);
1127 template<typename _Tp1
>
1128 bool operator<(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1129 template<typename _Tp1
>
1130 bool operator<=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1131 template<typename _Tp1
>
1132 bool operator>(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1133 template<typename _Tp1
>
1134 bool operator>=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1137 // Used by __enable_shared_from_this.
1139 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1142 _M_refcount
= __refcount
;
1145 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1146 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1147 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1148 friend class enable_shared_from_this
<_Tp
>;
1150 _Tp
* _M_ptr
; // Contained pointer.
1151 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1154 // 20.8.13.3.7 weak_ptr specialized algorithms.
1155 template<typename _Tp
, _Lock_policy _Lp
>
1157 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1161 template<typename _Tp
> struct owner_less
;
1163 template<typename _Tp
, typename _Tp1
>
1164 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1167 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1168 { return __lhs
.owner_before(__rhs
); }
1170 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1171 { return __lhs
.owner_before(__rhs
); }
1173 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1174 { return __lhs
.owner_before(__rhs
); }
1177 template<typename _Tp
, _Lock_policy _Lp
>
1178 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1179 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1182 template<typename _Tp
, _Lock_policy _Lp
>
1183 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1184 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1189 template<typename _Tp
, _Lock_policy _Lp
>
1190 class __enable_shared_from_this
1193 __enable_shared_from_this() { }
1195 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1197 __enable_shared_from_this
&
1198 operator=(const __enable_shared_from_this
&)
1201 ~__enable_shared_from_this() { }
1204 __shared_ptr
<_Tp
, _Lp
>
1206 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1208 __shared_ptr
<const _Tp
, _Lp
>
1209 shared_from_this() const
1210 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1213 template<typename _Tp1
>
1215 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1216 { _M_weak_this
._M_assign(__p
, __n
); }
1218 template<typename _Tp1
>
1220 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1221 const __enable_shared_from_this
* __pe
,
1225 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1228 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1233 // The actual shared_ptr, with forwarding constructors and
1234 // assignment operators.
1235 template<typename _Tp
>
1237 : public __shared_ptr
<_Tp
>
1241 : __shared_ptr
<_Tp
>() { }
1243 template<typename _Tp1
>
1245 shared_ptr(_Tp1
* __p
)
1246 : __shared_ptr
<_Tp
>(__p
) { }
1248 template<typename _Tp1
, typename _Deleter
>
1249 shared_ptr(_Tp1
* __p
, _Deleter __d
)
1250 : __shared_ptr
<_Tp
>(__p
, __d
) { }
1252 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
1253 shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
1254 : __shared_ptr
<_Tp
>(__p
, __d
, __a
) { }
1256 // Aliasing constructor
1257 template<typename _Tp1
>
1258 shared_ptr(const shared_ptr
<_Tp1
>& __r
, _Tp
* __p
)
1259 : __shared_ptr
<_Tp
>(__r
, __p
) { }
1261 template<typename _Tp1
>
1262 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
1263 : __shared_ptr
<_Tp
>(__r
) { }
1265 shared_ptr(shared_ptr
&& __r
)
1266 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1268 template<typename _Tp1
>
1269 shared_ptr(shared_ptr
<_Tp1
>&& __r
)
1270 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1272 template<typename _Tp1
>
1274 shared_ptr(const weak_ptr
<_Tp1
>& __r
)
1275 : __shared_ptr
<_Tp
>(__r
) { }
1277 #if _GLIBCXX_DEPRECATED
1278 template<typename _Tp1
>
1280 shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
1281 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1284 template<typename _Tp1
, typename _Del
>
1286 shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
1288 template<typename _Tp1
, typename _Del
>
1290 shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1291 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1293 template<typename _Tp1
>
1295 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1297 this->__shared_ptr
<_Tp
>::operator=(__r
);
1301 #if _GLIBCXX_DEPRECATED
1302 template<typename _Tp1
>
1304 operator=(std::auto_ptr
<_Tp1
>&& __r
)
1306 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1312 operator=(shared_ptr
&& __r
)
1314 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1318 template<class _Tp1
>
1320 operator=(shared_ptr
<_Tp1
>&& __r
)
1322 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1326 template<typename _Tp1
, typename _Del
>
1328 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
1330 template<typename _Tp1
, typename _Del
>
1332 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1334 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1339 // This constructor is non-standard, it is used by allocate_shared.
1340 template<typename _Alloc
, typename
... _Args
>
1341 shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
1342 : __shared_ptr
<_Tp
>(__tag
, __a
, std::forward
<_Args
>(__args
)...)
1345 template<typename _Tp1
, typename _Alloc
, typename
... _Args
>
1346 friend shared_ptr
<_Tp1
>
1347 allocate_shared(_Alloc __a
, _Args
&&... __args
);
1350 // 20.8.13.2.7 shared_ptr comparisons
1351 template<typename _Tp1
, typename _Tp2
>
1353 operator==(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1354 { return __a
.get() == __b
.get(); }
1356 template<typename _Tp1
, typename _Tp2
>
1358 operator!=(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1359 { return __a
.get() != __b
.get(); }
1361 template<typename _Tp1
, typename _Tp2
>
1363 operator<(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1364 { return __a
.get() < __b
.get(); }
1366 template<typename _Tp
>
1367 struct less
<shared_ptr
<_Tp
>>
1368 : public _Sp_less
<shared_ptr
<_Tp
>>
1371 // 20.8.13.2.9 shared_ptr specialized algorithms.
1372 template<typename _Tp
>
1374 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>& __b
)
1377 template<typename _Tp
>
1379 swap(shared_ptr
<_Tp
>&& __a
, shared_ptr
<_Tp
>& __b
)
1382 template<typename _Tp
>
1384 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>&& __b
)
1387 // 20.8.13.2.10 shared_ptr casts.
1388 template<typename _Tp
, typename _Tp1
>
1389 inline shared_ptr
<_Tp
>
1390 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1391 { return shared_ptr
<_Tp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1393 template<typename _Tp
, typename _Tp1
>
1394 inline shared_ptr
<_Tp
>
1395 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1396 { return shared_ptr
<_Tp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1398 template<typename _Tp
, typename _Tp1
>
1399 inline shared_ptr
<_Tp
>
1400 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1402 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1403 return shared_ptr
<_Tp
>(__r
, __p
);
1404 return shared_ptr
<_Tp
>();
1409 // The actual weak_ptr, with forwarding constructors and
1410 // assignment operators.
1411 template<typename _Tp
>
1413 : public __weak_ptr
<_Tp
>
1417 : __weak_ptr
<_Tp
>() { }
1419 template<typename _Tp1
>
1420 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1421 : __weak_ptr
<_Tp
>(__r
) { }
1423 template<typename _Tp1
>
1424 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1425 : __weak_ptr
<_Tp
>(__r
) { }
1427 template<typename _Tp1
>
1429 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1431 this->__weak_ptr
<_Tp
>::operator=(__r
);
1435 template<typename _Tp1
>
1437 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1439 this->__weak_ptr
<_Tp
>::operator=(__r
);
1444 lock() const // never throws
1447 if (this->expired())
1448 return shared_ptr
<_Tp
>();
1452 return shared_ptr
<_Tp
>(*this);
1454 catch(const bad_weak_ptr
&)
1456 return shared_ptr
<_Tp
>();
1459 return this->expired() ? shared_ptr
<_Tp
>()
1460 : shared_ptr
<_Tp
>(*this);
1465 template<typename _Tp1
>
1466 bool operator<(const weak_ptr
<_Tp1
>&) const = delete;
1467 template<typename _Tp1
>
1468 bool operator<=(const weak_ptr
<_Tp1
>&) const = delete;
1469 template<typename _Tp1
>
1470 bool operator>(const weak_ptr
<_Tp1
>&) const = delete;
1471 template<typename _Tp1
>
1472 bool operator>=(const weak_ptr
<_Tp1
>&) const = delete;
1475 // 20.8.13.3.7 weak_ptr specialized algorithms.
1476 template<typename _Tp
>
1478 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
)
1482 template<typename _Tp
>
1483 struct owner_less
<shared_ptr
<_Tp
>>
1484 : public _Sp_owner_less
<shared_ptr
<_Tp
>, weak_ptr
<_Tp
>>
1487 template<typename _Tp
>
1488 struct owner_less
<weak_ptr
<_Tp
>>
1489 : public _Sp_owner_less
<weak_ptr
<_Tp
>, shared_ptr
<_Tp
>>
1492 /// enable_shared_from_this
1493 template<typename _Tp
>
1494 class enable_shared_from_this
1497 enable_shared_from_this() { }
1499 enable_shared_from_this(const enable_shared_from_this
&) { }
1501 enable_shared_from_this
&
1502 operator=(const enable_shared_from_this
&)
1505 ~enable_shared_from_this() { }
1510 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1512 shared_ptr
<const _Tp
>
1513 shared_from_this() const
1514 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1517 template<typename _Tp1
>
1519 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1520 { _M_weak_this
._M_assign(__p
, __n
); }
1522 template<typename _Tp1
>
1524 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1525 const enable_shared_from_this
* __pe
,
1529 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1532 mutable weak_ptr
<_Tp
> _M_weak_this
;
1535 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1536 inline __shared_ptr
<_Tp
, _Lp
>
1537 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1539 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1540 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1543 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1544 inline __shared_ptr
<_Tp
, _Lp
>
1545 __make_shared(_Args
&&... __args
)
1547 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1548 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1549 std::forward
<_Args
>(__args
)...);
1552 /** @brief Create an object that is owned by a shared_ptr.
1553 * @param __a An allocator.
1554 * @param __args Arguments for the @a _Tp object's constructor.
1555 * @return A shared_ptr that owns the newly created object.
1556 * @throw An exception thrown from @a _Alloc::allocate or from the
1557 * constructor of @a _Tp.
1559 * A copy of @a __a will be used to allocate memory for the shared_ptr
1560 * and the new object.
1562 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
1563 inline shared_ptr
<_Tp
>
1564 allocate_shared(_Alloc __a
, _Args
&&... __args
)
1566 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), std::forward
<_Alloc
>(__a
),
1567 std::forward
<_Args
>(__args
)...);
1570 /** @brief Create an object that is owned by a shared_ptr.
1571 * @param __args Arguments for the @a _Tp object's constructor.
1572 * @return A shared_ptr that owns the newly created object.
1573 * @throw std::bad_alloc, or an exception thrown from the
1574 * constructor of @a _Tp.
1576 template<typename _Tp
, typename
... _Args
>
1577 inline shared_ptr
<_Tp
>
1578 make_shared(_Args
&&... __args
)
1580 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1581 return allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
1582 std::forward
<_Args
>(__args
)...);