Chris@16: #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // shared_array.hpp Chris@16: // Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // Copyright (c) 2001, 2002, 2012 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation. Chris@16: // Chris@16: Chris@16: #include // for broken compiler workarounds Chris@16: Chris@16: #include // TR1 cyclic inclusion fix Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include // for std::ptrdiff_t Chris@16: #include // for std::swap Chris@16: #include // for std::less Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: // Chris@16: // shared_array Chris@16: // Chris@16: // shared_array extends shared_ptr to arrays. Chris@16: // The array pointed to is deleted when the last shared_array pointing to it Chris@16: // is destroyed or reset. Chris@16: // Chris@16: Chris@16: template class shared_array Chris@16: { Chris@16: private: Chris@16: Chris@16: // Borland 5.5.1 specific workarounds Chris@16: typedef checked_array_deleter deleter; Chris@16: typedef shared_array this_type; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef T element_type; Chris@16: Chris@16: shared_array() BOOST_NOEXCEPT : px( 0 ), pn() Chris@16: { Chris@16: } Chris@16: Chris@101: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@101: Chris@101: shared_array( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT : px( 0 ), pn() Chris@101: { Chris@101: } Chris@101: Chris@101: #endif Chris@101: Chris@16: template Chris@16: explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter() ) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y[], T[] >(); Chris@16: } Chris@16: Chris@16: // Chris@16: // Requirements: D's copy constructor must not throw Chris@16: // Chris@16: // shared_array will release p by calling d(p) Chris@16: // Chris@16: Chris@16: template shared_array( Y * p, D d ): px( p ), pn( p, d ) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y[], T[] >(); Chris@16: } Chris@16: Chris@16: // As above, but with allocator. A's copy constructor shall not throw. Chris@16: Chris@16: template shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a ) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y[], T[] >(); Chris@16: } Chris@16: Chris@16: // generated copy constructor, destructor are fine... Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: // ... except in C++0x, move disables the implicit copy Chris@16: Chris@16: shared_array( shared_array const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) Chris@16: { Chris@16: } Chris@16: Chris@16: shared_array( shared_array && r ) BOOST_NOEXCEPT : px( r.px ), pn() Chris@16: { Chris@16: pn.swap( r.pn ); Chris@16: r.px = 0; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // conversion Chris@16: Chris@16: template Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: shared_array( shared_array const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() ) Chris@16: Chris@16: #else Chris@16: Chris@16: shared_array( shared_array const & r ) Chris@16: Chris@16: #endif Chris@16: BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) // never throws Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y[], T[] >(); Chris@16: } Chris@16: Chris@16: // aliasing Chris@16: Chris@16: template< class Y > Chris@16: shared_array( shared_array const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn ) Chris@16: { Chris@16: } Chris@16: Chris@16: // assignment Chris@16: Chris@16: shared_array & operator=( shared_array const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( r ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400) Chris@16: Chris@16: template Chris@16: shared_array & operator=( shared_array const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( r ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: shared_array & operator=( shared_array && r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( static_cast< shared_array && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: shared_array & operator=( shared_array && r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( static_cast< shared_array && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: void reset() BOOST_NOEXCEPT Chris@16: { Chris@16: this_type().swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( Y * p ) // Y must be complete Chris@16: { Chris@16: BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors Chris@16: this_type( p ).swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( Y * p, D d ) Chris@16: { Chris@16: this_type( p, d ).swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( Y * p, D d, A a ) Chris@16: { Chris@16: this_type( p, d, a ).swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( shared_array const & r, element_type * p ) Chris@16: { Chris@16: this_type( r, p ).swap( *this ); Chris@16: } Chris@16: Chris@16: T & operator[] (std::ptrdiff_t i) const // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) Chris@16: { Chris@16: BOOST_ASSERT(px != 0); Chris@16: BOOST_ASSERT(i >= 0); Chris@16: return px[i]; Chris@16: } Chris@16: Chris@16: T * get() const BOOST_NOEXCEPT Chris@16: { Chris@16: return px; Chris@16: } Chris@16: Chris@16: // implicit conversion to "bool" Chris@16: #include Chris@16: Chris@16: bool unique() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.unique(); Chris@16: } Chris@16: Chris@16: long use_count() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.use_count(); Chris@16: } Chris@16: Chris@16: void swap(shared_array & other) BOOST_NOEXCEPT Chris@16: { Chris@16: std::swap(px, other.px); Chris@16: pn.swap(other.pn); Chris@16: } Chris@16: Chris@16: void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const Chris@16: { Chris@16: return pn.get_deleter( ti ); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: template friend class shared_array; Chris@16: Chris@16: T * px; // contained pointer Chris@16: detail::shared_count pn; // reference counter Chris@16: Chris@16: }; // shared_array Chris@16: Chris@16: template inline bool operator==(shared_array const & a, shared_array const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.get() == b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator!=(shared_array const & a, shared_array const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.get() != b.get(); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: template inline bool operator==( shared_array const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator==( boost::detail::sp_nullptr_t, shared_array const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( shared_array const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() != 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( boost::detail::sp_nullptr_t, shared_array const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() != 0; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template inline bool operator<(shared_array const & a, shared_array const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return std::less()(a.get(), b.get()); Chris@16: } Chris@16: Chris@16: template void swap(shared_array & a, shared_array & b) BOOST_NOEXCEPT Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: template< class D, class T > D * get_deleter( shared_array const & p ) Chris@16: { Chris@16: return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID(D) ) ); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED