Line data Source code
1 : #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED 2 : #define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED 3 : 4 : // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. 5 : // Copyright (c) 2001, 2002 Peter Dimov 6 : // 7 : // Distributed under the Boost Software License, Version 1.0. (See 8 : // accompanying file LICENSE_1_0.txt or copy at 9 : // http://www.boost.org/LICENSE_1_0.txt) 10 : // 11 : // See http://www.boost.org/libs/smart_ptr/ for documentation. 12 : 13 : #include <boost/smart_ptr/detail/sp_noexcept.hpp> 14 : #include <boost/smart_ptr/detail/deprecated_macros.hpp> 15 : #include <boost/core/checked_delete.hpp> 16 : #include <boost/assert.hpp> 17 : #include <boost/config.hpp> 18 : #include <boost/config/workaround.hpp> 19 : 20 : #include <cstddef> // for std::ptrdiff_t 21 : 22 : namespace boost 23 : { 24 : 25 : // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to 26 : // is guaranteed, either on destruction of the scoped_array or via an explicit 27 : // reset(). Use shared_array or std::vector if your needs are more complex. 28 : 29 : template<class T> class scoped_array // noncopyable 30 : { 31 : private: 32 : 33 : T * px; 34 : 35 : scoped_array(scoped_array const &); 36 : scoped_array & operator=(scoped_array const &); 37 : 38 : typedef scoped_array<T> this_type; 39 : 40 : void operator==( scoped_array const& ) const; 41 : void operator!=( scoped_array const& ) const; 42 : 43 : public: 44 : 45 : typedef T element_type; 46 : 47 1168 : explicit scoped_array( T * p = 0 ) noexcept : px( p ) 48 584 : { 49 1168 : } 50 : 51 584 : ~scoped_array() noexcept 52 292 : { 53 292 : boost::checked_array_delete( px ); 54 584 : } 55 : 56 292 : void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT 57 : { 58 292 : BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors 59 292 : this_type(p).swap(*this); 60 292 : } 61 : 62 : T & operator[](std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT 63 : { 64 : BOOST_ASSERT( px != 0 ); 65 : BOOST_ASSERT( i >= 0 ); 66 : return px[i]; 67 : } 68 : 69 1315 : T * get() const noexcept 70 : { 71 1315 : return px; 72 : } 73 : 74 1315 : explicit operator bool () const noexcept 75 : { 76 1315 : return px != 0; 77 : } 78 : 79 292 : void swap(scoped_array & b) noexcept 80 : { 81 292 : T * tmp = b.px; 82 292 : b.px = px; 83 292 : px = tmp; 84 292 : } 85 : }; 86 : 87 : template<class T> inline bool operator==( scoped_array<T> const & p, std::nullptr_t ) noexcept 88 : { 89 : return p.get() == 0; 90 : } 91 : 92 : template<class T> inline bool operator==( std::nullptr_t, scoped_array<T> const & p ) noexcept 93 : { 94 : return p.get() == 0; 95 : } 96 : 97 : template<class T> inline bool operator!=( scoped_array<T> const & p, std::nullptr_t ) noexcept 98 : { 99 : return p.get() != 0; 100 : } 101 : 102 : template<class T> inline bool operator!=( std::nullptr_t, scoped_array<T> const & p ) noexcept 103 : { 104 : return p.get() != 0; 105 : } 106 : 107 : template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) noexcept 108 : { 109 : a.swap(b); 110 : } 111 : 112 : } // namespace boost 113 : 114 : #endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED