1 #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
7 // Copyright (c) 2001, 2002, 2003 Peter Dimov
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
16 #include <memory> // boost.TR1 include order fix
17 #include <boost/smart_ptr/detail/shared_count.hpp>
18 #include <boost/smart_ptr/shared_ptr.hpp>
23 template<class T> class weak_ptr
27 // Borland 5.5.1 specific workarounds
28 typedef weak_ptr<T> this_type;
32 typedef T element_type;
34 weak_ptr(): px(0), pn() // never throws in 1.30+
38 // generated copy constructor, assignment, destructor are fine...
40 #if defined( BOOST_HAS_RVALUE_REFS )
42 // ... except in C++0x, move disables the implicit copy
44 weak_ptr( weak_ptr const & r ): px( r.px ), pn( r.pn ) // never throws
48 weak_ptr & operator=( weak_ptr const & r ) // never throws
58 // The "obvious" converting constructor implementation:
61 // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
65 // has a serious problem.
67 // r.px may already have been invalidated. The px(r.px)
68 // conversion may require access to *r.px (virtual inheritance).
70 // It is not possible to avoid spurious access violations since
71 // in multithreaded programs r.px may be invalidated at any point.
75 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
77 weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
81 weak_ptr( weak_ptr<Y> const & r )
84 : px(r.lock().get()), pn(r.pn) // never throws
88 #if defined( BOOST_HAS_RVALUE_REFS )
91 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
93 weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
97 weak_ptr( weak_ptr<Y> && r )
100 : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) // never throws
105 // for better efficiency in the T == Y case
106 weak_ptr( weak_ptr && r ): px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) // never throws
111 // for better efficiency in the T == Y case
112 weak_ptr & operator=( weak_ptr && r ) // never throws
114 this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
122 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
124 weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
128 weak_ptr( shared_ptr<Y> const & r )
131 : px( r.px ), pn( r.pn ) // never throws
135 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
138 weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
145 #if defined( BOOST_HAS_RVALUE_REFS )
148 weak_ptr & operator=( weak_ptr<Y> && r )
150 this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
157 weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
166 shared_ptr<T> lock() const // never throws
168 return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
171 long use_count() const // never throws
173 return pn.use_count();
176 bool expired() const // never throws
178 return pn.use_count() == 0;
181 bool _empty() const // extension, not in std::weak_ptr
186 void reset() // never throws in 1.30+
188 this_type().swap(*this);
191 void swap(this_type & other) // never throws
193 std::swap(px, other.px);
197 void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
203 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
208 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const
213 // Tasteless as this may seem, making all members public allows member templates
214 // to work in the absence of member template friends. (Matthew Langston)
216 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
220 template<class Y> friend class weak_ptr;
221 template<class Y> friend class shared_ptr;
225 T * px; // contained pointer
226 boost::detail::weak_count pn; // reference counter
230 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
232 return a.owner_before( b );
235 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
242 #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED