mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-08-02 14:14:27 +02:00
Merged revision(s) 57197, 57206, 57423, 57518 from trunk:
Renamed enable_shared_from_this2 to enable_shared_from_raw and added shared_from_raw free function. These changes fix the pointer value in shared_ptr which were obtained before an external shared_ptr has taken ownership of the object (for example when a shared_ptr to this is obtained in an object's constructor). ........ Brought back code which fixes get_deleter when it is called on a deleter which has been wrapped inside a deleter_wrapper by "shared_from_raw() in constructors" support. ........ Added weak_from_raw(), for use in conjunction with enable_shared_from_raw base class. ........ Fixed access to enable_shared_from_raw::weak_this_ when BOOST_NO_MEMBER_TEMPLATE_FRIENDS is defined. ........ [SVN r81337]
This commit is contained in:
144
include/boost/smart_ptr/enable_shared_from_raw.hpp
Normal file
144
include/boost/smart_ptr/enable_shared_from_raw.hpp
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
#ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
|
||||||
|
#define BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
|
||||||
|
|
||||||
|
//
|
||||||
|
// enable_shared_from_raw.hpp
|
||||||
|
//
|
||||||
|
// Copyright 2002, 2009 Peter Dimov
|
||||||
|
// Copyright 2008-2009 Frank Mori Hess
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/weak_ptr.hpp>
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
#include <boost/detail/workaround.hpp>
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
|
||||||
|
template<typename T> boost::weak_ptr<T> weak_from_raw(T *);
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
class enable_shared_from_raw
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
|
||||||
|
enable_shared_from_raw()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_shared_from_raw( enable_shared_from_raw const & )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_shared_from_raw & operator=( enable_shared_from_raw const & )
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~enable_shared_from_raw()
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void init_weak_once() const
|
||||||
|
{
|
||||||
|
if( weak_this_.expired() )
|
||||||
|
{
|
||||||
|
shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
|
||||||
|
weak_this_ = shared_this_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
||||||
|
public:
|
||||||
|
#else
|
||||||
|
private:
|
||||||
|
template<class Y> friend class shared_ptr;
|
||||||
|
template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
|
||||||
|
template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
|
||||||
|
template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
shared_ptr<void> shared_from_this()
|
||||||
|
{
|
||||||
|
init_weak_once();
|
||||||
|
return shared_ptr<void>( weak_this_ );
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<const void> shared_from_this() const
|
||||||
|
{
|
||||||
|
init_weak_once();
|
||||||
|
return shared_ptr<const void>( weak_this_ );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: invoked automatically by shared_ptr; do not call
|
||||||
|
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( ppx != 0 );
|
||||||
|
|
||||||
|
if( weak_this_.expired() )
|
||||||
|
{
|
||||||
|
weak_this_ = *ppx;
|
||||||
|
}
|
||||||
|
else if( shared_this_.use_count() != 0 )
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
|
||||||
|
|
||||||
|
detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
|
||||||
|
BOOST_ASSERT( pd != 0 );
|
||||||
|
|
||||||
|
pd->set_deleter( *ppx );
|
||||||
|
|
||||||
|
ppx->reset( shared_this_, ppx->get() );
|
||||||
|
shared_this_.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutable weak_ptr<void> weak_this_;
|
||||||
|
private:
|
||||||
|
mutable shared_ptr<void> shared_this_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
boost::shared_ptr<T> shared_from_raw(T *p)
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(p != 0);
|
||||||
|
return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
boost::weak_ptr<T> weak_from_raw(T *p)
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(p != 0);
|
||||||
|
boost::weak_ptr<T> result;
|
||||||
|
result._internal_aliasing_assign(p->enable_shared_from_raw::weak_this_, p);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe )
|
||||||
|
{
|
||||||
|
if( pe != 0 )
|
||||||
|
{
|
||||||
|
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namepsace detail
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
|
@@ -1,132 +0,0 @@
|
|||||||
#ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
|
|
||||||
#define BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
|
|
||||||
|
|
||||||
//
|
|
||||||
// enable_shared_from_this2.hpp
|
|
||||||
//
|
|
||||||
// Copyright 2002, 2009 Peter Dimov
|
|
||||||
// Copyright 2008 Frank Mori Hess
|
|
||||||
//
|
|
||||||
// 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
|
|
||||||
//
|
|
||||||
|
|
||||||
#include <boost/config.hpp>
|
|
||||||
#include <boost/shared_ptr.hpp>
|
|
||||||
#include <boost/assert.hpp>
|
|
||||||
#include <boost/detail/workaround.hpp>
|
|
||||||
|
|
||||||
namespace boost
|
|
||||||
{
|
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
|
|
||||||
class esft2_deleter_wrapper
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
|
|
||||||
shared_ptr<void> deleter_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
esft2_deleter_wrapper()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
template< class T > void set_deleter( shared_ptr<T> const & deleter )
|
|
||||||
{
|
|
||||||
deleter_ = deleter;
|
|
||||||
}
|
|
||||||
|
|
||||||
template< class T> void operator()( T* )
|
|
||||||
{
|
|
||||||
BOOST_ASSERT( deleter_.use_count() <= 1 );
|
|
||||||
deleter_.reset();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template< class T > class enable_shared_from_this2
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
|
|
||||||
enable_shared_from_this2()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
enable_shared_from_this2( enable_shared_from_this2 const & )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
enable_shared_from_this2 & operator=( enable_shared_from_this2 const & )
|
|
||||||
{
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~enable_shared_from_this2()
|
|
||||||
{
|
|
||||||
BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
mutable weak_ptr<T> weak_this_;
|
|
||||||
mutable shared_ptr<T> shared_this_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
shared_ptr<T> shared_from_this()
|
|
||||||
{
|
|
||||||
init_weak_once();
|
|
||||||
return shared_ptr<T>( weak_this_ );
|
|
||||||
}
|
|
||||||
|
|
||||||
shared_ptr<T const> shared_from_this() const
|
|
||||||
{
|
|
||||||
init_weak_once();
|
|
||||||
return shared_ptr<T>( weak_this_ );
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
void init_weak_once() const
|
|
||||||
{
|
|
||||||
if( weak_this_._empty() )
|
|
||||||
{
|
|
||||||
shared_this_.reset( static_cast< T* >( 0 ), detail::esft2_deleter_wrapper() );
|
|
||||||
weak_this_ = shared_this_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public: // actually private, but avoids compiler template friendship issues
|
|
||||||
|
|
||||||
// Note: invoked automatically by shared_ptr; do not call
|
|
||||||
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
|
|
||||||
{
|
|
||||||
BOOST_ASSERT( ppx != 0 );
|
|
||||||
|
|
||||||
if( weak_this_.use_count() == 0 )
|
|
||||||
{
|
|
||||||
weak_this_ = shared_ptr<T>( *ppx, py );
|
|
||||||
}
|
|
||||||
else if( shared_this_.use_count() != 0 )
|
|
||||||
{
|
|
||||||
BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
|
|
||||||
|
|
||||||
detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
|
|
||||||
BOOST_ASSERT( pd != 0 );
|
|
||||||
|
|
||||||
pd->set_deleter( *ppx );
|
|
||||||
|
|
||||||
ppx->reset( shared_this_, ppx->get() );
|
|
||||||
shared_this_.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace boost
|
|
||||||
|
|
||||||
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
|
|
@@ -57,7 +57,7 @@ namespace boost
|
|||||||
template<class T> class shared_ptr;
|
template<class T> class shared_ptr;
|
||||||
template<class T> class weak_ptr;
|
template<class T> class weak_ptr;
|
||||||
template<class T> class enable_shared_from_this;
|
template<class T> class enable_shared_from_this;
|
||||||
template<class T> class enable_shared_from_this2;
|
class enable_shared_from_raw;
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
@@ -106,13 +106,7 @@ template< class X, class Y, class T > inline void sp_enable_shared_from_this( bo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 )
|
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
|
||||||
{
|
|
||||||
if( pe != 0 )
|
|
||||||
{
|
|
||||||
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef _MANAGED
|
#ifdef _MANAGED
|
||||||
|
|
||||||
@@ -612,6 +606,9 @@ template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::
|
|||||||
|
|
||||||
// get_deleter
|
// get_deleter
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
#if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
|
#if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
|
||||||
( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
|
( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
|
||||||
( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
|
( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
|
||||||
@@ -619,7 +616,7 @@ template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::
|
|||||||
// g++ 2.9x doesn't allow static_cast<X const *>(void *)
|
// g++ 2.9x doesn't allow static_cast<X const *>(void *)
|
||||||
// apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
|
// apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
|
||||||
|
|
||||||
template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
|
template<class D, class T> D * basic_get_deleter(shared_ptr<T> const & p)
|
||||||
{
|
{
|
||||||
void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
|
void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
|
||||||
return const_cast<D *>(static_cast<D const *>(q));
|
return const_cast<D *>(static_cast<D const *>(q));
|
||||||
@@ -627,13 +624,55 @@ template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
|
template<class D, class T> D * basic_get_deleter(shared_ptr<T> const & p)
|
||||||
{
|
{
|
||||||
return static_cast<D *>(p._internal_get_deleter(BOOST_SP_TYPEID(D)));
|
return static_cast<D *>(p._internal_get_deleter(BOOST_SP_TYPEID(D)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
class esft2_deleter_wrapper
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
shared_ptr<void> deleter_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
esft2_deleter_wrapper()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template< class T > void set_deleter( shared_ptr<T> const & deleter )
|
||||||
|
{
|
||||||
|
deleter_ = deleter;
|
||||||
|
}
|
||||||
|
template<typename D> D* get_deleter() const
|
||||||
|
{
|
||||||
|
return boost::detail::basic_get_deleter<D>(deleter_);
|
||||||
|
}
|
||||||
|
template< class T> void operator()( T* )
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( deleter_.use_count() <= 1 );
|
||||||
|
deleter_.reset();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
|
||||||
|
{
|
||||||
|
D *del = detail::basic_get_deleter<D>(p);
|
||||||
|
if(del == 0)
|
||||||
|
{
|
||||||
|
detail::esft2_deleter_wrapper *del_wrapper = detail::basic_get_deleter<detail::esft2_deleter_wrapper>(p);
|
||||||
|
// The following get_deleter method call is fully qualified because
|
||||||
|
// older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
|
||||||
|
if(del_wrapper) del = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
|
||||||
|
}
|
||||||
|
return del;
|
||||||
|
}
|
||||||
|
|
||||||
// atomic access
|
// atomic access
|
||||||
|
|
||||||
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
|
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
|
||||||
|
@@ -194,10 +194,11 @@ public:
|
|||||||
pn.swap(other.pn);
|
pn.swap(other.pn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
|
template<typename Y>
|
||||||
|
void _internal_aliasing_assign(weak_ptr<Y> const & r, T * px2)
|
||||||
{
|
{
|
||||||
px = px2;
|
px = px2;
|
||||||
pn = pn2;
|
pn = r.pn;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
|
template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
|
||||||
|
Reference in New Issue
Block a user