1 #ifndef BOOST_WEAK_PTR_HPP_INCLUDED
2 #define BOOST_WEAK_PTR_HPP_INCLUDED
7 // Copyright (c) 2001, 2002, 2003 Peter Dimov
9 // Permission to copy, use, modify, sell and distribute this software
10 // is granted provided this copyright notice appears in all copies.
11 // This software is provided "as is" without express or implied
12 // warranty, and with no claim as to its suitability for any purpose.
14 // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
17 #include <boost/shared_ptr.hpp>
19 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
20 # pragma warning(push)
21 # pragma warning(disable:4284) // odd return type for operator->
27 template<class T> class weak_ptr
31 // Borland 5.5.1 specific workarounds
32 typedef weak_ptr<T> this_type;
36 typedef T element_type;
38 weak_ptr(): px(0), pn() // never throws in 1.30+
42 // generated copy constructor, assignment, destructor are fine
46 // The "obvious" converting constructor implementation:
49 // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
53 // has a serious problem.
55 // r.px may already have been invalidated. The px(r.px)
56 // conversion may require access to *r.px (virtual inheritance).
58 // It is not possible to avoid spurious access violations since
59 // in multithreaded programs r.px may be invalidated at any point.
63 weak_ptr(weak_ptr<Y> const & r): pn(r.pn) // never throws
69 weak_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
73 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
76 weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
84 weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
93 shared_ptr<T> lock() const // never throws
95 #if defined(BOOST_HAS_THREADS)
97 // optimization: avoid throw overhead
100 return shared_ptr<element_type>();
105 return shared_ptr<element_type>(*this);
107 catch(bad_weak_ptr const &)
109 // Q: how can we get here?
110 // A: another thread may have invalidated r after the use_count test above.
111 return shared_ptr<element_type>();
116 // optimization: avoid try/catch overhead when single threaded
117 return expired()? shared_ptr<element_type>(): shared_ptr<element_type>(*this);
122 long use_count() const // never throws
124 return pn.use_count();
127 bool expired() const // never throws
129 return pn.use_count() == 0;
132 void reset() // never throws in 1.30+
134 this_type().swap(*this);
137 void swap(this_type & other) // never throws
139 std::swap(px, other.px);
143 void _internal_assign(T * px2, detail::shared_count const & pn2)
149 template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
154 // Tasteless as this may seem, making all members public allows member templates
155 // to work in the absence of member template friends. (Matthew Langston)
157 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
161 template<class Y> friend class weak_ptr;
162 template<class Y> friend class shared_ptr;
166 T * px; // contained pointer
167 detail::weak_count pn; // reference counter
171 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
173 return a._internal_less(b);
176 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
181 // deprecated, provided for backward compatibility
182 template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r)
190 # pragma warning(pop)
193 #endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED