static_move_ptr.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // (C) Copyright Thorsten Ottosen 2005.
  2. // (C) Copyright Jonathan Turkanis 2004.
  3. // (C) Copyright Daniel Wallin 2004.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  6. // Implementation of the move_ptr from the "Move Proposal"
  7. // (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm)
  8. // enhanced to support custom deleters and safe boolean conversions.
  9. //
  10. // The implementation is based on an implementation by Daniel Wallin, at
  11. // "http://aspn.activestate.com/ASPN/Mail/Message/Attachments/boost/
  12. // [email protected]/move_ptr.hpp". The current was adapted
  13. // by Jonathan Turkanis to incorporating ideas of Howard Hinnant and
  14. // Rani Sharoni.
  15. #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED
  16. #define BOOST_STATIC_MOVE_PTR_HPP_INCLUDED
  17. #include <boost/config.hpp> // Member template friends, put size_t in std.
  18. #include <cstddef> // size_t
  19. #include <boost/compressed_pair.hpp>
  20. #include <boost/ptr_container/detail/default_deleter.hpp>
  21. #include <boost/ptr_container/detail/is_convertible.hpp>
  22. #include <boost/ptr_container/detail/move.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include <boost/type_traits/add_reference.hpp>
  25. #include <boost/type_traits/is_array.hpp>
  26. #if defined(BOOST_MSVC)
  27. #pragma warning(push)
  28. #pragma warning(disable:4521) // Multiple copy constuctors.
  29. #endif
  30. namespace boost { namespace ptr_container_detail {
  31. template< typename T,
  32. typename Deleter =
  33. move_ptrs::default_deleter<T> >
  34. class static_move_ptr
  35. {
  36. public:
  37. typedef typename remove_bounds<T>::type element_type;
  38. typedef Deleter deleter_type;
  39. private:
  40. struct safe_bool_helper { int x; };
  41. typedef int safe_bool_helper::* safe_bool;
  42. typedef boost::compressed_pair<element_type*, Deleter> impl_type;
  43. public:
  44. typedef typename impl_type::second_reference deleter_reference;
  45. typedef typename impl_type::second_const_reference deleter_const_reference;
  46. // Constructors
  47. static_move_ptr() : impl_(0) { }
  48. static_move_ptr(const static_move_ptr& p)
  49. : impl_(p.get(), p.get_deleter())
  50. {
  51. const_cast<static_move_ptr&>(p).release();
  52. }
  53. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
  54. static_move_ptr( const move_ptrs::move_source<static_move_ptr<T,Deleter> >& src )
  55. #else
  56. static_move_ptr( const move_ptrs::move_source<static_move_ptr>& src )
  57. #endif
  58. : impl_(src.ptr().get(), src.ptr().get_deleter())
  59. {
  60. src.ptr().release();
  61. }
  62. template<typename TT>
  63. static_move_ptr(TT* tt, Deleter del)
  64. : impl_(tt, del)
  65. { }
  66. // Destructor
  67. ~static_move_ptr() { if (ptr()) get_deleter()(ptr()); }
  68. // Assignment
  69. static_move_ptr& operator=(static_move_ptr rhs)
  70. {
  71. rhs.swap(*this);
  72. return *this;
  73. }
  74. // Smart pointer interface
  75. element_type* get() const { return ptr(); }
  76. element_type& operator*()
  77. {
  78. /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
  79. }
  80. const element_type& operator*() const
  81. {
  82. /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
  83. }
  84. element_type* operator->()
  85. {
  86. /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
  87. }
  88. const element_type* operator->() const
  89. {
  90. /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
  91. }
  92. element_type* release()
  93. {
  94. element_type* result = ptr();
  95. ptr() = 0;
  96. return result;
  97. }
  98. void reset()
  99. {
  100. if (ptr()) get_deleter()(ptr());
  101. ptr() = 0;
  102. }
  103. template<typename TT>
  104. void reset(TT* tt, Deleter dd)
  105. {
  106. static_move_ptr(tt, dd).swap(*this);
  107. }
  108. operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; }
  109. void swap(static_move_ptr& p) { impl_.swap(p.impl_); }
  110. deleter_reference get_deleter() { return impl_.second(); }
  111. deleter_const_reference get_deleter() const { return impl_.second(); }
  112. private:
  113. template<typename TT, typename DD>
  114. void check(const static_move_ptr<TT, DD>&)
  115. {
  116. typedef move_ptrs::is_smart_ptr_convertible<TT, T> convertible;
  117. BOOST_STATIC_ASSERT(convertible::value);
  118. }
  119. #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_NO_SFINAE)
  120. // give up on this behavior
  121. #else
  122. template<typename Ptr> struct cant_move_from_const;
  123. template<typename TT, typename DD>
  124. struct cant_move_from_const< const static_move_ptr<TT, DD> > {
  125. typedef typename static_move_ptr<TT, DD>::error type;
  126. };
  127. template<typename Ptr>
  128. static_move_ptr(Ptr&, typename cant_move_from_const<Ptr>::type = 0);
  129. public:
  130. static_move_ptr(static_move_ptr&);
  131. private:
  132. template<typename TT, typename DD>
  133. static_move_ptr( static_move_ptr<TT, DD>&,
  134. typename
  135. move_ptrs::enable_if_convertible<
  136. TT, T, static_move_ptr&
  137. >::type::type* = 0 );
  138. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING || BOOST_NO_SFINAE
  139. //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  140. // template<typename TT, typename DD>
  141. // friend class static_move_ptr;
  142. //#else
  143. public:
  144. //#endif
  145. typename impl_type::first_reference
  146. ptr() { return impl_.first(); }
  147. typename impl_type::first_const_reference
  148. ptr() const { return impl_.first(); }
  149. impl_type impl_;
  150. };
  151. } // namespace ptr_container_detail
  152. } // End namespace boost.
  153. #if defined(BOOST_MSVC)
  154. #pragma warning(pop) // #pragma warning(disable:4251)
  155. #endif
  156. #endif // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED