7
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)
9
// Distributed under the Boost Software License, Version 1.0.
10
// See accompanying file LICENSE_1_0.txt or copy at
11
// http://www.boost.org/LICENSE_1_0.txt
13
13
// See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
16
#include <boost/shared_ptr.hpp>
18
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
19
# pragma warning(push)
20
# pragma warning(disable:4284) // odd return type for operator->
26
template<class T> class weak_ptr
30
// Borland 5.5.1 specific workarounds
31
typedef weak_ptr<T> this_type;
35
typedef T element_type;
37
weak_ptr(): px(0), pn() // never throws in 1.30+
41
// generated copy constructor, assignment, destructor are fine
45
// The "obvious" converting constructor implementation:
48
// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
52
// has a serious problem.
54
// r.px may already have been invalidated. The px(r.px)
55
// conversion may require access to *r.px (virtual inheritance).
57
// It is not possible to avoid spurious access violations since
58
// in multithreaded programs r.px may be invalidated at any point.
62
weak_ptr(weak_ptr<Y> const & r): pn(r.pn) // never throws
68
weak_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
72
#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
75
weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
83
weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
92
shared_ptr<T> lock() const // never throws
94
#if defined(BOOST_HAS_THREADS)
96
// optimization: avoid throw overhead
99
return shared_ptr<element_type>();
104
return shared_ptr<element_type>(*this);
106
catch(bad_weak_ptr const &)
108
// Q: how can we get here?
109
// A: another thread may have invalidated r after the use_count test above.
110
return shared_ptr<element_type>();
115
// optimization: avoid try/catch overhead when single threaded
116
return expired()? shared_ptr<element_type>(): shared_ptr<element_type>(*this);
121
long use_count() const // never throws
123
return pn.use_count();
126
bool expired() const // never throws
128
return pn.use_count() == 0;
131
void reset() // never throws in 1.30+
133
this_type().swap(*this);
136
void swap(this_type & other) // never throws
138
std::swap(px, other.px);
142
void _internal_assign(T * px2, detail::shared_count const & pn2)
148
template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
153
// Tasteless as this may seem, making all members public allows member templates
154
// to work in the absence of member template friends. (Matthew Langston)
156
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
160
template<class Y> friend class weak_ptr;
161
template<class Y> friend class shared_ptr;
165
T * px; // contained pointer
166
detail::weak_count pn; // reference counter
170
template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
172
return a._internal_less(b);
175
template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
180
// deprecated, provided for backward compatibility
181
template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r)
189
# pragma warning(pop)
16
#include <boost/smart_ptr/weak_ptr.hpp>
192
18
#endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED