scoped_array.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // See http://www.boost.org/libs/smart_ptr/ for documentation.
  11. #include <boost/config.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/checked_delete.hpp>
  14. #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
  15. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  16. #include <boost/config/workaround.hpp>
  17. #include <cstddef> // for std::ptrdiff_t
  18. namespace boost
  19. {
  20. // Debug hooks
  21. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  22. void sp_array_constructor_hook(void * p);
  23. void sp_array_destructor_hook(void * p);
  24. #endif
  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. template<class T> class scoped_array // noncopyable
  29. {
  30. private:
  31. T * px;
  32. scoped_array(scoped_array const &);
  33. scoped_array & operator=(scoped_array const &);
  34. typedef scoped_array<T> this_type;
  35. void operator==( scoped_array const& ) const;
  36. void operator!=( scoped_array const& ) const;
  37. public:
  38. typedef T element_type;
  39. explicit scoped_array( T * p = 0 ) BOOST_SP_NOEXCEPT : px( p )
  40. {
  41. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  42. boost::sp_array_constructor_hook( px );
  43. #endif
  44. }
  45. ~scoped_array() BOOST_SP_NOEXCEPT
  46. {
  47. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  48. boost::sp_array_destructor_hook( px );
  49. #endif
  50. boost::checked_array_delete( px );
  51. }
  52. void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
  53. {
  54. BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
  55. this_type(p).swap(*this);
  56. }
  57. T & operator[](std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
  58. {
  59. BOOST_ASSERT( px != 0 );
  60. BOOST_ASSERT( i >= 0 );
  61. return px[i];
  62. }
  63. T * get() const BOOST_SP_NOEXCEPT
  64. {
  65. return px;
  66. }
  67. // implicit conversion to "bool"
  68. #include <boost/smart_ptr/detail/operator_bool.hpp>
  69. void swap(scoped_array & b) BOOST_SP_NOEXCEPT
  70. {
  71. T * tmp = b.px;
  72. b.px = px;
  73. px = tmp;
  74. }
  75. };
  76. #if !defined( BOOST_NO_CXX11_NULLPTR )
  77. template<class T> inline bool operator==( scoped_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  78. {
  79. return p.get() == 0;
  80. }
  81. template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_array<T> const & p ) BOOST_SP_NOEXCEPT
  82. {
  83. return p.get() == 0;
  84. }
  85. template<class T> inline bool operator!=( scoped_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  86. {
  87. return p.get() != 0;
  88. }
  89. template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_array<T> const & p ) BOOST_SP_NOEXCEPT
  90. {
  91. return p.get() != 0;
  92. }
  93. #endif
  94. template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) BOOST_SP_NOEXCEPT
  95. {
  96. a.swap(b);
  97. }
  98. } // namespace boost
  99. #endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED