Chris@16: #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED Chris@16: Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // Copyright (c) 2001, 2002 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: // http://www.boost.org/libs/smart_ptr/scoped_array.htm Chris@16: // Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #include // for std::ptrdiff_t Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: // Debug hooks Chris@16: Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: Chris@16: void sp_array_constructor_hook(void * p); Chris@16: void sp_array_destructor_hook(void * p); Chris@16: Chris@16: #endif Chris@16: Chris@16: // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to Chris@16: // is guaranteed, either on destruction of the scoped_array or via an explicit Chris@16: // reset(). Use shared_array or std::vector if your needs are more complex. Chris@16: Chris@16: template class scoped_array // noncopyable Chris@16: { Chris@16: private: Chris@16: Chris@16: T * px; Chris@16: Chris@16: scoped_array(scoped_array const &); Chris@16: scoped_array & operator=(scoped_array const &); Chris@16: Chris@16: typedef scoped_array this_type; Chris@16: Chris@16: void operator==( scoped_array const& ) const; Chris@16: void operator!=( scoped_array const& ) const; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef T element_type; Chris@16: Chris@16: explicit scoped_array( T * p = 0 ) BOOST_NOEXCEPT : px( p ) Chris@16: { Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: boost::sp_array_constructor_hook( px ); Chris@16: #endif Chris@16: } Chris@16: Chris@16: ~scoped_array() // never throws Chris@16: { Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: boost::sp_array_destructor_hook( px ); Chris@16: #endif Chris@16: boost::checked_array_delete( px ); Chris@16: } Chris@16: Chris@16: void reset(T * p = 0) // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) 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: 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: void swap(scoped_array & b) BOOST_NOEXCEPT Chris@16: { Chris@16: T * tmp = b.px; Chris@16: b.px = px; Chris@16: px = tmp; Chris@16: } Chris@16: }; Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: template inline bool operator==( scoped_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, scoped_array const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( scoped_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, scoped_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 void swap(scoped_array & a, scoped_array & b) BOOST_NOEXCEPT Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED