scoped_array.hpp 2.7 KB

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