1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
44 /** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * You should not attempt to use it directly.
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
52 _GLIBCXX_BEGIN_NAMESPACE(std
)
54 // Forward declarations.
55 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
58 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
61 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
62 class __enable_shared_from_this
;
64 template<typename _Tp
>
67 template<typename _Tp
>
70 template<typename _Tp
>
73 template<typename _Tp
>
74 class enable_shared_from_this
;
76 template<_Lock_policy _Lp
= __default_lock_policy
>
79 template<_Lock_policy _Lp
= __default_lock_policy
>
83 // Counted ptr with no deleter or allocator support
84 template<typename _Ptr
, _Lock_policy _Lp
>
85 class _Sp_counted_ptr
: public _Sp_counted_base
<_Lp
>
89 _Sp_counted_ptr(_Ptr __p
)
93 _M_dispose() // nothrow
97 _M_destroy() // nothrow
101 _M_get_deleter(const std::type_info
&)
104 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
105 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
108 _Ptr _M_ptr
; // copy constructor must not throw
113 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() { }
117 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() { }
121 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() { }
123 // Support for custom deleter and/or allocator
124 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
125 class _Sp_counted_deleter
: public _Sp_counted_ptr
<_Ptr
, _Lp
>
127 typedef typename
_Alloc::template
128 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
130 // Helper class that stores the Deleter and also acts as an allocator.
131 // Used to dispose of the owned pointer and the internal refcount
132 // Requires that copies of _Alloc can free each other's memory.
134 : public _My_alloc_type
// copy constructor must not throw
136 _Deleter _M_del
; // copy constructor must not throw
137 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
138 : _My_alloc_type(__a
), _M_del(__d
) { }
142 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
145 // __d(__p) must not throw.
146 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
147 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
149 // __d(__p) must not throw.
150 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
151 : _Base_type(__p
), _M_del(__d
, __a
) { }
154 _M_dispose() // nothrow
155 { _M_del
._M_del(_Base_type::_M_ptr
); }
158 _M_destroy() // nothrow
160 _My_alloc_type
__a(_M_del
);
161 this->~_Sp_counted_deleter();
162 __a
.deallocate(this, 1);
166 _M_get_deleter(const std::type_info
& __ti
)
169 return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0;
176 _My_Deleter _M_del
; // copy constructor must not throw
179 // helpers for make_shared / allocate_shared
181 template<typename _Tp
>
182 struct _Sp_destroy_inplace
184 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
187 struct _Sp_make_shared_tag
{ };
189 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
190 class _Sp_counted_ptr_inplace
191 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
193 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
198 _Sp_counted_ptr_inplace(_Alloc __a
)
199 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
202 void* __p
= &_M_storage
;
203 ::new (__p
) _Tp(); // might throw
204 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
207 template<typename
... _Args
>
208 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
209 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
212 void* __p
= &_M_storage
;
213 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
214 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
217 // Override because the allocator needs to know the dynamic type
219 _M_destroy() // nothrow
221 typedef typename
_Alloc::template
222 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
223 _My_alloc_type
__a(_Base_type::_M_del
);
224 this->~_Sp_counted_ptr_inplace();
225 __a
.deallocate(this, 1);
228 // Sneaky trick so __shared_ptr can get the managed pointer
230 _M_get_deleter(const std::type_info
& __ti
)
233 return __ti
== typeid(_Sp_make_shared_tag
)
234 ? static_cast<void*>(&_M_storage
)
235 : _Base_type::_M_get_deleter(__ti
);
242 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
246 template<_Lock_policy _Lp
>
250 __shared_count() : _M_pi(0) // nothrow
253 template<typename _Ptr
>
255 __shared_count(_Ptr __p
) : _M_pi(0)
259 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
264 __throw_exception_again
;
268 template<typename _Ptr
, typename _Deleter
>
269 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
271 // The allocator's value_type doesn't matter, will rebind it anyway.
272 typedef std::allocator
<int> _Alloc
;
273 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
274 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
278 _M_pi
= __a2
.allocate(1);
279 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
283 __d(__p
); // Call _Deleter on __p.
285 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
286 __throw_exception_again
;
290 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
291 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
293 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
294 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
298 _M_pi
= __a2
.allocate(1);
299 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
303 __d(__p
); // Call _Deleter on __p.
305 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
306 __throw_exception_again
;
310 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
311 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
314 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
315 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
319 _M_pi
= __a2
.allocate(1);
320 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
321 std::forward
<_Args
>(__args
)...);
326 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
327 __throw_exception_again
;
331 #if _GLIBCXX_DEPRECATED
332 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
333 template<typename _Tp
>
335 __shared_count(std::auto_ptr
<_Tp
>&& __r
)
336 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
340 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
341 template<typename _Tp
, typename _Del
>
343 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
344 : _M_pi(_S_create_from_up(std::move(__r
)))
347 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
348 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
350 ~__shared_count() // nothrow
356 __shared_count(const __shared_count
& __r
)
357 : _M_pi(__r
._M_pi
) // nothrow
360 _M_pi
->_M_add_ref_copy();
364 operator=(const __shared_count
& __r
) // nothrow
366 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
370 __tmp
->_M_add_ref_copy();
379 _M_swap(__shared_count
& __r
) // nothrow
381 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
387 _M_get_use_count() const // nothrow
388 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
391 _M_unique() const // nothrow
392 { return this->_M_get_use_count() == 1; }
395 _M_get_deleter(const std::type_info
& __ti
) const
396 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
399 _M_less(const __shared_count
& __rhs
) const
400 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
403 _M_less(const __weak_count
<_Lp
>& __rhs
) const
404 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
406 // Friend function injected into enclosing namespace and found by ADL
408 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
409 { return __a
._M_pi
== __b
._M_pi
; }
412 friend class __weak_count
<_Lp
>;
414 template<typename _Tp
, typename _Del
>
415 static _Sp_counted_base
<_Lp
>*
416 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
417 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
419 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
420 _Lp
>(__r
.get(), __r
.get_deleter());
423 template<typename _Tp
, typename _Del
>
424 static _Sp_counted_base
<_Lp
>*
425 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
426 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
428 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
429 typedef std::reference_wrapper
<_Del1
> _Del2
;
430 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
431 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
434 _Sp_counted_base
<_Lp
>* _M_pi
;
438 template<_Lock_policy _Lp
>
442 __weak_count() : _M_pi(0) // nothrow
445 __weak_count(const __shared_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
448 _M_pi
->_M_weak_add_ref();
451 __weak_count(const __weak_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
454 _M_pi
->_M_weak_add_ref();
457 ~__weak_count() // nothrow
460 _M_pi
->_M_weak_release();
464 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
466 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
468 __tmp
->_M_weak_add_ref();
470 _M_pi
->_M_weak_release();
476 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
478 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
480 __tmp
->_M_weak_add_ref();
482 _M_pi
->_M_weak_release();
488 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
490 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
496 _M_get_use_count() const // nothrow
497 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
500 _M_less(const __weak_count
& __rhs
) const
501 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
504 _M_less(const __shared_count
<_Lp
>& __rhs
) const
505 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
507 // Friend function injected into enclosing namespace and found by ADL
509 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
510 { return __a
._M_pi
== __b
._M_pi
; }
513 friend class __shared_count
<_Lp
>;
515 _Sp_counted_base
<_Lp
>* _M_pi
;
518 // Now that __weak_count is defined we can define this constructor:
519 template<_Lock_policy _Lp
>
520 inline __shared_count
<_Lp
>:: __shared_count(const __weak_count
<_Lp
>& __r
)
524 _M_pi
->_M_add_ref_lock();
526 __throw_bad_weak_ptr();
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 __shared_ptr() : _M_ptr(0), _M_refcount() // never throws
561 template<typename _Tp1
>
562 explicit __shared_ptr(_Tp1
* __p
) : _M_ptr(__p
), _M_refcount(__p
)
564 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
565 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
566 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
569 template<typename _Tp1
, typename _Deleter
>
570 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
571 : _M_ptr(__p
), _M_refcount(__p
, __d
)
573 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
574 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
575 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
578 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
579 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
580 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
582 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
583 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
584 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
587 template<typename _Deleter
>
588 __shared_ptr(nullptr_t __p
, _Deleter __d
)
589 : _M_ptr(0), _M_refcount(__p
, __d
)
592 template<typename _Deleter
, typename _Alloc
>
593 __shared_ptr(nullptr_t __p
, _Deleter __d
, const _Alloc
& __a
)
594 : _M_ptr(0), _M_refcount(__p
, __d
, __a
)
597 template<typename _Tp1
>
598 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
599 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
602 // generated copy constructor, assignment, destructor are fine.
604 template<typename _Tp1
, typename
= typename
605 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
606 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
607 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
610 __shared_ptr(__shared_ptr
&& __r
)
611 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
613 _M_refcount
._M_swap(__r
._M_refcount
);
617 template<typename _Tp1
, typename
= typename
618 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
619 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
620 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
622 _M_refcount
._M_swap(__r
._M_refcount
);
626 template<typename _Tp1
>
627 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
628 : _M_refcount(__r
._M_refcount
) // may throw
630 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
632 // It is now safe to copy __r._M_ptr, as
633 // _M_refcount(__r._M_refcount) did not throw.
637 // If an exception is thrown this constructor has no effect.
638 template<typename _Tp1
, typename _Del
>
639 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
640 : _M_ptr(__r
.get()), _M_refcount()
642 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
643 _Tp1
* __tmp
= __r
.get();
644 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
645 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
648 #if _GLIBCXX_DEPRECATED
649 // Postcondition: use_count() == 1 and __r.get() == 0
650 template<typename _Tp1
>
651 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
652 : _M_ptr(__r
.get()), _M_refcount()
654 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
655 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
656 _Tp1
* __tmp
= __r
.get();
657 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
658 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
662 /* TODO: use delegating constructor */
663 __shared_ptr(nullptr_t
) : _M_ptr(0), _M_refcount() // never throws
666 template<typename _Tp1
>
668 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
671 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
675 #if _GLIBCXX_DEPRECATED
676 template<typename _Tp1
>
678 operator=(std::auto_ptr
<_Tp1
>&& __r
)
680 __shared_ptr(std::move(__r
)).swap(*this);
686 operator=(__shared_ptr
&& __r
)
688 __shared_ptr(std::move(__r
)).swap(*this);
694 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
696 __shared_ptr(std::move(__r
)).swap(*this);
700 template<typename _Tp1
, typename _Del
>
702 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
704 __shared_ptr(std::move(__r
)).swap(*this);
709 reset() // never throws
710 { __shared_ptr().swap(*this); }
712 template<typename _Tp1
>
714 reset(_Tp1
* __p
) // _Tp1 must be complete.
716 // Catch self-reset errors.
717 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
718 __shared_ptr(__p
).swap(*this);
721 template<typename _Tp1
, typename _Deleter
>
723 reset(_Tp1
* __p
, _Deleter __d
)
724 { __shared_ptr(__p
, __d
).swap(*this); }
726 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
728 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
729 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
731 // Allow class instantiation when _Tp is [cv-qual] void.
732 typename
std::add_lvalue_reference
<_Tp
>::type
733 operator*() const // never throws
735 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
740 operator->() const // never throws
742 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
747 get() const // never throws
750 explicit operator bool() const // never throws
751 { return _M_ptr
== 0 ? false : true; }
754 unique() const // never throws
755 { return _M_refcount
._M_unique(); }
758 use_count() const // never throws
759 { return _M_refcount
._M_get_use_count(); }
762 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
764 std::swap(_M_ptr
, __other
._M_ptr
);
765 _M_refcount
._M_swap(__other
._M_refcount
);
768 template<typename _Tp1
>
770 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
771 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
773 template<typename _Tp1
>
775 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
776 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
780 // This constructor is non-standard, it is used by allocate_shared.
781 template<typename _Alloc
, typename
... _Args
>
782 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
783 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
784 std::forward
<_Args
>(__args
)...)
786 // _M_ptr needs to point to the newly constructed object.
787 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
788 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
789 _M_ptr
= static_cast<_Tp
*>(__p
);
790 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
793 template<typename _Alloc
>
796 void operator()(_Tp
* __ptr
)
798 _M_alloc
.destroy(__ptr
);
799 _M_alloc
.deallocate(__ptr
, 1);
804 template<typename _Alloc
, typename
... _Args
>
805 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
806 : _M_ptr(), _M_refcount()
808 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
809 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
810 _M_ptr
= __del
._M_alloc
.allocate(1);
813 __del
._M_alloc
.construct(_M_ptr
, std::forward
<_Args
>(__args
)...);
817 __del
._M_alloc
.deallocate(_M_ptr
, 1);
818 __throw_exception_again
;
820 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
821 _M_refcount
._M_swap(__count
);
822 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
826 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
828 friend __shared_ptr
<_Tp1
, _Lp1
>
829 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
833 _M_get_deleter(const std::type_info
& __ti
) const
834 { return _M_refcount
._M_get_deleter(__ti
); }
836 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
837 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
839 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
840 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
842 _Tp
* _M_ptr
; // Contained pointer.
843 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
847 // 20.8.13.2.7 shared_ptr comparisons
848 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
850 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
851 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
852 { return __a
.get() == __b
.get(); }
854 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
856 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
857 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
858 { return __a
.get() != __b
.get(); }
860 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
862 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
863 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
864 { return __a
.get() < __b
.get(); }
866 template<typename _Sp
>
867 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
870 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
872 typedef typename
_Sp::element_type element_type
;
873 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
877 template<typename _Tp
, _Lock_policy _Lp
>
878 struct less
<__shared_ptr
<_Tp
, _Lp
>>
879 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
882 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
883 template<typename _Tp
, _Lock_policy _Lp
>
885 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
886 const __shared_ptr
<_Tp
, _Lp
>& __b
)
887 { return __a
.get() > __b
.get(); }
889 template<typename _Tp
, _Lock_policy _Lp
>
891 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
892 const __shared_ptr
<_Tp
, _Lp
>& __b
)
893 { return __a
.get() >= __b
.get(); }
895 template<typename _Tp
, _Lock_policy _Lp
>
897 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
898 const __shared_ptr
<_Tp
, _Lp
>& __b
)
899 { return __a
.get() <= __b
.get(); }
901 // 2.2.3.8 shared_ptr specialized algorithms.
902 template<typename _Tp
, _Lock_policy _Lp
>
904 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
907 // 2.2.3.9 shared_ptr casts
909 // The seemingly equivalent code:
910 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
911 // will eventually result in undefined behaviour, attempting to
912 // delete the same object twice.
913 /// static_pointer_cast
914 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
915 inline __shared_ptr
<_Tp
, _Lp
>
916 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
917 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
919 // The seemingly equivalent code:
920 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
921 // will eventually result in undefined behaviour, attempting to
922 // delete the same object twice.
923 /// const_pointer_cast
924 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
925 inline __shared_ptr
<_Tp
, _Lp
>
926 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
927 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
929 // The seemingly equivalent code:
930 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
931 // will eventually result in undefined behaviour, attempting to
932 // delete the same object twice.
933 /// dynamic_pointer_cast
934 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
935 inline __shared_ptr
<_Tp
, _Lp
>
936 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
938 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
939 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
940 return __shared_ptr
<_Tp
, _Lp
>();
944 template<typename _Tp
, _Lock_policy _Lp
>
948 typedef _Tp element_type
;
950 __weak_ptr() : _M_ptr(0), _M_refcount() // never throws
953 // Generated copy constructor, assignment, destructor are fine.
955 // The "obvious" converting constructor implementation:
957 // template<typename _Tp1>
958 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
959 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
962 // has a serious problem.
964 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
965 // conversion may require access to *__r._M_ptr (virtual inheritance).
967 // It is not possible to avoid spurious access violations since
968 // in multithreaded programs __r._M_ptr may be invalidated at any point.
969 template<typename _Tp1
, typename
= typename
970 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
971 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
972 : _M_refcount(__r
._M_refcount
) // never throws
973 { _M_ptr
= __r
.lock().get(); }
975 template<typename _Tp1
, typename
= typename
976 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
977 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
978 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
981 template<typename _Tp1
>
983 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
985 _M_ptr
= __r
.lock().get();
986 _M_refcount
= __r
._M_refcount
;
990 template<typename _Tp1
>
992 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
995 _M_refcount
= __r
._M_refcount
;
999 __shared_ptr
<_Tp
, _Lp
>
1000 lock() const // never throws
1003 // Optimization: avoid throw overhead.
1005 return __shared_ptr
<element_type
, _Lp
>();
1009 return __shared_ptr
<element_type
, _Lp
>(*this);
1011 __catch(const bad_weak_ptr
&)
1013 // Q: How can we get here?
1014 // A: Another thread may have invalidated r after the
1015 // use_count test above.
1016 return __shared_ptr
<element_type
, _Lp
>();
1020 // Optimization: avoid try/catch overhead when single threaded.
1021 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1022 : __shared_ptr
<element_type
, _Lp
>(*this);
1028 use_count() const // never throws
1029 { return _M_refcount
._M_get_use_count(); }
1032 expired() const // never throws
1033 { return _M_refcount
._M_get_use_count() == 0; }
1035 template<typename _Tp1
>
1037 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1038 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1040 template<typename _Tp1
>
1042 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1043 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1046 reset() // never throws
1047 { __weak_ptr().swap(*this); }
1050 swap(__weak_ptr
& __s
) // never throws
1052 std::swap(_M_ptr
, __s
._M_ptr
);
1053 _M_refcount
._M_swap(__s
._M_refcount
);
1057 // Used by __enable_shared_from_this.
1059 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1062 _M_refcount
= __refcount
;
1065 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1066 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1067 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1068 friend class enable_shared_from_this
<_Tp
>;
1070 _Tp
* _M_ptr
; // Contained pointer.
1071 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1074 // 20.8.13.3.7 weak_ptr specialized algorithms.
1075 template<typename _Tp
, _Lock_policy _Lp
>
1077 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1080 template<typename _Tp
, typename _Tp1
>
1081 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1084 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1085 { return __lhs
.owner_before(__rhs
); }
1088 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1089 { return __lhs
.owner_before(__rhs
); }
1092 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1093 { return __lhs
.owner_before(__rhs
); }
1096 template<typename _Tp
, _Lock_policy _Lp
>
1097 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1098 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1101 template<typename _Tp
, _Lock_policy _Lp
>
1102 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1103 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1107 template<typename _Tp
, _Lock_policy _Lp
>
1108 class __enable_shared_from_this
1111 __enable_shared_from_this() { }
1113 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1115 __enable_shared_from_this
&
1116 operator=(const __enable_shared_from_this
&)
1119 ~__enable_shared_from_this() { }
1122 __shared_ptr
<_Tp
, _Lp
>
1124 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1126 __shared_ptr
<const _Tp
, _Lp
>
1127 shared_from_this() const
1128 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1131 template<typename _Tp1
>
1133 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1134 { _M_weak_this
._M_assign(__p
, __n
); }
1136 template<typename _Tp1
>
1138 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1139 const __enable_shared_from_this
* __pe
,
1143 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1146 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1150 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1151 inline __shared_ptr
<_Tp
, _Lp
>
1152 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1154 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1155 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1158 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1159 inline __shared_ptr
<_Tp
, _Lp
>
1160 __make_shared(_Args
&&... __args
)
1162 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1163 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1164 std::forward
<_Args
>(__args
)...);
1167 /// std::hash specialization for __shared_ptr.
1168 template<typename _Tp
, _Lock_policy _Lp
>
1169 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1170 : public std::unary_function
<__shared_ptr
<_Tp
, _Lp
>, size_t>
1173 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const
1174 { return std::hash
<_Tp
*>()(__s
.get()); }
1177 _GLIBCXX_END_NAMESPACE
1179 #endif // _SHARED_PTR_BASE_H