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
>
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 // If an exception is thrown this constructor has no effect.
610 template<typename _Tp1
, typename _Del
>
611 explicit __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
612 : _M_ptr(__r
.get()), _M_refcount()
614 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
615 _Tp1
* __tmp
= __r
.get();
616 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
617 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
620 #if _GLIBCXX_DEPRECATED
621 // Postcondition: use_count() == 1 and __r.get() == 0
622 template<typename _Tp1
>
623 explicit __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
624 : _M_ptr(__r
.get()), _M_refcount()
626 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
627 // TODO requires _Tp1 is complete, delete __r.release() well-formed
628 _Tp1
* __tmp
= __r
.get();
629 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
630 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
634 template<typename _Tp1
>
636 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
639 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
643 #if _GLIBCXX_DEPRECATED
644 template<typename _Tp1
>
646 operator=(std::auto_ptr
<_Tp1
>&& __r
)
648 __shared_ptr(std::move(__r
)).swap(*this);
654 operator=(__shared_ptr
&& __r
)
656 __shared_ptr(std::move(__r
)).swap(*this);
662 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
664 __shared_ptr(std::move(__r
)).swap(*this);
668 template<typename _Tp1
, typename _Del
>
670 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
672 __shared_ptr(std::move(__r
)).swap(*this);
677 reset() // never throws
678 { __shared_ptr().swap(*this); }
680 template<typename _Tp1
>
682 reset(_Tp1
* __p
) // _Tp1 must be complete.
684 // Catch self-reset errors.
685 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
686 __shared_ptr(__p
).swap(*this);
689 template<typename _Tp1
, typename _Deleter
>
691 reset(_Tp1
* __p
, _Deleter __d
)
692 { __shared_ptr(__p
, __d
).swap(*this); }
694 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
696 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
697 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
699 // Allow class instantiation when _Tp is [cv-qual] void.
700 typename
std::add_lvalue_reference
<_Tp
>::type
701 operator*() const // never throws
703 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
708 operator->() const // never throws
710 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
715 get() const // never throws
718 explicit operator bool() const // never throws
719 { return _M_ptr
== 0 ? false : true; }
722 unique() const // never throws
723 { return _M_refcount
._M_unique(); }
726 use_count() const // never throws
727 { return _M_refcount
._M_get_use_count(); }
730 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
732 std::swap(_M_ptr
, __other
._M_ptr
);
733 _M_refcount
._M_swap(__other
._M_refcount
);
736 template<typename _Tp1
>
738 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
739 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
741 template<typename _Tp1
>
743 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
744 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
748 // This constructor is non-standard, it is used by allocate_shared.
749 template<typename _Alloc
, typename
... _Args
>
750 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
751 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
752 std::forward
<_Args
>(__args
)...)
754 // _M_ptr needs to point to the newly constructed object.
755 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
756 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
757 _M_ptr
= static_cast<_Tp
*>(__p
);
758 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
761 template<typename _Alloc
>
764 void operator()(_Tp
* __ptr
)
766 _M_alloc
.destroy(__ptr
);
767 _M_alloc
.deallocate(__ptr
, 1);
772 template<typename _Alloc
, typename
... _Args
>
773 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
774 : _M_ptr(), _M_refcount()
776 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
777 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
778 _M_ptr
= __del
._M_alloc
.allocate(1);
781 __del
._M_alloc
.construct(_M_ptr
, std::forward
<_Args
>(__args
)...);
785 __del
._M_alloc
.deallocate(_M_ptr
, 1);
786 __throw_exception_again
;
788 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
789 _M_refcount
._M_swap(__count
);
790 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
794 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
796 friend __shared_ptr
<_Tp1
, _Lp1
>
797 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
801 _M_get_deleter(const std::type_info
& __ti
) const
802 { return _M_refcount
._M_get_deleter(__ti
); }
804 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
805 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
807 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
808 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
810 _Tp
* _M_ptr
; // Contained pointer.
811 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
815 // 20.8.13.2.7 shared_ptr comparisons
816 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
818 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
819 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
820 { return __a
.get() == __b
.get(); }
822 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
824 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
825 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
826 { return __a
.get() != __b
.get(); }
828 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
830 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
831 const __shared_ptr
<_Tp2
, _Lp
>& __b
)
832 { return __a
.get() < __b
.get(); }
834 template<typename _Sp
>
835 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
838 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const
840 typedef typename
_Sp::element_type element_type
;
841 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
845 template<typename _Tp
, _Lock_policy _Lp
>
846 struct less
<__shared_ptr
<_Tp
, _Lp
>>
847 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
850 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
851 template<typename _Tp
, _Lock_policy _Lp
>
853 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
,
854 const __shared_ptr
<_Tp
, _Lp
>& __b
)
855 { return __a
.get() > __b
.get(); }
857 template<typename _Tp
, _Lock_policy _Lp
>
859 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
860 const __shared_ptr
<_Tp
, _Lp
>& __b
)
861 { return __a
.get() >= __b
.get(); }
863 template<typename _Tp
, _Lock_policy _Lp
>
865 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
,
866 const __shared_ptr
<_Tp
, _Lp
>& __b
)
867 { return __a
.get() <= __b
.get(); }
869 // 2.2.3.8 shared_ptr specialized algorithms.
870 template<typename _Tp
, _Lock_policy _Lp
>
872 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
875 // 2.2.3.9 shared_ptr casts
877 // The seemingly equivalent code:
878 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
879 // will eventually result in undefined behaviour, attempting to
880 // delete the same object twice.
881 /// static_pointer_cast
882 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
883 inline __shared_ptr
<_Tp
, _Lp
>
884 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
885 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
887 // The seemingly equivalent code:
888 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
889 // will eventually result in undefined behaviour, attempting to
890 // delete the same object twice.
891 /// const_pointer_cast
892 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
893 inline __shared_ptr
<_Tp
, _Lp
>
894 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
895 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
897 // The seemingly equivalent code:
898 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
899 // will eventually result in undefined behaviour, attempting to
900 // delete the same object twice.
901 /// dynamic_pointer_cast
902 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
903 inline __shared_ptr
<_Tp
, _Lp
>
904 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
906 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
907 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
908 return __shared_ptr
<_Tp
, _Lp
>();
912 template<typename _Tp
, _Lock_policy _Lp
>
916 typedef _Tp element_type
;
918 __weak_ptr() : _M_ptr(0), _M_refcount() // never throws
921 // Generated copy constructor, assignment, destructor are fine.
923 // The "obvious" converting constructor implementation:
925 // template<typename _Tp1>
926 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
927 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
930 // has a serious problem.
932 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
933 // conversion may require access to *__r._M_ptr (virtual inheritance).
935 // It is not possible to avoid spurious access violations since
936 // in multithreaded programs __r._M_ptr may be invalidated at any point.
937 template<typename _Tp1
>
938 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
939 : _M_refcount(__r
._M_refcount
) // never throws
941 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
942 _M_ptr
= __r
.lock().get();
945 template<typename _Tp1
>
946 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
947 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
948 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
950 template<typename _Tp1
>
952 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
954 _M_ptr
= __r
.lock().get();
955 _M_refcount
= __r
._M_refcount
;
959 template<typename _Tp1
>
961 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
964 _M_refcount
= __r
._M_refcount
;
968 __shared_ptr
<_Tp
, _Lp
>
969 lock() const // never throws
972 // Optimization: avoid throw overhead.
974 return __shared_ptr
<element_type
, _Lp
>();
978 return __shared_ptr
<element_type
, _Lp
>(*this);
980 __catch(const bad_weak_ptr
&)
982 // Q: How can we get here?
983 // A: Another thread may have invalidated r after the
984 // use_count test above.
985 return __shared_ptr
<element_type
, _Lp
>();
989 // Optimization: avoid try/catch overhead when single threaded.
990 return expired() ? __shared_ptr
<element_type
, _Lp
>()
991 : __shared_ptr
<element_type
, _Lp
>(*this);
997 use_count() const // never throws
998 { return _M_refcount
._M_get_use_count(); }
1001 expired() const // never throws
1002 { return _M_refcount
._M_get_use_count() == 0; }
1004 template<typename _Tp1
>
1006 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1007 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1009 template<typename _Tp1
>
1011 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1012 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1015 reset() // never throws
1016 { __weak_ptr().swap(*this); }
1019 swap(__weak_ptr
& __s
) // never throws
1021 std::swap(_M_ptr
, __s
._M_ptr
);
1022 _M_refcount
._M_swap(__s
._M_refcount
);
1026 // Used by __enable_shared_from_this.
1028 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1031 _M_refcount
= __refcount
;
1034 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1035 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1036 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1037 friend class enable_shared_from_this
<_Tp
>;
1039 _Tp
* _M_ptr
; // Contained pointer.
1040 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1043 // 20.8.13.3.7 weak_ptr specialized algorithms.
1044 template<typename _Tp
, _Lock_policy _Lp
>
1046 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1049 template<typename _Tp
, typename _Tp1
>
1050 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1053 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1054 { return __lhs
.owner_before(__rhs
); }
1057 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1058 { return __lhs
.owner_before(__rhs
); }
1061 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1062 { return __lhs
.owner_before(__rhs
); }
1065 template<typename _Tp
, _Lock_policy _Lp
>
1066 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1067 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1070 template<typename _Tp
, _Lock_policy _Lp
>
1071 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1072 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1076 template<typename _Tp
, _Lock_policy _Lp
>
1077 class __enable_shared_from_this
1080 __enable_shared_from_this() { }
1082 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1084 __enable_shared_from_this
&
1085 operator=(const __enable_shared_from_this
&)
1088 ~__enable_shared_from_this() { }
1091 __shared_ptr
<_Tp
, _Lp
>
1093 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1095 __shared_ptr
<const _Tp
, _Lp
>
1096 shared_from_this() const
1097 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1100 template<typename _Tp1
>
1102 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1103 { _M_weak_this
._M_assign(__p
, __n
); }
1105 template<typename _Tp1
>
1107 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1108 const __enable_shared_from_this
* __pe
,
1112 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1115 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1119 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1120 inline __shared_ptr
<_Tp
, _Lp
>
1121 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1123 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1124 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1127 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1128 inline __shared_ptr
<_Tp
, _Lp
>
1129 __make_shared(_Args
&&... __args
)
1131 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1132 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1133 std::forward
<_Args
>(__args
)...);
1136 _GLIBCXX_END_NAMESPACE
1138 #endif // _SHARED_PTR_BASE_H