mirror of
https://github.com/boostorg/smart_ptr.git
synced 2026-04-29 18:44:15 +02:00
Add weak_from_this.
This commit is contained in:
@@ -175,5 +175,8 @@ import testing ;
|
||||
[ run weak_from_raw_test4.cpp ]
|
||||
|
||||
[ compile sp_explicit_inst_test.cpp ]
|
||||
|
||||
[ run weak_from_this_test.cpp ]
|
||||
[ run weak_from_this_test2.cpp ]
|
||||
;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// weak_from_raw_test4.cpp
|
||||
//
|
||||
// As weak_from_raw_test2.cpp, but uses weak_from_this
|
||||
// As weak_from_raw_test2.cpp, but uses weak_from_raw
|
||||
// in the constructor
|
||||
//
|
||||
// Copyright (c) 2014, 2015 Peter Dimov
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <boost/config.hpp>
|
||||
|
||||
//
|
||||
// weak_from_this_test.cpp
|
||||
//
|
||||
// Copyright (c) 2002, 2003, 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/enable_shared_from_this.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
class V: public boost::enable_shared_from_this<V>
|
||||
{
|
||||
};
|
||||
|
||||
void test()
|
||||
{
|
||||
boost::shared_ptr<V> p( new V );
|
||||
|
||||
boost::weak_ptr<V> q = p;
|
||||
BOOST_TEST( !q.expired() );
|
||||
|
||||
boost::weak_ptr<V> q2 = p->weak_from_this();
|
||||
BOOST_TEST( !q2.expired() );
|
||||
BOOST_TEST( !(q < q2) && !(q2 < q) );
|
||||
|
||||
V v2( *p );
|
||||
|
||||
boost::weak_ptr<V> q3 = v2.weak_from_this();
|
||||
BOOST_TEST( q3.expired() );
|
||||
|
||||
*p = V();
|
||||
|
||||
boost::weak_ptr<V> q4 = p->shared_from_this();
|
||||
BOOST_TEST( !q4.expired() );
|
||||
BOOST_TEST( !(q < q4) && !(q4 < q) );
|
||||
BOOST_TEST( !(q2 < q4) && !(q4 < q2) );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test();
|
||||
return boost::report_errors();
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// weak_from_this_test2.cpp
|
||||
//
|
||||
// Tests weak_from_this in a destructor
|
||||
//
|
||||
// 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_this.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
class X: public boost::enable_shared_from_this< X >
|
||||
{
|
||||
private:
|
||||
|
||||
boost::weak_ptr<X> px_;
|
||||
|
||||
public:
|
||||
|
||||
X()
|
||||
{
|
||||
boost::weak_ptr<X> p1 = weak_from_this();
|
||||
BOOST_TEST( p1._empty() );
|
||||
BOOST_TEST( p1.expired() );
|
||||
}
|
||||
|
||||
void check()
|
||||
{
|
||||
boost::weak_ptr<X> p2 = weak_from_this();
|
||||
BOOST_TEST( !p2.expired() );
|
||||
|
||||
BOOST_TEST( p2.lock().get() == this );
|
||||
|
||||
px_ = p2;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
boost::weak_ptr<X> p3 = weak_from_this();
|
||||
BOOST_TEST( p3.expired() );
|
||||
|
||||
BOOST_TEST( !(px_ < p3) && !(p3 < px_) );
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::shared_ptr< X > px( new X );
|
||||
px->check();
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user