File indexing completed on 2025-12-16 10:08:51
0001 #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
0014 #include <boost/smart_ptr/detail/deprecated_macros.hpp>
0015 #include <boost/core/checked_delete.hpp>
0016 #include <boost/assert.hpp>
0017 #include <boost/config.hpp>
0018 #include <boost/config/workaround.hpp>
0019
0020 #include <cstddef> // for std::ptrdiff_t
0021
0022 namespace boost
0023 {
0024
0025
0026
0027 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0028
0029 void sp_array_constructor_hook(void * p);
0030 void sp_array_destructor_hook(void * p);
0031
0032 #endif
0033
0034
0035
0036
0037
0038 template<class T> class scoped_array
0039 {
0040 private:
0041
0042 T * px;
0043
0044 scoped_array(scoped_array const &);
0045 scoped_array & operator=(scoped_array const &);
0046
0047 typedef scoped_array<T> this_type;
0048
0049 void operator==( scoped_array const& ) const;
0050 void operator!=( scoped_array const& ) const;
0051
0052 public:
0053
0054 typedef T element_type;
0055
0056 explicit scoped_array( T * p = 0 ) noexcept : px( p )
0057 {
0058 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0059 boost::sp_array_constructor_hook( px );
0060 #endif
0061 }
0062
0063 ~scoped_array() noexcept
0064 {
0065 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0066 boost::sp_array_destructor_hook( px );
0067 #endif
0068 boost::checked_array_delete( px );
0069 }
0070
0071 void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
0072 {
0073 BOOST_ASSERT( p == 0 || p != px );
0074 this_type(p).swap(*this);
0075 }
0076
0077 T & operator[](std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
0078 {
0079 BOOST_ASSERT( px != 0 );
0080 BOOST_ASSERT( i >= 0 );
0081 return px[i];
0082 }
0083
0084 T * get() const noexcept
0085 {
0086 return px;
0087 }
0088
0089 explicit operator bool () const noexcept
0090 {
0091 return px != 0;
0092 }
0093
0094 void swap(scoped_array & b) noexcept
0095 {
0096 T * tmp = b.px;
0097 b.px = px;
0098 px = tmp;
0099 }
0100 };
0101
0102 template<class T> inline bool operator==( scoped_array<T> const & p, std::nullptr_t ) noexcept
0103 {
0104 return p.get() == 0;
0105 }
0106
0107 template<class T> inline bool operator==( std::nullptr_t, scoped_array<T> const & p ) noexcept
0108 {
0109 return p.get() == 0;
0110 }
0111
0112 template<class T> inline bool operator!=( scoped_array<T> const & p, std::nullptr_t ) noexcept
0113 {
0114 return p.get() != 0;
0115 }
0116
0117 template<class T> inline bool operator!=( std::nullptr_t, scoped_array<T> const & p ) noexcept
0118 {
0119 return p.get() != 0;
0120 }
0121
0122 template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) noexcept
0123 {
0124 a.swap(b);
0125 }
0126
0127 }
0128
0129 #endif