1 // <bits/shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007, 2008 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
64 // counted ptr with no deleter or allocator support
65 template<typename _Ptr
, _Lock_policy _Lp
>
67 : public _Sp_counted_base
<_Lp
>
70 _Sp_counted_ptr(_Ptr __p
)
74 _M_dispose() // nothrow
78 _M_destroy() // nothrow
82 _M_get_deleter(const std::type_info
& __ti
)
86 _Sp_counted_ptr(const _Sp_counted_ptr
&);
87 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&);
90 _Ptr _M_ptr
; // copy constructor must not throw
93 // support for custom deleter and/or allocator
94 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
95 class _Sp_counted_deleter
96 : public _Sp_counted_ptr
<_Ptr
, _Lp
>
98 typedef typename
_Alloc::template
99 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
101 // Helper class that stores the Deleter and also acts as an allocator.
102 // Used to dispose of the owned pointer and the internal refcount
103 // Requires that copies of _Alloc can free each other's memory.
105 : public _My_alloc_type
// copy constructor must not throw
107 _Deleter _M_del
; // copy constructor must not throw
108 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
109 : _My_alloc_type(__a
), _M_del(__d
) { }
113 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
118 * @pre __d(__p) must not throw.
120 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
121 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
125 * @pre __d(__p) must not throw.
127 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
128 : _Base_type(__p
), _M_del(__d
, __a
) { }
131 _M_dispose() // nothrow
132 { _M_del
._M_del(_Base_type::_M_ptr
); }
135 _M_destroy() // nothrow
137 _My_alloc_type
__a(_M_del
);
138 this->~_Sp_counted_deleter();
139 __a
.deallocate(this, 1);
143 _M_get_deleter(const std::type_info
& __ti
)
144 { return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0; }
147 _Sp_counted_deleter(const _Sp_counted_deleter
&);
148 _Sp_counted_deleter
& operator=(const _Sp_counted_deleter
&);
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 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
370 { return __a
._M_pi
== __b
._M_pi
; }
373 operator<(const __shared_count
& __a
, const __shared_count
& __b
)
374 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
377 _M_get_deleter(const std::type_info
& __ti
) const
378 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
381 friend class __weak_count
<_Lp
>;
383 template<typename _Tp
, typename _Del
>
384 static _Sp_counted_base
<_Lp
>*
385 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
386 typename
std::enable_if
<!std::is_reference
<_Del
>::value
>::type
* = 0)
388 return new _Sp_counted_deleter
<_Tp
*, _Del
, std::allocator
<_Tp
>,
389 _Lp
>(__r
.get(), __r
.get_deleter());
392 template<typename _Tp
, typename _Del
>
393 static _Sp_counted_base
<_Lp
>*
394 _S_create_from_up(std::unique_ptr
<_Tp
, _Del
>&& __r
,
395 typename
std::enable_if
<std::is_reference
<_Del
>::value
>::type
* = 0)
397 typedef typename
std::remove_reference
<_Del
>::type _Del1
;
398 typedef std::reference_wrapper
<_Del1
> _Del2
;
399 return new _Sp_counted_deleter
<_Tp
*, _Del2
, std::allocator
<_Tp
>,
400 _Lp
>(__r
.get(), std::ref(__r
.get_deleter()));
403 _Sp_counted_base
<_Lp
>* _M_pi
;
407 template<_Lock_policy _Lp
>
412 : _M_pi(0) // nothrow
415 __weak_count(const __shared_count
<_Lp
>& __r
)
416 : _M_pi(__r
._M_pi
) // nothrow
419 _M_pi
->_M_weak_add_ref();
422 __weak_count(const __weak_count
<_Lp
>& __r
)
423 : _M_pi(__r
._M_pi
) // nothrow
426 _M_pi
->_M_weak_add_ref();
429 ~__weak_count() // nothrow
432 _M_pi
->_M_weak_release();
436 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
438 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
440 __tmp
->_M_weak_add_ref();
442 _M_pi
->_M_weak_release();
448 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
450 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
452 __tmp
->_M_weak_add_ref();
454 _M_pi
->_M_weak_release();
460 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
462 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
468 _M_get_use_count() const // nothrow
469 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
472 operator==(const __weak_count
<_Lp
>& __a
, const __weak_count
<_Lp
>& __b
)
473 { return __a
._M_pi
== __b
._M_pi
; }
476 operator<(const __weak_count
<_Lp
>& __a
, const __weak_count
<_Lp
>& __b
)
477 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
480 friend class __shared_count
<_Lp
>;
482 _Sp_counted_base
<_Lp
>* _M_pi
;
485 // now that __weak_count is defined we can define this constructor:
486 template<_Lock_policy _Lp
>
488 __shared_count
<_Lp
>::
489 __shared_count(const __weak_count
<_Lp
>& __r
)
493 _M_pi
->_M_add_ref_lock();
495 __throw_bad_weak_ptr();
498 // Forward declarations.
499 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
502 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
505 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
506 class __enable_shared_from_this
;
508 template<typename _Tp
>
511 template<typename _Tp
>
514 template<typename _Tp
>
515 class enable_shared_from_this
;
517 // Support for enable_shared_from_this.
519 // Friend of __enable_shared_from_this.
520 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
522 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
523 const __enable_shared_from_this
<_Tp1
,
526 // Friend of enable_shared_from_this.
527 template<typename _Tp1
, typename _Tp2
>
529 __enable_shared_from_this_helper(const __shared_count
<>&,
530 const enable_shared_from_this
<_Tp1
>*,
533 template<_Lock_policy _Lp
>
535 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
540 * @class __shared_ptr
542 * A smart pointer with reference-counted copy semantics.
543 * The object pointed to is deleted when the last shared_ptr pointing to
544 * it is destroyed or reset.
546 template<typename _Tp
, _Lock_policy _Lp
>
550 typedef _Tp element_type
;
552 /** @brief Construct an empty %__shared_ptr.
553 * @post use_count()==0 && get()==0
556 : _M_ptr(0), _M_refcount() // never throws
559 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
560 * @param __p A pointer that is convertible to element_type*.
561 * @post use_count() == 1 && get() == __p
562 * @throw std::bad_alloc, in which case @c delete @a __p is called.
564 template<typename _Tp1
>
566 __shared_ptr(_Tp1
* __p
)
567 : _M_ptr(__p
), _M_refcount(__p
)
569 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
570 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
571 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
575 // Requirements: _Deleter's copy constructor and destructor must
578 // __shared_ptr will release __p by calling __d(__p)
580 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
581 * and the deleter @a __d.
582 * @param __p A pointer.
583 * @param __d A deleter.
584 * @post use_count() == 1 && get() == __p
585 * @throw std::bad_alloc, in which case @a __d(__p) is called.
587 template<typename _Tp1
, typename _Deleter
>
588 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
589 : _M_ptr(__p
), _M_refcount(__p
, __d
)
591 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
592 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
593 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
597 // Requirements: _Deleter's copy constructor and destructor must
598 // not throw _Alloc's copy constructor and destructor must not
601 // __shared_ptr will release __p by calling __d(__p)
603 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
604 * and the deleter @a __d.
605 * @param __p A pointer.
606 * @param __d A deleter.
607 * @param __a An allocator.
608 * @post use_count() == 1 && get() == __p
609 * @throw std::bad_alloc, in which case @a __d(__p) is called.
611 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
612 __shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
613 : _M_ptr(__p
), _M_refcount(__p
, __d
, __a
)
615 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
616 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
617 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
620 /** @brief Constructs a %__shared_ptr instance that stores @a __p
621 * and shares ownership with @a __r.
622 * @param __r A %__shared_ptr.
623 * @param __p A pointer that will remain valid while @a *__r is valid.
624 * @post get() == __p && use_count() == __r.use_count()
626 * This can be used to construct a @c shared_ptr to a sub-object
627 * of an object managed by an existing @c shared_ptr.
630 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
631 * shared_ptr<int> pi(pii, &pii->first);
632 * assert(pii.use_count() == 2);
635 template<typename _Tp1
>
636 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
)
637 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
640 // generated copy constructor, assignment, destructor are fine.
642 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
643 * otherwise construct a %__shared_ptr that shares ownership
645 * @param __r A %__shared_ptr.
646 * @post get() == __r.get() && use_count() == __r.use_count()
648 template<typename _Tp1
>
649 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
650 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
651 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
653 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
654 * @param __r A %__shared_ptr rvalue.
655 * @post *this contains the old value of @a __r, @a __r is empty.
657 __shared_ptr(__shared_ptr
&& __r
)
658 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
660 _M_refcount
._M_swap(__r
._M_refcount
);
664 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
665 * @param __r A %__shared_ptr rvalue.
666 * @post *this contains the old value of @a __r, @a __r is empty.
668 template<typename _Tp1
>
669 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
670 : _M_ptr(__r
._M_ptr
), _M_refcount() // never throws
672 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
673 _M_refcount
._M_swap(__r
._M_refcount
);
677 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
678 * and stores a copy of the pointer stored in @a __r.
679 * @param __r A weak_ptr.
680 * @post use_count() == __r.use_count()
681 * @throw bad_weak_ptr when __r.expired(),
682 * in which case the constructor has no effect.
684 template<typename _Tp1
>
686 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
687 : _M_refcount(__r
._M_refcount
) // may throw
689 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
690 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
695 template<typename _Tp1
, typename _Del
>
697 __shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
700 * If an exception is thrown this constructor has no effect.
702 template<typename _Tp1
, typename _Del
>
704 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
705 : _M_ptr(__r
.get()), _M_refcount()
707 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
708 _Tp1
* __tmp
= __r
.get();
709 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
710 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
713 #if _GLIBCXX_DEPRECATED
715 * @post use_count() == 1 and __r.get() == 0
717 template<typename _Tp1
>
719 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
720 : _M_ptr(__r
.get()), _M_refcount()
722 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
723 // TODO requires _Tp1 is complete, delete __r.release() well-formed
724 _Tp1
* __tmp
= __r
.get();
725 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
726 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
730 template<typename _Tp1
>
732 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
735 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
739 #if _GLIBCXX_DEPRECATED
740 template<typename _Tp1
>
742 operator=(std::auto_ptr
<_Tp1
>&& __r
)
744 __shared_ptr(std::move(__r
)).swap(*this);
750 operator=(__shared_ptr
&& __r
)
752 __shared_ptr(std::move(__r
)).swap(*this);
758 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
)
760 __shared_ptr(std::move(__r
)).swap(*this);
764 template<typename _Tp1
, typename _Del
>
766 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
768 template<typename _Tp1
, typename _Del
>
770 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
772 __shared_ptr(std::move(__r
)).swap(*this);
777 reset() // never throws
778 { __shared_ptr().swap(*this); }
780 template<typename _Tp1
>
782 reset(_Tp1
* __p
) // _Tp1 must be complete.
784 // Catch self-reset errors.
785 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
786 __shared_ptr(__p
).swap(*this);
789 template<typename _Tp1
, typename _Deleter
>
791 reset(_Tp1
* __p
, _Deleter __d
)
792 { __shared_ptr(__p
, __d
).swap(*this); }
794 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
796 reset(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
797 { __shared_ptr(__p
, __d
, __a
).swap(*this); }
799 // Allow class instantiation when _Tp is [cv-qual] void.
800 typename
std::add_lvalue_reference
<_Tp
>::type
801 operator*() const // never throws
803 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
808 operator->() const // never throws
810 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
815 get() const // never throws
818 // Implicit conversion to "bool"
820 typedef _Tp
* __shared_ptr::*__unspecified_bool_type
;
823 operator __unspecified_bool_type() const // never throws
824 { return _M_ptr
== 0 ? 0 : &__shared_ptr::_M_ptr
; }
827 unique() const // never throws
828 { return _M_refcount
._M_unique(); }
831 use_count() const // never throws
832 { return _M_refcount
._M_get_use_count(); }
835 swap(__shared_ptr
<_Tp
, _Lp
>&& __other
) // never throws
837 std::swap(_M_ptr
, __other
._M_ptr
);
838 _M_refcount
._M_swap(__other
._M_refcount
);
842 // This constructor is non-standard, it is used by allocate_shared.
843 template<typename _Alloc
, typename
... _Args
>
844 __shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
845 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
846 std::forward
<_Args
>(__args
)...)
848 // _M_ptr needs to point to the newly constructed object.
849 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
850 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
851 _M_ptr
= static_cast<_Tp
*>(__p
);
852 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
855 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
857 friend __shared_ptr
<_Tp1
, _Lp1
>
858 __allocate_shared(_Alloc __a
, _Args
&&... __args
);
862 _M_get_deleter(const std::type_info
& __ti
) const
863 { return _M_refcount
._M_get_deleter(__ti
); }
865 template<typename _Tp1
, _Lock_policy _Lp1
>
867 _M_less(const __shared_ptr
<_Tp1
, _Lp1
>& __rhs
) const
868 { return _M_refcount
< __rhs
._M_refcount
; }
870 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
871 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
873 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
874 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
876 // Friends injected into enclosing namespace and found by ADL:
877 template<typename _Tp1
>
879 operator==(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
880 { return __a
.get() == __b
.get(); }
882 template<typename _Tp1
>
884 operator!=(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
885 { return __a
.get() != __b
.get(); }
887 template<typename _Tp1
>
889 operator<(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
890 { return __a
._M_less(__b
); }
892 _Tp
* _M_ptr
; // Contained pointer.
893 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
896 // 2.2.3.8 shared_ptr specialized algorithms.
897 template<typename _Tp
, _Lock_policy _Lp
>
899 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
902 template<typename _Tp
, _Lock_policy _Lp
>
904 swap(__shared_ptr
<_Tp
, _Lp
>&& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
907 template<typename _Tp
, _Lock_policy _Lp
>
909 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>&& __b
)
912 // 2.2.3.9 shared_ptr casts
913 /** @warning The seemingly equivalent
914 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
915 * will eventually result in undefined behaviour,
916 * attempting to delete the same object twice.
918 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
919 inline __shared_ptr
<_Tp
, _Lp
>
920 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
921 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
923 /** @warning The seemingly equivalent
924 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
925 * will eventually result in undefined behaviour,
926 * attempting to delete the same object twice.
928 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
929 inline __shared_ptr
<_Tp
, _Lp
>
930 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
931 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
933 /** @warning The seemingly equivalent
934 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
935 * will eventually result in undefined behaviour,
936 * attempting to delete the same object twice.
938 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
939 inline __shared_ptr
<_Tp
, _Lp
>
940 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
942 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
943 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
944 return __shared_ptr
<_Tp
, _Lp
>();
947 // 2.2.3.7 shared_ptr I/O
948 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
949 std::basic_ostream
<_Ch
, _Tr
>&
950 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
951 const __shared_ptr
<_Tp
, _Lp
>& __p
)
957 // 2.2.3.10 shared_ptr get_deleter (experimental)
958 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
960 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
961 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
964 template<typename _Tp
, _Lock_policy _Lp
>
968 typedef _Tp element_type
;
971 : _M_ptr(0), _M_refcount() // never throws
974 // Generated copy constructor, assignment, destructor are fine.
976 // The "obvious" converting constructor implementation:
978 // template<typename _Tp1>
979 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
980 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
983 // has a serious problem.
985 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
986 // conversion may require access to *__r._M_ptr (virtual inheritance).
988 // It is not possible to avoid spurious access violations since
989 // in multithreaded programs __r._M_ptr may be invalidated at any point.
990 template<typename _Tp1
>
991 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
992 : _M_refcount(__r
._M_refcount
) // never throws
994 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
995 _M_ptr
= __r
.lock().get();
998 template<typename _Tp1
>
999 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
1000 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
1001 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
1003 template<typename _Tp1
>
1005 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1007 _M_ptr
= __r
.lock().get();
1008 _M_refcount
= __r
._M_refcount
;
1012 template<typename _Tp1
>
1014 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
1016 _M_ptr
= __r
._M_ptr
;
1017 _M_refcount
= __r
._M_refcount
;
1021 __shared_ptr
<_Tp
, _Lp
>
1022 lock() const // never throws
1025 // Optimization: avoid throw overhead.
1027 return __shared_ptr
<element_type
, _Lp
>();
1031 return __shared_ptr
<element_type
, _Lp
>(*this);
1033 catch(const bad_weak_ptr
&)
1035 // Q: How can we get here?
1036 // A: Another thread may have invalidated r after the
1037 // use_count test above.
1038 return __shared_ptr
<element_type
, _Lp
>();
1042 // Optimization: avoid try/catch overhead when single threaded.
1043 return expired() ? __shared_ptr
<element_type
, _Lp
>()
1044 : __shared_ptr
<element_type
, _Lp
>(*this);
1050 use_count() const // never throws
1051 { return _M_refcount
._M_get_use_count(); }
1054 expired() const // never throws
1055 { return _M_refcount
._M_get_use_count() == 0; }
1058 reset() // never throws
1059 { __weak_ptr().swap(*this); }
1062 swap(__weak_ptr
& __s
) // never throws
1064 std::swap(_M_ptr
, __s
._M_ptr
);
1065 _M_refcount
._M_swap(__s
._M_refcount
);
1069 // Used by __enable_shared_from_this.
1071 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
1074 _M_refcount
= __refcount
;
1077 template<typename _Tp1
>
1079 _M_less(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1080 { return _M_refcount
< __rhs
._M_refcount
; }
1082 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1083 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1084 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1085 friend class enable_shared_from_this
<_Tp
>;
1087 // Friend injected into namespace and found by ADL.
1088 template<typename _Tp1
>
1090 operator<(const __weak_ptr
& __lhs
, const __weak_ptr
<_Tp1
, _Lp
>& __rhs
)
1091 { return __lhs
._M_less(__rhs
); }
1093 _Tp
* _M_ptr
; // Contained pointer.
1094 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1097 // 2.2.4.7 weak_ptr specialized algorithms.
1098 template<typename _Tp
, _Lock_policy _Lp
>
1100 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
1104 template<typename _Tp
, _Lock_policy _Lp
>
1105 class __enable_shared_from_this
1108 __enable_shared_from_this() { }
1110 __enable_shared_from_this(const __enable_shared_from_this
&) { }
1112 __enable_shared_from_this
&
1113 operator=(const __enable_shared_from_this
&)
1116 ~__enable_shared_from_this() { }
1119 __shared_ptr
<_Tp
, _Lp
>
1121 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1123 __shared_ptr
<const _Tp
, _Lp
>
1124 shared_from_this() const
1125 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1128 template<typename _Tp1
>
1130 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
1131 { _M_weak_this
._M_assign(__p
, __n
); }
1133 template<typename _Tp1
>
1135 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1136 const __enable_shared_from_this
* __pe
,
1140 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1143 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1148 // The actual shared_ptr, with forwarding constructors and
1149 // assignment operators.
1150 template<typename _Tp
>
1152 : public __shared_ptr
<_Tp
>
1156 : __shared_ptr
<_Tp
>() { }
1158 template<typename _Tp1
>
1160 shared_ptr(_Tp1
* __p
)
1161 : __shared_ptr
<_Tp
>(__p
) { }
1163 template<typename _Tp1
, typename _Deleter
>
1164 shared_ptr(_Tp1
* __p
, _Deleter __d
)
1165 : __shared_ptr
<_Tp
>(__p
, __d
) { }
1167 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
1168 shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
1169 : __shared_ptr
<_Tp
>(__p
, __d
, __a
) { }
1171 // Aliasing constructor
1172 template<typename _Tp1
>
1173 shared_ptr(const shared_ptr
<_Tp1
>& __r
, _Tp
* __p
)
1174 : __shared_ptr
<_Tp
>(__r
, __p
) { }
1176 template<typename _Tp1
>
1177 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
1178 : __shared_ptr
<_Tp
>(__r
) { }
1180 shared_ptr(shared_ptr
&& __r
)
1181 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1183 template<typename _Tp1
>
1184 shared_ptr(shared_ptr
<_Tp1
>&& __r
)
1185 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1187 template<typename _Tp1
>
1189 shared_ptr(const weak_ptr
<_Tp1
>& __r
)
1190 : __shared_ptr
<_Tp
>(__r
) { }
1192 #if _GLIBCXX_DEPRECATED
1193 template<typename _Tp1
>
1195 shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
1196 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1199 template<typename _Tp1
, typename _Del
>
1201 shared_ptr(const std::unique_ptr
<_Tp1
, _Del
>&) = delete;
1203 template<typename _Tp1
, typename _Del
>
1205 shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1206 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
1208 template<typename _Tp1
>
1210 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1212 this->__shared_ptr
<_Tp
>::operator=(__r
);
1216 #if _GLIBCXX_DEPRECATED
1217 template<typename _Tp1
>
1219 operator=(std::auto_ptr
<_Tp1
>&& __r
)
1221 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1227 operator=(shared_ptr
&& __r
)
1229 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1233 template<class _Tp1
>
1235 operator=(shared_ptr
<_Tp1
>&& __r
)
1237 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1241 template<typename _Tp1
, typename _Del
>
1243 operator=(const std::unique_ptr
<_Tp1
, _Del
>& __r
) = delete;
1245 template<typename _Tp1
, typename _Del
>
1247 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1249 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
1254 // This constructor is non-standard, it is used by allocate_shared.
1255 template<typename _Alloc
, typename
... _Args
>
1256 shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
1257 : __shared_ptr
<_Tp
>(__tag
, __a
, std::forward
<_Args
>(__args
)...)
1260 template<typename _Tp1
, typename _Alloc
, typename
... _Args
>
1261 friend shared_ptr
<_Tp1
>
1262 allocate_shared(_Alloc __a
, _Args
&&... __args
);
1265 // 20.7.12.2.9 shared_ptr specialized algorithms.
1266 template<typename _Tp
>
1268 swap(__shared_ptr
<_Tp
>& __a
, __shared_ptr
<_Tp
>& __b
)
1271 template<typename _Tp
>
1273 swap(__shared_ptr
<_Tp
>&& __a
, __shared_ptr
<_Tp
>& __b
)
1276 template<typename _Tp
>
1278 swap(__shared_ptr
<_Tp
>& __a
, __shared_ptr
<_Tp
>&& __b
)
1281 template<typename _Tp
, typename _Tp1
>
1282 inline shared_ptr
<_Tp
>
1283 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1284 { return shared_ptr
<_Tp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1286 template<typename _Tp
, typename _Tp1
>
1287 inline shared_ptr
<_Tp
>
1288 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1289 { return shared_ptr
<_Tp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1291 template<typename _Tp
, typename _Tp1
>
1292 inline shared_ptr
<_Tp
>
1293 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1295 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1296 return shared_ptr
<_Tp
>(__r
, __p
);
1297 return shared_ptr
<_Tp
>();
1302 // The actual weak_ptr, with forwarding constructors and
1303 // assignment operators.
1304 template<typename _Tp
>
1306 : public __weak_ptr
<_Tp
>
1310 : __weak_ptr
<_Tp
>() { }
1312 template<typename _Tp1
>
1313 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1314 : __weak_ptr
<_Tp
>(__r
) { }
1316 template<typename _Tp1
>
1317 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1318 : __weak_ptr
<_Tp
>(__r
) { }
1320 template<typename _Tp1
>
1322 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1324 this->__weak_ptr
<_Tp
>::operator=(__r
);
1328 template<typename _Tp1
>
1330 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1332 this->__weak_ptr
<_Tp
>::operator=(__r
);
1337 lock() const // never throws
1340 if (this->expired())
1341 return shared_ptr
<_Tp
>();
1345 return shared_ptr
<_Tp
>(*this);
1347 catch(const bad_weak_ptr
&)
1349 return shared_ptr
<_Tp
>();
1352 return this->expired() ? shared_ptr
<_Tp
>()
1353 : shared_ptr
<_Tp
>(*this);
1358 /// enable_shared_from_this
1359 template<typename _Tp
>
1360 class enable_shared_from_this
1363 enable_shared_from_this() { }
1365 enable_shared_from_this(const enable_shared_from_this
&) { }
1367 enable_shared_from_this
&
1368 operator=(const enable_shared_from_this
&)
1371 ~enable_shared_from_this() { }
1376 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1378 shared_ptr
<const _Tp
>
1379 shared_from_this() const
1380 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1383 template<typename _Tp1
>
1385 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1386 { _M_weak_this
._M_assign(__p
, __n
); }
1388 template<typename _Tp1
>
1390 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1391 const enable_shared_from_this
* __pe
,
1395 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1398 mutable weak_ptr
<_Tp
> _M_weak_this
;
1401 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1402 inline __shared_ptr
<_Tp
, _Lp
>
1403 __allocate_shared(_Alloc __a
, _Args
&&... __args
)
1405 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(),
1406 std::forward
<_Alloc
>(__a
), std::forward
<_Args
>(__args
)...);
1409 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1410 inline __shared_ptr
<_Tp
, _Lp
>
1411 __make_shared(_Args
&&... __args
)
1413 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1414 return __allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1415 std::forward
<_Args
>(__args
)...);
1418 /** @brief Create an object that is owned by a shared_ptr.
1419 * @param __a An allocator.
1420 * @param __args Arguments for the @a _Tp object's constructor.
1421 * @return A shared_ptr that owns the newly created object.
1422 * @throw An exception thrown from @a _Alloc::allocate or from the
1423 * constructor of @a _Tp.
1425 * A copy of @a __a will be used to allocate memory for the shared_ptr
1426 * and the new object.
1428 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
1429 inline shared_ptr
<_Tp
>
1430 allocate_shared(_Alloc __a
, _Args
&&... __args
)
1432 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), std::forward
<_Alloc
>(__a
),
1433 std::forward
<_Args
>(__args
)...);
1436 /** @brief Create an object that is owned by a shared_ptr.
1437 * @param __args Arguments for the @a _Tp object's constructor.
1438 * @return A shared_ptr that owns the newly created object.
1439 * @throw std::bad_alloc, or an exception thrown from the
1440 * constructor of @a _Tp.
1442 template<typename _Tp
, typename
... _Args
>
1443 inline shared_ptr
<_Tp
>
1444 make_shared(_Args
&&... __args
)
1446 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1447 return allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
1448 std::forward
<_Args
>(__args
)...);