1 #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001-2008 Peter Dimov
10 // Distributed under the Boost Software License, Version 1.0. (See
11 // accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
17 #include <boost/config.hpp> // for broken compiler workarounds
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20 #include <boost/smart_ptr/detail/shared_ptr_nmt.hpp>
23 // In order to avoid circular dependencies with Boost.TR1
24 // we make sure that our include of <memory> doesn't try to
25 // pull in the TR1 headers: that's why we use this header
26 // rather than including <memory> directly:
27 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
29 #include <boost/assert.hpp>
30 #include <boost/checked_delete.hpp>
31 #include <boost/throw_exception.hpp>
32 #include <boost/smart_ptr/detail/shared_count.hpp>
33 #include <boost/detail/workaround.hpp>
34 #include <boost/smart_ptr/detail/sp_convertible.hpp>
36 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
37 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
38 #include <boost/memory_order.hpp>
41 #include <algorithm> // for std::swap
42 #include <functional> // for std::less
43 #include <typeinfo> // for std::bad_cast
44 #include <cstddef> // for std::size_t
46 #if !defined(BOOST_NO_IOSTREAM)
47 #if !defined(BOOST_NO_IOSFWD)
48 #include <iosfwd> // for std::basic_ostream
57 template<class T> class shared_ptr;
58 template<class T> class weak_ptr;
59 template<class T> class enable_shared_from_this;
60 template<class T> class enable_shared_from_this2;
65 struct static_cast_tag {};
66 struct const_cast_tag {};
67 struct dynamic_cast_tag {};
68 struct polymorphic_cast_tag {};
70 template<class T> struct shared_ptr_traits
72 typedef T & reference;
75 template<> struct shared_ptr_traits<void>
77 typedef void reference;
80 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
82 template<> struct shared_ptr_traits<void const>
84 typedef void reference;
87 template<> struct shared_ptr_traits<void volatile>
89 typedef void reference;
92 template<> struct shared_ptr_traits<void const volatile>
94 typedef void reference;
99 // enable_shared_from_this support
101 template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> const * ppx, Y const * py, boost::enable_shared_from_this< T > const * pe )
105 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
109 template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_this2< T > const * pe )
113 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
119 // Avoid C4793, ... causes native code generation
121 struct sp_any_pointer
123 template<class T> sp_any_pointer( T* ) {}
126 inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
132 inline void sp_enable_shared_from_this( ... )
138 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
140 // rvalue auto_ptr support based on a technique by Dave Abrahams
142 template< class T, class R > struct sp_enable_if_auto_ptr
146 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
153 } // namespace detail
159 // An enhanced relative of scoped_ptr with reference counted copy semantics.
160 // The object pointed to is deleted when the last shared_ptr pointing to it
161 // is destroyed or reset.
164 template<class T> class shared_ptr
168 // Borland 5.5.1 specific workaround
169 typedef shared_ptr<T> this_type;
173 typedef T element_type;
174 typedef T value_type;
176 typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
178 shared_ptr(): px(0), pn() // never throws in 1.30+
183 explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
185 boost::detail::sp_enable_shared_from_this( this, p, p );
189 // Requirements: D's copy constructor must not throw
191 // shared_ptr will release p by calling d(p)
194 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
196 boost::detail::sp_enable_shared_from_this( this, p, p );
199 // As above, but with allocator. A's copy constructor shall not throw.
201 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
203 boost::detail::sp_enable_shared_from_this( this, p, p );
206 // generated copy constructor, destructor are fine...
208 #if defined( BOOST_HAS_RVALUE_REFS )
210 // ... except in C++0x, move disables the implicit copy
212 shared_ptr( shared_ptr const & r ): px( r.px ), pn( r.pn ) // never throws
219 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
221 // it is now safe to copy r.px, as pn(r.pn) did not throw
226 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag ): px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() ) // never throws
235 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
237 shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
241 shared_ptr( shared_ptr<Y> const & r )
244 : px( r.px ), pn( r.pn ) // never throws
250 shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn ) // never throws
255 shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
260 shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
265 shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
267 if(px == 0) // need to allocate new counter -- the cast failed
269 pn = boost::detail::shared_count();
274 shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
278 boost::throw_exception(std::bad_cast());
282 #ifndef BOOST_NO_AUTO_PTR
285 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
288 pn = boost::detail::shared_count(r);
289 boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
292 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
295 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
297 typename Ap::element_type * tmp = r.get();
298 pn = boost::detail::shared_count( r );
299 boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
303 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
305 #endif // BOOST_NO_AUTO_PTR
309 shared_ptr & operator=( shared_ptr const & r ) // never throws
311 this_type(r).swap(*this);
315 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
318 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
320 this_type(r).swap(*this);
326 #ifndef BOOST_NO_AUTO_PTR
329 shared_ptr & operator=( std::auto_ptr<Y> & r )
331 this_type(r).swap(*this);
335 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
338 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
340 this_type( r ).swap( *this );
345 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
347 #endif // BOOST_NO_AUTO_PTR
351 #if defined( BOOST_HAS_RVALUE_REFS )
353 shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws
360 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
362 shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
366 shared_ptr( shared_ptr<Y> && r )
369 : px( r.px ), pn() // never throws
375 shared_ptr & operator=( shared_ptr && r ) // never throws
377 this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
382 shared_ptr & operator=( shared_ptr<Y> && r ) // never throws
384 this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
390 void reset() // never throws in 1.30+
392 this_type().swap(*this);
395 template<class Y> void reset(Y * p) // Y must be complete
397 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
398 this_type(p).swap(*this);
401 template<class Y, class D> void reset( Y * p, D d )
403 this_type( p, d ).swap( *this );
406 template<class Y, class D, class A> void reset( Y * p, D d, A a )
408 this_type( p, d, a ).swap( *this );
411 template<class Y> void reset( shared_ptr<Y> const & r, T * p )
413 this_type( r, p ).swap( *this );
416 reference operator* () const // never throws
418 BOOST_ASSERT(px != 0);
422 T * operator-> () const // never throws
424 BOOST_ASSERT(px != 0);
428 T * get() const // never throws
433 // implicit conversion to "bool"
434 #include <boost/smart_ptr/detail/operator_bool.hpp>
436 bool unique() const // never throws
441 long use_count() const // never throws
443 return pn.use_count();
446 void swap(shared_ptr<T> & other) // never throws
448 std::swap(px, other.px);
452 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const
457 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
462 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const
464 return pn.get_deleter( ti );
467 bool _internal_equiv( shared_ptr const & r ) const
469 return px == r.px && pn == r.pn;
472 // Tasteless as this may seem, making all members public allows member templates
473 // to work in the absence of member template friends. (Matthew Langston)
475 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
479 template<class Y> friend class shared_ptr;
480 template<class Y> friend class weak_ptr;
485 T * px; // contained pointer
486 boost::detail::shared_count pn; // reference counter
490 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
492 return a.get() == b.get();
495 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
497 return a.get() != b.get();
500 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
502 // Resolve the ambiguity between our op!= and the one in rel_ops
504 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
506 return a.get() != b.get();
511 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
513 return a.owner_before( b );
516 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
521 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
523 return shared_ptr<T>(r, boost::detail::static_cast_tag());
526 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
528 return shared_ptr<T>(r, boost::detail::const_cast_tag());
531 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
533 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
536 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
538 template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
540 return shared_ptr<T>(r, boost::detail::static_cast_tag());
543 template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
545 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
548 template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
550 return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
553 template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
555 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
556 return shared_static_cast<T>(r);
559 // get_pointer() enables boost::mem_fn to recognize shared_ptr
561 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
568 #if !defined(BOOST_NO_IOSTREAM)
570 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
572 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
580 // in STLport's no-iostreams mode no iostream symbols can be used
581 #ifndef _STLP_NO_IOSTREAMS
583 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
584 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
585 using std::basic_ostream;
586 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
588 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
595 #endif // _STLP_NO_IOSTREAMS
597 #endif // __GNUC__ < 3
599 #endif // !defined(BOOST_NO_IOSTREAM)
603 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
604 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
605 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
607 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
608 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
610 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
612 void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
613 return const_cast<D *>(static_cast<D const *>(q));
618 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
620 return static_cast<D *>(p._internal_get_deleter(BOOST_SP_TYPEID(D)));
627 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
629 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ )
634 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
636 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
640 template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, memory_order /*mo*/ )
642 return atomic_load( p );
645 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
647 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
651 template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
653 atomic_store( p, r ); // std::move( r )
656 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
658 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
664 return r; // return std::move( r )
667 template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
669 return atomic_exchange( p, r ); // std::move( r )
672 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
674 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
678 if( p->_internal_equiv( *v ) )
688 shared_ptr<T> tmp( *p );
697 template<class T> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, memory_order /*success*/, memory_order /*failure*/ )
699 return atomic_compare_exchange( p, v, w ); // std::move( w )
702 #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
706 template< class T > struct hash;
708 template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p )
710 return boost::hash< T* >()( p.get() );
715 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
717 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED