123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- // make_local_shared_esft_test.cpp
- //
- // Copyright 2007-2009, 2017 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/config.hpp>
- #if defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) || defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES )
- int main()
- {
- }
- #else
- #include <boost/core/lightweight_test.hpp>
- #include <boost/smart_ptr/make_local_shared.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/enable_shared_from_this.hpp>
- class X: public boost::enable_shared_from_this<X>
- {
- private:
- X( X const & );
- X & operator=( X const & );
- public:
- static int instances;
- explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
- {
- ++instances;
- }
- ~X()
- {
- --instances;
- }
- };
- int X::instances = 0;
- int main()
- {
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >();
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared_noinit< X >();
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6, 7 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- {
- boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
- BOOST_TEST( px.use_count() == 1 );
- BOOST_TEST( X::instances == 1 );
- try
- {
- boost::shared_ptr< X > qx = px->shared_from_this();
- BOOST_TEST( px == qx );
- BOOST_TEST( !( px < qx ) && !( qx < px ) );
- px.reset();
- BOOST_TEST( X::instances == 1 );
- }
- catch( boost::bad_weak_ptr const& )
- {
- BOOST_ERROR( "px->shared_from_this() failed" );
- }
- }
- BOOST_TEST( X::instances == 0 );
- return boost::report_errors();
- }
- #endif
|