scoped_ptr.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_SCOPED_PTR_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_disable_deprecated.hpp>
  12. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  13. #include <boost/smart_ptr/detail/deprecated_macros.hpp>
  14. #include <boost/core/checked_delete.hpp>
  15. #include <boost/assert.hpp>
  16. #include <boost/config/workaround.hpp>
  17. #include <boost/config.hpp>
  18. #include <cstddef>
  19. #ifndef BOOST_NO_AUTO_PTR
  20. # include <memory> // for std::auto_ptr
  21. #endif
  22. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  23. #pragma GCC diagnostic push
  24. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  25. #endif
  26. namespace boost
  27. {
  28. // scoped_ptr mimics a built-in pointer except that it guarantees deletion
  29. // of the object pointed to, either on destruction of the scoped_ptr or via
  30. // an explicit reset(). scoped_ptr is a simple solution for simple needs;
  31. // use shared_ptr or std::auto_ptr if your needs are more complex.
  32. template<class T> class scoped_ptr // noncopyable
  33. {
  34. private:
  35. T * px;
  36. scoped_ptr(scoped_ptr const &);
  37. scoped_ptr & operator=(scoped_ptr const &);
  38. typedef scoped_ptr<T> this_type;
  39. void operator==( scoped_ptr const& ) const;
  40. void operator!=( scoped_ptr const& ) const;
  41. public:
  42. typedef T element_type;
  43. explicit scoped_ptr( T * p = 0 ) noexcept : px( p )
  44. {
  45. }
  46. #ifndef BOOST_NO_AUTO_PTR
  47. explicit scoped_ptr( std::auto_ptr<T> p ) noexcept : px( p.release() )
  48. {
  49. }
  50. #endif
  51. ~scoped_ptr() noexcept
  52. {
  53. boost::checked_delete( px );
  54. }
  55. void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
  56. {
  57. BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
  58. this_type(p).swap(*this);
  59. }
  60. T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT
  61. {
  62. BOOST_ASSERT( px != 0 );
  63. return *px;
  64. }
  65. T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT
  66. {
  67. BOOST_ASSERT( px != 0 );
  68. return px;
  69. }
  70. T * get() const noexcept
  71. {
  72. return px;
  73. }
  74. explicit operator bool () const noexcept
  75. {
  76. return px != 0;
  77. }
  78. void swap(scoped_ptr & b) noexcept
  79. {
  80. T * tmp = b.px;
  81. b.px = px;
  82. px = tmp;
  83. }
  84. };
  85. template<class T> inline bool operator==( scoped_ptr<T> const & p, std::nullptr_t ) noexcept
  86. {
  87. return p.get() == 0;
  88. }
  89. template<class T> inline bool operator==( std::nullptr_t, scoped_ptr<T> const & p ) noexcept
  90. {
  91. return p.get() == 0;
  92. }
  93. template<class T> inline bool operator!=( scoped_ptr<T> const & p, std::nullptr_t ) noexcept
  94. {
  95. return p.get() != 0;
  96. }
  97. template<class T> inline bool operator!=( std::nullptr_t, scoped_ptr<T> const & p ) noexcept
  98. {
  99. return p.get() != 0;
  100. }
  101. template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) noexcept
  102. {
  103. a.swap(b);
  104. }
  105. // get_pointer(p) is a generic way to say p.get()
  106. template<class T> inline T * get_pointer(scoped_ptr<T> const & p) noexcept
  107. {
  108. return p.get();
  109. }
  110. } // namespace boost
  111. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  112. #pragma GCC diagnostic pop
  113. #endif
  114. #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED