mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-29 20:37:13 +02:00
@ -198,19 +198,7 @@ public:
|
||||
boost::detail::sp_enable_shared_from_this( this, p, p );
|
||||
}
|
||||
|
||||
// generated copy constructor, assignment, destructor are fine...
|
||||
|
||||
// except that Borland C++ has a bug, and g++ with -Wsynth warns
|
||||
#if defined(__BORLANDC__) || defined(__GNUC__)
|
||||
|
||||
shared_ptr & operator=(shared_ptr const & r) // never throws
|
||||
{
|
||||
px = r.px;
|
||||
pn = r.pn; // shared_count::op= doesn't throw
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
// generated copy constructor, destructor are fine
|
||||
|
||||
template<class Y>
|
||||
explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
|
||||
@ -301,13 +289,20 @@ public:
|
||||
|
||||
#endif // BOOST_NO_AUTO_PTR
|
||||
|
||||
#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
|
||||
// assignment
|
||||
|
||||
shared_ptr & operator=( shared_ptr const & r ) // never throws
|
||||
{
|
||||
this_type(r).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
|
||||
|
||||
template<class Y>
|
||||
shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
|
||||
{
|
||||
px = r.px;
|
||||
pn = r.pn; // shared_count::op= doesn't throw
|
||||
this_type(r).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -52,5 +52,9 @@ import testing ;
|
||||
[ run esft_second_ptr_test.cpp ]
|
||||
[ run make_shared_esft_test.cpp ]
|
||||
[ run allocate_shared_esft_test.cpp ]
|
||||
[ run sp_recursive_assign_test.cpp ]
|
||||
[ run sp_recursive_assign2_test.cpp ]
|
||||
[ run sp_recursive_assign_rv_test.cpp ]
|
||||
[ run sp_recursive_assign2_rv_test.cpp ]
|
||||
;
|
||||
}
|
||||
|
114
test/sp_recursive_assign2_rv_test.cpp
Normal file
114
test/sp_recursive_assign2_rv_test.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
//
|
||||
// sp_recursive_assign2_rv_test.cpp
|
||||
//
|
||||
// Copyright 2009 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/shared_ptr.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
//
|
||||
|
||||
class X
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
X( X const& );
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
class Y
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Y()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Y()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Y( Y const& );
|
||||
};
|
||||
|
||||
int Y::instances = 0;
|
||||
|
||||
static boost::shared_ptr<void> s_pv;
|
||||
|
||||
class Z
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Z()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Z()
|
||||
{
|
||||
--instances;
|
||||
s_pv = boost::shared_ptr<Y>( new Y );
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Z( Z const& );
|
||||
};
|
||||
|
||||
int Z::instances = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
s_pv = boost::shared_ptr<Z>( new Z );
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 1 );
|
||||
|
||||
s_pv = boost::shared_ptr<X>( new X );
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
s_pv = boost::shared_ptr<Y>();
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
122
test/sp_recursive_assign2_test.cpp
Normal file
122
test/sp_recursive_assign2_test.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
//
|
||||
// sp_recursive_assign2_test.cpp
|
||||
//
|
||||
// Copyright 2009 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/shared_ptr.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
//
|
||||
|
||||
class X
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
X( X const& );
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
class Y
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Y()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Y()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Y( Y const& );
|
||||
};
|
||||
|
||||
int Y::instances = 0;
|
||||
|
||||
static boost::shared_ptr<void> s_pv;
|
||||
|
||||
class Z
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Z()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Z()
|
||||
{
|
||||
--instances;
|
||||
|
||||
boost::shared_ptr<Y> pv( new Y );
|
||||
s_pv = pv;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Z( Z const& );
|
||||
};
|
||||
|
||||
int Z::instances = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
{
|
||||
boost::shared_ptr<Z> pv( new Z );
|
||||
s_pv = pv;
|
||||
}
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 1 );
|
||||
|
||||
{
|
||||
boost::shared_ptr<X> pv( new X );
|
||||
s_pv = pv;
|
||||
}
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
s_pv.reset();
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
114
test/sp_recursive_assign_rv_test.cpp
Normal file
114
test/sp_recursive_assign_rv_test.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
//
|
||||
// sp_recursive_assign_rv_test.cpp
|
||||
//
|
||||
// Copyright 2009 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/shared_ptr.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
//
|
||||
|
||||
class X
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
X( X const& );
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
class Y
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Y()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Y()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Y( Y const& );
|
||||
};
|
||||
|
||||
int Y::instances = 0;
|
||||
|
||||
static boost::shared_ptr<void> s_pv;
|
||||
|
||||
class Z
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Z()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Z()
|
||||
{
|
||||
--instances;
|
||||
s_pv = boost::shared_ptr<void>( new Y );
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Z( Z const& );
|
||||
};
|
||||
|
||||
int Z::instances = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
s_pv = boost::shared_ptr<void>( new Z );
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 1 );
|
||||
|
||||
s_pv = boost::shared_ptr<void>( new X );
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
s_pv = boost::shared_ptr<void>();
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
122
test/sp_recursive_assign_test.cpp
Normal file
122
test/sp_recursive_assign_test.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
//
|
||||
// sp_recursive_assign_test.cpp
|
||||
//
|
||||
// Copyright 2009 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/shared_ptr.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
//
|
||||
|
||||
class X
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
X( X const& );
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
class Y
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Y()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Y()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Y( Y const& );
|
||||
};
|
||||
|
||||
int Y::instances = 0;
|
||||
|
||||
static boost::shared_ptr<void> s_pv;
|
||||
|
||||
class Z
|
||||
{
|
||||
public:
|
||||
|
||||
static int instances;
|
||||
|
||||
Z()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~Z()
|
||||
{
|
||||
--instances;
|
||||
|
||||
boost::shared_ptr<void> pv( new Y );
|
||||
s_pv = pv;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Z( Z const& );
|
||||
};
|
||||
|
||||
int Z::instances = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
{
|
||||
boost::shared_ptr<void> pv( new Z );
|
||||
s_pv = pv;
|
||||
}
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 1 );
|
||||
|
||||
{
|
||||
boost::shared_ptr<void> pv( new X );
|
||||
s_pv = pv;
|
||||
}
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
s_pv.reset();
|
||||
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
BOOST_TEST( Z::instances == 0 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user