scoped_array.hpp 3.3 KB

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