scoped_ptr.hpp 4.0 KB

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