Chris@16: #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED Chris@16: #define BOOST_SHARED_PTR_132_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // shared_ptr.hpp Chris@16: // Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // Copyright (c) 2001, 2002, 2003 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. Chris@16: // Chris@16: Chris@16: #include // for broken compiler workarounds Chris@16: Chris@16: #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) Chris@16: #include Chris@16: #else Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include // for std::auto_ptr Chris@16: #include // for std::swap Chris@16: #include // for std::less Chris@16: #include // for std::bad_cast Chris@16: #include // for std::basic_ostream Chris@16: Chris@16: #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable:4284) // odd return type for operator-> Chris@16: #endif Chris@16: Chris@16: namespace boost_132 { Chris@16: Chris@16: template class weak_ptr; Chris@16: template class enable_shared_from_this; Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: struct static_cast_tag {}; Chris@16: struct const_cast_tag {}; Chris@16: struct dynamic_cast_tag {}; Chris@16: struct polymorphic_cast_tag {}; Chris@16: Chris@16: template struct shared_ptr_traits Chris@16: { Chris@16: typedef T & reference; Chris@16: }; Chris@16: Chris@16: template<> struct shared_ptr_traits Chris@16: { Chris@16: typedef void reference; Chris@16: }; Chris@16: Chris@16: #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS) Chris@16: Chris@16: template<> struct shared_ptr_traits Chris@16: { Chris@16: typedef void reference; Chris@16: }; Chris@16: Chris@16: template<> struct shared_ptr_traits Chris@16: { Chris@16: typedef void reference; Chris@16: }; Chris@16: Chris@16: template<> struct shared_ptr_traits Chris@16: { Chris@16: typedef void reference; Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: // enable_shared_from_this support Chris@16: Chris@16: template void sp_enable_shared_from_this( shared_count const & pn, enable_shared_from_this< T > const * pe, Y const * px ) Chris@16: { Chris@16: if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast(px), pn); Chris@16: } Chris@16: Chris@16: inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... ) Chris@16: { Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: // Chris@16: // shared_ptr Chris@16: // Chris@16: // An enhanced relative of scoped_ptr with reference counted copy semantics. Chris@16: // The object pointed to is deleted when the last shared_ptr pointing to it Chris@16: // is destroyed or reset. Chris@16: // Chris@16: Chris@16: template class shared_ptr Chris@16: { Chris@16: private: Chris@16: // Borland 5.5.1 specific workaround Chris@16: typedef shared_ptr< T > this_type; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef T element_type; Chris@16: typedef T value_type; Chris@16: typedef T * pointer; Chris@101: typedef typename detail::shared_ptr_traits< T >::reference reference; Chris@16: Chris@16: shared_ptr(): px(0), pn() // never throws in 1.30+ Chris@16: { Chris@16: } Chris@16: Chris@16: #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x564) ) Chris@16: template Chris@16: explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter()) // Y must be complete Chris@16: #else Chris@16: template Chris@16: explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter()) // Y must be complete Chris@16: #endif Chris@16: { Chris@16: detail::sp_enable_shared_from_this( pn, p, p ); Chris@16: } Chris@16: Chris@16: // Chris@16: // Requirements: D's copy constructor must not throw Chris@16: // Chris@16: // shared_ptr will release p by calling d(p) Chris@16: // Chris@16: Chris@16: template shared_ptr(Y * p, D d): px(p), pn(p, d) Chris@16: { Chris@16: detail::sp_enable_shared_from_this( pn, p, p ); Chris@16: } Chris@16: Chris@16: // generated copy constructor, assignment, destructor are fine... Chris@16: Chris@16: // except that Borland C++ has a bug, and g++ with -Wsynth warns Chris@16: #if defined(__BORLANDC__) || defined(__GNUC__) Chris@16: Chris@16: shared_ptr & operator=(shared_ptr const & r) // never throws Chris@16: { Chris@16: px = r.px; Chris@16: pn = r.pn; // shared_count::op= doesn't throw Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: explicit shared_ptr(weak_ptr const & r): pn(r.pn) // may throw Chris@16: { Chris@16: // it is now safe to copy r.px, as pn(r.pn) did not throw Chris@16: px = r.px; Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws Chris@16: { Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr(shared_ptr const & r, detail::static_cast_tag): px(static_cast(r.px)), pn(r.pn) Chris@16: { Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr(shared_ptr const & r, detail::const_cast_tag): px(const_cast(r.px)), pn(r.pn) Chris@16: { Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr(shared_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) Chris@16: { Chris@16: if(px == 0) // need to allocate new counter -- the cast failed Chris@16: { Chris@16: pn = detail::shared_count(); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr(shared_ptr const & r, detail::polymorphic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) Chris@16: { Chris@16: if(px == 0) Chris@16: { Chris@16: boost::serialization::throw_exception(std::bad_cast()); Chris@16: } Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: Chris@16: template Chris@16: explicit shared_ptr(std::auto_ptr & r): px(r.get()), pn() Chris@16: { Chris@16: Y * tmp = r.get(); Chris@16: pn = detail::shared_count(r); Chris@16: detail::sp_enable_shared_from_this( pn, tmp, tmp ); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200) Chris@16: Chris@16: template Chris@16: shared_ptr & operator=(shared_ptr const & r) // never throws Chris@16: { Chris@16: px = r.px; Chris@16: pn = r.pn; // shared_count::op= doesn't throw Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: Chris@16: template Chris@16: shared_ptr & operator=(std::auto_ptr & r) Chris@16: { Chris@16: this_type(r).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: void reset() // never throws in 1.30+ Chris@16: { Chris@16: this_type().swap(*this); Chris@16: } Chris@16: Chris@16: template void reset(Y * p) // Y must be complete Chris@16: { Chris@16: BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors Chris@16: this_type(p).swap(*this); Chris@16: } Chris@16: Chris@16: template void reset(Y * p, D d) Chris@16: { Chris@16: this_type(p, d).swap(*this); Chris@16: } Chris@16: Chris@16: reference operator* () const // never throws Chris@16: { Chris@16: BOOST_ASSERT(px != 0); Chris@16: return *px; Chris@16: } Chris@16: Chris@16: T * operator-> () const // never throws Chris@16: { Chris@16: BOOST_ASSERT(px != 0); Chris@16: return px; Chris@16: } Chris@16: Chris@16: T * get() const // never throws Chris@16: { Chris@16: return px; Chris@16: } Chris@16: Chris@16: // implicit conversion to "bool" Chris@16: Chris@16: #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530) Chris@16: Chris@16: operator bool () const Chris@16: { Chris@16: return px != 0; Chris@16: } Chris@16: Chris@16: #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) Chris@16: typedef T * (this_type::*unspecified_bool_type)() const; Chris@16: Chris@16: operator unspecified_bool_type() const // never throws Chris@16: { Chris@16: return px == 0? 0: &this_type::get; Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: typedef T * this_type::*unspecified_bool_type; Chris@16: Chris@16: operator unspecified_bool_type() const // never throws Chris@16: { Chris@16: return px == 0? 0: &this_type::px; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // operator! is redundant, but some compilers need it Chris@16: Chris@16: bool operator! () const // never throws Chris@16: { Chris@16: return px == 0; Chris@16: } Chris@16: Chris@16: bool unique() const // never throws Chris@16: { Chris@16: return pn.unique(); Chris@16: } Chris@16: Chris@16: long use_count() const // never throws Chris@16: { Chris@16: return pn.use_count(); Chris@16: } Chris@16: Chris@16: void swap(shared_ptr< T > & other) // never throws Chris@16: { Chris@16: std::swap(px, other.px); Chris@16: pn.swap(other.pn); Chris@16: } Chris@16: Chris@16: template bool _internal_less(shared_ptr const & rhs) const Chris@16: { Chris@16: return pn < rhs.pn; Chris@16: } Chris@16: Chris@16: void * _internal_get_deleter(std::type_info const & ti) const Chris@16: { Chris@16: return pn.get_deleter(ti); Chris@16: } Chris@16: Chris@16: // Tasteless as this may seem, making all members public allows member templates Chris@16: // to work in the absence of member template friends. (Matthew Langston) Chris@16: Chris@16: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS Chris@16: Chris@16: private: Chris@16: Chris@16: template friend class shared_ptr; Chris@16: template friend class weak_ptr; Chris@16: Chris@16: Chris@16: #endif Chris@16: public: // for serialization Chris@16: T * px; // contained pointer Chris@16: detail::shared_count pn; // reference counter Chris@16: Chris@16: }; // shared_ptr Chris@16: Chris@16: template inline bool operator==(shared_ptr< T > const & a, shared_ptr const & b) Chris@16: { Chris@16: return a.get() == b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator!=(shared_ptr< T > const & a, shared_ptr const & b) Chris@16: { Chris@16: return a.get() != b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator<(shared_ptr< T > const & a, shared_ptr const & b) Chris@16: { Chris@16: return a._internal_less(b); Chris@16: } Chris@16: Chris@16: template inline void swap(shared_ptr< T > & a, shared_ptr< T > & b) Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: template shared_ptr< T > static_pointer_cast(shared_ptr const & r) Chris@16: { Chris@16: return shared_ptr< T >(r, detail::static_cast_tag()); Chris@16: } Chris@16: Chris@16: template shared_ptr< T > const_pointer_cast(shared_ptr const & r) Chris@16: { Chris@16: return shared_ptr< T >(r, detail::const_cast_tag()); Chris@16: } Chris@16: Chris@16: template shared_ptr< T > dynamic_pointer_cast(shared_ptr const & r) Chris@16: { Chris@16: return shared_ptr< T >(r, detail::dynamic_cast_tag()); Chris@16: } Chris@16: Chris@16: // shared_*_cast names are deprecated. Use *_pointer_cast instead. Chris@16: Chris@16: template shared_ptr< T > shared_static_cast(shared_ptr const & r) Chris@16: { Chris@16: return shared_ptr< T >(r, detail::static_cast_tag()); Chris@16: } Chris@16: Chris@16: template shared_ptr< T > shared_dynamic_cast(shared_ptr const & r) Chris@16: { Chris@16: return shared_ptr< T >(r, detail::dynamic_cast_tag()); Chris@16: } Chris@16: Chris@16: template shared_ptr< T > shared_polymorphic_cast(shared_ptr const & r) Chris@16: { Chris@16: return shared_ptr< T >(r, detail::polymorphic_cast_tag()); Chris@16: } Chris@16: Chris@16: template shared_ptr< T > shared_polymorphic_downcast(shared_ptr const & r) Chris@16: { Chris@16: BOOST_ASSERT(dynamic_cast(r.get()) == r.get()); Chris@16: return shared_static_cast< T >(r); Chris@16: } Chris@16: Chris@16: // get_pointer() enables boost::mem_fn to recognize shared_ptr Chris@16: Chris@16: template inline T * get_pointer(shared_ptr< T > const & p) Chris@16: { Chris@16: return p.get(); Chris@16: } Chris@16: Chris@16: // operator<< Chris@16: Chris@16: Chris@101: template std::basic_ostream & operator<< (std::basic_ostream & os, shared_ptr const & p) Chris@16: { Chris@16: os << p.get(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: // get_deleter (experimental) Chris@16: Chris@101: #if defined(__EDG_VERSION__) && (__EDG_VERSION__ <= 238) Chris@16: Chris@16: // g++ 2.9x doesn't allow static_cast(void *) Chris@16: // apparently EDG 2.38 also doesn't accept it Chris@16: Chris@16: template D * get_deleter(shared_ptr< T > const & p) Chris@16: { Chris@16: void const * q = p._internal_get_deleter(typeid(D)); Chris@16: return const_cast(static_cast(q)); Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: template D * get_deleter(shared_ptr< T > const & p) Chris@16: { Chris@16: return static_cast(p._internal_get_deleter(typeid(D))); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: # pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) Chris@16: Chris@16: #endif // #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED