mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-29 12:27:14 +02:00
Make shared_from_raw and weak_from_raw return consistent values in a constructor, regardless of order, as suggested by Gavin Lambert in #8.
This commit is contained in:
@ -53,7 +53,7 @@ protected:
|
||||
|
||||
private:
|
||||
|
||||
void init_weak_once() const
|
||||
void init_if_expired() const
|
||||
{
|
||||
if( weak_this_.expired() )
|
||||
{
|
||||
@ -62,6 +62,15 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void init_if_empty() const
|
||||
{
|
||||
if( weak_this_._empty() )
|
||||
{
|
||||
shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
|
||||
weak_this_ = shared_this_;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
||||
public:
|
||||
#else
|
||||
@ -74,7 +83,7 @@ private:
|
||||
|
||||
shared_ptr<void const volatile> shared_from_this() const
|
||||
{
|
||||
init_weak_once();
|
||||
init_if_expired();
|
||||
return shared_ptr<void const volatile>( weak_this_ );
|
||||
}
|
||||
|
||||
@ -83,6 +92,17 @@ private:
|
||||
return const_cast< enable_shared_from_raw const * >( this )->shared_from_this();
|
||||
}
|
||||
|
||||
weak_ptr<void const volatile> weak_from_this() const
|
||||
{
|
||||
init_if_empty();
|
||||
return weak_this_;
|
||||
}
|
||||
|
||||
weak_ptr<void const volatile> weak_from_this() const volatile
|
||||
{
|
||||
return const_cast< enable_shared_from_raw const * >( this )->weak_from_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
|
||||
{
|
||||
@ -125,7 +145,7 @@ 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);
|
||||
result._internal_aliasing_assign(p->enable_shared_from_raw::weak_from_this(), p);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -171,6 +171,8 @@ import testing ;
|
||||
|
||||
[ run weak_from_raw_test.cpp ]
|
||||
[ run weak_from_raw_test2.cpp ]
|
||||
[ run weak_from_raw_test3.cpp ]
|
||||
[ run weak_from_raw_test4.cpp ]
|
||||
|
||||
[ compile sp_explicit_inst_test.cpp ]
|
||||
;
|
||||
|
@ -12,7 +12,6 @@
|
||||
|
||||
|
||||
#include <boost/smart_ptr/enable_shared_from_raw.hpp>
|
||||
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
|
||||
@ -23,7 +22,7 @@ void basic_weak_from_raw_test()
|
||||
{
|
||||
X *p(new X);
|
||||
boost::weak_ptr<X> weak = boost::weak_from_raw(p);
|
||||
BOOST_TEST(weak.expired());
|
||||
BOOST_TEST(!weak.expired());
|
||||
boost::shared_ptr<X> shared(p);
|
||||
weak = boost::weak_from_raw(p);
|
||||
BOOST_TEST(weak.expired() == false);
|
||||
|
46
test/weak_from_raw_test3.cpp
Normal file
46
test/weak_from_raw_test3.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// weak_from_raw_test3.cpp
|
||||
//
|
||||
// Test that weak_from_raw and shared_from_raw
|
||||
// return consistent values from a constructor
|
||||
//
|
||||
// Copyright (c) 2015 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
|
||||
//
|
||||
|
||||
#include <boost/smart_ptr/enable_shared_from_raw.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
class X: public boost::enable_shared_from_raw
|
||||
{
|
||||
public:
|
||||
|
||||
X()
|
||||
{
|
||||
boost::weak_ptr<X> p1 = boost::weak_from_raw( this );
|
||||
BOOST_TEST( !p1.expired() );
|
||||
|
||||
boost::weak_ptr<X> p2 = boost::weak_from_raw( this );
|
||||
BOOST_TEST( !p2.expired() );
|
||||
BOOST_TEST( !( p1 < p2 ) && !( p2 < p1 ) );
|
||||
|
||||
boost::weak_ptr<X> p3 = boost::shared_from_raw( this );
|
||||
BOOST_TEST( !( p1 < p3 ) && !( p3 < p1 ) );
|
||||
|
||||
boost::weak_ptr<X> p4 = boost::weak_from_raw( this );
|
||||
BOOST_TEST( !p4.expired() );
|
||||
BOOST_TEST( !( p3 < p4 ) && !( p4 < p3 ) );
|
||||
BOOST_TEST( !( p1 < p4 ) && !( p4 < p1 ) );
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::shared_ptr< X > px( new X );
|
||||
return boost::report_errors();
|
||||
}
|
67
test/weak_from_raw_test4.cpp
Normal file
67
test/weak_from_raw_test4.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
//
|
||||
// weak_from_raw_test4.cpp
|
||||
//
|
||||
// As weak_from_raw_test2.cpp, but uses weak_from_this
|
||||
// in the constructor
|
||||
//
|
||||
// Copyright (c) 2014, 2015 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
|
||||
//
|
||||
|
||||
#include <boost/smart_ptr/enable_shared_from_raw.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
class X;
|
||||
|
||||
static boost::weak_ptr< X > r_;
|
||||
|
||||
void register_( boost::weak_ptr< X > const & r )
|
||||
{
|
||||
r_ = r;
|
||||
}
|
||||
|
||||
void check_( boost::weak_ptr< X > const & r )
|
||||
{
|
||||
BOOST_TEST( !( r < r_ ) && !( r_ < r ) );
|
||||
}
|
||||
|
||||
void unregister_( boost::weak_ptr< X > const & r )
|
||||
{
|
||||
BOOST_TEST( !( r < r_ ) && !( r_ < r ) );
|
||||
r_.reset();
|
||||
}
|
||||
|
||||
class X: public boost::enable_shared_from_raw
|
||||
{
|
||||
public:
|
||||
|
||||
X()
|
||||
{
|
||||
register_( boost::weak_from_raw( this ) );
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
unregister_( boost::weak_from_raw( this ) );
|
||||
}
|
||||
|
||||
void check()
|
||||
{
|
||||
check_( boost::weak_from_raw( this ) );
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::shared_ptr< X > px( new X );
|
||||
px->check();
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user