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 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20 #include <boost/smart_ptr/detail/shared_array_nmt.hpp>
23 #include <memory> // TR1 cyclic inclusion fix
25 #include <boost/assert.hpp>
26 #include <boost/checked_delete.hpp>
28 #include <boost/smart_ptr/shared_ptr.hpp>
29 #include <boost/smart_ptr/detail/shared_count.hpp>
30 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
31 #include <boost/detail/workaround.hpp>
33 #include <cstddef> // for std::ptrdiff_t
34 #include <algorithm> // for std::swap
35 #include <functional> // for std::less
43 // shared_array extends shared_ptr to arrays.
44 // The array pointed to is deleted when the last shared_array pointing to it
45 // is destroyed or reset.
48 template<class T> class shared_array
52 // Borland 5.5.1 specific workarounds
53 typedef checked_array_deleter<T> deleter;
54 typedef shared_array<T> this_type;
58 typedef T element_type;
60 shared_array() BOOST_NOEXCEPT : px( 0 ), pn()
65 explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
67 boost::detail::sp_assert_convertible< Y[], T[] >();
71 // Requirements: D's copy constructor must not throw
73 // shared_array will release p by calling d(p)
76 template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
78 boost::detail::sp_assert_convertible< Y[], T[] >();
81 // As above, but with allocator. A's copy constructor shall not throw.
83 template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
85 boost::detail::sp_assert_convertible< Y[], T[] >();
88 // generated copy constructor, destructor are fine...
90 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
92 // ... except in C++0x, move disables the implicit copy
94 shared_array( shared_array const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
98 shared_array( shared_array && r ) BOOST_NOEXCEPT : px( r.px ), pn()
109 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
111 shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
115 shared_array( shared_array<Y> const & r )
118 BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) // never throws
120 boost::detail::sp_assert_convertible< Y[], T[] >();
126 shared_array( shared_array<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
132 shared_array & operator=( shared_array const & r ) BOOST_NOEXCEPT
134 this_type( r ).swap( *this );
138 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
141 shared_array & operator=( shared_array<Y> const & r ) BOOST_NOEXCEPT
143 this_type( r ).swap( *this );
149 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
151 shared_array & operator=( shared_array && r ) BOOST_NOEXCEPT
153 this_type( static_cast< shared_array && >( r ) ).swap( *this );
158 shared_array & operator=( shared_array<Y> && r ) BOOST_NOEXCEPT
160 this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
166 void reset() BOOST_NOEXCEPT
168 this_type().swap( *this );
171 template<class Y> void reset( Y * p ) // Y must be complete
173 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
174 this_type( p ).swap( *this );
177 template<class Y, class D> void reset( Y * p, D d )
179 this_type( p, d ).swap( *this );
182 template<class Y, class D, class A> void reset( Y * p, D d, A a )
184 this_type( p, d, a ).swap( *this );
187 template<class Y> void reset( shared_array<Y> const & r, element_type * p )
189 this_type( r, p ).swap( *this );
192 T & operator[] (std::ptrdiff_t i) const // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
194 BOOST_ASSERT(px != 0);
195 BOOST_ASSERT(i >= 0);
199 T * get() const BOOST_NOEXCEPT
204 // implicit conversion to "bool"
205 #include <boost/smart_ptr/detail/operator_bool.hpp>
207 bool unique() const BOOST_NOEXCEPT
212 long use_count() const BOOST_NOEXCEPT
214 return pn.use_count();
217 void swap(shared_array<T> & other) BOOST_NOEXCEPT
219 std::swap(px, other.px);
223 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const
225 return pn.get_deleter( ti );
230 template<class Y> friend class shared_array;
232 T * px; // contained pointer
233 detail::shared_count pn; // reference counter
237 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
239 return a.get() == b.get();
242 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
244 return a.get() != b.get();
247 #if !defined( BOOST_NO_CXX11_NULLPTR )
249 template<class T> inline bool operator==( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
254 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
259 template<class T> inline bool operator!=( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
264 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_NOEXCEPT
271 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) BOOST_NOEXCEPT
273 return std::less<T*>()(a.get(), b.get());
276 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) BOOST_NOEXCEPT
281 template< class D, class T > D * get_deleter( shared_array<T> const & p )
283 return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID(D) ) );
288 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
290 #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED