Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
1 #ifndef BOOST_WEAK_PTR_HPP_INCLUDED
2 #define BOOST_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/detail/shared_count.hpp>
18 #include <boost/shared_ptr.hpp>
20 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
21 # pragma warning(push)
22 # pragma warning(disable:4284) // odd return type for operator->
28 template<class T> class weak_ptr
32 // Borland 5.5.1 specific workarounds
33 typedef weak_ptr<T> this_type;
37 typedef T element_type;
39 weak_ptr(): px(0), pn() // never throws in 1.30+
43 // generated copy constructor, assignment, destructor are fine
47 // The "obvious" converting constructor implementation:
50 // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
54 // has a serious problem.
56 // r.px may already have been invalidated. The px(r.px)
57 // conversion may require access to *r.px (virtual inheritance).
59 // It is not possible to avoid spurious access violations since
60 // in multithreaded programs r.px may be invalidated at any point.
64 weak_ptr(weak_ptr<Y> const & r): pn(r.pn) // never throws
70 weak_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
74 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
77 weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
85 weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
94 shared_ptr<T> lock() const // never throws
96 #if defined(BOOST_HAS_THREADS)
98 // optimization: avoid throw overhead
101 return shared_ptr<element_type>();
106 return shared_ptr<element_type>(*this);
108 catch(bad_weak_ptr const &)
110 // Q: how can we get here?
111 // A: another thread may have invalidated r after the use_count test above.
112 return shared_ptr<element_type>();
117 // optimization: avoid try/catch overhead when single threaded
118 return expired()? shared_ptr<element_type>(): shared_ptr<element_type>(*this);
123 long use_count() const // never throws
125 return pn.use_count();
128 bool expired() const // never throws
130 return pn.use_count() == 0;
133 void reset() // never throws in 1.30+
135 this_type().swap(*this);
138 void swap(this_type & other) // never throws
140 std::swap(px, other.px);
144 void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
150 template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
155 // Tasteless as this may seem, making all members public allows member templates
156 // to work in the absence of member template friends. (Matthew Langston)
158 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
162 template<class Y> friend class weak_ptr;
163 template<class Y> friend class shared_ptr;
167 T * px; // contained pointer
168 boost::detail::weak_count pn; // reference counter
172 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
174 return a._internal_less(b);
177 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
182 // deprecated, provided for backward compatibility
183 template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r)
191 # pragma warning(pop)
194 #endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED