2002-11-18 14:37:02 +00:00
|
|
|
#ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
|
|
|
|
#define BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
|
|
|
|
|
|
|
|
//
|
|
|
|
// enable_shared_from_this.hpp
|
|
|
|
//
|
|
|
|
// Copyright (c) 2002 Peter Dimov
|
|
|
|
//
|
2004-07-26 00:32:12 +00:00
|
|
|
// 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)
|
2002-11-18 14:37:02 +00:00
|
|
|
//
|
|
|
|
// http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
|
|
|
|
//
|
|
|
|
|
2008-04-22 19:48:39 +00:00
|
|
|
#include <boost/detail/shared_count.hpp>
|
2002-11-19 16:18:18 +00:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/config.hpp>
|
2002-11-18 14:37:02 +00:00
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
template<class T> class enable_shared_from_this
|
|
|
|
{
|
2008-03-20 19:32:43 +00:00
|
|
|
void init_internal_shared_once() const
|
|
|
|
{
|
2008-04-23 06:12:39 +00:00
|
|
|
if( !owned() && _internal_shared_count.empty() )
|
2008-03-20 19:32:43 +00:00
|
|
|
{
|
2008-04-23 19:32:44 +00:00
|
|
|
detail::shared_count( (void*)0, detail::sp_deleter_wrapper() ).swap(_internal_shared_count);
|
2008-04-23 06:12:39 +00:00
|
|
|
_internal_weak_count = _internal_shared_count;
|
2008-03-20 19:32:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool owned() const
|
|
|
|
{
|
|
|
|
return _owned;
|
|
|
|
}
|
|
|
|
|
2008-04-23 06:12:39 +00:00
|
|
|
mutable detail::shared_count _internal_shared_count;
|
2008-04-22 19:48:39 +00:00
|
|
|
mutable detail::weak_count _internal_weak_count;
|
2008-03-20 19:32:43 +00:00
|
|
|
mutable bool _owned;
|
2008-03-20 22:10:52 +00:00
|
|
|
|
2003-03-28 12:27:55 +00:00
|
|
|
protected:
|
|
|
|
|
2008-03-20 19:32:43 +00:00
|
|
|
enable_shared_from_this():
|
|
|
|
_owned(false)
|
2003-03-28 12:27:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-03-20 22:10:52 +00:00
|
|
|
enable_shared_from_this(enable_shared_from_this const &):
|
|
|
|
_owned(false)
|
2003-03-28 12:27:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_shared_from_this & operator=(enable_shared_from_this const &)
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-03-20 19:32:43 +00:00
|
|
|
// virtual destructor because we need a vtable for dynamic_cast from base to derived to work
|
|
|
|
virtual ~enable_shared_from_this()
|
2003-03-28 12:27:55 +00:00
|
|
|
{
|
2008-03-20 19:32:43 +00:00
|
|
|
// make sure no dangling shared_ptr objects were created by the
|
|
|
|
// user calling shared_from_this() but never passing ownership of the object
|
|
|
|
// to a shared_ptr.
|
2008-04-23 06:12:39 +00:00
|
|
|
BOOST_ASSERT(owned() || _internal_shared_count.use_count() <= 1);
|
2003-03-28 12:27:55 +00:00
|
|
|
}
|
|
|
|
|
2002-11-18 14:37:02 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
shared_ptr<T> shared_from_this()
|
|
|
|
{
|
2008-03-20 19:32:43 +00:00
|
|
|
init_internal_shared_once();
|
2008-04-22 19:48:39 +00:00
|
|
|
T * p = dynamic_cast<T *>(this);
|
|
|
|
return shared_ptr<T>( detail::shared_count( _internal_weak_count ), p );
|
2002-11-18 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<T const> shared_from_this() const
|
|
|
|
{
|
2008-03-20 19:32:43 +00:00
|
|
|
init_internal_shared_once();
|
2008-04-22 19:48:39 +00:00
|
|
|
T const * p = dynamic_cast<T const *>(this);
|
|
|
|
return shared_ptr<T const>( detail::shared_count( _internal_weak_count ), p );
|
2002-11-18 14:37:02 +00:00
|
|
|
}
|
|
|
|
|
2008-03-20 19:32:43 +00:00
|
|
|
template<typename U>
|
|
|
|
void _internal_accept_owner(shared_ptr<U> &owner) const
|
|
|
|
{
|
2008-03-25 15:46:40 +00:00
|
|
|
if( !_owned )
|
|
|
|
{
|
2008-04-23 06:12:39 +00:00
|
|
|
if( _internal_shared_count.empty() )
|
2008-04-16 00:06:29 +00:00
|
|
|
{
|
2008-04-22 19:48:39 +00:00
|
|
|
_internal_weak_count = owner.get_shared_count();
|
2008-04-16 00:06:29 +00:00
|
|
|
}else
|
|
|
|
{
|
2008-04-17 13:40:44 +00:00
|
|
|
BOOST_ASSERT(owner.unique()); // no weak_ptrs to owner should exist either, but there's no way to check that
|
2008-04-23 06:12:39 +00:00
|
|
|
typedef detail::sp_deleter_wrapper D;
|
|
|
|
D * pd = static_cast<D *>(_internal_shared_count.get_deleter(BOOST_SP_TYPEID(D)));
|
2008-04-16 00:06:29 +00:00
|
|
|
BOOST_ASSERT( pd != 0 );
|
|
|
|
pd->set_deleter(owner);
|
|
|
|
|
2008-04-23 06:12:39 +00:00
|
|
|
shared_ptr<U>( _internal_shared_count, owner.get() ).swap( owner );
|
|
|
|
detail::shared_count().swap(_internal_shared_count);
|
2008-04-16 00:06:29 +00:00
|
|
|
}
|
2008-03-25 15:46:40 +00:00
|
|
|
_owned = true;
|
|
|
|
}
|
2008-03-20 19:32:43 +00:00
|
|
|
}
|
2002-11-18 14:37:02 +00:00
|
|
|
};
|
|
|
|
|
2008-04-12 18:22:18 +00:00
|
|
|
template< class T, class Y > inline void sp_accept_owner( boost::shared_ptr<Y> * ptr, boost::enable_shared_from_this<T> const * pe )
|
|
|
|
{
|
|
|
|
if( pe != 0 )
|
|
|
|
{
|
|
|
|
pe->_internal_accept_owner( *ptr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template< class T, class Y > inline void sp_accept_owner( boost::shared_ptr<Y> * ptr, boost::enable_shared_from_this<T> const * pe, void * /*pd*/ )
|
|
|
|
{
|
|
|
|
if( pe != 0 )
|
|
|
|
{
|
|
|
|
pe->_internal_accept_owner( *ptr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template< class T, class Y > inline void sp_accept_owner( boost::shared_ptr<Y> * /*ptr*/, boost::enable_shared_from_this<T> const * /*pe*/, boost::detail::sp_deleter_wrapper * /*pd*/ )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-11-18 14:37:02 +00:00
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
|