1 // <tr1/boost_shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 boost_memory.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
54 #ifndef _BOOST_SHARED_PTR_H
55 #define _BOOST_SHARED_PTR_H 1
63 class bad_weak_ptr
: public std::exception
67 virtual char const* what() const throw()
69 return "tr1::bad_weak_ptr";
73 // Helper for exception objects in <tr1/memory>
74 // TODO this should be defined in a different file.
76 __throw_bad_weak_ptr()
86 template <typename _Tp
>
89 typedef void result_type
;
90 typedef _Tp
* argument_type
;
93 operator()(_Tp
* p
) const
98 class _Sp_counted_base
103 : _M_use_count(1), _M_weak_count(1)
107 ~_Sp_counted_base() // nothrow
110 // dispose() is called when _M_use_count drops to zero, to release
111 // the resources managed by *this.
113 dispose() = 0; // nothrow
115 // destroy() is called when _M_weak_count drops to zero.
123 get_deleter(const std::type_info
&) = 0;
128 __gnu_cxx::__atomic_add(&_M_use_count
, 1);
134 if (_M_use_count
<= 0) // TODO not yet MT safe XXX
136 __throw_bad_weak_ptr();
138 __gnu_cxx::__atomic_add(&_M_use_count
, 1);
144 if (__gnu_cxx::__exchange_and_add(&_M_use_count
, -1) <= 1)
152 weak_add_ref() // nothrow
154 __gnu_cxx::__atomic_add(&_M_weak_count
, 1);
158 weak_release() // nothrow
160 if (__gnu_cxx::__exchange_and_add(&_M_weak_count
, -1) <= 1)
167 use_count() const // nothrow
169 return _M_use_count
; // XXX is this MT safe?
174 _Sp_counted_base(_Sp_counted_base
const&);
175 _Sp_counted_base
& operator= (_Sp_counted_base
const&);
177 _Atomic_word _M_use_count
; // #shared
178 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
181 template <typename _Ptr
, typename _Deleter
>
182 class _Sp_counted_base_impl
: public _Sp_counted_base
188 * @pre d(p) must not throw.
190 _Sp_counted_base_impl(_Ptr __p
, _Deleter __d
)
191 : _M_ptr(__p
), _M_del(__d
)
201 get_deleter(const std::type_info
& __ti
)
203 return __ti
== typeid(_Deleter
) ? &_M_del
: 0;
207 _Sp_counted_base_impl(const _Sp_counted_base_impl
&);
208 _Sp_counted_base_impl
& operator=(const _Sp_counted_base_impl
&);
210 _Ptr _M_ptr
; // copy constructor must not throw
211 _Deleter _M_del
; // copy constructor must not throw
220 _Sp_counted_base
* _M_pi
;
222 friend class weak_count
;
227 : _M_pi(0) // nothrow
230 template <typename _Ptr
, typename _Deleter
>
231 shared_count(_Ptr __p
, _Deleter __d
)
236 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Deleter
>(__p
, __d
);
240 __d(__p
); // delete __p
241 __throw_exception_again
;
245 // auto_ptr<_Tp> is special cased to provide the strong guarantee
247 template <typename _Tp
>
248 explicit shared_count(std::auto_ptr
<_Tp
>& __r
)
249 : _M_pi(new _Sp_counted_base_impl
<_Tp
*,_Sp_deleter
<_Tp
> >(
250 __r
.get(), _Sp_deleter
<_Tp
>()
254 // throws bad_weak_ptr when __r.use_count() == 0
255 explicit shared_count(const weak_count
& __r
);
257 ~shared_count() // nothrow
263 shared_count(const shared_count
& __r
)
264 : _M_pi(__r
._M_pi
) // nothrow
267 _M_pi
->add_ref_copy();
271 operator=(const shared_count
& __r
) // nothrow
273 _Sp_counted_base
* __tmp
= __r
._M_pi
;
278 __tmp
->add_ref_copy();
286 void swap(shared_count
& __r
) // nothrow
288 _Sp_counted_base
* __tmp
= __r
._M_pi
;
294 use_count() const // nothrow
295 { return _M_pi
!= 0 ? _M_pi
->use_count() : 0; }
298 unique() const // nothrow
299 { return this->use_count() == 1; }
302 operator==(const shared_count
& __a
, const shared_count
& __b
)
303 { return __a
._M_pi
== __b
._M_pi
; }
306 operator<(const shared_count
& __a
, const shared_count
& __b
)
307 { return std::less
<_Sp_counted_base
*>()(__a
._M_pi
, __b
._M_pi
); }
310 get_deleter(const std::type_info
& __ti
) const
311 { return _M_pi
? _M_pi
->get_deleter(__ti
) : 0; }
319 _Sp_counted_base
* _M_pi
;
321 friend class shared_count
;
326 : _M_pi(0) // nothrow
329 weak_count(const shared_count
& __r
)
330 : _M_pi(__r
._M_pi
) // nothrow
333 _M_pi
->weak_add_ref();
336 weak_count(const weak_count
& __r
)
337 : _M_pi(__r
._M_pi
) // nothrow
340 _M_pi
->weak_add_ref();
343 ~weak_count() // nothrow
346 _M_pi
->weak_release();
350 operator=(const shared_count
& __r
) // nothrow
352 _Sp_counted_base
* __tmp
= __r
._M_pi
;
354 __tmp
->weak_add_ref();
356 _M_pi
->weak_release();
363 operator=(const weak_count
& __r
) // nothrow
365 _Sp_counted_base
* __tmp
= __r
._M_pi
;
367 __tmp
->weak_add_ref();
369 _M_pi
->weak_release();
376 swap(weak_count
& __r
) // nothrow
378 _Sp_counted_base
* __tmp
= __r
._M_pi
;
384 use_count() const // nothrow
385 { return _M_pi
!= 0 ? _M_pi
->use_count() : 0; }
388 operator==(const weak_count
& __a
, const weak_count
& __b
)
389 { return __a
._M_pi
== __b
._M_pi
; }
392 operator<(const weak_count
& __a
, const weak_count
& __b
)
393 { return std::less
<_Sp_counted_base
*>()(__a
._M_pi
, __b
._M_pi
); }
397 shared_count::shared_count(const weak_count
& __r
)
402 _M_pi
->add_ref_lock();
406 __throw_bad_weak_ptr();
411 template <typename _Tp
> class weak_ptr
;
412 template <typename _Tp
> class enable_shared_from_this
;
414 struct __static_cast_tag
{};
415 struct __const_cast_tag
{};
416 struct __dynamic_cast_tag
{};
417 struct __polymorphic_cast_tag
{};
419 template<class _Tp
> struct shared_ptr_traits
421 typedef _Tp
& reference
;
424 template<> struct shared_ptr_traits
<void>
426 typedef void reference
;
429 template<> struct shared_ptr_traits
<void const>
431 typedef void reference
;
434 template<> struct shared_ptr_traits
<void volatile>
436 typedef void reference
;
439 template<> struct shared_ptr_traits
<void const volatile>
441 typedef void reference
;
445 // enable_shared_from_this support
447 // friend of enable_shared_from_this
448 template <typename _Tp1
, typename _Tp2
>
450 __enable_shared_from_this( const shared_count
& __pn
,
451 const enable_shared_from_this
<_Tp1
>* __pe
,
455 __enable_shared_from_this(const shared_count
&, ...)
459 * @class shared_ptr <tr1/memory>
461 * A smart pointer with reference-counted copy semantics.
462 * The object pointed to is deleted when the last shared_ptr pointing to it
463 * is destroyed or reset.
466 template <typename _Tp
>
469 typedef typename shared_ptr_traits
<_Tp
>::reference _Reference
;
473 typedef _Tp element_type
;
475 /** @brief Construct an empty %shared_ptr.
476 * @post use_count()==0 && get()==0
478 shared_ptr() : _M_ptr(0), _M_refcount() // never throws
481 /** @brief Construct a %shared_ptr that owns the pointer @a p.
482 * @param p A pointer that is convertible to element_type*.
483 * @post use_count()==1 && get()==p
484 * @throw std::bad_alloc, in which case @c delete @a p is called.
486 template <typename _Tp1
>
487 explicit shared_ptr(_Tp1
* __p
)
488 : _M_ptr(__p
), _M_refcount(__p
, _Sp_deleter
<_Tp1
>())
490 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
491 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
493 __enable_shared_from_this( _M_refcount
, __p
, __p
);
497 // Requirements: D's copy constructor and destructor must not throw
499 // shared_ptr will release p by calling d(p)
501 /** @brief Construct a %shared_ptr that owns the pointer @a p
502 * and the deleter @a d.
503 * @param p A pointer.
504 * @param d A deleter.
505 * @post use_count()==1 && get()==p
506 * @throw std::bad_alloc, in which case @a d(p) is called.
508 template <typename _Tp1
, typename _Deleter
>
509 shared_ptr(_Tp1
* __p
, _Deleter __d
)
510 : _M_ptr(__p
), _M_refcount(__p
, __d
)
512 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
513 // TODO requires D is CopyConstructible and d(p) well-formed
515 __enable_shared_from_this( _M_refcount
, __p
, __p
);
518 // generated copy constructor, assignment, destructor are fine.
520 /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise
521 * construct a %shared_ptr that shares ownership with @a r.
522 * @param r A %shared_ptr.
523 * @post get()==r.get() && use_count()==r.use_count()
524 * @throw std::bad_alloc, in which case
526 template <typename _Tp1
>
527 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
528 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
530 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
533 /** @brief Constructs a %shared_ptr that shares ownership with @a r
534 * and stores a copy of the pointer stored in @a r.
535 * @param r A weak_ptr.
536 * @post use_count()==r.use_count()
537 * @throw bad_weak_ptr when r.expired(),
538 * in which case the constructor has no effect.
540 template <typename _Tp1
>
541 explicit shared_ptr(const weak_ptr
<_Tp1
>& __r
)
542 : _M_refcount(__r
._M_refcount
) // may throw
544 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
545 // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
551 * @post use_count()==1 and r.get()==0
553 template <typename _Tp1
>
554 explicit shared_ptr(std::auto_ptr
<_Tp1
>& __r
)
555 : _M_ptr(__r
.get()), _M_refcount()
557 // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
558 // delete r.release() well-formed
559 _Tp1
* __tmp
= __r
.get();
560 _M_refcount
= shared_count(__r
);
562 __enable_shared_from_this( _M_refcount
, __tmp
, __tmp
);
565 template <typename _Tp1
>
566 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __static_cast_tag
)
567 : _M_ptr(static_cast<element_type
*>(__r
._M_ptr
))
568 , _M_refcount(__r
._M_refcount
)
571 template <typename _Tp1
>
572 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __const_cast_tag
)
573 : _M_ptr(const_cast<element_type
*>(__r
._M_ptr
))
574 , _M_refcount(__r
._M_refcount
)
577 template <typename _Tp1
>
578 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __dynamic_cast_tag
)
579 : _M_ptr(dynamic_cast<element_type
*>(__r
._M_ptr
))
580 , _M_refcount(__r
._M_refcount
)
582 if (_M_ptr
== 0) // need to allocate new counter -- the cast failed
584 _M_refcount
= shared_count();
588 template <typename _Tp1
>
590 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
593 _M_refcount
= __r
._M_refcount
; // shared_count::op= doesn't throw
597 template <typename _Tp1
>
599 operator=(std::auto_ptr
<_Tp1
>& __r
)
601 shared_ptr(__r
).swap(*this);
606 reset() // never throws
607 { shared_ptr().swap(*this); }
609 template <typename _Tp1
>
611 reset(_Tp1
* __p
) // _Tp1 must be complete
613 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
); // catch self-reset errors
614 shared_ptr(__p
).swap(*this);
617 template <typename _Tp1
, typename _Deleter
>
619 reset(_Tp1
* __p
, _Deleter __d
)
620 { shared_ptr(__p
, __d
).swap(*this); }
622 // error to instantiate if _Tp is [cv-qual] void
624 operator*() const // never throws
626 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
631 operator->() const // never throws
633 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
638 get() const // never throws
641 // implicit conversion to "bool"
643 typedef _Tp
* shared_ptr::*__unspecified_bool_type
;
646 operator __unspecified_bool_type() const // never throws
647 { return _M_ptr
== 0 ? 0 : &shared_ptr::_M_ptr
; }
650 unique() const // never throws
651 { return _M_refcount
.unique(); }
654 use_count() const // never throws
655 { return _M_refcount
.use_count(); }
658 swap(shared_ptr
<_Tp
>& __other
) // never throws
660 std::swap(_M_ptr
, __other
._M_ptr
);
661 _M_refcount
.swap(__other
._M_refcount
);
665 template <typename _Tp1
>
667 _M_less(const shared_ptr
<_Tp1
>& __rhs
) const
668 { return _M_refcount
< __rhs
._M_refcount
; }
671 _M_get_deleter(const std::type_info
& __ti
) const
672 { return _M_refcount
.get_deleter(__ti
); }
674 template <typename _Tp1
> friend class shared_ptr
;
675 template <typename _Tp1
> friend class weak_ptr
;
677 // friends injected into enclosing namespace and found by ADL:
679 // get_deleter (experimental)
680 template <typename _Del
>
682 get_deleter(const shared_ptr
& __p
)
683 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
685 template <typename _Tp1
>
687 operator==(const shared_ptr
& __a
, const shared_ptr
<_Tp1
>& __b
)
688 { return __a
.get() == __b
.get(); }
690 template <typename _Tp1
>
692 operator!=(const shared_ptr
& __a
, const shared_ptr
<_Tp1
>& __b
)
693 { return __a
.get() != __b
.get(); }
695 template <typename _Tp1
>
697 operator<(const shared_ptr
& __a
, const shared_ptr
<_Tp1
>& __b
)
698 { return __a
._M_less(__b
); }
700 _Tp
* _M_ptr
; // contained pointer
701 shared_count _M_refcount
; // reference counter
704 // 2.2.3.9 shared_ptr casts
706 /** @warning The seemingly equivalent
707 * <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
708 * will eventually result in undefined behaviour,
709 * attempting to delete the same object twice.
711 template <typename _Tp
, typename _Tp1
>
713 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
715 return shared_ptr
<_Tp
>(__r
, __static_cast_tag());
718 /** @warning The seemingly equivalent
719 * <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
720 * will eventually result in undefined behaviour,
721 * attempting to delete the same object twice.
723 template <typename _Tp
, typename _Tp1
>
725 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
727 return shared_ptr
<_Tp
>(__r
, __const_cast_tag());
730 /** @warning The seemingly equivalent
731 * <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
732 * will eventually result in undefined behaviour,
733 * attempting to delete the same object twice.
735 template <typename _Tp
, typename _Tp1
>
737 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
739 return shared_ptr
<_Tp
>(__r
, __dynamic_cast_tag());
743 template <typename _Ch
, typename _Tr
, typename _Tp
>
744 std::basic_ostream
<_Ch
,_Tr
>&
745 operator<<(std::basic_ostream
<_Ch
,_Tr
>& __os
, const shared_ptr
<_Tp
>& __p
)
752 template <typename _Tp
>
757 typedef _Tp element_type
;
760 : _M_ptr(0), _M_refcount() // never throws
763 // generated copy constructor, assignment, destructor are fine
766 // The "obvious" converting constructor implementation:
769 // weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
773 // has a serious problem.
775 // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
776 // conversion may require access to *r._M_ptr (virtual inheritance).
778 // It is not possible to avoid spurious access violations since
779 // in multithreaded programs r._M_ptr may be invalidated at any point.
782 template <typename _Tp1
>
783 weak_ptr(const weak_ptr
<_Tp1
>& r
)
784 : _M_refcount(r
._M_refcount
) // never throws
786 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
787 _M_ptr
= r
.lock().get();
790 template <typename _Tp1
>
791 weak_ptr(const shared_ptr
<_Tp1
>& r
)
792 : _M_ptr(r
._M_ptr
), _M_refcount(r
._M_refcount
) // never throws
794 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
797 template <typename _Tp1
>
799 operator=(const weak_ptr
<_Tp1
>& r
) // never throws
801 _M_ptr
= r
.lock().get();
802 _M_refcount
= r
._M_refcount
;
806 template <typename _Tp1
>
808 operator=(const shared_ptr
<_Tp1
>& r
) // never throws
811 _M_refcount
= r
._M_refcount
;
816 lock() const // never throws
820 // optimization: avoid throw overhead
823 return shared_ptr
<element_type
>();
828 return shared_ptr
<element_type
>(*this);
830 catch (const bad_weak_ptr
&)
832 // Q: how can we get here?
833 // A: another thread may have invalidated r after the use_count test above.
834 return shared_ptr
<element_type
>();
839 // optimization: avoid try/catch overhead when single threaded
840 return expired() ? shared_ptr
<element_type
>() : shared_ptr
<element_type
>(*this);
847 use_count() const // never throws
848 { return _M_refcount
.use_count(); }
851 expired() const // never throws
852 { return _M_refcount
.use_count() == 0; }
855 reset() // never throws
856 { weak_ptr().swap(*this); }
859 swap(weak_ptr
& __s
) // never throws
861 std::swap(_M_ptr
, __s
._M_ptr
);
862 _M_refcount
.swap(__s
._M_refcount
);
867 template <typename _Tp1
>
869 _M_less(const weak_ptr
<_Tp1
>& __rhs
) const
870 { return _M_refcount
< __rhs
._M_refcount
; }
872 // used by __enable_shared_from_this
874 _M_assign(_Tp
* __ptr
, const shared_count
& __refcount
)
877 _M_refcount
= __refcount
;
880 // friend injected into namespace and found by ADL
882 template <typename _Tp1
>
884 operator<(const weak_ptr
& __lhs
, const weak_ptr
<_Tp1
>& __rhs
)
885 { return __lhs
._M_less(__rhs
); }
887 template <typename _Tp1
> friend class weak_ptr
;
888 template <typename _Tp1
> friend class shared_ptr
;
889 friend class enable_shared_from_this
<_Tp
>;
891 _Tp
* _M_ptr
; // contained pointer
892 weak_count _M_refcount
; // reference counter
898 template <typename _Tp
>
899 class enable_shared_from_this
903 enable_shared_from_this()
906 enable_shared_from_this(const enable_shared_from_this
&)
909 enable_shared_from_this
&
910 operator=(const enable_shared_from_this
&)
913 ~enable_shared_from_this()
921 shared_ptr
<_Tp
> p(this->_M_weak_this
);
925 shared_ptr
<const _Tp
>
926 shared_from_this() const
928 shared_ptr
<const _Tp
> p(this->_M_weak_this
);
933 template <typename _Tp1
>
935 _M_weak_assign(_Tp1
* __p
, const shared_count
& __n
) const
936 { _M_weak_this
._M_assign(__p
, __n
); }
938 template <typename _Tp1
>
940 __enable_shared_from_this( const shared_count
& __pn
, const enable_shared_from_this
* __pe
, const _Tp1
* __px
)
943 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
946 mutable weak_ptr
<_Tp
> _M_weak_this
;
952 * @brief std::swap() specialisation for shared_ptr.
953 * @relates shared_ptr.
955 template <typename _Tp
>
957 swap(tr1::shared_ptr
<_Tp
>& __a
, tr1::shared_ptr
<_Tp
>& __b
)
963 * @brief std::swap() specialisation for weak_ptr.
966 template <typename _Tp
>
968 swap(tr1::weak_ptr
<_Tp
>& __a
, tr1::weak_ptr
<_Tp
>& __b
)