1 | #ifndef BOOST_WEAK_PTR_HPP_INCLUDED
|
---|
2 | #define BOOST_WEAK_PTR_HPP_INCLUDED
|
---|
3 |
|
---|
4 | //
|
---|
5 | // weak_ptr.hpp
|
---|
6 | //
|
---|
7 | // Copyright (c) 2001, 2002, 2003 Peter Dimov
|
---|
8 | //
|
---|
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)
|
---|
12 | //
|
---|
13 | // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <memory> // boost.TR1 include order fix
|
---|
17 | #include <boost/detail/shared_count.hpp>
|
---|
18 | #include <boost/shared_ptr.hpp>
|
---|
19 |
|
---|
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->
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | namespace boost
|
---|
26 | {
|
---|
27 |
|
---|
28 | template<class T> class weak_ptr
|
---|
29 | {
|
---|
30 | private:
|
---|
31 |
|
---|
32 | // Borland 5.5.1 specific workarounds
|
---|
33 | typedef weak_ptr<T> this_type;
|
---|
34 |
|
---|
35 | public:
|
---|
36 |
|
---|
37 | typedef T element_type;
|
---|
38 |
|
---|
39 | weak_ptr(): px(0), pn() // never throws in 1.30+
|
---|
40 | {
|
---|
41 | }
|
---|
42 |
|
---|
43 | // generated copy constructor, assignment, destructor are fine
|
---|
44 |
|
---|
45 |
|
---|
46 | //
|
---|
47 | // The "obvious" converting constructor implementation:
|
---|
48 | //
|
---|
49 | // template<class Y>
|
---|
50 | // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
|
---|
51 | // {
|
---|
52 | // }
|
---|
53 | //
|
---|
54 | // has a serious problem.
|
---|
55 | //
|
---|
56 | // r.px may already have been invalidated. The px(r.px)
|
---|
57 | // conversion may require access to *r.px (virtual inheritance).
|
---|
58 | //
|
---|
59 | // It is not possible to avoid spurious access violations since
|
---|
60 | // in multithreaded programs r.px may be invalidated at any point.
|
---|
61 | //
|
---|
62 |
|
---|
63 | template<class Y>
|
---|
64 | #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
---|
65 |
|
---|
66 | weak_ptr( weak_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
|
---|
67 |
|
---|
68 | #else
|
---|
69 |
|
---|
70 | weak_ptr( weak_ptr<Y> const & r )
|
---|
71 |
|
---|
72 | #endif
|
---|
73 | : pn(r.pn) // never throws
|
---|
74 | {
|
---|
75 | px = r.lock().get();
|
---|
76 | }
|
---|
77 |
|
---|
78 | template<class Y>
|
---|
79 | #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
---|
80 |
|
---|
81 | weak_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
|
---|
82 |
|
---|
83 | #else
|
---|
84 |
|
---|
85 | weak_ptr( shared_ptr<Y> const & r )
|
---|
86 |
|
---|
87 | #endif
|
---|
88 | : px( r.px ), pn( r.pn ) // never throws
|
---|
89 | {
|
---|
90 | }
|
---|
91 |
|
---|
92 | #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
|
---|
93 |
|
---|
94 | template<class Y>
|
---|
95 | weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
|
---|
96 | {
|
---|
97 | px = r.lock().get();
|
---|
98 | pn = r.pn;
|
---|
99 | return *this;
|
---|
100 | }
|
---|
101 |
|
---|
102 | template<class Y>
|
---|
103 | weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
|
---|
104 | {
|
---|
105 | px = r.px;
|
---|
106 | pn = r.pn;
|
---|
107 | return *this;
|
---|
108 | }
|
---|
109 |
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | shared_ptr<T> lock() const // never throws
|
---|
113 | {
|
---|
114 | return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
|
---|
115 | }
|
---|
116 |
|
---|
117 | long use_count() const // never throws
|
---|
118 | {
|
---|
119 | return pn.use_count();
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool expired() const // never throws
|
---|
123 | {
|
---|
124 | return pn.use_count() == 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void reset() // never throws in 1.30+
|
---|
128 | {
|
---|
129 | this_type().swap(*this);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void swap(this_type & other) // never throws
|
---|
133 | {
|
---|
134 | std::swap(px, other.px);
|
---|
135 | pn.swap(other.pn);
|
---|
136 | }
|
---|
137 |
|
---|
138 | void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
|
---|
139 | {
|
---|
140 | px = px2;
|
---|
141 | pn = pn2;
|
---|
142 | }
|
---|
143 |
|
---|
144 | template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
|
---|
145 | {
|
---|
146 | return pn < rhs.pn;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Tasteless as this may seem, making all members public allows member templates
|
---|
150 | // to work in the absence of member template friends. (Matthew Langston)
|
---|
151 |
|
---|
152 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
---|
153 |
|
---|
154 | private:
|
---|
155 |
|
---|
156 | template<class Y> friend class weak_ptr;
|
---|
157 | template<class Y> friend class shared_ptr;
|
---|
158 |
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | T * px; // contained pointer
|
---|
162 | boost::detail::weak_count pn; // reference counter
|
---|
163 |
|
---|
164 | }; // weak_ptr
|
---|
165 |
|
---|
166 | template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
|
---|
167 | {
|
---|
168 | return a._internal_less(b);
|
---|
169 | }
|
---|
170 |
|
---|
171 | template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
|
---|
172 | {
|
---|
173 | a.swap(b);
|
---|
174 | }
|
---|
175 |
|
---|
176 | } // namespace boost
|
---|
177 |
|
---|
178 | #ifdef BOOST_MSVC
|
---|
179 | # pragma warning(pop)
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | #endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED
|
---|