scoped_array.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
  3. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  4. // Copyright (c) 2001, 2002 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // http://www.boost.org/libs/smart_ptr/scoped_array.htm
  11. //
  12. #include <boost/assert.hpp>
  13. #include <boost/checked_delete.hpp>
  14. #include <boost/config.hpp> // in case ptrdiff_t not in std
  15. #include <boost/detail/workaround.hpp>
  16. #include <cstddef> // for std::ptrdiff_t
  17. namespace boost
  18. {
  19. // Debug hooks
  20. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  21. void sp_array_constructor_hook(void * p);
  22. void sp_array_destructor_hook(void * p);
  23. #endif
  24. // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
  25. // is guaranteed, either on destruction of the scoped_array or via an explicit
  26. // reset(). Use shared_array or std::vector if your needs are more complex.
  27. template<class T> class scoped_array // noncopyable
  28. {
  29. private:
  30. T * px;
  31. scoped_array(scoped_array const &);
  32. scoped_array & operator=(scoped_array const &);
  33. typedef scoped_array<T> this_type;
  34. void operator==( scoped_array const& ) const;
  35. void operator!=( scoped_array const& ) const;
  36. public:
  37. typedef T element_type;
  38. explicit scoped_array( T * p = 0 ) : px( p ) // never throws
  39. {
  40. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  41. boost::sp_array_constructor_hook( px );
  42. #endif
  43. }
  44. ~scoped_array() // never throws
  45. {
  46. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  47. boost::sp_array_destructor_hook( px );
  48. #endif
  49. boost::checked_array_delete( px );
  50. }
  51. void reset(T * p = 0) // never throws
  52. {
  53. BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
  54. this_type(p).swap(*this);
  55. }
  56. T & operator[](std::ptrdiff_t i) const // never throws
  57. {
  58. BOOST_ASSERT( px != 0 );
  59. BOOST_ASSERT( i >= 0 );
  60. return px[i];
  61. }
  62. T * get() const // never throws
  63. {
  64. return px;
  65. }
  66. // implicit conversion to "bool"
  67. #include <boost/smart_ptr/detail/operator_bool.hpp>
  68. void swap(scoped_array & b) // never throws
  69. {
  70. T * tmp = b.px;
  71. b.px = px;
  72. px = tmp;
  73. }
  74. };
  75. template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws
  76. {
  77. a.swap(b);
  78. }
  79. } // namespace boost
  80. #endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED