1 #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
\r
7 // Copyright (c) 2001, 2002, 2003 Peter Dimov
\r
9 // Distributed under the Boost Software License, Version 1.0. (See
\r
10 // accompanying file LICENSE_1_0.txt or copy at
\r
11 // http://www.boost.org/LICENSE_1_0.txt)
\r
13 // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
\r
16 #include <memory> // boost.TR1 include order fix
\r
17 #include <boost/smart_ptr/detail/shared_count.hpp>
\r
18 #include <boost/smart_ptr/shared_ptr.hpp>
\r
20 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
\r
21 # pragma warning(push)
\r
22 # pragma warning(disable:4284) // odd return type for operator->
\r
28 template<class T> class weak_ptr
\r
32 // Borland 5.5.1 specific workarounds
\r
33 typedef weak_ptr<T> this_type;
\r
37 typedef T element_type;
\r
39 weak_ptr(): px(0), pn() // never throws in 1.30+
\r
43 // generated copy constructor, assignment, destructor are fine
\r
47 // The "obvious" converting constructor implementation:
\r
49 // template<class Y>
\r
50 // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
\r
54 // has a serious problem.
\r
56 // r.px may already have been invalidated. The px(r.px)
\r
57 // conversion may require access to *r.px (virtual inheritance).
\r
59 // It is not possible to avoid spurious access violations since
\r
60 // in multithreaded programs r.px may be invalidated at any point.
\r
64 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
\r
66 weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
\r
70 weak_ptr( weak_ptr<Y> const & r )
\r
73 : px(r.lock().get()), pn(r.pn) // never throws
\r
77 #if defined( BOOST_HAS_RVALUE_REFS )
\r
80 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
\r
82 weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
\r
86 weak_ptr( weak_ptr<Y> && r )
\r
89 : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) // never throws
\r
94 // for better efficiency in the T == Y case
\r
95 weak_ptr( weak_ptr && r ): px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) // never throws
\r
100 // for better efficiency in the T == Y case
\r
101 weak_ptr & operator=( weak_ptr && r ) // never throws
\r
103 this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
\r
111 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
\r
113 weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
\r
117 weak_ptr( shared_ptr<Y> const & r )
\r
120 : px( r.px ), pn( r.pn ) // never throws
\r
124 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
\r
127 weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
\r
129 px = r.lock().get();
\r
134 #if defined( BOOST_HAS_RVALUE_REFS )
\r
137 weak_ptr & operator=( weak_ptr<Y> && r )
\r
139 this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
\r
146 weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
\r
155 shared_ptr<T> lock() const // never throws
\r
157 return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
\r
160 long use_count() const // never throws
\r
162 return pn.use_count();
\r
165 bool expired() const // never throws
\r
167 return pn.use_count() == 0;
\r
170 bool _empty() const // extension, not in std::weak_ptr
\r
175 void reset() // never throws in 1.30+
\r
177 this_type().swap(*this);
\r
180 void swap(this_type & other) // never throws
\r
182 std::swap(px, other.px);
\r
186 void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
\r
192 template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
\r
194 return pn < rhs.pn;
\r
197 // Tasteless as this may seem, making all members public allows member templates
\r
198 // to work in the absence of member template friends. (Matthew Langston)
\r
200 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
\r
204 template<class Y> friend class weak_ptr;
\r
205 template<class Y> friend class shared_ptr;
\r
209 T * px; // contained pointer
\r
210 boost::detail::weak_count pn; // reference counter
\r
214 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
\r
216 return a._internal_less(b);
\r
219 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
\r
224 } // namespace boost
\r
227 # pragma warning(pop)
\r
230 #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
\r