123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701 |
- #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
- #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
- #include <boost/config.hpp> // for broken compiler workarounds
- #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
- #include <boost/smart_ptr/detail/shared_ptr_nmt.hpp>
- #else
- #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
- #include <boost/assert.hpp>
- #include <boost/checked_delete.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/smart_ptr/detail/shared_count.hpp>
- #include <boost/detail/workaround.hpp>
- #include <boost/smart_ptr/detail/sp_convertible.hpp>
- #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
- #include <boost/smart_ptr/detail/spinlock_pool.hpp>
- #include <boost/memory_order.hpp>
- #endif
- #include <algorithm> // for std::swap
- #include <functional> // for std::less
- #include <typeinfo> // for std::bad_cast
- #if !defined(BOOST_NO_IOSTREAM)
- #if !defined(BOOST_NO_IOSFWD)
- #include <iosfwd> // for std::basic_ostream
- #else
- #include <ostream>
- #endif
- #endif
- #ifdef BOOST_MSVC
- # pragma warning(push)
- # pragma warning(disable:4284)
- #endif
- namespace boost
- {
- template<class T> class shared_ptr;
- template<class T> class weak_ptr;
- template<class T> class enable_shared_from_this;
- template<class T> class enable_shared_from_this2;
- namespace detail
- {
- struct static_cast_tag {};
- struct const_cast_tag {};
- struct dynamic_cast_tag {};
- struct polymorphic_cast_tag {};
- template<class T> struct shared_ptr_traits
- {
- typedef T & reference;
- };
- template<> struct shared_ptr_traits<void>
- {
- typedef void reference;
- };
- #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
- template<> struct shared_ptr_traits<void const>
- {
- typedef void reference;
- };
- template<> struct shared_ptr_traits<void volatile>
- {
- typedef void reference;
- };
- template<> struct shared_ptr_traits<void const volatile>
- {
- typedef void reference;
- };
- #endif
- 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 )
- {
- if( pe != 0 )
- {
- pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
- }
- }
- 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 )
- {
- if( pe != 0 )
- {
- pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
- }
- }
- #ifdef _MANAGED
- struct sp_any_pointer
- {
- template<class T> sp_any_pointer( T* ) {}
- };
- inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
- {
- }
- #else
- inline void sp_enable_shared_from_this( ... )
- {
- }
- #endif
- #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
- template< class T, class R > struct sp_enable_if_auto_ptr
- {
- };
- template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
- {
- typedef R type;
- };
- #endif
- }
- template<class T> class shared_ptr
- {
- private:
-
- typedef shared_ptr<T> this_type;
- public:
- typedef T element_type;
- typedef T value_type;
- typedef T * pointer;
- typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
- shared_ptr(): px(0), pn()
- {
- }
- template<class Y>
- explicit shared_ptr( Y * p ): px( p ), pn( p )
- {
- boost::detail::sp_enable_shared_from_this( this, p, p );
- }
-
-
-
-
-
- template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
- {
- boost::detail::sp_enable_shared_from_this( this, p, p );
- }
-
- template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
- {
- boost::detail::sp_enable_shared_from_this( this, p, p );
- }
- template<class Y>
- explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn)
- {
-
- px = r.px;
- }
- template<class Y>
- shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag ): px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
- {
- if( !pn.empty() )
- {
- px = r.px;
- }
- }
- template<class Y>
- #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
- shared_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
- #else
- shared_ptr( shared_ptr<Y> const & r )
- #endif
- : px( r.px ), pn( r.pn )
- {
- }
-
- template< class Y >
- shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn )
- {
- }
- template<class Y>
- shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
- {
- }
- template<class Y>
- shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
- {
- }
- template<class Y>
- shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
- {
- if(px == 0)
- {
- pn = boost::detail::shared_count();
- }
- }
- template<class Y>
- shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
- {
- if(px == 0)
- {
- boost::throw_exception(std::bad_cast());
- }
- }
- #ifndef BOOST_NO_AUTO_PTR
- template<class Y>
- explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
- {
- Y * tmp = r.get();
- pn = boost::detail::shared_count(r);
- boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
- }
- #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
- template<class Ap>
- explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
- {
- typename Ap::element_type * tmp = r.get();
- pn = boost::detail::shared_count( r );
- boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
- }
- #endif
- #endif
-
- shared_ptr & operator=( shared_ptr const & r )
- {
- this_type(r).swap(*this);
- return *this;
- }
- #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
- template<class Y>
- shared_ptr & operator=(shared_ptr<Y> const & r)
- {
- this_type(r).swap(*this);
- return *this;
- }
- #endif
- #ifndef BOOST_NO_AUTO_PTR
- template<class Y>
- shared_ptr & operator=( std::auto_ptr<Y> & r )
- {
- this_type(r).swap(*this);
- return *this;
- }
- #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
- template<class Ap>
- typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
- {
- this_type( r ).swap( *this );
- return *this;
- }
- #endif
- #endif
- #if defined( BOOST_HAS_RVALUE_REFS )
- shared_ptr( shared_ptr && r ): px( r.px ), pn()
- {
- pn.swap( r.pn );
- r.px = 0;
- }
- template<class Y>
- #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
- shared_ptr( shared_ptr<Y> && r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
- #else
- shared_ptr( shared_ptr<Y> && r )
- #endif
- : px( r.px ), pn()
- {
- pn.swap( r.pn );
- r.px = 0;
- }
- shared_ptr & operator=( shared_ptr && r )
- {
- this_type( std::move( r ) ).swap( *this );
- return *this;
- }
- template<class Y>
- shared_ptr & operator=( shared_ptr<Y> && r )
- {
- this_type( std::move( r ) ).swap( *this );
- return *this;
- }
- #endif
- void reset()
- {
- this_type().swap(*this);
- }
- template<class Y> void reset(Y * p)
- {
- BOOST_ASSERT(p == 0 || p != px);
- this_type(p).swap(*this);
- }
- template<class Y, class D> void reset( Y * p, D d )
- {
- this_type( p, d ).swap( *this );
- }
- template<class Y, class D, class A> void reset( Y * p, D d, A a )
- {
- this_type( p, d, a ).swap( *this );
- }
- template<class Y> void reset( shared_ptr<Y> const & r, T * p )
- {
- this_type( r, p ).swap( *this );
- }
- reference operator* () const
- {
- BOOST_ASSERT(px != 0);
- return *px;
- }
- T * operator-> () const
- {
- BOOST_ASSERT(px != 0);
- return px;
- }
- T * get() const
- {
- return px;
- }
- #include <boost/smart_ptr/detail/operator_bool.hpp>
- bool unique() const
- {
- return pn.unique();
- }
- long use_count() const
- {
- return pn.use_count();
- }
- void swap(shared_ptr<T> & other)
- {
- std::swap(px, other.px);
- pn.swap(other.pn);
- }
- template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
- {
- return pn < rhs.pn;
- }
- void * _internal_get_deleter( detail::sp_typeinfo const & ti ) const
- {
- return pn.get_deleter( ti );
- }
- bool _internal_equiv( shared_ptr const & r ) const
- {
- return px == r.px && pn == r.pn;
- }
- // Tasteless as this may seem, making all members public allows member templates
- // to work in the absence of member template friends. (Matthew Langston)
- #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
- private:
- template<class Y> friend class shared_ptr;
- template<class Y> friend class weak_ptr;
- #endif
- T * px;
- boost::detail::shared_count pn;
- };
- template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
- {
- return a.get() == b.get();
- }
- template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
- {
- return a.get() != b.get();
- }
- #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
- template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
- {
- return a.get() != b.get();
- }
- #endif
- template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
- {
- return a._internal_less(b);
- }
- template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
- {
- a.swap(b);
- }
- template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
- {
- return shared_ptr<T>(r, boost::detail::static_cast_tag());
- }
- template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
- {
- return shared_ptr<T>(r, boost::detail::const_cast_tag());
- }
- template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
- {
- return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
- }
- template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
- {
- return shared_ptr<T>(r, boost::detail::static_cast_tag());
- }
- template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
- {
- return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
- }
- template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
- {
- return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
- }
- template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
- {
- BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
- return shared_static_cast<T>(r);
- }
- template<class T> inline T * get_pointer(shared_ptr<T> const & p)
- {
- return p.get();
- }
- #if !defined(BOOST_NO_IOSTREAM)
- #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
- template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
- {
- os << p.get();
- return os;
- }
- #else
- // in STLport's no-iostreams mode no iostream symbols can be used
- #ifndef _STLP_NO_IOSTREAMS
- # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
- // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
- using std::basic_ostream;
- template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
- # else
- template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
- # endif
- {
- os << p.get();
- return os;
- }
- #endif // _STLP_NO_IOSTREAMS
- #endif // __GNUC__ < 3
- #endif // !defined(BOOST_NO_IOSTREAM)
- // get_deleter
- #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
- ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
- ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
- // g++ 2.9x doesn't allow static_cast<X const *>(void *)
- template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
- {
- void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
- return const_cast<D *>(static_cast<D const *>(q));
- }
- #else
- template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
- {
- return static_cast<D *>(p._internal_get_deleter(BOOST_SP_TYPEID(D)));
- }
- #endif
- #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
- template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * )
- {
- return false;
- }
- template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
- {
- boost::detail::spinlock_pool<2>::scoped_lock lock( p );
- return *p;
- }
- template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, memory_order )
- {
- return atomic_load( p );
- }
- template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
- {
- boost::detail::spinlock_pool<2>::scoped_lock lock( p );
- p->swap( r );
- }
- template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order )
- {
- atomic_store( p, r );
- }
- template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
- {
- boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
- sp.lock();
- p->swap( r );
- sp.unlock();
- return r;
- }
- template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order )
- {
- return atomic_exchange( p, r );
- }
- template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
- {
- boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
- sp.lock();
- if( p->_internal_equiv( *v ) )
- {
- p->swap( w );
- sp.unlock();
- return true;
- }
- else
- {
- shared_ptr<T> tmp( *p );
- sp.unlock();
- tmp.swap( *v );
- return false;
- }
- }
- template<class T> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, memory_order , memory_order )
- {
- return atomic_compare_exchange( p, v, w );
- }
- #endif
- }
- #ifdef BOOST_MSVC
- # pragma warning(pop)
- #endif
- #endif
- #endif
|