1 #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
2 #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001, 2002, 2012 Peter Dimov
10 // Distributed under the Boost Software License, Version 1.0. (See
11 // accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
17 #include <boost/config.hpp> // for broken compiler workarounds
19 #include <memory> // TR1 cyclic inclusion fix
21 #include <boost/assert.hpp>
22 #include <boost/checked_delete.hpp>
24 #include <boost/smart_ptr/shared_ptr.hpp>
25 #include <boost/smart_ptr/detail/shared_count.hpp>
26 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
27 #include <boost/detail/workaround.hpp>
29 #include <cstddef> // for std::ptrdiff_t
30 #include <algorithm> // for std::swap
31 #include <functional> // for std::less
39 // shared_array extends shared_ptr to arrays.
40 // The array pointed to is deleted when the last shared_array pointing to it
41 // is destroyed or reset.
44 template<class T> class shared_array
48 // Borland 5.5.1 specific workarounds
49 typedef checked_array_deleter<T> deleter;
50 typedef shared_array<T> this_type;
54 typedef T element_type;
56 shared_array() BOOST_NOEXCEPT : px( 0 ), pn()
60 #if !defined( BOOST_NO_CXX11_NULLPTR )
62 shared_array( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT : px( 0 ), pn()
69 explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
71 boost::detail::sp_assert_convertible< Y[], T[] >();
75 // Requirements: D's copy constructor must not throw
77 // shared_array will release p by calling d(p)
80 template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
82 boost::detail::sp_assert_convertible< Y[], T[] >();
85 // As above, but with allocator. A's copy constructor shall not throw.
87 template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
89 boost::detail::sp_assert_convertible< Y[], T[] >();
92 // generated copy constructor, destructor are fine...
94 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
96 // ... except in C++0x, move disables the implicit copy
98 shared_array( shared_array const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
102 shared_array( shared_array && r ) BOOST_NOEXCEPT : px( r.px ), pn()
113 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
115 shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
119 shared_array( shared_array<Y> const & r )
122 BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) // never throws
124 boost::detail::sp_assert_convertible< Y[], T[] >();
130 shared_array( shared_array<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
136 shared_array & operator=( shared_array const & r ) BOOST_NOEXCEPT
138 this_type( r ).swap( *this );
142 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
145 shared_array & operator=( shared_array<Y> const & r ) BOOST_NOEXCEPT
147 this_type( r ).swap( *this );
153 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
155 shared_array & operator=( shared_array && r ) BOOST_NOEXCEPT
157 this_type( static_cast< shared_array && >( r ) ).swap( *this );
162 shared_array & operator=( shared_array<Y> && r ) BOOST_NOEXCEPT
164 this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
170 void reset() BOOST_NOEXCEPT
172 this_type().swap( *this );
175 template<class Y> void reset( Y * p ) // Y must be complete
177 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
178 this_type( p ).swap( *this );
181 template<class Y, class D> void reset( Y * p, D d )
183 this_type( p, d ).swap( *this );
186 template<class Y, class D, class A> void reset( Y * p, D d, A a )
188 this_type( p, d, a ).swap( *this );
191 template<class Y> void reset( shared_array<Y> const & r, element_type * p )
193 this_type( r, p ).swap( *this );
196 T & operator[] (std::ptrdiff_t i) const // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
198 BOOST_ASSERT(px != 0);
199 BOOST_ASSERT(i >= 0);
203 T * get() const BOOST_NOEXCEPT
208 // implicit conversion to "bool"
209 #include <boost/smart_ptr/detail/operator_bool.hpp>
211 bool unique() const BOOST_NOEXCEPT
216 long use_count() const BOOST_NOEXCEPT
218 return pn.use_count();
221 void swap(shared_array<T> & other) BOOST_NOEXCEPT
223 std::swap(px, other.px);
227 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const
229 return pn.get_deleter( ti );
234 template<class Y> friend class shared_array;
236 T * px; // contained pointer
237 detail::shared_count pn; // reference counter
241 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
243 return a.get() == b.get();
246 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
248 return a.get() != b.get();
251 #if !defined( BOOST_NO_CXX11_NULLPTR )
253 template<class T> inline bool operator==( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
258 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
263 template<class T> inline bool operator!=( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
268 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
275 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
277 return std::less<T*>()(a.get(), b.get());
280 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) BOOST_NOEXCEPT
285 template< class D, class T > D * get_deleter( shared_array<T> const & p )
287 return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID(D) ) );
292 #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED