scoped_ptr.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is the adaptation for Interprocess of boost/scoped_ptr.hpp
  4. //
  5. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  6. // (C) Copyright Peter Dimov 2001, 2002
  7. // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // See http://www.boost.org/libs/interprocess for documentation.
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
  15. #define BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
  16. #include <boost/interprocess/detail/config_begin.hpp>
  17. #include <boost/interprocess/detail/workaround.hpp>
  18. #include <boost/interprocess/detail/pointer_type.hpp>
  19. #include <boost/interprocess/detail/utilities.hpp>
  20. #include <boost/assert.hpp>
  21. //!\file
  22. //!Describes the smart pointer scoped_ptr
  23. namespace boost {
  24. namespace interprocess {
  25. //!scoped_ptr stores a pointer to a dynamically allocated object.
  26. //!The object pointed to is guaranteed to be deleted, either on destruction
  27. //!of the scoped_ptr, or via an explicit reset. The user can avoid this
  28. //!deletion using release().
  29. //!scoped_ptr is parameterized on T (the type of the object pointed to) and
  30. //!Deleter (the functor to be executed to delete the internal pointer).
  31. //!The internal pointer will be of the same pointer type as typename
  32. //!Deleter::pointer type (that is, if typename Deleter::pointer is
  33. //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).
  34. template<class T, class Deleter>
  35. class scoped_ptr
  36. : private Deleter
  37. {
  38. /// @cond
  39. scoped_ptr(scoped_ptr const &);
  40. scoped_ptr & operator=(scoped_ptr const &);
  41. typedef scoped_ptr<T, Deleter> this_type;
  42. typedef typename ipcdetail::add_reference<T>::type reference;
  43. /// @endcond
  44. public:
  45. typedef T element_type;
  46. typedef Deleter deleter_type;
  47. typedef typename ipcdetail::pointer_type<T, Deleter>::type pointer;
  48. //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d.
  49. //!Does not throw.
  50. explicit scoped_ptr(const pointer &p = 0, const Deleter &d = Deleter())
  51. : Deleter(d), m_ptr(p) // throws if pointer/Deleter copy ctor throws
  52. {}
  53. //!If the stored pointer is not 0, destroys the object pointed to by the stored pointer.
  54. //!calling the operator() of the stored deleter. Never throws
  55. ~scoped_ptr()
  56. {
  57. if(m_ptr){
  58. Deleter &del = static_cast<Deleter&>(*this);
  59. del(m_ptr);
  60. }
  61. }
  62. //!Deletes the object pointed to by the stored pointer and then
  63. //!stores a copy of p. Never throws
  64. void reset(const pointer &p = 0) // never throws
  65. { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p).swap(*this); }
  66. //!Deletes the object pointed to by the stored pointer and then
  67. //!stores a copy of p and a copy of d.
  68. void reset(const pointer &p, const Deleter &d) // never throws
  69. { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p, d).swap(*this); }
  70. //!Assigns internal pointer as 0 and returns previous pointer. This will
  71. //!avoid deletion on destructor
  72. pointer release()
  73. { pointer tmp(m_ptr); m_ptr = 0; return tmp; }
  74. //!Returns a reference to the object pointed to by the stored pointer.
  75. //!Never throws.
  76. reference operator*() const
  77. { BOOST_ASSERT(m_ptr != 0); return *m_ptr; }
  78. //!Returns the internal stored pointer.
  79. //!Never throws.
  80. pointer &operator->()
  81. { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
  82. //!Returns the internal stored pointer.
  83. //!Never throws.
  84. const pointer &operator->() const
  85. { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
  86. //!Returns the stored pointer.
  87. //!Never throws.
  88. pointer & get()
  89. { return m_ptr; }
  90. //!Returns the stored pointer.
  91. //!Never throws.
  92. const pointer & get() const
  93. { return m_ptr; }
  94. typedef pointer this_type::*unspecified_bool_type;
  95. //!Conversion to bool
  96. //!Never throws
  97. operator unspecified_bool_type() const
  98. { return m_ptr == 0? 0: &this_type::m_ptr; }
  99. //!Returns true if the stored pointer is 0.
  100. //!Never throws.
  101. bool operator! () const // never throws
  102. { return m_ptr == 0; }
  103. //!Exchanges the internal pointer and deleter with other scoped_ptr
  104. //!Never throws.
  105. void swap(scoped_ptr & b) // never throws
  106. { ipcdetail::do_swap<Deleter>(*this, b); ipcdetail::do_swap(m_ptr, b.m_ptr); }
  107. /// @cond
  108. private:
  109. pointer m_ptr;
  110. /// @endcond
  111. };
  112. //!Exchanges the internal pointer and deleter with other scoped_ptr
  113. //!Never throws.
  114. template<class T, class D> inline
  115. void swap(scoped_ptr<T, D> & a, scoped_ptr<T, D> & b)
  116. { a.swap(b); }
  117. //!Returns a copy of the stored pointer
  118. //!Never throws
  119. template<class T, class D> inline
  120. typename scoped_ptr<T, D>::pointer to_raw_pointer(scoped_ptr<T, D> const & p)
  121. { return p.get(); }
  122. } // namespace interprocess
  123. /// @cond
  124. #if defined(_MSC_VER) && (_MSC_VER < 1400)
  125. template<class T, class D> inline
  126. T *to_raw_pointer(boost::interprocess::scoped_ptr<T, D> const & p)
  127. { return p.get(); }
  128. #endif
  129. /// @endcond
  130. } // namespace boost
  131. #include <boost/interprocess/detail/config_end.hpp>
  132. #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED