scoped_ptr.hpp 4.0 KB

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