weak_ptr.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
  3. //
  4. // weak_ptr.hpp
  5. //
  6. // Copyright (c) 2001, 2002, 2003 Peter Dimov
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
  13. //
  14. #include <memory> // boost.TR1 include order fix
  15. #include <boost/smart_ptr/detail/shared_count.hpp>
  16. #include <boost/smart_ptr/shared_ptr.hpp>
  17. namespace boost
  18. {
  19. template<class T> class weak_ptr
  20. {
  21. private:
  22. // Borland 5.5.1 specific workarounds
  23. typedef weak_ptr<T> this_type;
  24. public:
  25. typedef typename boost::detail::sp_element< T >::type element_type;
  26. weak_ptr() BOOST_NOEXCEPT : px(0), pn() // never throws in 1.30+
  27. {
  28. }
  29. // generated copy constructor, assignment, destructor are fine...
  30. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  31. // ... except in C++0x, move disables the implicit copy
  32. weak_ptr( weak_ptr const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
  33. {
  34. }
  35. weak_ptr & operator=( weak_ptr const & r ) BOOST_NOEXCEPT
  36. {
  37. px = r.px;
  38. pn = r.pn;
  39. return *this;
  40. }
  41. #endif
  42. //
  43. // The "obvious" converting constructor implementation:
  44. //
  45. // template<class Y>
  46. // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
  47. // {
  48. // }
  49. //
  50. // has a serious problem.
  51. //
  52. // r.px may already have been invalidated. The px(r.px)
  53. // conversion may require access to *r.px (virtual inheritance).
  54. //
  55. // It is not possible to avoid spurious access violations since
  56. // in multithreaded programs r.px may be invalidated at any point.
  57. //
  58. template<class Y>
  59. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  60. weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  61. #else
  62. weak_ptr( weak_ptr<Y> const & r )
  63. #endif
  64. BOOST_NOEXCEPT : px(r.lock().get()), pn(r.pn)
  65. {
  66. boost::detail::sp_assert_convertible< Y, T >();
  67. }
  68. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  69. template<class Y>
  70. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  71. weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  72. #else
  73. weak_ptr( weak_ptr<Y> && r )
  74. #endif
  75. BOOST_NOEXCEPT : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  76. {
  77. boost::detail::sp_assert_convertible< Y, T >();
  78. r.px = 0;
  79. }
  80. // for better efficiency in the T == Y case
  81. weak_ptr( weak_ptr && r )
  82. BOOST_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  83. {
  84. r.px = 0;
  85. }
  86. // for better efficiency in the T == Y case
  87. weak_ptr & operator=( weak_ptr && r ) BOOST_NOEXCEPT
  88. {
  89. this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
  90. return *this;
  91. }
  92. #endif
  93. template<class Y>
  94. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  95. weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  96. #else
  97. weak_ptr( shared_ptr<Y> const & r )
  98. #endif
  99. BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
  100. {
  101. boost::detail::sp_assert_convertible< Y, T >();
  102. }
  103. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
  104. template<class Y>
  105. weak_ptr & operator=( weak_ptr<Y> const & r ) BOOST_NOEXCEPT
  106. {
  107. boost::detail::sp_assert_convertible< Y, T >();
  108. px = r.lock().get();
  109. pn = r.pn;
  110. return *this;
  111. }
  112. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  113. template<class Y>
  114. weak_ptr & operator=( weak_ptr<Y> && r ) BOOST_NOEXCEPT
  115. {
  116. this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
  117. return *this;
  118. }
  119. #endif
  120. template<class Y>
  121. weak_ptr & operator=( shared_ptr<Y> const & r ) BOOST_NOEXCEPT
  122. {
  123. boost::detail::sp_assert_convertible< Y, T >();
  124. px = r.px;
  125. pn = r.pn;
  126. return *this;
  127. }
  128. #endif
  129. shared_ptr<T> lock() const BOOST_NOEXCEPT
  130. {
  131. return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
  132. }
  133. long use_count() const BOOST_NOEXCEPT
  134. {
  135. return pn.use_count();
  136. }
  137. bool expired() const BOOST_NOEXCEPT
  138. {
  139. return pn.use_count() == 0;
  140. }
  141. bool _empty() const // extension, not in std::weak_ptr
  142. {
  143. return pn.empty();
  144. }
  145. void reset() BOOST_NOEXCEPT // never throws in 1.30+
  146. {
  147. this_type().swap(*this);
  148. }
  149. void swap(this_type & other) BOOST_NOEXCEPT
  150. {
  151. std::swap(px, other.px);
  152. pn.swap(other.pn);
  153. }
  154. template<typename Y>
  155. void _internal_aliasing_assign(weak_ptr<Y> const & r, element_type * px2)
  156. {
  157. px = px2;
  158. pn = r.pn;
  159. }
  160. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
  161. {
  162. return pn < rhs.pn;
  163. }
  164. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
  165. {
  166. return pn < rhs.pn;
  167. }
  168. // Tasteless as this may seem, making all members public allows member templates
  169. // to work in the absence of member template friends. (Matthew Langston)
  170. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  171. private:
  172. template<class Y> friend class weak_ptr;
  173. template<class Y> friend class shared_ptr;
  174. #endif
  175. element_type * px; // contained pointer
  176. boost::detail::weak_count pn; // reference counter
  177. }; // weak_ptr
  178. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) BOOST_NOEXCEPT
  179. {
  180. return a.owner_before( b );
  181. }
  182. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) BOOST_NOEXCEPT
  183. {
  184. a.swap(b);
  185. }
  186. } // namespace boost
  187. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED