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/ 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/smart_ptr/detail/sp_noexcept.hpp>
28 #include <boost/detail/workaround.hpp>
30 #include <cstddef> // for std::ptrdiff_t
31 #include <algorithm> // for std::swap
32 #include <functional> // for std::less
40 // shared_array extends shared_ptr to arrays.
41 // The array pointed to is deleted when the last shared_array pointing to it
42 // is destroyed or reset.
45 template<class T> class shared_array
49 // Borland 5.5.1 specific workarounds
50 typedef checked_array_deleter<T> deleter;
51 typedef shared_array<T> this_type;
55 typedef T element_type;
57 shared_array() BOOST_SP_NOEXCEPT : px( 0 ), pn()
61 #if !defined( BOOST_NO_CXX11_NULLPTR )
63 shared_array( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn()
70 explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
72 boost::detail::sp_assert_convertible< Y[], T[] >();
76 // Requirements: D's copy constructor must not throw
78 // shared_array will release p by calling d(p)
81 template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
83 boost::detail::sp_assert_convertible< Y[], T[] >();
86 // As above, but with allocator. A's copy constructor shall not throw.
88 template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
90 boost::detail::sp_assert_convertible< Y[], T[] >();
93 // generated copy constructor, destructor are fine...
95 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
97 // ... except in C++0x, move disables the implicit copy
99 shared_array( shared_array const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
103 shared_array( shared_array && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn()
114 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
116 shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
120 shared_array( shared_array<Y> const & r )
123 BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
125 boost::detail::sp_assert_convertible< Y[], T[] >();
131 shared_array( shared_array<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
137 shared_array & operator=( shared_array const & r ) BOOST_SP_NOEXCEPT
139 this_type( r ).swap( *this );
143 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
146 shared_array & operator=( shared_array<Y> const & r ) BOOST_SP_NOEXCEPT
148 this_type( r ).swap( *this );
154 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
156 shared_array & operator=( shared_array && r ) BOOST_SP_NOEXCEPT
158 this_type( static_cast< shared_array && >( r ) ).swap( *this );
163 shared_array & operator=( shared_array<Y> && r ) BOOST_SP_NOEXCEPT
165 this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
171 void reset() BOOST_SP_NOEXCEPT
173 this_type().swap( *this );
176 template<class Y> void reset( Y * p ) // Y must be complete
178 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
179 this_type( p ).swap( *this );
182 template<class Y, class D> void reset( Y * p, D d )
184 this_type( p, d ).swap( *this );
187 template<class Y, class D, class A> void reset( Y * p, D d, A a )
189 this_type( p, d, a ).swap( *this );
192 template<class Y> void reset( shared_array<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
194 this_type( r, p ).swap( *this );
197 T & operator[] (std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
199 BOOST_ASSERT(px != 0);
200 BOOST_ASSERT(i >= 0);
204 T * get() const BOOST_SP_NOEXCEPT
209 // implicit conversion to "bool"
210 #include <boost/smart_ptr/detail/operator_bool.hpp>
212 bool unique() const BOOST_SP_NOEXCEPT
217 long use_count() const BOOST_SP_NOEXCEPT
219 return pn.use_count();
222 void swap(shared_array<T> & other) BOOST_SP_NOEXCEPT
224 std::swap(px, other.px);
228 void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
230 return pn.get_deleter( ti );
235 template<class Y> friend class shared_array;
237 T * px; // contained pointer
238 detail::shared_count pn; // reference counter
242 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) BOOST_SP_NOEXCEPT
244 return a.get() == b.get();
247 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) BOOST_SP_NOEXCEPT
249 return a.get() != b.get();
252 #if !defined( BOOST_NO_CXX11_NULLPTR )
254 template<class T> inline bool operator==( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
259 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_SP_NOEXCEPT
264 template<class T> inline bool operator!=( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
269 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_SP_NOEXCEPT
276 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) BOOST_SP_NOEXCEPT
278 return std::less<T*>()(a.get(), b.get());
281 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) BOOST_SP_NOEXCEPT
286 template< class D, class T > D * get_deleter( shared_array<T> const & p ) BOOST_SP_NOEXCEPT
288 return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID_(D) ) );
293 #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED