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.
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
62 _GLIBCXX_BEGIN_NAMESPACE(std
)
65 * @addtogroup pointer_abstractions
69 // counted ptr with no deleter or allocator support
70 template<typename _Ptr
, _Lock_policy _Lp
>
72 : public _Sp_counted_base
<_Lp
>
75 _Sp_counted_ptr(_Ptr __p
)
79 _M_dispose() // nothrow
83 _M_destroy() // nothrow
87 _M_get_deleter(const std::type_info
& __ti
)
90 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
91 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
94 _Ptr _M_ptr
; // copy constructor must not throw
97 // support for custom deleter and/or allocator
98 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
99 class _Sp_counted_deleter
100 : public _Sp_counted_ptr
<_Ptr
, _Lp
>
102 typedef typename
_Alloc::template
103 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
105 // Helper class that stores the Deleter and also acts as an allocator.
106 // Used to dispose of the owned pointer and the internal refcount
107 // Requires that copies of _Alloc can free each other's memory.
109 : public _My_alloc_type
// copy constructor must not throw
111 _Deleter _M_del
; // copy constructor must not throw
112 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
113 : _My_alloc_type(__a
), _M_del(__d
) { }
117 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
122 * @pre __d(__p) must not throw.
124 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
125 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
129 * @pre __d(__p) must not throw.
131 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
132 : _Base_type(__p
), _M_del(__d
, __a
) { }
135 _M_dispose() // nothrow
136 { _M_del
._M_del(_Base_type::_M_ptr
); }
139 _M_destroy() // nothrow
141 _My_alloc_type
__a(_M_del
);
142 this->~_Sp_counted_deleter();
143 __a
.deallocate(this, 1);
147 _M_get_deleter(const std::type_info
& __ti
)
148 { return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0; }
151 _My_Deleter _M_del
; // copy constructor must not throw
154 // helpers for make_shared / allocate_shared
156 template<typename _Tp
>
157 struct _Sp_destroy_inplace
159 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
162 struct _Sp_make_shared_tag
{ };
164 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
165 class _Sp_counted_ptr_inplace
166 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
168 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
172 _Sp_counted_ptr_inplace(_Alloc __a
)
173 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
176 void* __p
= &_M_storage
;
177 ::new (__p
) _Tp(); // might throw
178 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
181 template<typename
... _Args
>
182 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
183 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
186 void* __p
= &_M_storage
;
187 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
188 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
191 // override because the allocator needs to know the dynamic type
193 _M_destroy() // nothrow
195 typedef typename
_Alloc::template
196 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
197 _My_alloc_type
__a(_Base_type::_M_del
);
198 this->~_Sp_counted_ptr_inplace();
199 __a
.deallocate(this, 1);
202 // sneaky trick so __shared_ptr can get the managed pointer
204 _M_get_deleter(const std::type_info
& __ti
)
206 return __ti
== typeid(_Sp_make_shared_tag
)
207 ? static_cast<void*>(&_M_storage
)
208 : _Base_type::_M_get_deleter(__ti
);
212 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
216 template<_Lock_policy _Lp
= __default_lock_policy
>
219 template<_Lock_policy _Lp
= __default_lock_policy
>
224 : _M_pi(0) // nothrow
227 template<typename _Ptr
>
228 __shared_count(_Ptr __p
) : _M_pi(0)
232 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
237 __throw_exception_again
;
241 template<typename _Ptr
, typename _Deleter
>
242 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
244 // allocator's value_type doesn't matter, will rebind it anyway
245 typedef std::allocator
<int> _Alloc
;
246 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
247 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
251 _M_pi
= __a2
.allocate(1);
252 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
256 __d(__p
); // Call _Deleter on __p.
258 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
259 __throw_exception_again
;
263 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
264 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
266 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
267 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
271 _M_pi
= __a2
.allocate(1);
272 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
276 __d(__p
); // Call _Deleter on __p.
278 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
279 __throw_exception_again
;
283 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
284 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
287 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
288 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
292 _M_pi
= __a2
.allocate(1);
293 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
294 std::forward
<_Args
>(__args
)...);
299 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
300 __throw_exception_again
;
304 #if _GLIBCXX_DEPRECATED
305 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
306 template<typename _Tp
>
308 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
309 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
313 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
314 template<typename _Tp
, typename _Del
>
316 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
317 : _M_pi(_S_create_from_up(std::move(__r
)))
320 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
322 __shared_count(const __weak_count
<_Lp
>& __r
);
324 ~__shared_count() // nothrow
330 __shared_count(const __shared_count
& __r
)
331 : _M_pi(__r
._M_pi
) // nothrow
334 _M_pi
->_M_add_ref_copy();
338 operator=(const __shared_count
& __r
) // nothrow
340 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
344 __tmp
->_M_add_ref_copy();
353 _M_swap(__shared_count
& __r
) // nothrow
355 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
361 _M_get_use_count() const // nothrow
362 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
365 _M_unique() const // nothrow
366 { return this->_M_get_use_count() == 1; }
369 _M_get_deleter(const std::type_info
& __ti
) const
370 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
373 _M_less(const __shared_count
& __rhs
) const
374 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
377 _M_less(const __weak_count
<_Lp
>& __rhs
) const
378 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
380 // friend function injected into enclosing namespace and found by ADL
382 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
383 { return __a
._M_pi
== __b
._M_pi
; }
386 friend class __weak_count
<_Lp
>;
388 template<typename _Tp
, typename _Del
>
389 static _Sp_counted_base
<_Lp
>*
390 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
391 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
393 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
394 _Lp
>(__r
.get(), __r
.get_deleter());
397 template<typename _Tp
, typename _Del
>
398 static _Sp_counted_base
<_Lp
>*
399 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
400 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
402 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
403 typedef std::reference_wrapper
<_Del1
> _Del2
;
404 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
405 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
408 _Sp_counted_base
<_Lp
>* _M_pi
;
412 template<_Lock_policy _Lp
>
417 : _M_pi(0) // nothrow
420 __weak_count(const __shared_count
<_Lp
>& __r
)
421 : _M_pi(__r
._M_pi
) // nothrow
424 _M_pi
->_M_weak_add_ref();
427 __weak_count(const __weak_count
<_Lp
>& __r
)
428 : _M_pi(__r
._M_pi
) // nothrow
431 _M_pi
->_M_weak_add_ref();
434 ~__weak_count() // nothrow
437 _M_pi
->_M_weak_release();
441 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
443 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
445 __tmp
->_M_weak_add_ref();
447 _M_pi
->_M_weak_release();
453 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
455 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
457 __tmp
->_M_weak_add_ref();
459 _M_pi
->_M_weak_release();
465 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
467 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
473 _M_get_use_count() const // nothrow
474 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
477 _M_less(const __weak_count
& __rhs
) const
478 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
481 _M_less(const __shared_count
<_Lp
>& __rhs
) const
482 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
484 // friend function injected into enclosing namespace and found by ADL
486 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
487 { return __a
._M_pi
== __b
._M_pi
; }
490 friend class __shared_count
<_Lp
>;
492 _Sp_counted_base
<_Lp
>* _M_pi
;
495 // now that __weak_count is defined we can define this constructor:
496 template<_Lock_policy _Lp
>
498 __shared_count
<_Lp
>::
499 __shared_count(const __weak_count
<_Lp
>& __r
)
503 _M_pi
->_M_add_ref_lock();
505 __throw_bad_weak_ptr();
508 // Forward declarations.
509 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
512 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
515 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
516 class __enable_shared_from_this
;
518 template<typename _Tp
>
521 template<typename _Tp
>
524 template<typename _Tp
>
525 class enable_shared_from_this
;
527 // Support for enable_shared_from_this.
529 // Friend of __enable_shared_from_this.
530 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
532 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
533 const __enable_shared_from_this
<_Tp1
,
536 // Friend of enable_shared_from_this.
537 template<typename _Tp1
, typename _Tp2
>
539 __enable_shared_from_this_helper(const __shared_count
<>&,
540 const enable_shared_from_this
<_Tp1
>*,
543 template<_Lock_policy _Lp
>
545 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
549 template<typename _Tp
, _Lock_policy _Lp
>
553 typedef _Tp element_type
;
555 /** @brief Construct an empty %__shared_ptr.
556 * @post use_count()==0 && get()==0
559 : _M_ptr(0), _M_refcount() // never throws
562 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
563 * @param __p A pointer that is convertible to element_type*.
564 * @post use_count() == 1 && get() == __p
565 * @throw std::bad_alloc, in which case @c delete @a __p is called.
567 template<typename _Tp1
>
569 __shared_ptr(_Tp1
* __p
)
570 : _M_ptr(__p
), _M_refcount(__p
)
572 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
573 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
574 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
578 // Requirements: _Deleter's copy constructor and destructor must
581 // __shared_ptr will release __p by calling __d(__p)
583 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
584 * and the deleter @a __d.
585 * @param __p A pointer.
586 * @param __d A deleter.
587 * @post use_count() == 1 && get() == __p
588 * @throw std::bad_alloc, in which case @a __d(__p) is called.
590 template<typename _Tp1
, typename _Deleter
>
591 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
592 : _M_ptr(__p
), _M_refcount(__p
, __d
)
594 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
595 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
596 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
600 // Requirements: _Deleter's copy constructor and destructor must
601 // not throw _Alloc's copy constructor and destructor must not
604 // __shared_ptr will release __p by calling __d(__p)
606 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
607 * and the deleter @a __d.
608 * @param __p A pointer.
609 * @param __d A deleter.
610 * @param __a An allocator.
611 * @post use_count() == 1 && get() == __p
612 * @throw std::bad_alloc, in which case @a __d(__p) is called.
614 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
615 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
616 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
618 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
619 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
620 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
623 /** @brief Constructs a %__shared_ptr instance that stores @a __p
624 * and shares ownership with @a __r.
625 * @param __r A %__shared_ptr.
626 * @param __p A pointer that will remain valid while @a *__r is valid.
627 * @post get() == __p && use_count() == __r.use_count()
629 * This can be used to construct a @c shared_ptr to a sub-object
630 * of an object managed by an existing @c shared_ptr.
633 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
634 * shared_ptr<int> pi(pii, &pii->first);
635 * assert(pii.use_count() == 2);
638 template<typename _Tp1
>
639 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
640 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
643 // generated copy constructor, assignment, destructor are fine.
645 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
646 * otherwise construct a %__shared_ptr that shares ownership
648 * @param __r A %__shared_ptr.
649 * @post get() == __r.get() && use_count() == __r.use_count()
651 template<typename _Tp1
>
652 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
653 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
654 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
656 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
657 * @param __r A %__shared_ptr rvalue.
658 * @post *this contains the old value of @a __r, @a __r is empty.
660 __shared_ptr(__shared_ptr
&& __r
)
661 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
663 _M_refcount
._M_swap(__r
._M_refcount
);
667 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
668 * @param __r A %__shared_ptr rvalue.
669 * @post *this contains the old value of @a __r, @a __r is empty.
671 template<typename _Tp1
>
672 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
673 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
675 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
676 _M_refcount
._M_swap(__r
._M_refcount
);
680 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
681 * and stores a copy of the pointer stored in @a __r.
682 * @param __r A weak_ptr.
683 * @post use_count() == __r.use_count()
684 * @throw bad_weak_ptr when __r.expired(),
685 * in which case the constructor has no effect.
687 template<typename _Tp1
>
689 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
690 : _M_refcount(__r
._M_refcount
) // may throw
692 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
693 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
698 template<typename _Tp1
, typename _Del
>
700 __shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
703 * If an exception is thrown this constructor has no effect.
705 template<typename _Tp1
, typename _Del
>
707 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
708 : _M_ptr(__r
.get()), _M_refcount()
710 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
711 _Tp1
* __tmp
= __r
.get();
712 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
713 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
716 #if _GLIBCXX_DEPRECATED
718 * @post use_count() == 1 and __r.get() == 0
720 template<typename _Tp1
>
722 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
723 : _M_ptr(__r
.get()), _M_refcount()
725 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
726 // TODO requires _Tp1 is complete, delete __r.release() well-formed
727 _Tp1
* __tmp
= __r
.get();
728 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
729 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
733 template<typename _Tp1
>
735 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
738 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
742 #if _GLIBCXX_DEPRECATED
743 template<typename _Tp1
>
745 operator=(std::auto_ptr
<_Tp1
>&& __r
)
747 __shared_ptr(std::move(__r
)).swap(*this);
753 operator=(__shared_ptr
&& __r
)
755 __shared_ptr(std::move(__r
)).swap(*this);
761 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
763 __shared_ptr(std::move(__r
)).swap(*this);
767 template<typename _Tp1
, typename _Del
>
769 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
771 template<typename _Tp1
, typename _Del
>
773 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
775 __shared_ptr(std::move(__r
)).swap(*this);
780 reset() // never throws
781 { __shared_ptr().swap(*this); }
783 template<typename _Tp1
>
785 reset(_Tp1
* __p
) // _Tp1 must be complete.
787 // Catch self-reset errors.
788 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
789 __shared_ptr(__p
).swap(*this);
792 template<typename _Tp1
, typename _Deleter
>
794 reset(_Tp1
* __p
, _Deleter __d
)
795 { __shared_ptr(__p
, __d
).swap(*this); }
797 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
799 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
800 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
802 // Allow class instantiation when _Tp is [cv-qual] void.
803 typename
std::add_lvalue_reference
<_Tp
>::type
804 operator*() const // never throws
806 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
811 operator->() const // never throws
813 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
818 get() const // never throws
821 // Implicit conversion to "bool"
823 typedef _Tp
* __shared_ptr::*__unspecified_bool_type
;
826 operator __unspecified_bool_type() const // never throws
827 { return _M_ptr
== 0 ? 0 : &__shared_ptr::_M_ptr
; }
830 unique() const // never throws
831 { return _M_refcount
._M_unique(); }
834 use_count() const // never throws
835 { return _M_refcount
._M_get_use_count(); }
838 swap(__shared_ptr
<_Tp
, _Lp
>&& __other
) // never throws
840 std::swap(_M_ptr
, __other
._M_ptr
);
841 _M_refcount
._M_swap(__other
._M_refcount
);
844 template<typename _Tp1
>
846 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
847 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
849 template<typename _Tp1
>
851 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
852 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
855 // This constructor is non-standard, it is used by allocate_shared.
856 template<typename _Alloc
, typename
... _Args
>
857 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
858 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
859 std::forward
<_Args
>(__args
)...)
861 // _M_ptr needs to point to the newly constructed object.
862 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
863 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
864 _M_ptr
= static_cast<_Tp
*>(__p
);
865 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
868 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
870 friend __shared_ptr
<_Tp1
, _Lp1
>
871 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
875 _M_get_deleter(const std::type_info
& __ti
) const
876 { return _M_refcount
._M_get_deleter(__ti
); }
878 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
879 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
881 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
882 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
884 _Tp
* _M_ptr
; // Contained pointer.
885 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
888 // 20.8.13.2.7 shared_ptr comparisons
889 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
891 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
892 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
893 { return __a
.get() == __b
.get(); }
895 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
897 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
898 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
899 { return __a
.get() != __b
.get(); }
901 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
903 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
904 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
905 { return __a
.get() < __b
.get(); }
907 template<typename _Sp
>
908 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
911 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
913 return std::less
<typename
_Sp::element_type
*>()(__lhs
.get(),
918 template<typename _Tp
, _Lock_policy _Lp
>
919 struct less
<__shared_ptr
<_Tp
, _Lp
>>
920 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
923 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
924 template<typename _Tp
, _Lock_policy _Lp
>
926 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
927 const __shared_ptr
<_Tp
, _Lp
>& __b
)
928 { return __a
.get() > __b
.get(); }
930 template<typename _Tp
, _Lock_policy _Lp
>
932 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
933 const __shared_ptr
<_Tp
, _Lp
>& __b
)
934 { return __a
.get() >= __b
.get(); }
936 template<typename _Tp
, _Lock_policy _Lp
>
938 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
939 const __shared_ptr
<_Tp
, _Lp
>& __b
)
940 { return __a
.get() <= __b
.get(); }
942 // 2.2.3.8 shared_ptr specialized algorithms.
943 template<typename _Tp
, _Lock_policy _Lp
>
945 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
948 template<typename _Tp
, _Lock_policy _Lp
>
950 swap(__shared_ptr
<_Tp
, _Lp
>&& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
953 template<typename _Tp
, _Lock_policy _Lp
>
955 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>&& __b
)
958 // 2.2.3.9 shared_ptr casts
959 /** @warning The seemingly equivalent
960 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
961 * will eventually result in undefined behaviour,
962 * attempting to delete the same object twice.
964 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
965 inline __shared_ptr
<_Tp
, _Lp
>
966 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
967 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
969 /** @warning The seemingly equivalent
970 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
971 * will eventually result in undefined behaviour,
972 * attempting to delete the same object twice.
974 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
975 inline __shared_ptr
<_Tp
, _Lp
>
976 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
977 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
979 /** @warning The seemingly equivalent
980 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
981 * will eventually result in undefined behaviour,
982 * attempting to delete the same object twice.
984 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
985 inline __shared_ptr
<_Tp
, _Lp
>
986 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
988 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
989 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
990 return __shared_ptr
<_Tp
, _Lp
>();
993 // 2.2.3.7 shared_ptr I/O
994 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
995 std::basic_ostream
<_Ch
, _Tr
>&
996 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
997 const __shared_ptr
<_Tp
, _Lp
>& __p
)
1003 // 2.2.3.10 shared_ptr get_deleter (experimental)
1004 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
1006 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
1007 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
1010 template<typename _Tp
, _Lock_policy _Lp
>
1014 typedef _Tp element_type
;
1017 : _M_ptr(0), _M_refcount() // never throws
1020 // Generated copy constructor, assignment, destructor are fine.
1022 // The "obvious" converting constructor implementation:
1024 // template<typename _Tp1>
1025 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1026 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1029 // has a serious problem.
1031 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1032 // conversion may require access to *__r._M_ptr (virtual inheritance).
1034 // It is not possible to avoid spurious access violations since
1035 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1036 template<typename _Tp1
>
1037 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
1038 : _M_refcount(__r
._M_refcount
) // never throws
1040 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
1041 _M_ptr
= __r
.lock().get();
1044 template<typename _Tp1
>
1045 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1046 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1047 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
1049 template<typename _Tp1
>
1051 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1053 _M_ptr
= __r
.lock().get();
1054 _M_refcount
= __r
._M_refcount
;
1058 template<typename _Tp1
>
1060 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1062 _M_ptr
= __r
._M_ptr
;
1063 _M_refcount
= __r
._M_refcount
;
1067 __shared_ptr
<_Tp
, _Lp
>
1068 lock() const // never throws
1071 // Optimization: avoid throw overhead.
1073 return __shared_ptr
<element_type
, _Lp
>();
1077 return __shared_ptr
<element_type
, _Lp
>(*this);
1079 __catch(const bad_weak_ptr
&)
1081 // Q: How can we get here?
1082 // A: Another thread may have invalidated r after the
1083 // use_count test above.
1084 return __shared_ptr
<element_type
, _Lp
>();
1088 // Optimization: avoid try/catch overhead when single threaded.
1089 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1090 : __shared_ptr
<element_type
, _Lp
>(*this);
1096 use_count() const // never throws
1097 { return _M_refcount
._M_get_use_count(); }
1100 expired() const // never throws
1101 { return _M_refcount
._M_get_use_count() == 0; }
1103 template<typename _Tp1
>
1105 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1106 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1108 template<typename _Tp1
>
1110 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1111 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1114 reset() // never throws
1115 { __weak_ptr().swap(*this); }
1118 swap(__weak_ptr
& __s
) // never throws
1120 std::swap(_M_ptr
, __s
._M_ptr
);
1121 _M_refcount
._M_swap(__s
._M_refcount
);
1125 template<typename _Tp1
>
1126 bool operator<(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
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;
1135 // Used by __enable_shared_from_this.
1137 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1140 _M_refcount
= __refcount
;
1143 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1144 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1145 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1146 friend class enable_shared_from_this
<_Tp
>;
1148 _Tp
* _M_ptr
; // Contained pointer.
1149 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1152 // 20.8.13.3.7 weak_ptr specialized algorithms.
1153 template<typename _Tp
, _Lock_policy _Lp
>
1155 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1159 template<typename _Tp
> struct owner_less
;
1161 template<typename _Tp
, typename _Tp1
>
1162 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1165 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1166 { return __lhs
.owner_before(__rhs
); }
1168 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1169 { return __lhs
.owner_before(__rhs
); }
1171 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1172 { return __lhs
.owner_before(__rhs
); }
1175 template<typename _Tp
, _Lock_policy _Lp
>
1176 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1177 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1180 template<typename _Tp
, _Lock_policy _Lp
>
1181 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1182 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1187 template<typename _Tp
, _Lock_policy _Lp
>
1188 class __enable_shared_from_this
1191 __enable_shared_from_this() { }
1193 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1195 __enable_shared_from_this
&
1196 operator=(const __enable_shared_from_this
&)
1199 ~__enable_shared_from_this() { }
1202 __shared_ptr
<_Tp
, _Lp
>
1204 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1206 __shared_ptr
<const _Tp
, _Lp
>
1207 shared_from_this() const
1208 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1211 template<typename _Tp1
>
1213 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1214 { _M_weak_this
._M_assign(__p
, __n
); }
1216 template<typename _Tp1
>
1218 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1219 const __enable_shared_from_this
* __pe
,
1223 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1226 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1230 * @brief A smart pointer with reference-counted copy semantics.
1232 * The object pointed to is deleted when the last shared_ptr pointing to
1233 * it is destroyed or reset.
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 * @brief A smart pointer with weak semantics.
1411 * With forwarding constructors and assignment operators.
1413 template<typename _Tp
>
1415 : public __weak_ptr
<_Tp
>
1419 : __weak_ptr
<_Tp
>() { }
1421 template<typename _Tp1
>
1422 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1423 : __weak_ptr
<_Tp
>(__r
) { }
1425 template<typename _Tp1
>
1426 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1427 : __weak_ptr
<_Tp
>(__r
) { }
1429 template<typename _Tp1
>
1431 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1433 this->__weak_ptr
<_Tp
>::operator=(__r
);
1437 template<typename _Tp1
>
1439 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1441 this->__weak_ptr
<_Tp
>::operator=(__r
);
1446 lock() const // never throws
1449 if (this->expired())
1450 return shared_ptr
<_Tp
>();
1454 return shared_ptr
<_Tp
>(*this);
1456 __catch(const bad_weak_ptr
&)
1458 return shared_ptr
<_Tp
>();
1461 return this->expired() ? shared_ptr
<_Tp
>()
1462 : shared_ptr
<_Tp
>(*this);
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;
1473 template<typename _Tp1
>
1474 bool operator>=(const weak_ptr
<_Tp1
>&) const = delete;
1477 // 20.8.13.3.7 weak_ptr specialized algorithms.
1478 template<typename _Tp
>
1480 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
)
1484 template<typename _Tp
>
1485 struct owner_less
<shared_ptr
<_Tp
>>
1486 : public _Sp_owner_less
<shared_ptr
<_Tp
>, weak_ptr
<_Tp
>>
1489 template<typename _Tp
>
1490 struct owner_less
<weak_ptr
<_Tp
>>
1491 : public _Sp_owner_less
<weak_ptr
<_Tp
>, shared_ptr
<_Tp
>>
1495 * @brief Base class allowing use of member function shared_from_this.
1497 template<typename _Tp
>
1498 class enable_shared_from_this
1501 enable_shared_from_this() { }
1503 enable_shared_from_this(const enable_shared_from_this
&) { }
1505 enable_shared_from_this
&
1506 operator=(const enable_shared_from_this
&)
1509 ~enable_shared_from_this() { }
1514 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1516 shared_ptr
<const _Tp
>
1517 shared_from_this() const
1518 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1521 template<typename _Tp1
>
1523 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1524 { _M_weak_this
._M_assign(__p
, __n
); }
1526 template<typename _Tp1
>
1528 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1529 const enable_shared_from_this
* __pe
,
1533 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1536 mutable weak_ptr
<_Tp
> _M_weak_this
;
1539 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1540 inline __shared_ptr
<_Tp
, _Lp
>
1541 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1543 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1544 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1547 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1548 inline __shared_ptr
<_Tp
, _Lp
>
1549 __make_shared(_Args
&&... __args
)
1551 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1552 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1553 std::forward
<_Args
>(__args
)...);
1556 /** @brief Create an object that is owned by a shared_ptr.
1557 * @param __a An allocator.
1558 * @param __args Arguments for the @a _Tp object's constructor.
1559 * @return A shared_ptr that owns the newly created object.
1560 * @throw An exception thrown from @a _Alloc::allocate or from the
1561 * constructor of @a _Tp.
1563 * A copy of @a __a will be used to allocate memory for the shared_ptr
1564 * and the new object.
1566 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
1567 inline shared_ptr
<_Tp
>
1568 allocate_shared(_Alloc __a
, _Args
&&... __args
)
1570 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), std::forward
<_Alloc
>(__a
),
1571 std::forward
<_Args
>(__args
)...);
1574 /** @brief Create an object that is owned by a shared_ptr.
1575 * @param __args Arguments for the @a _Tp object's constructor.
1576 * @return A shared_ptr that owns the newly created object.
1577 * @throw std::bad_alloc, or an exception thrown from the
1578 * constructor of @a _Tp.
1580 template<typename _Tp
, typename
... _Args
>
1581 inline shared_ptr
<_Tp
>
1582 make_shared(_Args
&&... __args
)
1584 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1585 return allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
1586 std::forward
<_Args
>(__args
)...);
1589 // @} group pointer_abstractions
1591 _GLIBCXX_END_NAMESPACE