forked from boostorg/smart_ptr
Add tests for enable_shared_from
This commit is contained in:
@ -315,3 +315,7 @@ run sp_typeinfo_test.cpp : : : <rtti>off : sp_typeinfo_test_no_rtti ;
|
|||||||
run get_deleter_test.cpp : : : <rtti>off <toolset>gcc-4.4.7,<cxxstd>0x:<build>no : get_deleter_test_no_rtti ;
|
run get_deleter_test.cpp : : : <rtti>off <toolset>gcc-4.4.7,<cxxstd>0x:<build>no : get_deleter_test_no_rtti ;
|
||||||
run get_deleter_test2.cpp : : : <rtti>off <toolset>gcc-4.4.7,<cxxstd>0x:<build>no : get_deleter_test2_no_rtti ;
|
run get_deleter_test2.cpp : : : <rtti>off <toolset>gcc-4.4.7,<cxxstd>0x:<build>no : get_deleter_test2_no_rtti ;
|
||||||
run get_deleter_test3.cpp : : : <rtti>off <toolset>gcc-4.4.7,<cxxstd>0x:<build>no : get_deleter_test3_no_rtti ;
|
run get_deleter_test3.cpp : : : <rtti>off <toolset>gcc-4.4.7,<cxxstd>0x:<build>no : get_deleter_test3_no_rtti ;
|
||||||
|
|
||||||
|
run shared_from_test.cpp ;
|
||||||
|
run weak_from_test.cpp ;
|
||||||
|
run weak_from_test2.cpp ;
|
||||||
|
102
test/shared_from_test.cpp
Normal file
102
test/shared_from_test.cpp
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
|
||||||
|
// shared_from_test.cpp
|
||||||
|
//
|
||||||
|
// Copyright 2019 Peter Dimov
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/enable_shared_from.hpp>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
class X
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
int m_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
X(): m_() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Y: public boost::enable_shared_from
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
class Z: public X, public Y
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
boost::shared_ptr<Z> shared_from_this()
|
||||||
|
{
|
||||||
|
return boost::shared_from( this );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void null_deleter( void const* )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> p( new Z );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> q = p->shared_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( p, q );
|
||||||
|
BOOST_TEST( !( p < q ) && !( q < p ) );
|
||||||
|
}
|
||||||
|
catch( boost::bad_weak_ptr const & )
|
||||||
|
{
|
||||||
|
BOOST_ERROR( "p->shared_from_this() failed" );
|
||||||
|
}
|
||||||
|
|
||||||
|
Z v2( *p );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> q = v2.shared_from_this();
|
||||||
|
BOOST_ERROR( "v2.shared_from_this() failed to throw" );
|
||||||
|
}
|
||||||
|
catch( boost::bad_weak_ptr const & )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
*p = Z();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> q = p->shared_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( p, q );
|
||||||
|
BOOST_TEST( !( p < q ) && !( q < p ) );
|
||||||
|
}
|
||||||
|
catch( boost::bad_weak_ptr const & )
|
||||||
|
{
|
||||||
|
BOOST_ERROR( "p->shared_from_this() threw bad_weak_ptr after *p = Z()" );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> p2( p.get(), null_deleter );
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> q = p->shared_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( p, q );
|
||||||
|
BOOST_TEST( !( p < q ) && !( q < p ) );
|
||||||
|
}
|
||||||
|
catch( boost::bad_weak_ptr const& )
|
||||||
|
{
|
||||||
|
BOOST_ERROR( "p->shared_from_this() failed" );
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
97
test/weak_from_test.cpp
Normal file
97
test/weak_from_test.cpp
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
|
||||||
|
// weak_from_test.cpp
|
||||||
|
//
|
||||||
|
// Copyright 2019 Peter Dimov
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/enable_shared_from.hpp>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/weak_ptr.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
class X
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
int m_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
X(): m_() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Y: public boost::enable_shared_from
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
class Z: public X, public Y
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
boost::weak_ptr<Z> weak_from_this()
|
||||||
|
{
|
||||||
|
return boost::weak_from( this );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void null_deleter( void const* )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> sp( new Z );
|
||||||
|
boost::weak_ptr<Z> p( sp );
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::weak_ptr<Z> q = sp->weak_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( p.lock(), q.lock() );
|
||||||
|
BOOST_TEST( !( p < q ) && !( q < p ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
Z v2( *sp );
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::weak_ptr<Z> q = v2.weak_from_this();
|
||||||
|
BOOST_TEST( q.expired() );
|
||||||
|
}
|
||||||
|
|
||||||
|
*sp = Z();
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::weak_ptr<Z> q = sp->weak_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( p.lock(), q.lock() );
|
||||||
|
BOOST_TEST( !( p < q ) && !( q < p ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::shared_ptr<Z> sp2( sp.get(), null_deleter );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::weak_ptr<Z> q = sp->weak_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( p.lock(), q.lock() );
|
||||||
|
BOOST_TEST( !( p < q ) && !( q < p ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::weak_ptr<Z> p2 = sp->weak_from_this();
|
||||||
|
|
||||||
|
BOOST_TEST( !p.expired() );
|
||||||
|
BOOST_TEST( !p2.expired() );
|
||||||
|
|
||||||
|
sp.reset();
|
||||||
|
|
||||||
|
BOOST_TEST( p.expired() );
|
||||||
|
BOOST_TEST( p2.expired() );
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
57
test/weak_from_test2.cpp
Normal file
57
test/weak_from_test2.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
|
||||||
|
// weak_from_test2.cpp
|
||||||
|
//
|
||||||
|
// Tests weak_from in a destructor
|
||||||
|
//
|
||||||
|
// Copyright 2014, 2015, 2019 Peter Dimov
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/enable_shared_from.hpp>
|
||||||
|
#include <boost/weak_ptr.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
|
||||||
|
class X: public boost::enable_shared_from
|
||||||
|
{
|
||||||
|
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