1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
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
>
88 _Sp_counted_ptr(_Ptr __p
)
92 _M_dispose() // nothrow
96 _M_destroy() // nothrow
100 _M_get_deleter(const std::type_info
& __ti
)
103 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
104 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
107 _Ptr _M_ptr
; // copy constructor must not throw
110 // Support for custom deleter and/or allocator
111 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
112 class _Sp_counted_deleter
: public _Sp_counted_ptr
<_Ptr
, _Lp
>
114 typedef typename
_Alloc::template
115 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
117 // Helper class that stores the Deleter and also acts as an allocator.
118 // Used to dispose of the owned pointer and the internal refcount
119 // Requires that copies of _Alloc can free each other's memory.
121 : public _My_alloc_type
// copy constructor must not throw
123 _Deleter _M_del
; // copy constructor must not throw
124 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
125 : _My_alloc_type(__a
), _M_del(__d
) { }
129 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
132 // __d(__p) must not throw.
133 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
134 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
136 // __d(__p) must not throw.
137 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
138 : _Base_type(__p
), _M_del(__d
, __a
) { }
141 _M_dispose() // nothrow
142 { _M_del
._M_del(_Base_type::_M_ptr
); }
145 _M_destroy() // nothrow
147 _My_alloc_type
__a(_M_del
);
148 this->~_Sp_counted_deleter();
149 __a
.deallocate(this, 1);
153 _M_get_deleter(const std::type_info
& __ti
)
156 return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0;
163 _My_Deleter _M_del
; // copy constructor must not throw
166 // helpers for make_shared / allocate_shared
168 template<typename _Tp
>
169 struct _Sp_destroy_inplace
171 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
174 struct _Sp_make_shared_tag
{ };
176 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
177 class _Sp_counted_ptr_inplace
178 : public _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
180 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
184 _Sp_counted_ptr_inplace(_Alloc __a
)
185 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
188 void* __p
= &_M_storage
;
189 ::new (__p
) _Tp(); // might throw
190 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
193 template<typename
... _Args
>
194 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
195 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
198 void* __p
= &_M_storage
;
199 ::new (__p
) _Tp(std::forward
<_Args
>(__args
)...); // might throw
200 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
203 // Override because the allocator needs to know the dynamic type
205 _M_destroy() // nothrow
207 typedef typename
_Alloc::template
208 rebind
<_Sp_counted_ptr_inplace
>::other _My_alloc_type
;
209 _My_alloc_type
__a(_Base_type::_M_del
);
210 this->~_Sp_counted_ptr_inplace();
211 __a
.deallocate(this, 1);
214 // Sneaky trick so __shared_ptr can get the managed pointer
216 _M_get_deleter(const std::type_info
& __ti
)
219 return __ti
== typeid(_Sp_make_shared_tag
)
220 ? static_cast<void*>(&_M_storage
)
221 : _Base_type::_M_get_deleter(__ti
);
228 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
232 template<_Lock_policy _Lp
>
236 __shared_count() : _M_pi(0) // nothrow
239 template<typename _Ptr
>
240 __shared_count(_Ptr __p
) : _M_pi(0)
244 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
249 __throw_exception_again
;
253 template<typename _Ptr
, typename _Deleter
>
254 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
256 // The allocator's value_type doesn't matter, will rebind it anyway.
257 typedef std::allocator
<int> _Alloc
;
258 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
259 typedef std::allocator
<_Sp_cd_type
> _Alloc2
;
263 _M_pi
= __a2
.allocate(1);
264 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
268 __d(__p
); // Call _Deleter on __p.
270 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
271 __throw_exception_again
;
275 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
276 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
278 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
279 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
283 _M_pi
= __a2
.allocate(1);
284 ::new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
288 __d(__p
); // Call _Deleter on __p.
290 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
291 __throw_exception_again
;
295 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
296 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
299 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
300 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
304 _M_pi
= __a2
.allocate(1);
305 ::new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
306 std::forward
<_Args
>(__args
)...);
311 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
312 __throw_exception_again
;
316 #if _GLIBCXX_DEPRECATED
317 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
318 template<typename _Tp
>
319 explicit __shared_count(std::auto_ptr
<_Tp
>&& __r
)
320 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
324 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
325 template<typename _Tp
, typename _Del
>
326 explicit __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
)
327 : _M_pi(_S_create_from_up(std::move(__r
)))
330 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
331 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
333 ~__shared_count() // nothrow
339 __shared_count(const __shared_count
& __r
)
340 : _M_pi(__r
._M_pi
) // nothrow
343 _M_pi
->_M_add_ref_copy();
347 operator=(const __shared_count
& __r
) // nothrow
349 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
353 __tmp
->_M_add_ref_copy();
362 _M_swap(__shared_count
& __r
) // nothrow
364 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
370 _M_get_use_count() const // nothrow
371 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
374 _M_unique() const // nothrow
375 { return this->_M_get_use_count() == 1; }
378 _M_get_deleter(const std::type_info
& __ti
) const
379 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
382 _M_less(const __shared_count
& __rhs
) const
383 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
386 _M_less(const __weak_count
<_Lp
>& __rhs
) const
387 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
389 // Friend function injected into enclosing namespace and found by ADL
391 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
392 { return __a
._M_pi
== __b
._M_pi
; }
395 friend class __weak_count
<_Lp
>;
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 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
403 _Lp
>(__r
.get(), __r
.get_deleter());
406 template<typename _Tp
, typename _Del
>
407 static _Sp_counted_base
<_Lp
>*
408 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
409 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
411 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
412 typedef std::reference_wrapper
<_Del1
> _Del2
;
413 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
414 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
417 _Sp_counted_base
<_Lp
>* _M_pi
;
421 template<_Lock_policy _Lp
>
425 __weak_count() : _M_pi(0) // nothrow
428 __weak_count(const __shared_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
431 _M_pi
->_M_weak_add_ref();
434 __weak_count(const __weak_count
<_Lp
>& __r
) : _M_pi(__r
._M_pi
) // nothrow
437 _M_pi
->_M_weak_add_ref();
440 ~__weak_count() // nothrow
443 _M_pi
->_M_weak_release();
447 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
449 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
451 __tmp
->_M_weak_add_ref();
453 _M_pi
->_M_weak_release();
459 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
461 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
463 __tmp
->_M_weak_add_ref();
465 _M_pi
->_M_weak_release();
471 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
473 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
479 _M_get_use_count() const // nothrow
480 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
483 _M_less(const __weak_count
& __rhs
) const
484 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
487 _M_less(const __shared_count
<_Lp
>& __rhs
) const
488 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
490 // Friend function injected into enclosing namespace and found by ADL
492 operator==(const __weak_count
& __a
, const __weak_count
& __b
)
493 { return __a
._M_pi
== __b
._M_pi
; }
496 friend class __shared_count
<_Lp
>;
498 _Sp_counted_base
<_Lp
>* _M_pi
;
501 // Now that __weak_count is defined we can define this constructor:
502 template<_Lock_policy _Lp
>
503 inline __shared_count
<_Lp
>:: __shared_count(const __weak_count
<_Lp
>& __r
)
507 _M_pi
->_M_add_ref_lock();
509 __throw_bad_weak_ptr();
513 // Support for enable_shared_from_this.
515 // Friend of __enable_shared_from_this.
516 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
518 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
519 const __enable_shared_from_this
<_Tp1
,
522 // Friend of enable_shared_from_this.
523 template<typename _Tp1
, typename _Tp2
>
525 __enable_shared_from_this_helper(const __shared_count
<>&,
526 const enable_shared_from_this
<_Tp1
>*,
529 template<_Lock_policy _Lp
>
531 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
535 template<typename _Tp
, _Lock_policy _Lp
>
539 typedef _Tp element_type
;
541 __shared_ptr() : _M_ptr(0), _M_refcount() // never throws
544 template<typename _Tp1
>
545 explicit __shared_ptr(_Tp1
* __p
) : _M_ptr(__p
), _M_refcount(__p
)
547 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
548 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
549 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
552 template<typename _Tp1
, typename _Deleter
>
553 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
554 : _M_ptr(__p
), _M_refcount(__p
, __d
)
556 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
557 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
558 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
561 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
562 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
563 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
565 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
566 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
567 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
570 template<typename _Tp1
>
571 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
572 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
575 // generated copy constructor, assignment, destructor are fine.
577 template<typename _Tp1
>
578 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
579 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
580 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
582 __shared_ptr(__shared_ptr
&& __r
)
583 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
585 _M_refcount
._M_swap(__r
._M_refcount
);
589 template<typename _Tp1
>
590 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
591 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
593 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
594 _M_refcount
._M_swap(__r
._M_refcount
);
598 template<typename _Tp1
>
599 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
600 : _M_refcount(__r
._M_refcount
) // may throw
602 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
604 // It is now safe to copy __r._M_ptr, as
605 // _M_refcount(__r._M_refcount) did not throw.
609 template<typename _Tp1
, typename _Del
>
610 explicit __shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
612 // If an exception is thrown this constructor has no effect.
613 template<typename _Tp1
, typename _Del
>
614 explicit __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
615 : _M_ptr(__r
.get()), _M_refcount()
617 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
618 _Tp1
* __tmp
= __r
.get();
619 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
620 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
623 #if _GLIBCXX_DEPRECATED
624 // Postcondition: use_count() == 1 and __r.get() == 0
625 template<typename _Tp1
>
626 explicit __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
627 : _M_ptr(__r
.get()), _M_refcount()
629 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
630 // TODO requires _Tp1 is complete, delete __r.release() well-formed
631 _Tp1
* __tmp
= __r
.get();
632 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
633 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
637 template<typename _Tp1
>
639 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
642 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
646 #if _GLIBCXX_DEPRECATED
647 template<typename _Tp1
>
649 operator=(std::auto_ptr
<_Tp1
>&& __r
)
651 __shared_ptr(std::move(__r
)).swap(*this);
657 operator=(__shared_ptr
&& __r
)
659 __shared_ptr(std::move(__r
)).swap(*this);
665 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
667 __shared_ptr(std::move(__r
)).swap(*this);
671 template<typename _Tp1
, typename _Del
>
673 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
675 template<typename _Tp1
, typename _Del
>
677 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
679 __shared_ptr(std::move(__r
)).swap(*this);
684 reset() // never throws
685 { __shared_ptr().swap(*this); }
687 template<typename _Tp1
>
689 reset(_Tp1
* __p
) // _Tp1 must be complete.
691 // Catch self-reset errors.
692 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
693 __shared_ptr(__p
).swap(*this);
696 template<typename _Tp1
, typename _Deleter
>
698 reset(_Tp1
* __p
, _Deleter __d
)
699 { __shared_ptr(__p
, __d
).swap(*this); }
701 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
703 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
704 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
706 // Allow class instantiation when _Tp is [cv-qual] void.
707 typename
std::add_lvalue_reference
<_Tp
>::type
708 operator*() const // never throws
710 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
715 operator->() const // never throws
717 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
722 get() const // never throws
725 explicit operator bool() const // never throws
726 { return _M_ptr
== 0 ? false : true; }
729 unique() const // never throws
730 { return _M_refcount
._M_unique(); }
733 use_count() const // never throws
734 { return _M_refcount
._M_get_use_count(); }
737 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
739 std::swap(_M_ptr
, __other
._M_ptr
);
740 _M_refcount
._M_swap(__other
._M_refcount
);
743 template<typename _Tp1
>
745 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
746 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
748 template<typename _Tp1
>
750 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
751 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
755 // This constructor is non-standard, it is used by allocate_shared.
756 template<typename _Alloc
, typename
... _Args
>
757 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
758 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
759 std::forward
<_Args
>(__args
)...)
761 // _M_ptr needs to point to the newly constructed object.
762 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
763 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
764 _M_ptr
= static_cast<_Tp
*>(__p
);
765 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
768 template<typename _Alloc
>
771 void operator()(_Tp
* __ptr
)
773 _M_alloc
.destroy(__ptr
);
774 _M_alloc
.deallocate(__ptr
, 1);
779 template<typename _Alloc
, typename
... _Args
>
780 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
781 : _M_ptr(), _M_refcount()
783 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
784 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
785 _M_ptr
= __del
._M_alloc
.allocate(1);
788 __del
._M_alloc
.construct(_M_ptr
, std::forward
<_Args
>(__args
)...);
792 __del
._M_alloc
.deallocate(_M_ptr
, 1);
793 __throw_exception_again
;
795 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
796 _M_refcount
._M_swap(__count
);
797 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
801 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
803 friend __shared_ptr
<_Tp1
, _Lp1
>
804 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
808 _M_get_deleter(const std::type_info
& __ti
) const
809 { return _M_refcount
._M_get_deleter(__ti
); }
811 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
812 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
814 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
815 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
817 _Tp
* _M_ptr
; // Contained pointer.
818 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
822 // 20.8.13.2.7 shared_ptr comparisons
823 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
825 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
826 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
827 { return __a
.get() == __b
.get(); }
829 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
831 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
832 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
833 { return __a
.get() != __b
.get(); }
835 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
837 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
838 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
839 { return __a
.get() < __b
.get(); }
841 template<typename _Sp
>
842 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
845 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
847 typedef typename
_Sp::element_type element_type
;
848 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
852 template<typename _Tp
, _Lock_policy _Lp
>
853 struct less
<__shared_ptr
<_Tp
, _Lp
>>
854 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
857 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
858 template<typename _Tp
, _Lock_policy _Lp
>
860 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
861 const __shared_ptr
<_Tp
, _Lp
>& __b
)
862 { return __a
.get() > __b
.get(); }
864 template<typename _Tp
, _Lock_policy _Lp
>
866 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
867 const __shared_ptr
<_Tp
, _Lp
>& __b
)
868 { return __a
.get() >= __b
.get(); }
870 template<typename _Tp
, _Lock_policy _Lp
>
872 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
873 const __shared_ptr
<_Tp
, _Lp
>& __b
)
874 { return __a
.get() <= __b
.get(); }
876 // 2.2.3.8 shared_ptr specialized algorithms.
877 template<typename _Tp
, _Lock_policy _Lp
>
879 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
882 // 2.2.3.9 shared_ptr casts
884 // The seemingly equivalent code:
885 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
886 // will eventually result in undefined behaviour, attempting to
887 // delete the same object twice.
888 /// static_pointer_cast
889 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
890 inline __shared_ptr
<_Tp
, _Lp
>
891 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
892 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
894 // The seemingly equivalent code:
895 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
896 // will eventually result in undefined behaviour, attempting to
897 // delete the same object twice.
898 /// const_pointer_cast
899 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
900 inline __shared_ptr
<_Tp
, _Lp
>
901 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
902 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
904 // The seemingly equivalent code:
905 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
906 // will eventually result in undefined behaviour, attempting to
907 // delete the same object twice.
908 /// dynamic_pointer_cast
909 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
910 inline __shared_ptr
<_Tp
, _Lp
>
911 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
913 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
914 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
915 return __shared_ptr
<_Tp
, _Lp
>();
919 template<typename _Tp
, _Lock_policy _Lp
>
923 typedef _Tp element_type
;
925 __weak_ptr() : _M_ptr(0), _M_refcount() // never throws
928 // Generated copy constructor, assignment, destructor are fine.
930 // The "obvious" converting constructor implementation:
932 // template<typename _Tp1>
933 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
934 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
937 // has a serious problem.
939 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
940 // conversion may require access to *__r._M_ptr (virtual inheritance).
942 // It is not possible to avoid spurious access violations since
943 // in multithreaded programs __r._M_ptr may be invalidated at any point.
944 template<typename _Tp1
>
945 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
946 : _M_refcount(__r
._M_refcount
) // never throws
948 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
949 _M_ptr
= __r
.lock().get();
952 template<typename _Tp1
>
953 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
954 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
955 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
957 template<typename _Tp1
>
959 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
961 _M_ptr
= __r
.lock().get();
962 _M_refcount
= __r
._M_refcount
;
966 template<typename _Tp1
>
968 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
971 _M_refcount
= __r
._M_refcount
;
975 __shared_ptr
<_Tp
, _Lp
>
976 lock() const // never throws
979 // Optimization: avoid throw overhead.
981 return __shared_ptr
<element_type
, _Lp
>();
985 return __shared_ptr
<element_type
, _Lp
>(*this);
987 __catch(const bad_weak_ptr
&)
989 // Q: How can we get here?
990 // A: Another thread may have invalidated r after the
991 // use_count test above.
992 return __shared_ptr
<element_type
, _Lp
>();
996 // Optimization: avoid try/catch overhead when single threaded.
997 return expired() ? __shared_ptr
<element_type
, _Lp
>()
998 : __shared_ptr
<element_type
, _Lp
>(*this);
1004 use_count() const // never throws
1005 { return _M_refcount
._M_get_use_count(); }
1008 expired() const // never throws
1009 { return _M_refcount
._M_get_use_count() == 0; }
1011 template<typename _Tp1
>
1013 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1014 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1016 template<typename _Tp1
>
1018 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1019 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1022 reset() // never throws
1023 { __weak_ptr().swap(*this); }
1026 swap(__weak_ptr
& __s
) // never throws
1028 std::swap(_M_ptr
, __s
._M_ptr
);
1029 _M_refcount
._M_swap(__s
._M_refcount
);
1033 template<typename _Tp1
>
1034 bool operator<(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1036 template<typename _Tp1
>
1037 bool operator<=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1039 template<typename _Tp1
>
1040 bool operator>(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1042 template<typename _Tp1
>
1043 bool operator>=(const __weak_ptr
<_Tp1
, _Lp
>&) const = delete;
1046 // Used by __enable_shared_from_this.
1048 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1051 _M_refcount
= __refcount
;
1054 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1055 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1056 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1057 friend class enable_shared_from_this
<_Tp
>;
1059 _Tp
* _M_ptr
; // Contained pointer.
1060 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1063 // 20.8.13.3.7 weak_ptr specialized algorithms.
1064 template<typename _Tp
, _Lock_policy _Lp
>
1066 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1069 template<typename _Tp
, typename _Tp1
>
1070 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1073 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1074 { return __lhs
.owner_before(__rhs
); }
1077 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1078 { return __lhs
.owner_before(__rhs
); }
1081 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1082 { return __lhs
.owner_before(__rhs
); }
1085 template<typename _Tp
, _Lock_policy _Lp
>
1086 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1087 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1090 template<typename _Tp
, _Lock_policy _Lp
>
1091 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1092 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1096 template<typename _Tp
, _Lock_policy _Lp
>
1097 class __enable_shared_from_this
1100 __enable_shared_from_this() { }
1102 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1104 __enable_shared_from_this
&
1105 operator=(const __enable_shared_from_this
&)
1108 ~__enable_shared_from_this() { }
1111 __shared_ptr
<_Tp
, _Lp
>
1113 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1115 __shared_ptr
<const _Tp
, _Lp
>
1116 shared_from_this() const
1117 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1120 template<typename _Tp1
>
1122 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1123 { _M_weak_this
._M_assign(__p
, __n
); }
1125 template<typename _Tp1
>
1127 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1128 const __enable_shared_from_this
* __pe
,
1132 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1135 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1139 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1140 inline __shared_ptr
<_Tp
, _Lp
>
1141 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1143 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1144 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1147 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1148 inline __shared_ptr
<_Tp
, _Lp
>
1149 __make_shared(_Args
&&... __args
)
1151 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1152 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1153 std::forward
<_Args
>(__args
)...);
1156 _GLIBCXX_END_NAMESPACE
1158 #endif // _SHARED_PTR_BASE_H