|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r81272 - in trunk: boost/smart_ptr libs/smart_ptr/test
From: pdimov_at_[hidden]
Date: 2012-11-09 19:04:50
Author: pdimov
Date: 2012-11-09 19:04:49 EST (Fri, 09 Nov 2012)
New Revision: 81272
URL: http://svn.boost.org/trac/boost/changeset/81272
Log:
Updated shared_array to match shared_ptr. Refs #1113.
Text files modified:
trunk/boost/smart_ptr/shared_array.hpp | 106 ++++++++++++++++++++++++++++++++++++---
trunk/libs/smart_ptr/test/smart_ptr_test.cpp | 4
2 files changed, 100 insertions(+), 10 deletions(-)
Modified: trunk/boost/smart_ptr/shared_array.hpp
==============================================================================
--- trunk/boost/smart_ptr/shared_array.hpp (original)
+++ trunk/boost/smart_ptr/shared_array.hpp 2012-11-09 19:04:49 EST (Fri, 09 Nov 2012)
@@ -5,7 +5,7 @@
// shared_array.hpp
//
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
-// Copyright (c) 2001, 2002 Peter Dimov
+// Copyright (c) 2001, 2002, 2012 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
@@ -25,6 +25,7 @@
#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
+#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/detail/shared_count.hpp>
#include <boost/detail/workaround.hpp>
@@ -55,18 +56,32 @@
typedef T element_type;
- explicit shared_array(T * p = 0): px(p), pn(p, deleter())
+ shared_array(): px( 0 ), pn() // never throws
{
}
+ template<class Y>
+ explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
+ {
+ boost::detail::sp_assert_convertible< Y[], T[] >();
+ }
+
//
// Requirements: D's copy constructor must not throw
//
// shared_array will release p by calling d(p)
//
- template<class D> shared_array(T * p, D d): px(p), pn(p, d)
+ template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
+ {
+ boost::detail::sp_assert_convertible< Y[], T[] >();
+ }
+
+ // As above, but with allocator. A's copy constructor shall not throw.
+
+ template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
{
+ boost::detail::sp_assert_convertible< Y[], T[] >();
}
// generated copy constructor, destructor are fine...
@@ -79,8 +94,38 @@
{
}
+ shared_array( shared_array && r ): px( r.px ), pn() // never throws
+ {
+ pn.swap( r.pn );
+ r.px = 0;
+ }
+
#endif
+ // conversion
+
+ template<class Y>
+#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
+
+ shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
+
+#else
+
+ shared_array( shared_array<Y> const & r )
+
+#endif
+ : px( r.px ), pn( r.pn ) // never throws
+ {
+ boost::detail::sp_assert_convertible< Y[], T[] >();
+ }
+
+ // aliasing
+
+ template< class Y >
+ shared_array( shared_array<Y> const & r, element_type * p ): px( p ), pn( r.pn ) // never throws
+ {
+ }
+
// assignment
shared_array & operator=( shared_array const & r ) // never throws
@@ -89,15 +134,58 @@
return *this;
}
- void reset(T * p = 0)
+#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
+
+ template<class Y>
+ shared_array & operator=( shared_array<Y> const & r ) // never throws
+ {
+ this_type( r ).swap( *this );
+ return *this;
+ }
+
+#endif
+
+#if defined( BOOST_HAS_RVALUE_REFS )
+
+ shared_array & operator=( shared_array && r ) // never throws
+ {
+ this_type( static_cast< shared_array && >( r ) ).swap( *this );
+ return *this;
+ }
+
+ template<class Y>
+ shared_array & operator=( shared_array<Y> && r ) // never throws
{
- BOOST_ASSERT(p == 0 || p != px);
- this_type(p).swap(*this);
+ this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
+ return *this;
}
- template <class D> void reset(T * p, D d)
+#endif
+
+ void reset() // never throws
{
- this_type(p, d).swap(*this);
+ this_type().swap( *this );
+ }
+
+ template<class Y> void reset( Y * p ) // Y must be complete
+ {
+ BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
+ this_type( p ).swap( *this );
+ }
+
+ template<class Y, class D> void reset( Y * p, D d )
+ {
+ this_type( p, d ).swap( *this );
+ }
+
+ template<class Y, class D, class A> void reset( Y * p, D d, A a )
+ {
+ this_type( p, d, a ).swap( *this );
+ }
+
+ template<class Y> void reset( shared_array<Y> const & r, element_type * p )
+ {
+ this_type( r, p ).swap( *this );
}
T & operator[] (std::ptrdiff_t i) const // never throws
@@ -138,6 +226,8 @@
private:
+ template<class Y> friend class shared_array;
+
T * px; // contained pointer
detail::shared_count pn; // reference counter
Modified: trunk/libs/smart_ptr/test/smart_ptr_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/smart_ptr_test.cpp (original)
+++ trunk/libs/smart_ptr/test/smart_ptr_test.cpp 2012-11-09 19:04:49 EST (Fri, 09 Nov 2012)
@@ -236,7 +236,7 @@
ca2.reset();
BOOST_TEST( ca.use_count() == 2 );
BOOST_TEST( ca3.use_count() == 2 );
- BOOST_TEST( ca2.use_count() == 1 );
+ BOOST_TEST( ca2.use_count() == 0 );
ca.reset();
BOOST_TEST( ca.get() == 0 );
@@ -269,7 +269,7 @@
udta2.reset();
BOOST_TEST( udta2.get() == 0 );
BOOST_TEST( udta.use_count() == 1 );
- BOOST_TEST( udta2.use_count() == 1 );
+ BOOST_TEST( udta2.use_count() == 0 );
BOOST_TEST( UDT_use_count == 4 ); // reality check
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