1 #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
\r
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
\r
8 // Copyright (c) 2001-2008 Peter Dimov
\r
10 // Distributed under the Boost Software License, Version 1.0. (See
\r
11 // accompanying file LICENSE_1_0.txt or copy at
\r
12 // http://www.boost.org/LICENSE_1_0.txt)
\r
14 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
\r
17 #include <boost/config.hpp> // for broken compiler workarounds
\r
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
\r
20 #include <boost/smart_ptr/detail/shared_ptr_nmt.hpp>
\r
23 // In order to avoid circular dependencies with Boost.TR1
\r
24 // we make sure that our include of <memory> doesn't try to
\r
25 // pull in the TR1 headers: that's why we use this header
\r
26 // rather than including <memory> directly:
\r
27 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
\r
29 #include <boost/assert.hpp>
\r
30 #include <boost/checked_delete.hpp>
\r
31 #include <boost/throw_exception.hpp>
\r
32 #include <boost/smart_ptr/detail/shared_count.hpp>
\r
33 #include <boost/detail/workaround.hpp>
\r
34 #include <boost/smart_ptr/detail/sp_convertible.hpp>
\r
36 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
\r
37 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
\r
38 #include <boost/memory_order.hpp>
\r
41 #include <algorithm> // for std::swap
\r
42 #include <functional> // for std::less
\r
43 #include <typeinfo> // for std::bad_cast
\r
45 #if !defined(BOOST_NO_IOSTREAM)
\r
46 #if !defined(BOOST_NO_IOSFWD)
\r
47 #include <iosfwd> // for std::basic_ostream
\r
53 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
\r
54 # pragma warning(push)
\r
55 # pragma warning(disable:4284) // odd return type for operator->
\r
61 template<class T> class shared_ptr;
\r
62 template<class T> class weak_ptr;
\r
63 template<class T> class enable_shared_from_this;
\r
64 template<class T> class enable_shared_from_this2;
\r
69 struct static_cast_tag {};
\r
70 struct const_cast_tag {};
\r
71 struct dynamic_cast_tag {};
\r
72 struct polymorphic_cast_tag {};
\r
74 template<class T> struct shared_ptr_traits
\r
76 typedef T & reference;
\r
79 template<> struct shared_ptr_traits<void>
\r
81 typedef void reference;
\r
84 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
\r
86 template<> struct shared_ptr_traits<void const>
\r
88 typedef void reference;
\r
91 template<> struct shared_ptr_traits<void volatile>
\r
93 typedef void reference;
\r
96 template<> struct shared_ptr_traits<void const volatile>
\r
98 typedef void reference;
\r
103 // enable_shared_from_this support
\r
105 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 )
\r
109 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
\r
113 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 )
\r
117 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
\r
123 // Avoid C4793, ... causes native code generation
\r
125 struct sp_any_pointer
\r
127 template<class T> sp_any_pointer( T* ) {}
\r
130 inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
\r
136 inline void sp_enable_shared_from_this( ... )
\r
142 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
\r
144 // rvalue auto_ptr support based on a technique by Dave Abrahams
\r
146 template< class T, class R > struct sp_enable_if_auto_ptr
\r
150 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
\r
157 } // namespace detail
\r
163 // An enhanced relative of scoped_ptr with reference counted copy semantics.
\r
164 // The object pointed to is deleted when the last shared_ptr pointing to it
\r
165 // is destroyed or reset.
\r
168 template<class T> class shared_ptr
\r
172 // Borland 5.5.1 specific workaround
\r
173 typedef shared_ptr<T> this_type;
\r
177 typedef T element_type;
\r
178 typedef T value_type;
\r
179 typedef T * pointer;
\r
180 typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
\r
182 shared_ptr(): px(0), pn() // never throws in 1.30+
\r
187 explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
\r
189 boost::detail::sp_enable_shared_from_this( this, p, p );
\r
193 // Requirements: D's copy constructor must not throw
\r
195 // shared_ptr will release p by calling d(p)
\r
198 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
\r
200 boost::detail::sp_enable_shared_from_this( this, p, p );
\r
203 // As above, but with allocator. A's copy constructor shall not throw.
\r
205 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
\r
207 boost::detail::sp_enable_shared_from_this( this, p, p );
\r
210 // generated copy constructor, destructor are fine
\r
213 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
\r
215 // it is now safe to copy r.px, as pn(r.pn) did not throw
\r
220 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag ): px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() ) // never throws
\r
229 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
\r
231 shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
\r
235 shared_ptr( shared_ptr<Y> const & r )
\r
238 : px( r.px ), pn( r.pn ) // never throws
\r
243 template< class Y >
\r
244 shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn ) // never throws
\r
249 shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
\r
254 shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
\r
259 shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
\r
261 if(px == 0) // need to allocate new counter -- the cast failed
\r
263 pn = boost::detail::shared_count();
\r
268 shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
\r
272 boost::throw_exception(std::bad_cast());
\r
276 #ifndef BOOST_NO_AUTO_PTR
\r
279 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
\r
282 pn = boost::detail::shared_count(r);
\r
283 boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
\r
286 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
\r
289 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
\r
291 typename Ap::element_type * tmp = r.get();
\r
292 pn = boost::detail::shared_count( r );
\r
293 boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
\r
297 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
\r
299 #endif // BOOST_NO_AUTO_PTR
\r
303 shared_ptr & operator=( shared_ptr const & r ) // never throws
\r
305 this_type(r).swap(*this);
\r
309 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
\r
312 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
\r
314 this_type(r).swap(*this);
\r
320 #ifndef BOOST_NO_AUTO_PTR
\r
323 shared_ptr & operator=( std::auto_ptr<Y> & r )
\r
325 this_type(r).swap(*this);
\r
329 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
\r
332 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
\r
334 this_type( r ).swap( *this );
\r
339 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
\r
341 #endif // BOOST_NO_AUTO_PTR
\r
345 #if defined( BOOST_HAS_RVALUE_REFS )
\r
347 shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws
\r
354 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
\r
356 shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
\r
360 shared_ptr( shared_ptr<Y> && r )
\r
363 : px( r.px ), pn() // never throws
\r
369 shared_ptr & operator=( shared_ptr && r ) // never throws
\r
371 this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
\r
376 shared_ptr & operator=( shared_ptr<Y> && r ) // never throws
\r
378 this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
\r
384 void reset() // never throws in 1.30+
\r
386 this_type().swap(*this);
\r
389 template<class Y> void reset(Y * p) // Y must be complete
\r
391 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
\r
392 this_type(p).swap(*this);
\r
395 template<class Y, class D> void reset( Y * p, D d )
\r
397 this_type( p, d ).swap( *this );
\r
400 template<class Y, class D, class A> void reset( Y * p, D d, A a )
\r
402 this_type( p, d, a ).swap( *this );
\r
405 template<class Y> void reset( shared_ptr<Y> const & r, T * p )
\r
407 this_type( r, p ).swap( *this );
\r
410 reference operator* () const // never throws
\r
412 BOOST_ASSERT(px != 0);
\r
416 T * operator-> () const // never throws
\r
418 BOOST_ASSERT(px != 0);
\r
422 T * get() const // never throws
\r
427 // implicit conversion to "bool"
\r
428 #include <boost/smart_ptr/detail/operator_bool.hpp>
\r
430 bool unique() const // never throws
\r
432 return pn.unique();
\r
435 long use_count() const // never throws
\r
437 return pn.use_count();
\r
440 void swap(shared_ptr<T> & other) // never throws
\r
442 std::swap(px, other.px);
\r
446 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
\r
448 return pn < rhs.pn;
\r
451 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const
\r
453 return pn.get_deleter( ti );
\r
456 bool _internal_equiv( shared_ptr const & r ) const
\r
458 return px == r.px && pn == r.pn;
\r
461 // Tasteless as this may seem, making all members public allows member templates
\r
462 // to work in the absence of member template friends. (Matthew Langston)
\r
464 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
\r
468 template<class Y> friend class shared_ptr;
\r
469 template<class Y> friend class weak_ptr;
\r
474 T * px; // contained pointer
\r
475 boost::detail::shared_count pn; // reference counter
\r
479 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
\r
481 return a.get() == b.get();
\r
484 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
\r
486 return a.get() != b.get();
\r
489 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
\r
491 // Resolve the ambiguity between our op!= and the one in rel_ops
\r
493 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
\r
495 return a.get() != b.get();
\r
500 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
\r
502 return a._internal_less(b);
\r
505 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
\r
510 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
\r
512 return shared_ptr<T>(r, boost::detail::static_cast_tag());
\r
515 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
\r
517 return shared_ptr<T>(r, boost::detail::const_cast_tag());
\r
520 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
\r
522 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
\r
525 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
\r
527 template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
\r
529 return shared_ptr<T>(r, boost::detail::static_cast_tag());
\r
532 template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
\r
534 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
\r
537 template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
\r
539 return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
\r
542 template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
\r
544 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
\r
545 return shared_static_cast<T>(r);
\r
548 // get_pointer() enables boost::mem_fn to recognize shared_ptr
\r
550 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
\r
557 #if !defined(BOOST_NO_IOSTREAM)
\r
559 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
\r
561 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
\r
569 // in STLport's no-iostreams mode no iostream symbols can be used
\r
570 #ifndef _STLP_NO_IOSTREAMS
\r
572 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
\r
573 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
\r
574 using std::basic_ostream;
\r
575 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
\r
577 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
\r
584 #endif // _STLP_NO_IOSTREAMS
\r
586 #endif // __GNUC__ < 3
\r
588 #endif // !defined(BOOST_NO_IOSTREAM)
\r
592 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
\r
593 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
\r
594 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
\r
596 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
\r
597 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
\r
599 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
\r
601 void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
\r
602 return const_cast<D *>(static_cast<D const *>(q));
\r
607 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
\r
609 return static_cast<D *>(p._internal_get_deleter(BOOST_SP_TYPEID(D)));
\r
616 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
\r
618 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ )
\r
623 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
\r
625 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
\r
629 template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, memory_order /*mo*/ )
\r
631 return atomic_load( p );
\r
634 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
\r
636 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
\r
640 template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
\r
642 atomic_store( p, r ); // std::move( r )
\r
645 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
\r
647 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
\r
653 return r; // return std::move( r )
\r
656 template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
\r
658 return atomic_exchange( p, r ); // std::move( r )
\r
661 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
\r
663 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
\r
667 if( p->_internal_equiv( *v ) )
\r
677 shared_ptr<T> tmp( *p );
\r
686 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*/ )
\r
688 return atomic_compare_exchange( p, v, w ); // std::move( w )
\r
693 } // namespace boost
\r
696 # pragma warning(pop)
\r
699 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
\r
701 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
\r