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/ 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>
19 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
24 template<class T> class weak_ptr
28 // Borland 5.5.1 specific workarounds
29 typedef weak_ptr<T> this_type;
33 typedef typename boost::detail::sp_element< T >::type element_type;
35 BOOST_CONSTEXPR weak_ptr() BOOST_SP_NOEXCEPT : px(0), pn()
39 // generated copy constructor, assignment, destructor are fine...
41 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
43 // ... except in C++0x, move disables the implicit copy
45 weak_ptr( weak_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
49 weak_ptr & operator=( weak_ptr const & r ) BOOST_SP_NOEXCEPT
59 // The "obvious" converting constructor implementation:
62 // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn)
66 // has a serious problem.
68 // r.px may already have been invalidated. The px(r.px)
69 // conversion may require access to *r.px (virtual inheritance).
71 // It is not possible to avoid spurious access violations since
72 // in multithreaded programs r.px may be invalidated at any point.
76 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
78 weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
82 weak_ptr( weak_ptr<Y> const & r )
85 BOOST_SP_NOEXCEPT : px(r.lock().get()), pn(r.pn)
87 boost::detail::sp_assert_convertible< Y, T >();
90 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
93 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
95 weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
99 weak_ptr( weak_ptr<Y> && r )
102 BOOST_SP_NOEXCEPT : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
104 boost::detail::sp_assert_convertible< Y, T >();
108 // for better efficiency in the T == Y case
109 weak_ptr( weak_ptr && r )
110 BOOST_SP_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
115 // for better efficiency in the T == Y case
116 weak_ptr & operator=( weak_ptr && r ) BOOST_SP_NOEXCEPT
118 this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
126 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
128 weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
132 weak_ptr( shared_ptr<Y> const & r )
135 BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
137 boost::detail::sp_assert_convertible< Y, T >();
141 template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( r.pn )
145 template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( r.pn )
149 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
151 template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( std::move( r.pn ) )
157 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
160 weak_ptr & operator=( weak_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
162 boost::detail::sp_assert_convertible< Y, T >();
170 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
173 weak_ptr & operator=( weak_ptr<Y> && r ) BOOST_SP_NOEXCEPT
175 this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
182 weak_ptr & operator=( shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
184 boost::detail::sp_assert_convertible< Y, T >();
194 shared_ptr<T> lock() const BOOST_SP_NOEXCEPT
196 return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
199 long use_count() const BOOST_SP_NOEXCEPT
201 return pn.use_count();
204 bool expired() const BOOST_SP_NOEXCEPT
206 return pn.use_count() == 0;
209 bool _empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
214 bool empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
219 void reset() BOOST_SP_NOEXCEPT
221 this_type().swap(*this);
224 void swap(this_type & other) BOOST_SP_NOEXCEPT
226 std::swap(px, other.px);
230 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
235 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
240 // Tasteless as this may seem, making all members public allows member templates
241 // to work in the absence of member template friends. (Matthew Langston)
243 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
247 template<class Y> friend class weak_ptr;
248 template<class Y> friend class shared_ptr;
252 element_type * px; // contained pointer
253 boost::detail::weak_count pn; // reference counter
257 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) BOOST_SP_NOEXCEPT
259 return a.owner_before( b );
262 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) BOOST_SP_NOEXCEPT
267 #if defined(__cpp_deduction_guides)
269 template<class T> weak_ptr( shared_ptr<T> ) -> weak_ptr<T>;
275 #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED