scoped_ptr.hpp 3.6 KB

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