scoped_ptr.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #ifndef BOOST_CONFIG_HPP
  17. # include <boost/config.hpp>
  18. #endif
  19. #
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. #include <boost/interprocess/detail/config_begin.hpp>
  24. #include <boost/interprocess/detail/workaround.hpp>
  25. #include <boost/interprocess/detail/pointer_type.hpp>
  26. #include <boost/interprocess/detail/utilities.hpp>
  27. #include <boost/assert.hpp>
  28. #include <boost/move/adl_move_swap.hpp>
  29. //!\file
  30. //!Describes the smart pointer scoped_ptr
  31. namespace boost {
  32. namespace interprocess {
  33. //!scoped_ptr stores a pointer to a dynamically allocated object.
  34. //!The object pointed to is guaranteed to be deleted, either on destruction
  35. //!of the scoped_ptr, or via an explicit reset. The user can avoid this
  36. //!deletion using release().
  37. //!scoped_ptr is parameterized on T (the type of the object pointed to) and
  38. //!Deleter (the functor to be executed to delete the internal pointer).
  39. //!The internal pointer will be of the same pointer type as typename
  40. //!Deleter::pointer type (that is, if typename Deleter::pointer is
  41. //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).
  42. template<class T, class Deleter>
  43. class scoped_ptr
  44. : private Deleter
  45. {
  46. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  47. scoped_ptr(scoped_ptr const &);
  48. scoped_ptr & operator=(scoped_ptr const &);
  49. typedef scoped_ptr<T, Deleter> this_type;
  50. typedef typename ipcdetail::add_reference<T>::type reference;
  51. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  52. public:
  53. typedef T element_type;
  54. typedef Deleter deleter_type;
  55. typedef typename ipcdetail::pointer_type<T, Deleter>::type pointer;
  56. //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d.
  57. //!Does not throw.
  58. explicit scoped_ptr(const pointer &p = 0, const Deleter &d = Deleter())
  59. : Deleter(d), m_ptr(p) // throws if pointer/Deleter copy ctor throws
  60. {}
  61. //!If the stored pointer is not 0, destroys the object pointed to by the stored pointer.
  62. //!calling the operator() of the stored deleter. Never throws
  63. ~scoped_ptr()
  64. {
  65. if(m_ptr){
  66. Deleter &del = static_cast<Deleter&>(*this);
  67. del(m_ptr);
  68. }
  69. }
  70. //!Deletes the object pointed to by the stored pointer and then
  71. //!stores a copy of p. Never throws
  72. void reset(const pointer &p = 0) // never throws
  73. { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p).swap(*this); }
  74. //!Deletes the object pointed to by the stored pointer and then
  75. //!stores a copy of p and a copy of d.
  76. void reset(const pointer &p, const Deleter &d) // never throws
  77. { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p, d).swap(*this); }
  78. //!Assigns internal pointer as 0 and returns previous pointer. This will
  79. //!avoid deletion on destructor
  80. pointer release() BOOST_NOEXCEPT
  81. { pointer tmp(m_ptr); m_ptr = 0; return tmp; }
  82. //!Returns a reference to the object pointed to by the stored pointer.
  83. //!Never throws.
  84. reference operator*() const BOOST_NOEXCEPT
  85. { BOOST_ASSERT(m_ptr != 0); return *m_ptr; }
  86. //!Returns the internal stored pointer.
  87. //!Never throws.
  88. pointer &operator->() BOOST_NOEXCEPT
  89. { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
  90. //!Returns the internal stored pointer.
  91. //!Never throws.
  92. const pointer &operator->() const BOOST_NOEXCEPT
  93. { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
  94. //!Returns the stored pointer.
  95. //!Never throws.
  96. pointer & get() BOOST_NOEXCEPT
  97. { return m_ptr; }
  98. //!Returns the stored pointer.
  99. //!Never throws.
  100. const pointer & get() const BOOST_NOEXCEPT
  101. { return m_ptr; }
  102. typedef pointer this_type::*unspecified_bool_type;
  103. //!Conversion to bool
  104. //!Never throws
  105. operator unspecified_bool_type() const BOOST_NOEXCEPT
  106. { return m_ptr == 0? 0: &this_type::m_ptr; }
  107. //!Returns true if the stored pointer is 0.
  108. //!Never throws.
  109. bool operator! () const BOOST_NOEXCEPT // never throws
  110. { return m_ptr == 0; }
  111. //!Exchanges the internal pointer and deleter with other scoped_ptr
  112. //!Never throws.
  113. void swap(scoped_ptr & b) BOOST_NOEXCEPT // never throws
  114. {
  115. ::boost::adl_move_swap(static_cast<Deleter&>(*this), static_cast<Deleter&>(b));
  116. ::boost::adl_move_swap(m_ptr, b.m_ptr);
  117. }
  118. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  119. private:
  120. pointer m_ptr;
  121. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  122. };
  123. //!Exchanges the internal pointer and deleter with other scoped_ptr
  124. //!Never throws.
  125. template<class T, class D> inline
  126. void swap(scoped_ptr<T, D> & a, scoped_ptr<T, D> & b) BOOST_NOEXCEPT
  127. { a.swap(b); }
  128. //!Returns a copy of the stored pointer
  129. //!Never throws
  130. template<class T, class D> inline
  131. typename scoped_ptr<T, D>::pointer to_raw_pointer(scoped_ptr<T, D> const & p)
  132. { return p.get(); }
  133. } // namespace interprocess
  134. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  135. #if defined(_MSC_VER) && (_MSC_VER < 1400)
  136. template<class T, class D> inline
  137. T *to_raw_pointer(boost::interprocess::scoped_ptr<T, D> const & p)
  138. { return p.get(); }
  139. #endif
  140. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  141. } // namespace boost
  142. #include <boost/interprocess/detail/config_end.hpp>
  143. #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED