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, 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 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
69 { return "tr1::bad_weak_ptr"; }
72 // Helper for exception objects in <tr1/memory>
73 // TODO this should be defined in a different file.
75 __throw_bad_weak_ptr()
85 template<typename _Tp
>
88 typedef void result_type
;
89 typedef _Tp
* argument_type
;
92 operator()(_Tp
* p
) const
97 class _Sp_counted_base
102 : _M_use_count(1), _M_weak_count(1)
104 // For the case of __GTHREAD_MUTEX_INIT we haven't initialised
105 // the mutex yet, so do it now.
106 #if defined(__GTHREADS) && defined(__GTHREAD_MUTEX_INIT)
107 __gthread_mutex_t __tmp
= __GTHREAD_MUTEX_INIT
;
113 ~_Sp_counted_base() // nothrow
116 // dispose() is called when _M_use_count drops to zero, to release
117 // the resources managed by *this.
119 dispose() = 0; // nothrow
121 // destroy() is called when _M_weak_count drops to zero.
129 get_deleter(const std::type_info
&) = 0;
134 __gnu_cxx::__atomic_add(&_M_use_count
, 1);
140 __gnu_cxx::lock
lock(_M_mutex
);
141 if (__gnu_cxx::__exchange_and_add(&_M_use_count
, 1) == 0)
144 __throw_bad_weak_ptr();
151 if (__gnu_cxx::__exchange_and_add(&_M_use_count
, -1) == 1)
155 _GLIBCXX_READ_MEM_BARRIER
;
156 _GLIBCXX_WRITE_MEM_BARRIER
;
158 if (__gnu_cxx::__exchange_and_add(&_M_weak_count
, -1) == 1)
164 weak_add_ref() // nothrow
166 __gnu_cxx::__atomic_add(&_M_weak_count
, 1);
170 weak_release() // nothrow
172 if (__gnu_cxx::__exchange_and_add(&_M_weak_count
, -1) == 1)
175 _GLIBCXX_READ_MEM_BARRIER
;
176 _GLIBCXX_WRITE_MEM_BARRIER
;
183 use_count() const // nothrow
185 return _M_use_count
; // XXX is this MT safe?
190 _Sp_counted_base(_Sp_counted_base
const&);
191 _Sp_counted_base
& operator=(_Sp_counted_base
const&);
193 _Atomic_word _M_use_count
; // #shared
194 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
195 __gnu_cxx::mutex_type _M_mutex
;
198 template<typename _Ptr
, typename _Deleter
>
199 class _Sp_counted_base_impl
200 : public _Sp_counted_base
206 * @pre d(p) must not throw.
208 _Sp_counted_base_impl(_Ptr __p
, _Deleter __d
)
209 : _M_ptr(__p
), _M_del(__d
)
219 get_deleter(const std::type_info
& __ti
)
221 return __ti
== typeid(_Deleter
) ? &_M_del
: 0;
225 _Sp_counted_base_impl(const _Sp_counted_base_impl
&);
226 _Sp_counted_base_impl
& operator=(const _Sp_counted_base_impl
&);
228 _Ptr _M_ptr
; // copy constructor must not throw
229 _Deleter _M_del
; // copy constructor must not throw
238 _Sp_counted_base
* _M_pi
;
240 friend class weak_count
;
245 : _M_pi(0) // nothrow
248 template<typename _Ptr
, typename _Deleter
>
249 shared_count(_Ptr __p
, _Deleter __d
)
254 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Deleter
>(__p
, __d
);
258 __d(__p
); // delete __p
259 __throw_exception_again
;
263 // auto_ptr<_Tp> is special cased to provide the strong guarantee
265 template<typename _Tp
>
266 explicit shared_count(std::auto_ptr
<_Tp
>& __r
)
267 : _M_pi(new _Sp_counted_base_impl
<_Tp
*,
268 _Sp_deleter
<_Tp
> >(__r
.get(), _Sp_deleter
<_Tp
>()))
271 // throws bad_weak_ptr when __r.use_count() == 0
272 explicit shared_count(const weak_count
& __r
);
274 ~shared_count() // nothrow
280 shared_count(const shared_count
& __r
)
281 : _M_pi(__r
._M_pi
) // nothrow
284 _M_pi
->add_ref_copy();
288 operator=(const shared_count
& __r
) // nothrow
290 _Sp_counted_base
* __tmp
= __r
._M_pi
;
295 __tmp
->add_ref_copy();
303 void swap(shared_count
& __r
) // nothrow
305 _Sp_counted_base
* __tmp
= __r
._M_pi
;
311 use_count() const // nothrow
312 { return _M_pi
!= 0 ? _M_pi
->use_count() : 0; }
315 unique() const // nothrow
316 { return this->use_count() == 1; }
319 operator==(const shared_count
& __a
, const shared_count
& __b
)
320 { return __a
._M_pi
== __b
._M_pi
; }
323 operator<(const shared_count
& __a
, const shared_count
& __b
)
324 { return std::less
<_Sp_counted_base
*>()(__a
._M_pi
, __b
._M_pi
); }
327 get_deleter(const std::type_info
& __ti
) const
328 { return _M_pi
? _M_pi
->get_deleter(__ti
) : 0; }
335 _Sp_counted_base
* _M_pi
;
337 friend class shared_count
;
342 : _M_pi(0) // nothrow
345 weak_count(const shared_count
& __r
)
346 : _M_pi(__r
._M_pi
) // nothrow
349 _M_pi
->weak_add_ref();
352 weak_count(const weak_count
& __r
)
353 : _M_pi(__r
._M_pi
) // nothrow
356 _M_pi
->weak_add_ref();
359 ~weak_count() // nothrow
362 _M_pi
->weak_release();
366 operator=(const shared_count
& __r
) // nothrow
368 _Sp_counted_base
* __tmp
= __r
._M_pi
;
370 __tmp
->weak_add_ref();
372 _M_pi
->weak_release();
379 operator=(const weak_count
& __r
) // nothrow
381 _Sp_counted_base
* __tmp
= __r
._M_pi
;
383 __tmp
->weak_add_ref();
385 _M_pi
->weak_release();
392 swap(weak_count
& __r
) // nothrow
394 _Sp_counted_base
* __tmp
= __r
._M_pi
;
400 use_count() const // nothrow
401 { return _M_pi
!= 0 ? _M_pi
->use_count() : 0; }
404 operator==(const weak_count
& __a
, const weak_count
& __b
)
405 { return __a
._M_pi
== __b
._M_pi
; }
408 operator<(const weak_count
& __a
, const weak_count
& __b
)
409 { return std::less
<_Sp_counted_base
*>()(__a
._M_pi
, __b
._M_pi
); }
413 shared_count::shared_count(const weak_count
& __r
)
417 _M_pi
->add_ref_lock();
419 __throw_bad_weak_ptr();
424 template<typename _Tp
>
427 template<typename _Tp
>
430 template<typename _Tp
>
431 class enable_shared_from_this
;
433 struct __static_cast_tag
{};
434 struct __const_cast_tag
{};
435 struct __dynamic_cast_tag
{};
436 struct __polymorphic_cast_tag
{};
439 struct shared_ptr_traits
440 { typedef _Tp
& reference
; };
443 struct shared_ptr_traits
<void>
444 { typedef void reference
; };
447 struct shared_ptr_traits
<void const>
448 { typedef void reference
; };
451 struct shared_ptr_traits
<void volatile>
452 { typedef void reference
; };
455 struct shared_ptr_traits
<void const volatile>
456 { typedef void reference
; };
459 // enable_shared_from_this support
461 // friend of enable_shared_from_this
462 template<typename _Tp1
, typename _Tp2
>
464 __enable_shared_from_this(const shared_count
& __pn
,
465 const enable_shared_from_this
<_Tp1
>* __pe
,
469 __enable_shared_from_this(const shared_count
&, ...)
473 // get_deleter must be declared before friend declaration by shared_ptr.
474 template<typename _Del
, typename _Tp
>
475 _Del
* get_deleter(const shared_ptr
<_Tp
>&);
478 * @class shared_ptr <tr1/memory>
480 * A smart pointer with reference-counted copy semantics.
481 * The object pointed to is deleted when the last shared_ptr pointing to it
482 * is destroyed or reset.
484 template<typename _Tp
>
487 typedef typename shared_ptr_traits
<_Tp
>::reference _Reference
;
491 typedef _Tp element_type
;
493 /** @brief Construct an empty %shared_ptr.
494 * @post use_count()==0 && get()==0
497 : _M_ptr(0), _M_refcount() // never throws
500 /** @brief Construct a %shared_ptr that owns the pointer @a p.
501 * @param p A pointer that is convertible to element_type*.
502 * @post use_count() == 1 && get() == p
503 * @throw std::bad_alloc, in which case @c delete @a p is called.
505 template<typename _Tp1
>
506 explicit shared_ptr(_Tp1
* __p
)
507 : _M_ptr(__p
), _M_refcount(__p
, _Sp_deleter
<_Tp1
>())
509 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
510 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
512 __enable_shared_from_this( _M_refcount
, __p
, __p
);
516 // Requirements: D's copy constructor and destructor must not throw
518 // shared_ptr will release p by calling d(p)
520 /** @brief Construct a %shared_ptr that owns the pointer @a p
521 * and the deleter @a d.
522 * @param p A pointer.
523 * @param d A deleter.
524 * @post use_count() == 1 && get() == p
525 * @throw std::bad_alloc, in which case @a d(p) is called.
527 template<typename _Tp1
, typename _Deleter
>
528 shared_ptr(_Tp1
* __p
, _Deleter __d
)
529 : _M_ptr(__p
), _M_refcount(__p
, __d
)
531 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
532 // TODO requires D is CopyConstructible and d(p) well-formed
534 __enable_shared_from_this( _M_refcount
, __p
, __p
);
537 // generated copy constructor, assignment, destructor are fine.
539 /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise
540 * construct a %shared_ptr that shares ownership with @a r.
541 * @param r A %shared_ptr.
542 * @post get() == r.get() && use_count() == r.use_count()
543 * @throw std::bad_alloc, in which case
545 template<typename _Tp1
>
546 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
547 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
549 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
552 /** @brief Constructs a %shared_ptr that shares ownership with @a r
553 * and stores a copy of the pointer stored in @a r.
554 * @param r A weak_ptr.
555 * @post use_count() == r.use_count()
556 * @throw bad_weak_ptr when r.expired(),
557 * in which case the constructor has no effect.
559 template<typename _Tp1
>
560 explicit shared_ptr(const weak_ptr
<_Tp1
>& __r
)
561 : _M_refcount(__r
._M_refcount
) // may throw
563 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
564 // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
570 * @post use_count() == 1 and r.get() == 0
572 template<typename _Tp1
>
573 explicit shared_ptr(std::auto_ptr
<_Tp1
>& __r
)
574 : _M_ptr(__r
.get()), _M_refcount()
576 // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
577 // delete r.release() well-formed
578 _Tp1
* __tmp
= __r
.get();
579 _M_refcount
= shared_count(__r
);
581 __enable_shared_from_this( _M_refcount
, __tmp
, __tmp
);
584 template<typename _Tp1
>
585 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __static_cast_tag
)
586 : _M_ptr(static_cast<element_type
*>(__r
._M_ptr
)),
587 _M_refcount(__r
._M_refcount
)
590 template<typename _Tp1
>
591 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __const_cast_tag
)
592 : _M_ptr(const_cast<element_type
*>(__r
._M_ptr
)),
593 _M_refcount(__r
._M_refcount
)
596 template<typename _Tp1
>
597 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __dynamic_cast_tag
)
598 : _M_ptr(dynamic_cast<element_type
*>(__r
._M_ptr
)),
599 _M_refcount(__r
._M_refcount
)
601 if (_M_ptr
== 0) // need to allocate new counter -- the cast failed
602 _M_refcount
= shared_count();
605 template<typename _Tp1
>
607 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
610 _M_refcount
= __r
._M_refcount
; // shared_count::op= doesn't throw
614 template<typename _Tp1
>
616 operator=(std::auto_ptr
<_Tp1
>& __r
)
618 shared_ptr(__r
).swap(*this);
623 reset() // never throws
624 { shared_ptr().swap(*this); }
626 template<typename _Tp1
>
628 reset(_Tp1
* __p
) // _Tp1 must be complete
630 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
); // catch self-reset
632 shared_ptr(__p
).swap(*this);
635 template<typename _Tp1
, typename _Deleter
>
637 reset(_Tp1
* __p
, _Deleter __d
)
638 { shared_ptr(__p
, __d
).swap(*this); }
640 // error to instantiate if _Tp is [cv-qual] void
642 operator*() const // never throws
644 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
649 operator->() const // never throws
651 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
656 get() const // never throws
659 // implicit conversion to "bool"
661 typedef _Tp
* shared_ptr::*__unspecified_bool_type
;
664 operator __unspecified_bool_type() const // never throws
665 { return _M_ptr
== 0 ? 0 : &shared_ptr::_M_ptr
; }
668 unique() const // never throws
669 { return _M_refcount
.unique(); }
672 use_count() const // never throws
673 { return _M_refcount
.use_count(); }
676 swap(shared_ptr
<_Tp
>& __other
) // never throws
678 std::swap(_M_ptr
, __other
._M_ptr
);
679 _M_refcount
.swap(__other
._M_refcount
);
684 _M_get_deleter(const std::type_info
& __ti
) const
685 { return _M_refcount
.get_deleter(__ti
); }
687 template<typename _Tp1
>
689 _M_less(const shared_ptr
<_Tp1
>& __rhs
) const
690 { return _M_refcount
< __rhs
._M_refcount
; }
692 template<typename _Tp1
> friend class shared_ptr
;
693 template<typename _Tp1
> friend class weak_ptr
;
695 template<typename _Del
, typename _Tp1
>
696 friend _Del
* get_deleter(const shared_ptr
<_Tp1
>&);
698 // friends injected into enclosing namespace and found by ADL:
699 template<typename _Tp1
>
701 operator==(const shared_ptr
& __a
, const shared_ptr
<_Tp1
>& __b
)
702 { return __a
.get() == __b
.get(); }
704 template<typename _Tp1
>
706 operator!=(const shared_ptr
& __a
, const shared_ptr
<_Tp1
>& __b
)
707 { return __a
.get() != __b
.get(); }
709 template<typename _Tp1
>
711 operator<(const shared_ptr
& __a
, const shared_ptr
<_Tp1
>& __b
)
712 { return __a
._M_less(__b
); }
714 _Tp
* _M_ptr
; // contained pointer
715 shared_count _M_refcount
; // reference counter
718 // 2.2.3.8 shared_ptr specialized algorithms.
719 template<typename _Tp
>
721 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>& __b
)
724 // 2.2.3.9 shared_ptr casts
725 /** @warning The seemingly equivalent
726 * <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
727 * will eventually result in undefined behaviour,
728 * attempting to delete the same object twice.
730 template<typename _Tp
, typename _Tp1
>
732 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
734 return shared_ptr
<_Tp
>(__r
, __static_cast_tag());
737 /** @warning The seemingly equivalent
738 * <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
739 * will eventually result in undefined behaviour,
740 * attempting to delete the same object twice.
742 template<typename _Tp
, typename _Tp1
>
744 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
746 return shared_ptr
<_Tp
>(__r
, __const_cast_tag());
749 /** @warning The seemingly equivalent
750 * <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
751 * will eventually result in undefined behaviour,
752 * attempting to delete the same object twice.
754 template<typename _Tp
, typename _Tp1
>
756 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
758 return shared_ptr
<_Tp
>(__r
, __dynamic_cast_tag());
761 // 2.2.3.7 shared_ptr I/O
762 template<typename _Ch
, typename _Tr
, typename _Tp
>
763 std::basic_ostream
<_Ch
, _Tr
>&
764 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
, const shared_ptr
<_Tp
>& __p
)
770 // 2.2.3.10 shared_ptr get_deleter (experimental)
771 template<typename _Del
, typename _Tp
>
773 get_deleter(const shared_ptr
<_Tp
>& __p
)
774 { return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
))); }
777 template<typename _Tp
>
782 typedef _Tp element_type
;
785 : _M_ptr(0), _M_refcount() // never throws
788 // generated copy constructor, assignment, destructor are fine
791 // The "obvious" converting constructor implementation:
794 // weak_ptr(weak_ptr<Y> const & r)
795 // : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
798 // has a serious problem.
800 // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
801 // conversion may require access to *r._M_ptr (virtual inheritance).
803 // It is not possible to avoid spurious access violations since
804 // in multithreaded programs r._M_ptr may be invalidated at any point.
807 template<typename _Tp1
>
808 weak_ptr(const weak_ptr
<_Tp1
>& r
)
809 : _M_refcount(r
._M_refcount
) // never throws
811 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
812 _M_ptr
= r
.lock().get();
815 template<typename _Tp1
>
816 weak_ptr(const shared_ptr
<_Tp1
>& r
)
817 : _M_ptr(r
._M_ptr
), _M_refcount(r
._M_refcount
) // never throws
819 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
822 template<typename _Tp1
>
824 operator=(const weak_ptr
<_Tp1
>& r
) // never throws
826 _M_ptr
= r
.lock().get();
827 _M_refcount
= r
._M_refcount
;
831 template<typename _Tp1
>
833 operator=(const shared_ptr
<_Tp1
>& r
) // never throws
836 _M_refcount
= r
._M_refcount
;
841 lock() const // never throws
845 // optimization: avoid throw overhead
847 return shared_ptr
<element_type
>();
851 return shared_ptr
<element_type
>(*this);
853 catch (const bad_weak_ptr
&)
855 // Q: how can we get here?
856 // A: another thread may have invalidated r after the
857 // use_count test above.
858 return shared_ptr
<element_type
>();
863 // optimization: avoid try/catch overhead when single threaded
864 return expired() ? shared_ptr
<element_type
>()
865 : shared_ptr
<element_type
>(*this);
871 use_count() const // never throws
872 { return _M_refcount
.use_count(); }
875 expired() const // never throws
876 { return _M_refcount
.use_count() == 0; }
879 reset() // never throws
880 { weak_ptr().swap(*this); }
883 swap(weak_ptr
& __s
) // never throws
885 std::swap(_M_ptr
, __s
._M_ptr
);
886 _M_refcount
.swap(__s
._M_refcount
);
891 template<typename _Tp1
>
893 _M_less(const weak_ptr
<_Tp1
>& __rhs
) const
894 { return _M_refcount
< __rhs
._M_refcount
; }
896 // used by __enable_shared_from_this
898 _M_assign(_Tp
* __ptr
, const shared_count
& __refcount
)
901 _M_refcount
= __refcount
;
904 // friend injected into namespace and found by ADL
906 template<typename _Tp1
>
908 operator<(const weak_ptr
& __lhs
, const weak_ptr
<_Tp1
>& __rhs
)
909 { return __lhs
._M_less(__rhs
); }
911 template<typename _Tp1
> friend class weak_ptr
;
912 template<typename _Tp1
> friend class shared_ptr
;
913 friend class enable_shared_from_this
<_Tp
>;
915 _Tp
* _M_ptr
; // contained pointer
916 weak_count _M_refcount
; // reference counter
920 // 2.2.4.7 weak_ptr specialized algorithms.
921 template<typename _Tp
>
923 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
)
927 template<typename _Tp
>
928 class enable_shared_from_this
932 enable_shared_from_this()
935 enable_shared_from_this(const enable_shared_from_this
&)
938 enable_shared_from_this
&
939 operator=(const enable_shared_from_this
&)
942 ~enable_shared_from_this()
950 shared_ptr
<_Tp
> __p(this->_M_weak_this
);
954 shared_ptr
<const _Tp
>
955 shared_from_this() const
957 shared_ptr
<const _Tp
> __p(this->_M_weak_this
);
962 template<typename _Tp1
>
964 _M_weak_assign(_Tp1
* __p
, const shared_count
& __n
) const
965 { _M_weak_this
._M_assign(__p
, __n
); }
967 template<typename _Tp1
>
969 __enable_shared_from_this(const shared_count
& __pn
,
970 const enable_shared_from_this
* __pe
,
974 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
977 mutable weak_ptr
<_Tp
> _M_weak_this
;