Index: boost/enable_shared_from_this.hpp =================================================================== --- boost/enable_shared_from_this.hpp (revision 44152) +++ boost/enable_shared_from_this.hpp (working copy) @@ -29,8 +29,8 @@ { if(owned() == false && _internal_shared_this == 0) { - _internal_shared_this = shared_ptr(dynamic_cast(const_cast(this)), - detail::sp_deleter_wrapper(), detail::ignore_enable_shared_from_this_tag()); + T* p = dynamic_cast(const_cast(this)); + _internal_shared_this = shared_ptr(detail::shared_count(p,detail::sp_deleter_wrapper()),p); BOOST_ASSERT(_internal_shared_this.get() == this); _internal_weak_this = _internal_shared_this; } @@ -109,6 +109,14 @@ } }; +template void sp_accept_owner( shared_ptr * ptr, enable_shared_from_this const * pe ) +{ + if(pe != 0) + { + pe->_internal_accept_owner(*ptr); + } +} + } // namespace boost #endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED Index: boost/detail/shared_count.hpp =================================================================== --- boost/detail/shared_count.hpp (revision 44152) +++ boost/detail/shared_count.hpp (working copy) @@ -313,20 +313,28 @@ weak_count & operator= (shared_count const & r) // nothrow { sp_counted_base * tmp = r.pi_; - if(tmp != 0) tmp->weak_add_ref(); - if(pi_ != 0) pi_->weak_release(); - pi_ = tmp; + if( tmp != pi_ ) + { + if(tmp != 0) tmp->weak_add_ref(); + if(pi_ != 0) pi_->weak_release(); + pi_ = tmp; + } + return *this; } weak_count & operator= (weak_count const & r) // nothrow { sp_counted_base * tmp = r.pi_; - if(tmp != 0) tmp->weak_add_ref(); - if(pi_ != 0) pi_->weak_release(); - pi_ = tmp; + if( tmp != pi_ ) + { + if(tmp != 0) tmp->weak_add_ref(); + if(pi_ != 0) pi_->weak_release(); + pi_ = tmp; + } + return *this; } Index: boost/shared_ptr.hpp =================================================================== --- boost/shared_ptr.hpp (revision 44152) +++ boost/shared_ptr.hpp (working copy) @@ -50,7 +50,6 @@ template class shared_ptr; template class weak_ptr; -template class enable_shared_from_this; namespace detail { @@ -89,18 +88,8 @@ #endif -// enable_shared_from_this support +// sp_accept_owner support -struct ignore_enable_shared_from_this_tag {}; - -template void sp_enable_shared_from_this( boost::shared_ptr * ptr, boost::enable_shared_from_this const * pe ) -{ - if(pe != 0) - { - pe->_internal_accept_owner(*ptr); - } -} - #ifdef _MANAGED // Avoid C4793, ... causes native code generation @@ -110,18 +99,18 @@ template sp_any_pointer( T* ) {} }; -inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer ) +inline void sp_accept_owner( 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 +// Turn off: the last argument of the varargs function "sp_accept_owner" is unnamed # pragma set woff 3506 #endif -inline void sp_enable_shared_from_this( ... ) +inline void sp_accept_owner( ... ) { } @@ -178,7 +167,8 @@ template explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete { - boost::detail::sp_enable_shared_from_this( this, p ); + using detail::sp_accept_owner; + sp_accept_owner( this, p ); } // @@ -189,14 +179,16 @@ template shared_ptr(Y * p, D d): px(p), pn(p, d) { - boost::detail::sp_enable_shared_from_this( this, p ); + using detail::sp_accept_owner; + sp_accept_owner( this, 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( this, p ); + using detail::sp_accept_owner; + sp_accept_owner( this, p ); } // generated copy constructor, assignment, destructor are fine... @@ -221,6 +213,12 @@ } template + shared_ptr(detail::shared_count c, Y * p): px(p) // never throws + { + pn.swap(c); + } + + template shared_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws { } @@ -259,12 +257,6 @@ } } -// constructor that doesn't trigger enable_shared_from_this code, needed -// for enable_shared_from_this internal implementation - template shared_ptr(Y * p, D d, detail::ignore_enable_shared_from_this_tag): - px(p), pn(p, d) - {} - #ifndef BOOST_NO_AUTO_PTR template @@ -272,7 +264,8 @@ { Y * tmp = r.get(); pn = boost::detail::shared_count(r); - boost::detail::sp_enable_shared_from_this( this, tmp ); + using detail::sp_accept_owner; + sp_accept_owner( this, tmp ); } #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) @@ -282,7 +275,8 @@ { typename Ap::element_type * tmp = r.get(); pn = boost::detail::shared_count( r ); - boost::detail::sp_enable_shared_from_this( this, tmp ); + using detail::sp_accept_owner; + sp_accept_owner( this, tmp ); } @@ -478,6 +472,11 @@ return pn.get_deleter( ti ); } + detail::shared_count const & _internal_shared_count() const // never throws + { + return pn; + } + // Tasteless as this may seem, making all members public allows member templates // to work in the absence of member template friends. (Matthew Langston) Index: boost/enable_shared_from_this_light.hpp =================================================================== --- boost/enable_shared_from_this_light.hpp (revision 0) +++ boost/enable_shared_from_this_light.hpp (revision 0) @@ -0,0 +1,69 @@ +#ifndef BOOST_ENABLE_SHARED_FROM_THIS_LIGHT_HPP_INCLUDED +#define BOOST_ENABLE_SHARED_FROM_THIS_LIGHT_HPP_INCLUDED + +// +// enable_shared_from_this_light.hpp +// +// Copyright (c) 2002 Peter Dimov +// Copyright (c) 2008 Daniel Frey +// +// 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) +// +// http://www.boost.org/libs/smart_ptr/enable_shared_from_this_light.html +// + +#include +#include + +namespace boost +{ + +template class enable_shared_from_this_light +{ +protected: + + enable_shared_from_this_light() + { + } + + enable_shared_from_this_light(enable_shared_from_this_light const &) + { + } + + enable_shared_from_this_light & operator=(enable_shared_from_this_light const &) + { + return *this; + } + + ~enable_shared_from_this_light() + { + } + +public: + + shared_ptr shared_from_this() // never throws + { + return shared_ptr(detail::shared_count(_internal_weak_count),static_cast(this)); + } + + shared_ptr shared_from_this() const // never throws + { + return shared_ptr(detail::shared_count(_internal_weak_count),static_cast(this)); + } + + mutable detail::weak_count _internal_weak_count; +}; + +template void sp_accept_owner( shared_ptr * ptr, enable_shared_from_this_light const * pe ) +{ + if(pe != 0) + { + pe->_internal_weak_count = ptr->_internal_shared_count(); + } +} + +} // namespace boost + +#endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS_LIGHT_HPP_INCLUDED