00001 #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED 00002 #define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED 00003 00004 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. 00005 // Copyright (c) 2001, 2002 Peter Dimov 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See 00008 // accompanying file LICENSE_1_0.txt or copy at 00009 // http://www.boost.org/LICENSE_1_0.txt) 00010 // 00011 // http://www.boost.org/libs/smart_ptr/scoped_array.htm 00012 // 00013 00014 #include <boost/assert.hpp> 00015 #include <boost/checked_delete.hpp> 00016 #include <boost/config.hpp> // in case ptrdiff_t not in std 00017 00018 #include <boost/detail/workaround.hpp> 00019 00020 #include <cstddef> // for std::ptrdiff_t 00021 00022 namespace boost 00023 { 00024 00025 // Debug hooks 00026 00027 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) 00028 00029 void sp_array_constructor_hook(void * p); 00030 void sp_array_destructor_hook(void * p); 00031 00032 #endif 00033 00034 // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to 00035 // is guaranteed, either on destruction of the scoped_array or via an explicit 00036 // reset(). Use shared_array or std::vector if your needs are more complex. 00037 00038 template<class T> class scoped_array // noncopyable 00039 { 00040 private: 00041 00042 T * px; 00043 00044 scoped_array(scoped_array const &); 00045 scoped_array & operator=(scoped_array const &); 00046 00047 typedef scoped_array<T> this_type; 00048 00049 void operator==( scoped_array const& ) const; 00050 void operator!=( scoped_array const& ) const; 00051 00052 public: 00053 00054 typedef T element_type; 00055 00056 explicit scoped_array( T * p = 0 ) : px( p ) // never throws 00057 { 00058 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) 00059 boost::sp_array_constructor_hook( px ); 00060 #endif 00061 } 00062 00063 ~scoped_array() // never throws 00064 { 00065 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) 00066 boost::sp_array_destructor_hook( px ); 00067 #endif 00068 boost::checked_array_delete( px ); 00069 } 00070 00071 void reset(T * p = 0) // never throws 00072 { 00073 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors 00074 this_type(p).swap(*this); 00075 } 00076 00077 T & operator[](std::ptrdiff_t i) const // never throws 00078 { 00079 BOOST_ASSERT( px != 0 ); 00080 BOOST_ASSERT( i >= 0 ); 00081 return px[i]; 00082 } 00083 00084 T * get() const // never throws 00085 { 00086 return px; 00087 } 00088 00089 // implicit conversion to "bool" 00090 #include <boost/smart_ptr/detail/operator_bool.hpp> 00091 00092 void swap(scoped_array & b) // never throws 00093 { 00094 T * tmp = b.px; 00095 b.px = px; 00096 px = tmp; 00097 } 00098 }; 00099 00100 template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws 00101 { 00102 a.swap(b); 00103 } 00104 00105 } // namespace boost 00106 00107 #endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED