scoped_ptr.hpp 2.9 KB

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