1 // shared_ptr and weak_ptr implementation -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 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 3, 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 // 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.
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/>.
26 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
30 // Copyright (C) 2001, 2002, 2003 Peter Dimov
33 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // enable_shared_from_this.hpp
36 // Copyright (C) 2002 Peter Dimov
38 // Distributed under the Boost Software License, Version 1.0. (See
39 // accompanying file LICENSE_1_0.txt or copy at
40 // http://www.boost.org/LICENSE_1_0.txt)
42 // GCC Note: based on version 1.32.0 of the Boost library.
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.
50 #define _SHARED_PTR_H 1
52 #ifndef __GXX_EXPERIMENTAL_CXX0X__
53 # include <c++0x_warning.h>
56 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
57 # error C++0x header cannot be included from TR1 header
60 _GLIBCXX_BEGIN_NAMESPACE(std
)
63 * @addtogroup pointer_abstractions
67 // counted ptr with no deleter or allocator support
68 template<typename _Ptr
, _Lock_policy _Lp
>
70 : public _Sp_counted_base
<_Lp
>
73 _Sp_counted_ptr(_Ptr __p
)
77 _M_dispose() // nothrow
81 _M_destroy() // nothrow
85 _M_get_deleter(const std::type_info
& __ti
)
88 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
89 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
92 _Ptr _M_ptr
; // copy constructor must not throw
95 // support for custom deleter and/or allocator
96 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
97 class _Sp_counted_deleter
98 : public _Sp_counted_ptr
<_Ptr
, _Lp
>
100 typedef typename
_Alloc::template
101 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
103 // Helper class that stores the Deleter and also acts as an allocator.
104 // Used to dispose of the owned pointer and the internal refcount
105 // Requires that copies of _Alloc can free each other's memory.
107 : public _My_alloc_type
// copy constructor must not throw
109 _Deleter _M_del
; // copy constructor must not throw
110 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
111 : _My_alloc_type(__a
), _M_del(__d
) { }
115 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
120 * @pre __d(__p) must not throw.
122 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
123 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
127 * @pre __d(__p) must not throw.
129 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
130 : _Base_type(__p
), _M_del(__d
, __a
) { }
133 _M_dispose() // nothrow
134 { _M_del
._M_del(_Base_type::_M_ptr
); }
137 _M_destroy() // nothrow
139 _My_alloc_type
__a(_M_del
);
140 this->~_Sp_counted_deleter();
141 __a
.deallocate(this, 1);
145 _M_get_deleter(const std::type_info
& __ti
)
146 { return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0; }
149 _My_Deleter _M_del
; // copy constructor must not throw
152 // helpers for make_shared / allocate_shared
154 template<typename _Tp
>
155 struct _Sp_destroy_inplace
157 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
160 struct _Sp_make_shared_tag
{ };
162 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
163 class _Sp_counted_ptr_inplace
164 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
166 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
170 _Sp_counted_ptr_inplace(_Alloc __a
)
171 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
174 void* __p
= &_M_storage
;
175 ::new (__p
) _Tp(); // might throw
176 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
179 template<typename
... _Args
>
180 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
181 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
184 void* __p
= &_M_storage
;
185 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
186 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
189 // override because the allocator needs to know the dynamic type
191 _M_destroy() // nothrow
193 typedef typename
_Alloc::template
194 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
195 _My_alloc_type
__a(_Base_type::_M_del
);
196 this->~_Sp_counted_ptr_inplace();
197 __a
.deallocate(this, 1);
200 // sneaky trick so __shared_ptr can get the managed pointer
202 _M_get_deleter(const std::type_info
& __ti
)
204 return __ti
== typeid(_Sp_make_shared_tag
)
205 ? static_cast<void*>(&_M_storage
)
206 : _Base_type::_M_get_deleter(__ti
);
210 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
214 template<_Lock_policy _Lp
= __default_lock_policy
>
217 template<_Lock_policy _Lp
= __default_lock_policy
>
222 : _M_pi(0) // nothrow
225 template<typename _Ptr
>
226 __shared_count(_Ptr __p
) : _M_pi(0)
230 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
235 __throw_exception_again
;
239 template<typename _Ptr
, typename _Deleter
>
240 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
242 // allocator's value_type doesn't matter, will rebind it anyway
243 typedef std::allocator
<int> _Alloc
;
244 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
245 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
249 _M_pi
= __a2
.allocate(1);
250 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
254 __d(__p
); // Call _Deleter on __p.
256 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
257 __throw_exception_again
;
261 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
262 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
264 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
265 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
269 _M_pi
= __a2
.allocate(1);
270 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
274 __d(__p
); // Call _Deleter on __p.
276 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
277 __throw_exception_again
;
281 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
282 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
285 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
286 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
290 _M_pi
= __a2
.allocate(1);
291 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
292 std::forward
<_Args
>(__args
)...);
297 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
298 __throw_exception_again
;
302 #if _GLIBCXX_DEPRECATED
303 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
304 template<typename _Tp
>
306 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
307 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
311 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
312 template<typename _Tp
, typename _Del
>
314 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
315 : _M_pi(_S_create_from_up(std::move(__r
)))
318 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
320 __shared_count(const __weak_count
<_Lp
>& __r
);
322 ~__shared_count() // nothrow
328 __shared_count(const __shared_count
& __r
)
329 : _M_pi(__r
._M_pi
) // nothrow
332 _M_pi
->_M_add_ref_copy();
336 operator=(const __shared_count
& __r
) // nothrow
338 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
342 __tmp
->_M_add_ref_copy();
351 _M_swap(__shared_count
& __r
) // nothrow
353 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
359 _M_get_use_count() const // nothrow
360 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
363 _M_unique() const // nothrow
364 { return this->_M_get_use_count() == 1; }
367 _M_get_deleter(const std::type_info
& __ti
) const
368 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
371 _M_less(const __shared_count
& __rhs
) const
372 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
375 _M_less(const __weak_count
<_Lp
>& __rhs
) const
376 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
378 // friend function injected into enclosing namespace and found by ADL
380 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
381 { return __a
._M_pi
== __b
._M_pi
; }
384 friend class __weak_count
<_Lp
>;
386 template<typename _Tp
, typename _Del
>
387 static _Sp_counted_base
<_Lp
>*
388 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
389 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
391 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
392 _Lp
>(__r
.get(), __r
.get_deleter());
395 template<typename _Tp
, typename _Del
>
396 static _Sp_counted_base
<_Lp
>*
397 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
398 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
400 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
401 typedef std::reference_wrapper
<_Del1
> _Del2
;
402 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
403 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
406 _Sp_counted_base
<_Lp
>* _M_pi
;
410 template<_Lock_policy _Lp
>
415 : _M_pi(0) // nothrow
418 __weak_count(const __shared_count
<_Lp
>& __r
)
419 : _M_pi(__r
._M_pi
) // nothrow
422 _M_pi
->_M_weak_add_ref();
425 __weak_count(const __weak_count
<_Lp
>& __r
)
426 : _M_pi(__r
._M_pi
) // nothrow
429 _M_pi
->_M_weak_add_ref();
432 ~__weak_count() // nothrow
435 _M_pi
->_M_weak_release();
439 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
441 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
443 __tmp
->_M_weak_add_ref();
445 _M_pi
->_M_weak_release();
451 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
453 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
455 __tmp
->_M_weak_add_ref();
457 _M_pi
->_M_weak_release();
463 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
465 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
471 _M_get_use_count() const // nothrow
472 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
475 _M_less(const __weak_count
& __rhs
) const
476 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
479 _M_less(const __shared_count
<_Lp
>& __rhs
) const
480 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
482 // friend function injected into enclosing namespace and found by ADL
484 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
485 { return __a
._M_pi
== __b
._M_pi
; }
488 friend class __shared_count
<_Lp
>;
490 _Sp_counted_base
<_Lp
>* _M_pi
;
493 // now that __weak_count is defined we can define this constructor:
494 template<_Lock_policy _Lp
>
496 __shared_count
<_Lp
>::
497 __shared_count(const __weak_count
<_Lp
>& __r
)
501 _M_pi
->_M_add_ref_lock();
503 __throw_bad_weak_ptr();
506 // Forward declarations.
507 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
510 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
513 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
514 class __enable_shared_from_this
;
516 template<typename _Tp
>
519 template<typename _Tp
>
522 template<typename _Tp
>
523 class enable_shared_from_this
;
525 // Support for enable_shared_from_this.
527 // Friend of __enable_shared_from_this.
528 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
530 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
531 const __enable_shared_from_this
<_Tp1
,
534 // Friend of enable_shared_from_this.
535 template<typename _Tp1
, typename _Tp2
>
537 __enable_shared_from_this_helper(const __shared_count
<>&,
538 const enable_shared_from_this
<_Tp1
>*,
541 template<_Lock_policy _Lp
>
543 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
547 template<typename _Tp
, _Lock_policy _Lp
>
551 typedef _Tp element_type
;
553 /** @brief Construct an empty %__shared_ptr.
554 * @post use_count()==0 && get()==0
557 : _M_ptr(0), _M_refcount() // never throws
560 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
561 * @param __p A pointer that is convertible to element_type*.
562 * @post use_count() == 1 && get() == __p
563 * @throw std::bad_alloc, in which case @c delete @a __p is called.
565 template<typename _Tp1
>
567 __shared_ptr(_Tp1
* __p
)
568 : _M_ptr(__p
), _M_refcount(__p
)
570 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
571 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
572 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
576 // Requirements: _Deleter's copy constructor and destructor must
579 // __shared_ptr will release __p by calling __d(__p)
581 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
582 * and the deleter @a __d.
583 * @param __p A pointer.
584 * @param __d A deleter.
585 * @post use_count() == 1 && get() == __p
586 * @throw std::bad_alloc, in which case @a __d(__p) is called.
588 template<typename _Tp1
, typename _Deleter
>
589 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
590 : _M_ptr(__p
), _M_refcount(__p
, __d
)
592 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
593 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
594 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
598 // Requirements: _Deleter's copy constructor and destructor must
599 // not throw _Alloc's copy constructor and destructor must not
602 // __shared_ptr will release __p by calling __d(__p)
604 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
605 * and the deleter @a __d.
606 * @param __p A pointer.
607 * @param __d A deleter.
608 * @param __a An allocator.
609 * @post use_count() == 1 && get() == __p
610 * @throw std::bad_alloc, in which case @a __d(__p) is called.
612 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
613 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
614 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
616 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
617 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
618 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
621 /** @brief Constructs a %__shared_ptr instance that stores @a __p
622 * and shares ownership with @a __r.
623 * @param __r A %__shared_ptr.
624 * @param __p A pointer that will remain valid while @a *__r is valid.
625 * @post get() == __p && use_count() == __r.use_count()
627 * This can be used to construct a @c shared_ptr to a sub-object
628 * of an object managed by an existing @c shared_ptr.
631 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
632 * shared_ptr<int> pi(pii, &pii->first);
633 * assert(pii.use_count() == 2);
636 template<typename _Tp1
>
637 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
638 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
641 // generated copy constructor, assignment, destructor are fine.
643 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
644 * otherwise construct a %__shared_ptr that shares ownership
646 * @param __r A %__shared_ptr.
647 * @post get() == __r.get() && use_count() == __r.use_count()
649 template<typename _Tp1
>
650 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
651 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
652 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
654 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
655 * @param __r A %__shared_ptr rvalue.
656 * @post *this contains the old value of @a __r, @a __r is empty.
658 __shared_ptr(__shared_ptr
&& __r
)
659 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
661 _M_refcount
._M_swap(__r
._M_refcount
);
665 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
666 * @param __r A %__shared_ptr rvalue.
667 * @post *this contains the old value of @a __r, @a __r is empty.
669 template<typename _Tp1
>
670 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
671 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
673 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
674 _M_refcount
._M_swap(__r
._M_refcount
);
678 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
679 * and stores a copy of the pointer stored in @a __r.
680 * @param __r A weak_ptr.
681 * @post use_count() == __r.use_count()
682 * @throw bad_weak_ptr when __r.expired(),
683 * in which case the constructor has no effect.
685 template<typename _Tp1
>
687 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
688 : _M_refcount(__r
._M_refcount
) // may throw
690 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
691 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
696 template<typename _Tp1
, typename _Del
>
698 __shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
701 * If an exception is thrown this constructor has no effect.
703 template<typename _Tp1
, typename _Del
>
705 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
706 : _M_ptr(__r
.get()), _M_refcount()
708 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
709 _Tp1
* __tmp
= __r
.get();
710 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
711 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
714 #if _GLIBCXX_DEPRECATED
716 * @post use_count() == 1 and __r.get() == 0
718 template<typename _Tp1
>
720 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
721 : _M_ptr(__r
.get()), _M_refcount()
723 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
724 // TODO requires _Tp1 is complete, delete __r.release() well-formed
725 _Tp1
* __tmp
= __r
.get();
726 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
727 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
731 template<typename _Tp1
>
733 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
736 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
740 #if _GLIBCXX_DEPRECATED
741 template<typename _Tp1
>
743 operator=(std::auto_ptr
<_Tp1
>&& __r
)
745 __shared_ptr(std::move(__r
)).swap(*this);
751 operator=(__shared_ptr
&& __r
)
753 __shared_ptr(std::move(__r
)).swap(*this);
759 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
761 __shared_ptr(std::move(__r
)).swap(*this);
765 template<typename _Tp1
, typename _Del
>
767 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
769 template<typename _Tp1
, typename _Del
>
771 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
773 __shared_ptr(std::move(__r
)).swap(*this);
778 reset() // never throws
779 { __shared_ptr().swap(*this); }
781 template<typename _Tp1
>
783 reset(_Tp1
* __p
) // _Tp1 must be complete.
785 // Catch self-reset errors.
786 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
787 __shared_ptr(__p
).swap(*this);
790 template<typename _Tp1
, typename _Deleter
>
792 reset(_Tp1
* __p
, _Deleter __d
)
793 { __shared_ptr(__p
, __d
).swap(*this); }
795 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
797 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
798 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
800 // Allow class instantiation when _Tp is [cv-qual] void.
801 typename
std::add_lvalue_reference
<_Tp
>::type
802 operator*() const // never throws
804 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
809 operator->() const // never throws
811 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
816 get() const // never throws
819 explicit operator bool() const // never throws
820 { return _M_ptr
== 0 ? false : true; }
823 unique() const // never throws
824 { return _M_refcount
._M_unique(); }
827 use_count() const // never throws
828 { return _M_refcount
._M_get_use_count(); }
831 swap(__shared_ptr
<_Tp
, _Lp
>&& __other
) // never throws
833 std::swap(_M_ptr
, __other
._M_ptr
);
834 _M_refcount
._M_swap(__other
._M_refcount
);
837 template<typename _Tp1
>
839 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
840 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
842 template<typename _Tp1
>
844 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
845 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
848 // This constructor is non-standard, it is used by allocate_shared.
849 template<typename _Alloc
, typename
... _Args
>
850 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
851 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
852 std::forward
<_Args
>(__args
)...)
854 // _M_ptr needs to point to the newly constructed object.
855 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
856 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
857 _M_ptr
= static_cast<_Tp
*>(__p
);
858 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
861 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
863 friend __shared_ptr
<_Tp1
, _Lp1
>
864 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
868 _M_get_deleter(const std::type_info
& __ti
) const
869 { return _M_refcount
._M_get_deleter(__ti
); }
871 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
872 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
874 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
875 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
877 _Tp
* _M_ptr
; // Contained pointer.
878 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
881 // 20.8.13.2.7 shared_ptr comparisons
882 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
884 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
885 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
886 { return __a
.get() == __b
.get(); }
888 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
890 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
891 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
892 { return __a
.get() != __b
.get(); }
894 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
896 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
897 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
898 { return __a
.get() < __b
.get(); }
900 template<typename _Sp
>
901 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
904 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
906 return std::less
<typename
_Sp::element_type
*>()(__lhs
.get(),
911 template<typename _Tp
, _Lock_policy _Lp
>
912 struct less
<__shared_ptr
<_Tp
, _Lp
>>
913 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
916 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
917 template<typename _Tp
, _Lock_policy _Lp
>
919 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
920 const __shared_ptr
<_Tp
, _Lp
>& __b
)
921 { return __a
.get() > __b
.get(); }
923 template<typename _Tp
, _Lock_policy _Lp
>
925 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
926 const __shared_ptr
<_Tp
, _Lp
>& __b
)
927 { return __a
.get() >= __b
.get(); }
929 template<typename _Tp
, _Lock_policy _Lp
>
931 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
932 const __shared_ptr
<_Tp
, _Lp
>& __b
)
933 { return __a
.get() <= __b
.get(); }
935 // 2.2.3.8 shared_ptr specialized algorithms.
936 template<typename _Tp
, _Lock_policy _Lp
>
938 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
941 template<typename _Tp
, _Lock_policy _Lp
>
943 swap(__shared_ptr
<_Tp
, _Lp
>&& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
946 template<typename _Tp
, _Lock_policy _Lp
>
948 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>&& __b
)
951 // 2.2.3.9 shared_ptr casts
952 /** @warning The seemingly equivalent
953 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
954 * will eventually result in undefined behaviour,
955 * attempting to delete the same object twice.
957 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
958 inline __shared_ptr
<_Tp
, _Lp
>
959 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
960 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
962 /** @warning The seemingly equivalent
963 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
964 * will eventually result in undefined behaviour,
965 * attempting to delete the same object twice.
967 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
968 inline __shared_ptr
<_Tp
, _Lp
>
969 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
970 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
972 /** @warning The seemingly equivalent
973 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
974 * will eventually result in undefined behaviour,
975 * attempting to delete the same object twice.
977 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
978 inline __shared_ptr
<_Tp
, _Lp
>
979 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
981 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
982 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
983 return __shared_ptr
<_Tp
, _Lp
>();
986 // 2.2.3.7 shared_ptr I/O
987 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
988 std::basic_ostream
<_Ch
, _Tr
>&
989 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
990 const __shared_ptr
<_Tp
, _Lp
>& __p
)
996 // 2.2.3.10 shared_ptr get_deleter (experimental)
997 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
999 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
1000 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
1003 template<typename _Tp
, _Lock_policy _Lp
>
1007 typedef _Tp element_type
;
1010 : _M_ptr(0), _M_refcount() // never throws
1013 // Generated copy constructor, assignment, destructor are fine.
1015 // The "obvious" converting constructor implementation:
1017 // template<typename _Tp1>
1018 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1019 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1022 // has a serious problem.
1024 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1025 // conversion may require access to *__r._M_ptr (virtual inheritance).
1027 // It is not possible to avoid spurious access violations since
1028 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1029 template<typename _Tp1
>
1030 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
1031 : _M_refcount(__r
._M_refcount
) // never throws
1033 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
1034 _M_ptr
= __r
.lock().get();
1037 template<typename _Tp1
>
1038 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1039 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1040 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
1042 template<typename _Tp1
>
1044 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1046 _M_ptr
= __r
.lock().get();
1047 _M_refcount
= __r
._M_refcount
;
1051 template<typename _Tp1
>
1053 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1055 _M_ptr
= __r
._M_ptr
;
1056 _M_refcount
= __r
._M_refcount
;
1060 __shared_ptr
<_Tp
, _Lp
>
1061 lock() const // never throws
1064 // Optimization: avoid throw overhead.
1066 return __shared_ptr
<element_type
, _Lp
>();
1070 return __shared_ptr
<element_type
, _Lp
>(*this);
1072 __catch(const bad_weak_ptr
&)
1074 // Q: How can we get here?
1075 // A: Another thread may have invalidated r after the
1076 // use_count test above.
1077 return __shared_ptr
<element_type
, _Lp
>();
1081 // Optimization: avoid try/catch overhead when single threaded.
1082 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1083 : __shared_ptr
<element_type
, _Lp
>(*this);
1089 use_count() const // never throws
1090 { return _M_refcount
._M_get_use_count(); }
1093 expired() const // never throws
1094 { return _M_refcount
._M_get_use_count() == 0; }
1096 template<typename _Tp1
>
1098 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1099 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1101 template<typename _Tp1
>
1103 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1104 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1107 reset() // never throws
1108 { __weak_ptr().swap(*this); }
1111 swap(__weak_ptr
& __s
) // never throws
1113 std::swap(_M_ptr
, __s
._M_ptr
);
1114 _M_refcount
._M_swap(__s
._M_refcount
);
1118 template<typename _Tp1
>
1119 bool operator<(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1120 template<typename _Tp1
>
1121 bool operator<=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1122 template<typename _Tp1
>
1123 bool operator>(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1124 template<typename _Tp1
>
1125 bool operator>=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1128 // Used by __enable_shared_from_this.
1130 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1133 _M_refcount
= __refcount
;
1136 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1137 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1138 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1139 friend class enable_shared_from_this
<_Tp
>;
1141 _Tp
* _M_ptr
; // Contained pointer.
1142 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1145 // 20.8.13.3.7 weak_ptr specialized algorithms.
1146 template<typename _Tp
, _Lock_policy _Lp
>
1148 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1152 template<typename _Tp
> struct owner_less
;
1154 template<typename _Tp
, typename _Tp1
>
1155 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1158 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1159 { return __lhs
.owner_before(__rhs
); }
1161 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1162 { return __lhs
.owner_before(__rhs
); }
1164 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1165 { return __lhs
.owner_before(__rhs
); }
1168 template<typename _Tp
, _Lock_policy _Lp
>
1169 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1170 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1173 template<typename _Tp
, _Lock_policy _Lp
>
1174 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1175 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1180 template<typename _Tp
, _Lock_policy _Lp
>
1181 class __enable_shared_from_this
1184 __enable_shared_from_this() { }
1186 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1188 __enable_shared_from_this
&
1189 operator=(const __enable_shared_from_this
&)
1192 ~__enable_shared_from_this() { }
1195 __shared_ptr
<_Tp
, _Lp
>
1197 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1199 __shared_ptr
<const _Tp
, _Lp
>
1200 shared_from_this() const
1201 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1204 template<typename _Tp1
>
1206 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1207 { _M_weak_this
._M_assign(__p
, __n
); }
1209 template<typename _Tp1
>
1211 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1212 const __enable_shared_from_this
* __pe
,
1216 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1219 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1223 * @brief A smart pointer with reference-counted copy semantics.
1225 * The object pointed to is deleted when the last shared_ptr pointing to
1226 * it is destroyed or reset.
1228 template<typename _Tp
>
1230 : public __shared_ptr
<_Tp
>
1234 : __shared_ptr
<_Tp
>() { }
1236 template<typename _Tp1
>
1238 shared_ptr(_Tp1
* __p
)
1239 : __shared_ptr
<_Tp
>(__p
) { }
1241 template<typename _Tp1
, typename _Deleter
>
1242 shared_ptr(_Tp1
* __p
, _Deleter __d
)
1243 : __shared_ptr
<_Tp
>(__p
, __d
) { }
1245 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
1246 shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
1247 : __shared_ptr
<_Tp
>(__p
, __d
, __a
) { }
1249 // Aliasing constructor
1250 template<typename _Tp1
>
1251 shared_ptr(const shared_ptr
<_Tp1
>& __r
, _Tp
* __p
)
1252 : __shared_ptr
<_Tp
>(__r
, __p
) { }
1254 template<typename _Tp1
>
1255 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
1256 : __shared_ptr
<_Tp
>(__r
) { }
1258 shared_ptr(shared_ptr
&& __r
)
1259 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1261 template<typename _Tp1
>
1262 shared_ptr(shared_ptr
<_Tp1
>&& __r
)
1263 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1265 template<typename _Tp1
>
1267 shared_ptr(const weak_ptr
<_Tp1
>& __r
)
1268 : __shared_ptr
<_Tp
>(__r
) { }
1270 #if _GLIBCXX_DEPRECATED
1271 template<typename _Tp1
>
1273 shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
1274 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1277 template<typename _Tp1
, typename _Del
>
1279 shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
1281 template<typename _Tp1
, typename _Del
>
1283 shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1284 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1286 template<typename _Tp1
>
1288 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1290 this->__shared_ptr
<_Tp
>::operator=(__r
);
1294 #if _GLIBCXX_DEPRECATED
1295 template<typename _Tp1
>
1297 operator=(std::auto_ptr
<_Tp1
>&& __r
)
1299 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1305 operator=(shared_ptr
&& __r
)
1307 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1311 template<class _Tp1
>
1313 operator=(shared_ptr
<_Tp1
>&& __r
)
1315 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1319 template<typename _Tp1
, typename _Del
>
1321 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
1323 template<typename _Tp1
, typename _Del
>
1325 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1327 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1332 // This constructor is non-standard, it is used by allocate_shared.
1333 template<typename _Alloc
, typename
... _Args
>
1334 shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
1335 : __shared_ptr
<_Tp
>(__tag
, __a
, std::forward
<_Args
>(__args
)...)
1338 template<typename _Tp1
, typename _Alloc
, typename
... _Args
>
1339 friend shared_ptr
<_Tp1
>
1340 allocate_shared(_Alloc __a
, _Args
&&... __args
);
1343 // 20.8.13.2.7 shared_ptr comparisons
1344 template<typename _Tp1
, typename _Tp2
>
1346 operator==(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1347 { return __a
.get() == __b
.get(); }
1349 template<typename _Tp1
, typename _Tp2
>
1351 operator!=(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1352 { return __a
.get() != __b
.get(); }
1354 template<typename _Tp1
, typename _Tp2
>
1356 operator<(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1357 { return __a
.get() < __b
.get(); }
1359 template<typename _Tp
>
1360 struct less
<shared_ptr
<_Tp
>>
1361 : public _Sp_less
<shared_ptr
<_Tp
>>
1364 // 20.8.13.2.9 shared_ptr specialized algorithms.
1365 template<typename _Tp
>
1367 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>& __b
)
1370 template<typename _Tp
>
1372 swap(shared_ptr
<_Tp
>&& __a
, shared_ptr
<_Tp
>& __b
)
1375 template<typename _Tp
>
1377 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>&& __b
)
1380 // 20.8.13.2.10 shared_ptr casts.
1381 template<typename _Tp
, typename _Tp1
>
1382 inline shared_ptr
<_Tp
>
1383 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1384 { return shared_ptr
<_Tp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1386 template<typename _Tp
, typename _Tp1
>
1387 inline shared_ptr
<_Tp
>
1388 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1389 { return shared_ptr
<_Tp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1391 template<typename _Tp
, typename _Tp1
>
1392 inline shared_ptr
<_Tp
>
1393 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1395 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1396 return shared_ptr
<_Tp
>(__r
, __p
);
1397 return shared_ptr
<_Tp
>();
1402 * @brief A smart pointer with weak semantics.
1404 * With forwarding constructors and assignment operators.
1406 template<typename _Tp
>
1408 : public __weak_ptr
<_Tp
>
1412 : __weak_ptr
<_Tp
>() { }
1414 template<typename _Tp1
>
1415 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1416 : __weak_ptr
<_Tp
>(__r
) { }
1418 template<typename _Tp1
>
1419 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1420 : __weak_ptr
<_Tp
>(__r
) { }
1422 template<typename _Tp1
>
1424 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1426 this->__weak_ptr
<_Tp
>::operator=(__r
);
1430 template<typename _Tp1
>
1432 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1434 this->__weak_ptr
<_Tp
>::operator=(__r
);
1439 lock() const // never throws
1442 if (this->expired())
1443 return shared_ptr
<_Tp
>();
1447 return shared_ptr
<_Tp
>(*this);
1449 __catch(const bad_weak_ptr
&)
1451 return shared_ptr
<_Tp
>();
1454 return this->expired() ? shared_ptr
<_Tp
>()
1455 : shared_ptr
<_Tp
>(*this);
1460 template<typename _Tp1
>
1461 bool operator<(const weak_ptr
<_Tp1
>&) const = delete;
1462 template<typename _Tp1
>
1463 bool operator<=(const weak_ptr
<_Tp1
>&) const = delete;
1464 template<typename _Tp1
>
1465 bool operator>(const weak_ptr
<_Tp1
>&) const = delete;
1466 template<typename _Tp1
>
1467 bool operator>=(const weak_ptr
<_Tp1
>&) const = delete;
1470 // 20.8.13.3.7 weak_ptr specialized algorithms.
1471 template<typename _Tp
>
1473 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
)
1477 template<typename _Tp
>
1478 struct owner_less
<shared_ptr
<_Tp
>>
1479 : public _Sp_owner_less
<shared_ptr
<_Tp
>, weak_ptr
<_Tp
>>
1482 template<typename _Tp
>
1483 struct owner_less
<weak_ptr
<_Tp
>>
1484 : public _Sp_owner_less
<weak_ptr
<_Tp
>, shared_ptr
<_Tp
>>
1488 * @brief Base class allowing use of member function shared_from_this.
1490 template<typename _Tp
>
1491 class enable_shared_from_this
1494 enable_shared_from_this() { }
1496 enable_shared_from_this(const enable_shared_from_this
&) { }
1498 enable_shared_from_this
&
1499 operator=(const enable_shared_from_this
&)
1502 ~enable_shared_from_this() { }
1507 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1509 shared_ptr
<const _Tp
>
1510 shared_from_this() const
1511 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1514 template<typename _Tp1
>
1516 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1517 { _M_weak_this
._M_assign(__p
, __n
); }
1519 template<typename _Tp1
>
1521 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1522 const enable_shared_from_this
* __pe
,
1526 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1529 mutable weak_ptr
<_Tp
> _M_weak_this
;
1532 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1533 inline __shared_ptr
<_Tp
, _Lp
>
1534 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1536 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1537 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1540 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1541 inline __shared_ptr
<_Tp
, _Lp
>
1542 __make_shared(_Args
&&... __args
)
1544 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1545 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1546 std::forward
<_Args
>(__args
)...);
1549 /** @brief Create an object that is owned by a shared_ptr.
1550 * @param __a An allocator.
1551 * @param __args Arguments for the @a _Tp object's constructor.
1552 * @return A shared_ptr that owns the newly created object.
1553 * @throw An exception thrown from @a _Alloc::allocate or from the
1554 * constructor of @a _Tp.
1556 * A copy of @a __a will be used to allocate memory for the shared_ptr
1557 * and the new object.
1559 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
1560 inline shared_ptr
<_Tp
>
1561 allocate_shared(_Alloc __a
, _Args
&&... __args
)
1563 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), std::forward
<_Alloc
>(__a
),
1564 std::forward
<_Args
>(__args
)...);
1567 /** @brief Create an object that is owned by a shared_ptr.
1568 * @param __args Arguments for the @a _Tp object's constructor.
1569 * @return A shared_ptr that owns the newly created object.
1570 * @throw std::bad_alloc, or an exception thrown from the
1571 * constructor of @a _Tp.
1573 template<typename _Tp
, typename
... _Args
>
1574 inline shared_ptr
<_Tp
>
1575 make_shared(_Args
&&... __args
)
1577 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1578 return allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
1579 std::forward
<_Args
>(__args
)...);
1582 // @} group pointer_abstractions
1584 _GLIBCXX_END_NAMESPACE
1586 #endif // _SHARED_PTR_H