weak_ptr.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
  18. # pragma warning(push)
  19. # pragma warning(disable:4284) // odd return type for operator->
  20. #endif
  21. namespace boost
  22. {
  23. template<class T> class weak_ptr
  24. {
  25. private:
  26. // Borland 5.5.1 specific workarounds
  27. typedef weak_ptr<T> this_type;
  28. public:
  29. typedef T element_type;
  30. weak_ptr(): px(0), pn() // never throws in 1.30+
  31. {
  32. }
  33. // generated copy constructor, assignment, destructor are fine
  34. //
  35. // The "obvious" converting constructor implementation:
  36. //
  37. // template<class Y>
  38. // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
  39. // {
  40. // }
  41. //
  42. // has a serious problem.
  43. //
  44. // r.px may already have been invalidated. The px(r.px)
  45. // conversion may require access to *r.px (virtual inheritance).
  46. //
  47. // It is not possible to avoid spurious access violations since
  48. // in multithreaded programs r.px may be invalidated at any point.
  49. //
  50. template<class Y>
  51. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  52. weak_ptr( weak_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
  53. #else
  54. weak_ptr( weak_ptr<Y> const & r )
  55. #endif
  56. : px(r.lock().get()), pn(r.pn) // never throws
  57. {
  58. }
  59. #if defined( BOOST_HAS_RVALUE_REFS )
  60. template<class Y>
  61. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  62. weak_ptr( weak_ptr<Y> && r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
  63. #else
  64. weak_ptr( weak_ptr<Y> && r )
  65. #endif
  66. : px(r.lock().get()), pn(std::move(r.pn)) // never throws
  67. {
  68. r.px = 0;
  69. }
  70. // for better efficiency in the T == Y case
  71. weak_ptr( weak_ptr && r ): px( r.px ), pn(std::move(r.pn)) // never throws
  72. {
  73. r.px = 0;
  74. }
  75. // for better efficiency in the T == Y case
  76. weak_ptr & operator=( weak_ptr && r ) // never throws
  77. {
  78. this_type( std::move( r ) ).swap( *this );
  79. return *this;
  80. }
  81. #endif
  82. template<class Y>
  83. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  84. weak_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
  85. #else
  86. weak_ptr( shared_ptr<Y> const & r )
  87. #endif
  88. : px( r.px ), pn( r.pn ) // never throws
  89. {
  90. }
  91. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
  92. template<class Y>
  93. weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
  94. {
  95. px = r.lock().get();
  96. pn = r.pn;
  97. return *this;
  98. }
  99. #if defined( BOOST_HAS_RVALUE_REFS )
  100. template<class Y>
  101. weak_ptr & operator=(weak_ptr<Y> && r)
  102. {
  103. this_type( std::move( r ) ).swap( *this );
  104. return *this;
  105. }
  106. #endif
  107. template<class Y>
  108. weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
  109. {
  110. px = r.px;
  111. pn = r.pn;
  112. return *this;
  113. }
  114. #endif
  115. shared_ptr<T> lock() const // never throws
  116. {
  117. return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
  118. }
  119. long use_count() const // never throws
  120. {
  121. return pn.use_count();
  122. }
  123. bool expired() const // never throws
  124. {
  125. return pn.use_count() == 0;
  126. }
  127. bool _empty() const // extension, not in std::weak_ptr
  128. {
  129. return pn.empty();
  130. }
  131. void reset() // never throws in 1.30+
  132. {
  133. this_type().swap(*this);
  134. }
  135. void swap(this_type & other) // never throws
  136. {
  137. std::swap(px, other.px);
  138. pn.swap(other.pn);
  139. }
  140. void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
  141. {
  142. px = px2;
  143. pn = pn2;
  144. }
  145. template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
  146. {
  147. return pn < rhs.pn;
  148. }
  149. // Tasteless as this may seem, making all members public allows member templates
  150. // to work in the absence of member template friends. (Matthew Langston)
  151. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  152. private:
  153. template<class Y> friend class weak_ptr;
  154. template<class Y> friend class shared_ptr;
  155. #endif
  156. T * px; // contained pointer
  157. boost::detail::weak_count pn; // reference counter
  158. }; // weak_ptr
  159. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
  160. {
  161. return a._internal_less(b);
  162. }
  163. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
  164. {
  165. a.swap(b);
  166. }
  167. } // namespace boost
  168. #ifdef BOOST_MSVC
  169. # pragma warning(pop)
  170. #endif
  171. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED