#ifndef BOOST_SHARED_PTR_HPP_INCLUDED #define BOOST_SHARED_PTR_HPP_INCLUDED // // shared_ptr.hpp // // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. // Copyright (c) 2001-2007 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. // #include // for broken compiler workarounds #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) #include #else #include // for std::auto_ptr #include #include #include #include #include #include // for std::swap #include // for std::less #include // for std::bad_cast #include // for std::basic_ostream #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash # pragma warning(push) # pragma warning(disable:4284) // odd return type for operator-> #endif namespace boost { template class weak_ptr; template class enable_shared_from_this; namespace detail { struct static_cast_tag {}; struct const_cast_tag {}; struct dynamic_cast_tag {}; struct polymorphic_cast_tag {}; template struct shared_ptr_traits { typedef T & reference; }; template<> struct shared_ptr_traits { typedef void reference; }; #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS) template<> struct shared_ptr_traits { typedef void reference; }; template<> struct shared_ptr_traits { typedef void reference; }; template<> struct shared_ptr_traits { typedef void reference; }; #endif // enable_shared_from_this support template void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this const * pe, Y const * px ) { if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast(px), pn); } #ifdef _MANAGED // Avoid C4793, ... causes native code generation struct sp_any_pointer { template sp_any_pointer( T* ) {} }; inline void sp_enable_shared_from_this( shared_count const & /*pn*/, sp_any_pointer, sp_any_pointer ) { } #else // _MANAGED #ifdef sgi // Turn off: the last argument of the varargs function "sp_enable_shared_from_this" is unnamed # pragma set woff 3506 #endif inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... ) { } #ifdef sgi # pragma reset woff 3506 #endif #endif // _MANAGED #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR ) // rvalue auto_ptr support based on a technique by Dave Abrahams 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 } // namespace detail // // shared_ptr // // An enhanced relative of scoped_ptr with reference counted copy semantics. // The object pointed to is deleted when the last shared_ptr pointing to it // is destroyed or reset. // template class shared_ptr { private: // Borland 5.5.1 specific workaround typedef shared_ptr this_type; public: typedef T element_type; typedef T value_type; typedef T * pointer; typedef typename boost::detail::shared_ptr_traits::reference reference; shared_ptr(): px(0), pn() // never throws in 1.30+ { } template explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete { boost::detail::sp_enable_shared_from_this( pn, p, p ); } // // Requirements: D's copy constructor must not throw // // shared_ptr will release p by calling d(p) // template shared_ptr(Y * p, D d): px(p), pn(p, d) { boost::detail::sp_enable_shared_from_this( pn, p, p ); } // As above, but with allocator. A's copy constructor shall not throw. template shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a ) { boost::detail::sp_enable_shared_from_this( pn, p, p ); } // generated copy constructor, assignment, destructor are fine... // except that Borland C++ has a bug, and g++ with -Wsynth warns #if defined(__BORLANDC__) || defined(__GNUC__) shared_ptr & operator=(shared_ptr const & r) // never throws { px = r.px; pn = r.pn; // shared_count::op= doesn't throw return *this; } #endif template explicit shared_ptr(weak_ptr const & r): pn(r.pn) // may throw { // it is now safe to copy r.px, as pn(r.pn) did not throw px = r.px; } template shared_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws { } // aliasing template< class Y > shared_ptr( shared_ptr const & r, T * p ): px( p ), pn( r.pn ) // never throws { } template shared_ptr(shared_ptr const & r, boost::detail::static_cast_tag): px(static_cast(r.px)), pn(r.pn) { } template shared_ptr(shared_ptr const & r, boost::detail::const_cast_tag): px(const_cast(r.px)), pn(r.pn) { } template shared_ptr(shared_ptr const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { if(px == 0) // need to allocate new counter -- the cast failed { pn = boost::detail::shared_count(); } } template shared_ptr(shared_ptr const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { if(px == 0) { boost::throw_exception(std::bad_cast()); } } #ifndef BOOST_NO_AUTO_PTR template explicit shared_ptr(std::auto_ptr & r): px(r.get()), pn() { Y * tmp = r.get(); pn = boost::detail::shared_count(r); boost::detail::sp_enable_shared_from_this( pn, tmp, tmp ); } #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) template shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr::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( pn, tmp, tmp ); } #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #endif // BOOST_NO_AUTO_PTR #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300) template shared_ptr & operator=(shared_ptr const & r) // never throws { px = r.px; pn = r.pn; // shared_count::op= doesn't throw return *this; } #endif #ifndef BOOST_NO_AUTO_PTR template shared_ptr & operator=( std::auto_ptr & r ) { this_type(r).swap(*this); return *this; } #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) template typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r ) { this_type( r ).swap( *this ); return *this; } #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #endif // BOOST_NO_AUTO_PTR // Move support #if defined( BOOST_HAS_RVALUE_REFS ) shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws { pn.swap( r.pn ); r.px = 0; } template shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws { pn.swap( r.pn ); r.px = 0; } shared_ptr & operator=( shared_ptr && r ) // never throws { this_type( static_cast< shared_ptr && >( r ) ).swap( *this ); return *this; } template shared_ptr & operator=( shared_ptr && r ) // never throws { this_type( static_cast< shared_ptr && >( r ) ).swap( *this ); return *this; } #endif void reset() // never throws in 1.30+ { this_type().swap(*this); } template void reset(Y * p) // Y must be complete { BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors this_type(p).swap(*this); } template void reset( Y * p, D d ) { this_type( p, d ).swap( *this ); } template void reset( Y * p, D d, A a ) { this_type( p, d, a ).swap( *this ); } template void reset( shared_ptr const & r, T * p ) { this_type( r, p ).swap( *this ); } reference operator* () const // never throws { BOOST_ASSERT(px != 0); return *px; } T * operator-> () const // never throws { BOOST_ASSERT(px != 0); return px; } T * get() const // never throws { return px; } // implicit conversion to "bool" #if ( defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, < 0x570) ) || defined(__CINT__) operator bool () const { return px != 0; } #elif defined( _MANAGED ) static void unspecified_bool( this_type*** ) { } typedef void (*unspecified_bool_type)( this_type*** ); operator unspecified_bool_type() const // never throws { return px == 0? 0: unspecified_bool; } #elif \ ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \ ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) ) || \ ( defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590) ) typedef T * (this_type::*unspecified_bool_type)() const; operator unspecified_bool_type() const // never throws { return px == 0? 0: &this_type::get; } #else typedef T * this_type::*unspecified_bool_type; operator unspecified_bool_type() const // never throws { return px == 0? 0: &this_type::px; } #endif // operator! is redundant, but some compilers need it bool operator! () const // never throws { return px == 0; } bool unique() const // never throws { return pn.unique(); } long use_count() const // never throws { return pn.use_count(); } void swap(shared_ptr & other) // never throws { std::swap(px, other.px); pn.swap(other.pn); } template bool _internal_less(shared_ptr const & rhs) const { return pn < rhs.pn; } void * _internal_get_deleter( detail::sp_typeinfo const & ti ) const { return pn.get_deleter( ti ); } // 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 friend class shared_ptr; template friend class weak_ptr; #endif T * px; // contained pointer boost::detail::shared_count pn; // reference counter }; // shared_ptr template inline bool operator==(shared_ptr const & a, shared_ptr const & b) { return a.get() == b.get(); } template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) { return a.get() != b.get(); } #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 // Resolve the ambiguity between our op!= and the one in rel_ops template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) { return a.get() != b.get(); } #endif template inline bool operator<(shared_ptr const & a, shared_ptr const & b) { return a._internal_less(b); } template inline void swap(shared_ptr & a, shared_ptr & b) { a.swap(b); } template shared_ptr static_pointer_cast(shared_ptr const & r) { return shared_ptr(r, boost::detail::static_cast_tag()); } template shared_ptr const_pointer_cast(shared_ptr const & r) { return shared_ptr(r, boost::detail::const_cast_tag()); } template shared_ptr dynamic_pointer_cast(shared_ptr const & r) { return shared_ptr(r, boost::detail::dynamic_cast_tag()); } // shared_*_cast names are deprecated. Use *_pointer_cast instead. template shared_ptr shared_static_cast(shared_ptr const & r) { return shared_ptr(r, boost::detail::static_cast_tag()); } template shared_ptr shared_dynamic_cast(shared_ptr const & r) { return shared_ptr(r, boost::detail::dynamic_cast_tag()); } template shared_ptr shared_polymorphic_cast(shared_ptr const & r) { return shared_ptr(r, boost::detail::polymorphic_cast_tag()); } template shared_ptr shared_polymorphic_downcast(shared_ptr const & r) { BOOST_ASSERT(dynamic_cast(r.get()) == r.get()); return shared_static_cast(r); } // get_pointer() enables boost::mem_fn to recognize shared_ptr template inline T * get_pointer(shared_ptr const & p) { return p.get(); } // operator<< #if defined(__GNUC__) && (__GNUC__ < 3) template std::ostream & operator<< (std::ostream & os, shared_ptr 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 basic_ostream & operator<< (basic_ostream & os, shared_ptr const & p) # else template std::basic_ostream & operator<< (std::basic_ostream & os, shared_ptr const & p) # endif { os << p.get(); return os; } #endif // _STLP_NO_IOSTREAMS #endif // __GNUC__ < 3 // 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(void *) // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it template D * get_deleter(shared_ptr const & p) { void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D)); return const_cast(static_cast(q)); } #else template D * get_deleter(shared_ptr const & p) { return static_cast(p._internal_get_deleter(BOOST_SP_TYPEID(D))); } #endif } // namespace boost #ifdef BOOST_MSVC # pragma warning(pop) #endif #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) #endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED