|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r52795 - in sandbox/boost0x: boost/smart_ptr libs/smart_ptr/test
From: whatwasthataddress_at_[hidden]
Date: 2009-05-06 00:57:48
Author: tzlaine
Date: 2009-05-06 00:57:42 EDT (Wed, 06 May 2009)
New Revision: 52795
URL: http://svn.boost.org/trac/boost/changeset/52795
Log:
- Added weak_ptr move assignment operator.
- Changed the order of member initialization in weak_ptr's converting copy
ctor to match the order used in weak_ptr's converting assignment operator.
This reordering allowed the introduction of a weak_ptr converting move ctor.
- Removed nonconverting move ctor and nonconverting move assignment operator
from weak_ptr, since they were made redundant by the above.
- Added tests for new weak_ptr move ctor and move assignment.
Added:
sandbox/boost0x/libs/smart_ptr/test/weak_ptr_move_test.cpp (contents, props changed)
Text files modified:
sandbox/boost0x/boost/smart_ptr/weak_ptr.hpp | 48 +++++++++++++++++++++++----------------
sandbox/boost0x/libs/smart_ptr/test/Jamfile.v2 | 1
2 files changed, 29 insertions(+), 20 deletions(-)
Modified: sandbox/boost0x/boost/smart_ptr/weak_ptr.hpp
==============================================================================
--- sandbox/boost0x/boost/smart_ptr/weak_ptr.hpp (original)
+++ sandbox/boost0x/boost/smart_ptr/weak_ptr.hpp 2009-05-06 00:57:42 EDT (Wed, 06 May 2009)
@@ -70,11 +70,29 @@
weak_ptr( weak_ptr<Y> const & r )
#endif
- : pn(r.pn) // never throws
+ : px(r.lock().get()), pn(r.pn) // never throws
{
- px = r.lock().get();
}
+#if defined( BOOST_HAS_RVALUE_REFS )
+
+ template<class Y>
+#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
+
+ weak_ptr( weak_ptr<Y> && r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
+
+#else
+
+ weak_ptr( weak_ptr<Y> && r )
+
+#endif
+ : px(r.lock().get()), pn(std::move(r.pn)) // never throws
+ {
+ r.px = 0;
+ }
+
+#endif
+
template<class Y>
#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
@@ -99,37 +117,27 @@
return *this;
}
+#if defined( BOOST_HAS_RVALUE_REFS )
+
template<class Y>
- weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
+ weak_ptr & operator=(weak_ptr<Y> && r)
{
- px = r.px;
- pn = r.pn;
+ this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
return *this;
}
#endif
-
-// Move support
-
-#if defined( BOOST_HAS_RVALUE_REFS )
-
- weak_ptr( weak_ptr && r ): px( r.px ), pn() // never throws
- {
- pn.swap( r.pn );
- r.px = 0;
- }
-
- weak_ptr & operator=( weak_ptr && r ) // never throws
+ template<class Y>
+ weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
{
- this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
+ px = r.px;
+ pn = r.pn;
return *this;
}
#endif
-
-
shared_ptr<T> lock() const // never throws
{
return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
Modified: sandbox/boost0x/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- sandbox/boost0x/libs/smart_ptr/test/Jamfile.v2 (original)
+++ sandbox/boost0x/libs/smart_ptr/test/Jamfile.v2 2009-05-06 00:57:42 EDT (Wed, 06 May 2009)
@@ -16,6 +16,7 @@
[ run shared_ptr_basic_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-non-virtual-dtor ]
[ run shared_ptr_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-non-virtual-dtor ]
[ run weak_ptr_test.cpp ]
+ [ run weak_ptr_move_test.cpp ]
[ run shared_from_this_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-non-virtual-dtor ]
[ run get_deleter_test.cpp ]
[ run intrusive_ptr_test.cpp ]
Added: sandbox/boost0x/libs/smart_ptr/test/weak_ptr_move_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/boost0x/libs/smart_ptr/test/weak_ptr_move_test.cpp 2009-05-06 00:57:42 EDT (Wed, 06 May 2009)
@@ -0,0 +1,121 @@
+//
+// shared_ptr_move_test.cpp
+//
+// Copyright (c) 2007 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/weak_ptr.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#if defined( BOOST_HAS_RVALUE_REFS )
+
+struct X
+{
+ static long instances;
+
+ X()
+ {
+ ++instances;
+ }
+
+ ~X()
+ {
+ --instances;
+ }
+
+private:
+
+ X( X const & );
+ X & operator=( X const & );
+};
+
+long X::instances = 0;
+
+int main()
+{
+ BOOST_TEST( X::instances == 0 );
+
+ {
+ boost::shared_ptr<X> p_( new X );
+ boost::weak_ptr<X> p( p_ );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p.use_count() == 1 );
+
+ boost::weak_ptr<X> p2( static_cast< boost::weak_ptr<X> && >( p ) );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p2.use_count() == 1 );
+ BOOST_TEST( p.expired() );
+
+ boost::weak_ptr<void> p3( static_cast< boost::weak_ptr<X> && >( p2 ) );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p3.use_count() == 1 );
+ BOOST_TEST( p2.expired() );
+
+ p_.reset();
+ BOOST_TEST( X::instances == 0 );
+ BOOST_TEST( p3.expired() );
+ }
+
+ {
+ boost::shared_ptr<X> p_( new X );
+ boost::weak_ptr<X> p( p_ );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p.use_count() == 1 );
+
+ boost::weak_ptr<X> p2;
+ p2 = static_cast< boost::weak_ptr<X> && >( p );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p2.use_count() == 1 );
+ BOOST_TEST( p.expired() );
+
+ boost::weak_ptr<void> p3;
+ p3 = static_cast< boost::weak_ptr<X> && >( p2 );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p3.use_count() == 1 );
+ BOOST_TEST( p2.expired() );
+
+ p_.reset();
+ BOOST_TEST( X::instances == 0 );
+ BOOST_TEST( p3.expired() );
+ }
+
+ {
+ boost::shared_ptr<X> p_( new X );
+ boost::weak_ptr<X> p( p_ );
+ BOOST_TEST( X::instances == 1 );
+ BOOST_TEST( p.use_count() == 1 );
+
+ boost::shared_ptr<X> p_2( new X );
+ boost::weak_ptr<X> p2( p_2 );
+ BOOST_TEST( X::instances == 2 );
+ p2 = static_cast< boost::weak_ptr<X> && >( p );
+ BOOST_TEST( X::instances == 2 );
+ BOOST_TEST( p2.use_count() == 1 );
+ BOOST_TEST( p.expired() );
+ BOOST_TEST( p2.lock() != p_2 );
+
+ boost::shared_ptr<void> p_3( new X );
+ boost::weak_ptr<void> p3( p_3 );
+ BOOST_TEST( X::instances == 3 );
+ p3 = static_cast< boost::weak_ptr<X> && >( p2 );
+ BOOST_TEST( X::instances == 3 );
+ BOOST_TEST( p3.use_count() == 1 );
+ BOOST_TEST( p2.expired() );
+ BOOST_TEST( p3.lock() != p_3 );
+ }
+
+ return boost::report_errors();
+}
+
+#else // !defined( BOOST_HAS_RVALUE_REFS )
+
+int main()
+{
+ return 0;
+}
+
+#endif
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk