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 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.
55 #define _SHARED_PTR_H 1
57 #ifndef __GXX_EXPERIMENTAL_CXX0X__
58 # include <c++0x_warning.h>
61 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
62 # error C++0x header cannot be included from TR1 header
65 _GLIBCXX_BEGIN_NAMESPACE(std
)
68 * @addtogroup pointer_abstractions
72 // counted ptr with no deleter or allocator support
73 template<typename _Ptr
, _Lock_policy _Lp
>
75 : public _Sp_counted_base
<_Lp
>
78 _Sp_counted_ptr(_Ptr __p
)
82 _M_dispose() // nothrow
86 _M_destroy() // nothrow
90 _M_get_deleter(const std::type_info
& __ti
)
93 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
94 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
97 _Ptr _M_ptr
; // copy constructor must not throw
100 // support for custom deleter and/or allocator
101 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
102 class _Sp_counted_deleter
103 : public _Sp_counted_ptr
<_Ptr
, _Lp
>
105 typedef typename
_Alloc::template
106 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
108 // Helper class that stores the Deleter and also acts as an allocator.
109 // Used to dispose of the owned pointer and the internal refcount
110 // Requires that copies of _Alloc can free each other's memory.
112 : public _My_alloc_type
// copy constructor must not throw
114 _Deleter _M_del
; // copy constructor must not throw
115 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
116 : _My_alloc_type(__a
), _M_del(__d
) { }
120 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
125 * @pre __d(__p) must not throw.
127 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
128 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
132 * @pre __d(__p) must not throw.
134 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
135 : _Base_type(__p
), _M_del(__d
, __a
) { }
138 _M_dispose() // nothrow
139 { _M_del
._M_del(_Base_type::_M_ptr
); }
142 _M_destroy() // nothrow
144 _My_alloc_type
__a(_M_del
);
145 this->~_Sp_counted_deleter();
146 __a
.deallocate(this, 1);
150 _M_get_deleter(const std::type_info
& __ti
)
151 { return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0; }
154 _My_Deleter _M_del
; // copy constructor must not throw
157 // helpers for make_shared / allocate_shared
159 template<typename _Tp
>
160 struct _Sp_destroy_inplace
162 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
165 struct _Sp_make_shared_tag
{ };
167 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
168 class _Sp_counted_ptr_inplace
169 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
171 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
175 _Sp_counted_ptr_inplace(_Alloc __a
)
176 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
179 void* __p
= &_M_storage
;
180 ::new (__p
) _Tp(); // might throw
181 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
184 template<typename
... _Args
>
185 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
186 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
189 void* __p
= &_M_storage
;
190 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
191 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
194 // override because the allocator needs to know the dynamic type
196 _M_destroy() // nothrow
198 typedef typename
_Alloc::template
199 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
200 _My_alloc_type
__a(_Base_type::_M_del
);
201 this->~_Sp_counted_ptr_inplace();
202 __a
.deallocate(this, 1);
205 // sneaky trick so __shared_ptr can get the managed pointer
207 _M_get_deleter(const std::type_info
& __ti
)
209 return __ti
== typeid(_Sp_make_shared_tag
)
210 ? static_cast<void*>(&_M_storage
)
211 : _Base_type::_M_get_deleter(__ti
);
215 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
219 template<_Lock_policy _Lp
= __default_lock_policy
>
222 template<_Lock_policy _Lp
= __default_lock_policy
>
227 : _M_pi(0) // nothrow
230 template<typename _Ptr
>
231 __shared_count(_Ptr __p
) : _M_pi(0)
235 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
240 __throw_exception_again
;
244 template<typename _Ptr
, typename _Deleter
>
245 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
247 // allocator's value_type doesn't matter, will rebind it anyway
248 typedef std::allocator
<int> _Alloc
;
249 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
250 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
254 _M_pi
= __a2
.allocate(1);
255 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
259 __d(__p
); // Call _Deleter on __p.
261 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
262 __throw_exception_again
;
266 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
267 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
269 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
270 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
274 _M_pi
= __a2
.allocate(1);
275 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
279 __d(__p
); // Call _Deleter on __p.
281 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
282 __throw_exception_again
;
286 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
287 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
290 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
291 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
295 _M_pi
= __a2
.allocate(1);
296 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
297 std::forward
<_Args
>(__args
)...);
302 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
303 __throw_exception_again
;
307 #if _GLIBCXX_DEPRECATED
308 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
309 template<typename _Tp
>
311 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
312 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
316 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
317 template<typename _Tp
, typename _Del
>
319 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
320 : _M_pi(_S_create_from_up(std::move(__r
)))
323 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
325 __shared_count(const __weak_count
<_Lp
>& __r
);
327 ~__shared_count() // nothrow
333 __shared_count(const __shared_count
& __r
)
334 : _M_pi(__r
._M_pi
) // nothrow
337 _M_pi
->_M_add_ref_copy();
341 operator=(const __shared_count
& __r
) // nothrow
343 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
347 __tmp
->_M_add_ref_copy();
356 _M_swap(__shared_count
& __r
) // nothrow
358 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
364 _M_get_use_count() const // nothrow
365 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
368 _M_unique() const // nothrow
369 { return this->_M_get_use_count() == 1; }
372 _M_get_deleter(const std::type_info
& __ti
) const
373 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
376 _M_less(const __shared_count
& __rhs
) const
377 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
380 _M_less(const __weak_count
<_Lp
>& __rhs
) const
381 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
383 // friend function injected into enclosing namespace and found by ADL
385 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
386 { return __a
._M_pi
== __b
._M_pi
; }
389 friend class __weak_count
<_Lp
>;
391 template<typename _Tp
, typename _Del
>
392 static _Sp_counted_base
<_Lp
>*
393 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
394 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
396 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
397 _Lp
>(__r
.get(), __r
.get_deleter());
400 template<typename _Tp
, typename _Del
>
401 static _Sp_counted_base
<_Lp
>*
402 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
403 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
405 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
406 typedef std::reference_wrapper
<_Del1
> _Del2
;
407 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
408 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
411 _Sp_counted_base
<_Lp
>* _M_pi
;
415 template<_Lock_policy _Lp
>
420 : _M_pi(0) // nothrow
423 __weak_count(const __shared_count
<_Lp
>& __r
)
424 : _M_pi(__r
._M_pi
) // nothrow
427 _M_pi
->_M_weak_add_ref();
430 __weak_count(const __weak_count
<_Lp
>& __r
)
431 : _M_pi(__r
._M_pi
) // nothrow
434 _M_pi
->_M_weak_add_ref();
437 ~__weak_count() // nothrow
440 _M_pi
->_M_weak_release();
444 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
446 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
448 __tmp
->_M_weak_add_ref();
450 _M_pi
->_M_weak_release();
456 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
458 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
460 __tmp
->_M_weak_add_ref();
462 _M_pi
->_M_weak_release();
468 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
470 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
476 _M_get_use_count() const // nothrow
477 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
480 _M_less(const __weak_count
& __rhs
) const
481 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
484 _M_less(const __shared_count
<_Lp
>& __rhs
) const
485 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
487 // friend function injected into enclosing namespace and found by ADL
489 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
490 { return __a
._M_pi
== __b
._M_pi
; }
493 friend class __shared_count
<_Lp
>;
495 _Sp_counted_base
<_Lp
>* _M_pi
;
498 // now that __weak_count is defined we can define this constructor:
499 template<_Lock_policy _Lp
>
501 __shared_count
<_Lp
>::
502 __shared_count(const __weak_count
<_Lp
>& __r
)
506 _M_pi
->_M_add_ref_lock();
508 __throw_bad_weak_ptr();
511 // Forward declarations.
512 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
515 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
518 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
519 class __enable_shared_from_this
;
521 template<typename _Tp
>
524 template<typename _Tp
>
527 template<typename _Tp
>
528 class enable_shared_from_this
;
530 // Support for enable_shared_from_this.
532 // Friend of __enable_shared_from_this.
533 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
535 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
536 const __enable_shared_from_this
<_Tp1
,
539 // Friend of enable_shared_from_this.
540 template<typename _Tp1
, typename _Tp2
>
542 __enable_shared_from_this_helper(const __shared_count
<>&,
543 const enable_shared_from_this
<_Tp1
>*,
546 template<_Lock_policy _Lp
>
548 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
552 template<typename _Tp
, _Lock_policy _Lp
>
556 typedef _Tp element_type
;
558 /** @brief Construct an empty %__shared_ptr.
559 * @post use_count()==0 && get()==0
562 : _M_ptr(0), _M_refcount() // never throws
565 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
566 * @param __p A pointer that is convertible to element_type*.
567 * @post use_count() == 1 && get() == __p
568 * @throw std::bad_alloc, in which case @c delete @a __p is called.
570 template<typename _Tp1
>
572 __shared_ptr(_Tp1
* __p
)
573 : _M_ptr(__p
), _M_refcount(__p
)
575 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
576 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
577 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
581 // Requirements: _Deleter's copy constructor and destructor must
584 // __shared_ptr will release __p by calling __d(__p)
586 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
587 * and the deleter @a __d.
588 * @param __p A pointer.
589 * @param __d A deleter.
590 * @post use_count() == 1 && get() == __p
591 * @throw std::bad_alloc, in which case @a __d(__p) is called.
593 template<typename _Tp1
, typename _Deleter
>
594 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
595 : _M_ptr(__p
), _M_refcount(__p
, __d
)
597 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
598 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
599 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
603 // Requirements: _Deleter's copy constructor and destructor must
604 // not throw _Alloc's copy constructor and destructor must not
607 // __shared_ptr will release __p by calling __d(__p)
609 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
610 * and the deleter @a __d.
611 * @param __p A pointer.
612 * @param __d A deleter.
613 * @param __a An allocator.
614 * @post use_count() == 1 && get() == __p
615 * @throw std::bad_alloc, in which case @a __d(__p) is called.
617 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
618 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
619 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
621 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
622 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
623 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
626 /** @brief Constructs a %__shared_ptr instance that stores @a __p
627 * and shares ownership with @a __r.
628 * @param __r A %__shared_ptr.
629 * @param __p A pointer that will remain valid while @a *__r is valid.
630 * @post get() == __p && use_count() == __r.use_count()
632 * This can be used to construct a @c shared_ptr to a sub-object
633 * of an object managed by an existing @c shared_ptr.
636 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
637 * shared_ptr<int> pi(pii, &pii->first);
638 * assert(pii.use_count() == 2);
641 template<typename _Tp1
>
642 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
643 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
646 // generated copy constructor, assignment, destructor are fine.
648 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
649 * otherwise construct a %__shared_ptr that shares ownership
651 * @param __r A %__shared_ptr.
652 * @post get() == __r.get() && use_count() == __r.use_count()
654 template<typename _Tp1
>
655 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
656 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
657 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
659 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
660 * @param __r A %__shared_ptr rvalue.
661 * @post *this contains the old value of @a __r, @a __r is empty.
663 __shared_ptr(__shared_ptr
&& __r
)
664 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
666 _M_refcount
._M_swap(__r
._M_refcount
);
670 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
671 * @param __r A %__shared_ptr rvalue.
672 * @post *this contains the old value of @a __r, @a __r is empty.
674 template<typename _Tp1
>
675 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
676 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
678 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
679 _M_refcount
._M_swap(__r
._M_refcount
);
683 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
684 * and stores a copy of the pointer stored in @a __r.
685 * @param __r A weak_ptr.
686 * @post use_count() == __r.use_count()
687 * @throw bad_weak_ptr when __r.expired(),
688 * in which case the constructor has no effect.
690 template<typename _Tp1
>
692 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
693 : _M_refcount(__r
._M_refcount
) // may throw
695 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
696 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
701 template<typename _Tp1
, typename _Del
>
703 __shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
706 * If an exception is thrown this constructor has no effect.
708 template<typename _Tp1
, typename _Del
>
710 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
711 : _M_ptr(__r
.get()), _M_refcount()
713 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
714 _Tp1
* __tmp
= __r
.get();
715 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
716 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
719 #if _GLIBCXX_DEPRECATED
721 * @post use_count() == 1 and __r.get() == 0
723 template<typename _Tp1
>
725 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
726 : _M_ptr(__r
.get()), _M_refcount()
728 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
729 // TODO requires _Tp1 is complete, delete __r.release() well-formed
730 _Tp1
* __tmp
= __r
.get();
731 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
732 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
736 template<typename _Tp1
>
738 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
741 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
745 #if _GLIBCXX_DEPRECATED
746 template<typename _Tp1
>
748 operator=(std::auto_ptr
<_Tp1
>&& __r
)
750 __shared_ptr(std::move(__r
)).swap(*this);
756 operator=(__shared_ptr
&& __r
)
758 __shared_ptr(std::move(__r
)).swap(*this);
764 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
766 __shared_ptr(std::move(__r
)).swap(*this);
770 template<typename _Tp1
, typename _Del
>
772 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
774 template<typename _Tp1
, typename _Del
>
776 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
778 __shared_ptr(std::move(__r
)).swap(*this);
783 reset() // never throws
784 { __shared_ptr().swap(*this); }
786 template<typename _Tp1
>
788 reset(_Tp1
* __p
) // _Tp1 must be complete.
790 // Catch self-reset errors.
791 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
792 __shared_ptr(__p
).swap(*this);
795 template<typename _Tp1
, typename _Deleter
>
797 reset(_Tp1
* __p
, _Deleter __d
)
798 { __shared_ptr(__p
, __d
).swap(*this); }
800 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
802 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
803 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
805 // Allow class instantiation when _Tp is [cv-qual] void.
806 typename
std::add_lvalue_reference
<_Tp
>::type
807 operator*() const // never throws
809 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
814 operator->() const // never throws
816 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
821 get() const // never throws
824 // Implicit conversion to "bool"
826 typedef _Tp
* __shared_ptr::*__unspecified_bool_type
;
829 operator __unspecified_bool_type() const // never throws
830 { return _M_ptr
== 0 ? 0 : &__shared_ptr::_M_ptr
; }
833 unique() const // never throws
834 { return _M_refcount
._M_unique(); }
837 use_count() const // never throws
838 { return _M_refcount
._M_get_use_count(); }
841 swap(__shared_ptr
<_Tp
, _Lp
>&& __other
) // never throws
843 std::swap(_M_ptr
, __other
._M_ptr
);
844 _M_refcount
._M_swap(__other
._M_refcount
);
847 template<typename _Tp1
>
849 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
850 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
852 template<typename _Tp1
>
854 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
855 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
858 // This constructor is non-standard, it is used by allocate_shared.
859 template<typename _Alloc
, typename
... _Args
>
860 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
861 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
862 std::forward
<_Args
>(__args
)...)
864 // _M_ptr needs to point to the newly constructed object.
865 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
866 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
867 _M_ptr
= static_cast<_Tp
*>(__p
);
868 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
871 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
873 friend __shared_ptr
<_Tp1
, _Lp1
>
874 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
878 _M_get_deleter(const std::type_info
& __ti
) const
879 { return _M_refcount
._M_get_deleter(__ti
); }
881 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
882 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
884 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
885 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
887 _Tp
* _M_ptr
; // Contained pointer.
888 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
891 // 20.8.13.2.7 shared_ptr comparisons
892 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
894 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
895 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
896 { return __a
.get() == __b
.get(); }
898 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
900 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
901 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
902 { return __a
.get() != __b
.get(); }
904 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
906 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
907 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
908 { return __a
.get() < __b
.get(); }
910 template<typename _Sp
>
911 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
914 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
916 return std::less
<typename
_Sp::element_type
*>()(__lhs
.get(),
921 template<typename _Tp
, _Lock_policy _Lp
>
922 struct less
<__shared_ptr
<_Tp
, _Lp
>>
923 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
926 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
927 template<typename _Tp
, _Lock_policy _Lp
>
929 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
930 const __shared_ptr
<_Tp
, _Lp
>& __b
)
931 { return __a
.get() > __b
.get(); }
933 template<typename _Tp
, _Lock_policy _Lp
>
935 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
936 const __shared_ptr
<_Tp
, _Lp
>& __b
)
937 { return __a
.get() >= __b
.get(); }
939 template<typename _Tp
, _Lock_policy _Lp
>
941 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
942 const __shared_ptr
<_Tp
, _Lp
>& __b
)
943 { return __a
.get() <= __b
.get(); }
945 // 2.2.3.8 shared_ptr specialized algorithms.
946 template<typename _Tp
, _Lock_policy _Lp
>
948 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
951 template<typename _Tp
, _Lock_policy _Lp
>
953 swap(__shared_ptr
<_Tp
, _Lp
>&& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
956 template<typename _Tp
, _Lock_policy _Lp
>
958 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>&& __b
)
961 // 2.2.3.9 shared_ptr casts
962 /** @warning The seemingly equivalent
963 * <code>shared_ptr<_Tp, _Lp>(static_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 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
970 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
972 /** @warning The seemingly equivalent
973 * <code>shared_ptr<_Tp, _Lp>(const_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 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
980 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
982 /** @warning The seemingly equivalent
983 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
984 * will eventually result in undefined behaviour,
985 * attempting to delete the same object twice.
987 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
988 inline __shared_ptr
<_Tp
, _Lp
>
989 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
991 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
992 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
993 return __shared_ptr
<_Tp
, _Lp
>();
996 // 2.2.3.7 shared_ptr I/O
997 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
998 std::basic_ostream
<_Ch
, _Tr
>&
999 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
1000 const __shared_ptr
<_Tp
, _Lp
>& __p
)
1006 // 2.2.3.10 shared_ptr get_deleter (experimental)
1007 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
1009 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
1010 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
1013 template<typename _Tp
, _Lock_policy _Lp
>
1017 typedef _Tp element_type
;
1020 : _M_ptr(0), _M_refcount() // never throws
1023 // Generated copy constructor, assignment, destructor are fine.
1025 // The "obvious" converting constructor implementation:
1027 // template<typename _Tp1>
1028 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1029 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1032 // has a serious problem.
1034 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1035 // conversion may require access to *__r._M_ptr (virtual inheritance).
1037 // It is not possible to avoid spurious access violations since
1038 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1039 template<typename _Tp1
>
1040 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
1041 : _M_refcount(__r
._M_refcount
) // never throws
1043 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
1044 _M_ptr
= __r
.lock().get();
1047 template<typename _Tp1
>
1048 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1049 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1050 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
1052 template<typename _Tp1
>
1054 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1056 _M_ptr
= __r
.lock().get();
1057 _M_refcount
= __r
._M_refcount
;
1061 template<typename _Tp1
>
1063 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1065 _M_ptr
= __r
._M_ptr
;
1066 _M_refcount
= __r
._M_refcount
;
1070 __shared_ptr
<_Tp
, _Lp
>
1071 lock() const // never throws
1074 // Optimization: avoid throw overhead.
1076 return __shared_ptr
<element_type
, _Lp
>();
1080 return __shared_ptr
<element_type
, _Lp
>(*this);
1082 __catch(const bad_weak_ptr
&)
1084 // Q: How can we get here?
1085 // A: Another thread may have invalidated r after the
1086 // use_count test above.
1087 return __shared_ptr
<element_type
, _Lp
>();
1091 // Optimization: avoid try/catch overhead when single threaded.
1092 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1093 : __shared_ptr
<element_type
, _Lp
>(*this);
1099 use_count() const // never throws
1100 { return _M_refcount
._M_get_use_count(); }
1103 expired() const // never throws
1104 { return _M_refcount
._M_get_use_count() == 0; }
1106 template<typename _Tp1
>
1108 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1109 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1111 template<typename _Tp1
>
1113 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1114 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1117 reset() // never throws
1118 { __weak_ptr().swap(*this); }
1121 swap(__weak_ptr
& __s
) // never throws
1123 std::swap(_M_ptr
, __s
._M_ptr
);
1124 _M_refcount
._M_swap(__s
._M_refcount
);
1128 template<typename _Tp1
>
1129 bool operator<(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1130 template<typename _Tp1
>
1131 bool operator<=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1132 template<typename _Tp1
>
1133 bool operator>(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1134 template<typename _Tp1
>
1135 bool operator>=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1138 // Used by __enable_shared_from_this.
1140 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1143 _M_refcount
= __refcount
;
1146 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1147 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1148 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1149 friend class enable_shared_from_this
<_Tp
>;
1151 _Tp
* _M_ptr
; // Contained pointer.
1152 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1155 // 20.8.13.3.7 weak_ptr specialized algorithms.
1156 template<typename _Tp
, _Lock_policy _Lp
>
1158 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1162 template<typename _Tp
> struct owner_less
;
1164 template<typename _Tp
, typename _Tp1
>
1165 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1168 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1169 { return __lhs
.owner_before(__rhs
); }
1171 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1172 { return __lhs
.owner_before(__rhs
); }
1174 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1175 { return __lhs
.owner_before(__rhs
); }
1178 template<typename _Tp
, _Lock_policy _Lp
>
1179 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1180 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1183 template<typename _Tp
, _Lock_policy _Lp
>
1184 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1185 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1190 template<typename _Tp
, _Lock_policy _Lp
>
1191 class __enable_shared_from_this
1194 __enable_shared_from_this() { }
1196 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1198 __enable_shared_from_this
&
1199 operator=(const __enable_shared_from_this
&)
1202 ~__enable_shared_from_this() { }
1205 __shared_ptr
<_Tp
, _Lp
>
1207 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1209 __shared_ptr
<const _Tp
, _Lp
>
1210 shared_from_this() const
1211 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1214 template<typename _Tp1
>
1216 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1217 { _M_weak_this
._M_assign(__p
, __n
); }
1219 template<typename _Tp1
>
1221 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1222 const __enable_shared_from_this
* __pe
,
1226 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1229 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1233 * @brief A smart pointer with reference-counted copy semantics.
1235 * The object pointed to is deleted when the last shared_ptr pointing to
1236 * it is destroyed or reset.
1238 template<typename _Tp
>
1240 : public __shared_ptr
<_Tp
>
1244 : __shared_ptr
<_Tp
>() { }
1246 template<typename _Tp1
>
1248 shared_ptr(_Tp1
* __p
)
1249 : __shared_ptr
<_Tp
>(__p
) { }
1251 template<typename _Tp1
, typename _Deleter
>
1252 shared_ptr(_Tp1
* __p
, _Deleter __d
)
1253 : __shared_ptr
<_Tp
>(__p
, __d
) { }
1255 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
1256 shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
1257 : __shared_ptr
<_Tp
>(__p
, __d
, __a
) { }
1259 // Aliasing constructor
1260 template<typename _Tp1
>
1261 shared_ptr(const shared_ptr
<_Tp1
>& __r
, _Tp
* __p
)
1262 : __shared_ptr
<_Tp
>(__r
, __p
) { }
1264 template<typename _Tp1
>
1265 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
1266 : __shared_ptr
<_Tp
>(__r
) { }
1268 shared_ptr(shared_ptr
&& __r
)
1269 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1271 template<typename _Tp1
>
1272 shared_ptr(shared_ptr
<_Tp1
>&& __r
)
1273 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1275 template<typename _Tp1
>
1277 shared_ptr(const weak_ptr
<_Tp1
>& __r
)
1278 : __shared_ptr
<_Tp
>(__r
) { }
1280 #if _GLIBCXX_DEPRECATED
1281 template<typename _Tp1
>
1283 shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
1284 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1287 template<typename _Tp1
, typename _Del
>
1289 shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
1291 template<typename _Tp1
, typename _Del
>
1293 shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1294 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1296 template<typename _Tp1
>
1298 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1300 this->__shared_ptr
<_Tp
>::operator=(__r
);
1304 #if _GLIBCXX_DEPRECATED
1305 template<typename _Tp1
>
1307 operator=(std::auto_ptr
<_Tp1
>&& __r
)
1309 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1315 operator=(shared_ptr
&& __r
)
1317 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1321 template<class _Tp1
>
1323 operator=(shared_ptr
<_Tp1
>&& __r
)
1325 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1329 template<typename _Tp1
, typename _Del
>
1331 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
1333 template<typename _Tp1
, typename _Del
>
1335 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1337 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1342 // This constructor is non-standard, it is used by allocate_shared.
1343 template<typename _Alloc
, typename
... _Args
>
1344 shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
1345 : __shared_ptr
<_Tp
>(__tag
, __a
, std::forward
<_Args
>(__args
)...)
1348 template<typename _Tp1
, typename _Alloc
, typename
... _Args
>
1349 friend shared_ptr
<_Tp1
>
1350 allocate_shared(_Alloc __a
, _Args
&&... __args
);
1353 // 20.8.13.2.7 shared_ptr comparisons
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 _Tp1
, typename _Tp2
>
1361 operator!=(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1362 { return __a
.get() != __b
.get(); }
1364 template<typename _Tp1
, typename _Tp2
>
1366 operator<(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
1367 { return __a
.get() < __b
.get(); }
1369 template<typename _Tp
>
1370 struct less
<shared_ptr
<_Tp
>>
1371 : public _Sp_less
<shared_ptr
<_Tp
>>
1374 // 20.8.13.2.9 shared_ptr specialized algorithms.
1375 template<typename _Tp
>
1377 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>& __b
)
1380 template<typename _Tp
>
1382 swap(shared_ptr
<_Tp
>&& __a
, shared_ptr
<_Tp
>& __b
)
1385 template<typename _Tp
>
1387 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>&& __b
)
1390 // 20.8.13.2.10 shared_ptr casts.
1391 template<typename _Tp
, typename _Tp1
>
1392 inline shared_ptr
<_Tp
>
1393 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1394 { return shared_ptr
<_Tp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1396 template<typename _Tp
, typename _Tp1
>
1397 inline shared_ptr
<_Tp
>
1398 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1399 { return shared_ptr
<_Tp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1401 template<typename _Tp
, typename _Tp1
>
1402 inline shared_ptr
<_Tp
>
1403 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1405 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1406 return shared_ptr
<_Tp
>(__r
, __p
);
1407 return shared_ptr
<_Tp
>();
1412 * @brief A smart pointer with weak semantics.
1414 * With forwarding constructors and assignment operators.
1416 template<typename _Tp
>
1418 : public __weak_ptr
<_Tp
>
1422 : __weak_ptr
<_Tp
>() { }
1424 template<typename _Tp1
>
1425 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1426 : __weak_ptr
<_Tp
>(__r
) { }
1428 template<typename _Tp1
>
1429 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1430 : __weak_ptr
<_Tp
>(__r
) { }
1432 template<typename _Tp1
>
1434 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1436 this->__weak_ptr
<_Tp
>::operator=(__r
);
1440 template<typename _Tp1
>
1442 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1444 this->__weak_ptr
<_Tp
>::operator=(__r
);
1449 lock() const // never throws
1452 if (this->expired())
1453 return shared_ptr
<_Tp
>();
1457 return shared_ptr
<_Tp
>(*this);
1459 __catch(const bad_weak_ptr
&)
1461 return shared_ptr
<_Tp
>();
1464 return this->expired() ? shared_ptr
<_Tp
>()
1465 : shared_ptr
<_Tp
>(*this);
1470 template<typename _Tp1
>
1471 bool operator<(const weak_ptr
<_Tp1
>&) const = delete;
1472 template<typename _Tp1
>
1473 bool operator<=(const weak_ptr
<_Tp1
>&) const = delete;
1474 template<typename _Tp1
>
1475 bool operator>(const weak_ptr
<_Tp1
>&) const = delete;
1476 template<typename _Tp1
>
1477 bool operator>=(const weak_ptr
<_Tp1
>&) const = delete;
1480 // 20.8.13.3.7 weak_ptr specialized algorithms.
1481 template<typename _Tp
>
1483 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
)
1487 template<typename _Tp
>
1488 struct owner_less
<shared_ptr
<_Tp
>>
1489 : public _Sp_owner_less
<shared_ptr
<_Tp
>, weak_ptr
<_Tp
>>
1492 template<typename _Tp
>
1493 struct owner_less
<weak_ptr
<_Tp
>>
1494 : public _Sp_owner_less
<weak_ptr
<_Tp
>, shared_ptr
<_Tp
>>
1498 * @brief Base class allowing use of member function shared_from_this.
1500 template<typename _Tp
>
1501 class enable_shared_from_this
1504 enable_shared_from_this() { }
1506 enable_shared_from_this(const enable_shared_from_this
&) { }
1508 enable_shared_from_this
&
1509 operator=(const enable_shared_from_this
&)
1512 ~enable_shared_from_this() { }
1517 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1519 shared_ptr
<const _Tp
>
1520 shared_from_this() const
1521 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1524 template<typename _Tp1
>
1526 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1527 { _M_weak_this
._M_assign(__p
, __n
); }
1529 template<typename _Tp1
>
1531 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1532 const enable_shared_from_this
* __pe
,
1536 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1539 mutable weak_ptr
<_Tp
> _M_weak_this
;
1542 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1543 inline __shared_ptr
<_Tp
, _Lp
>
1544 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1546 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1547 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1550 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1551 inline __shared_ptr
<_Tp
, _Lp
>
1552 __make_shared(_Args
&&... __args
)
1554 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1555 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1556 std::forward
<_Args
>(__args
)...);
1559 /** @brief Create an object that is owned by a shared_ptr.
1560 * @param __a An allocator.
1561 * @param __args Arguments for the @a _Tp object's constructor.
1562 * @return A shared_ptr that owns the newly created object.
1563 * @throw An exception thrown from @a _Alloc::allocate or from the
1564 * constructor of @a _Tp.
1566 * A copy of @a __a will be used to allocate memory for the shared_ptr
1567 * and the new object.
1569 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
1570 inline shared_ptr
<_Tp
>
1571 allocate_shared(_Alloc __a
, _Args
&&... __args
)
1573 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), std::forward
<_Alloc
>(__a
),
1574 std::forward
<_Args
>(__args
)...);
1577 /** @brief Create an object that is owned by a shared_ptr.
1578 * @param __args Arguments for the @a _Tp object's constructor.
1579 * @return A shared_ptr that owns the newly created object.
1580 * @throw std::bad_alloc, or an exception thrown from the
1581 * constructor of @a _Tp.
1583 template<typename _Tp
, typename
... _Args
>
1584 inline shared_ptr
<_Tp
>
1585 make_shared(_Args
&&... __args
)
1587 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1588 return allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
1589 std::forward
<_Args
>(__args
)...);
1592 // @} group pointer_abstractions
1594 _GLIBCXX_END_NAMESPACE
1596 #endif // _SHARED_PTR_H