Chris@16
|
1 #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
|
Chris@16
|
2 #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
|
Chris@16
|
3
|
Chris@16
|
4 //
|
Chris@16
|
5 // weak_ptr.hpp
|
Chris@16
|
6 //
|
Chris@16
|
7 // Copyright (c) 2001, 2002, 2003 Peter Dimov
|
Chris@16
|
8 //
|
Chris@16
|
9 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
10 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
11 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
12 //
|
Chris@16
|
13 // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
|
Chris@16
|
14 //
|
Chris@16
|
15
|
Chris@16
|
16 #include <memory> // boost.TR1 include order fix
|
Chris@16
|
17 #include <boost/smart_ptr/detail/shared_count.hpp>
|
Chris@16
|
18 #include <boost/smart_ptr/shared_ptr.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost
|
Chris@16
|
21 {
|
Chris@16
|
22
|
Chris@16
|
23 template<class T> class weak_ptr
|
Chris@16
|
24 {
|
Chris@16
|
25 private:
|
Chris@16
|
26
|
Chris@16
|
27 // Borland 5.5.1 specific workarounds
|
Chris@16
|
28 typedef weak_ptr<T> this_type;
|
Chris@16
|
29
|
Chris@16
|
30 public:
|
Chris@16
|
31
|
Chris@16
|
32 typedef typename boost::detail::sp_element< T >::type element_type;
|
Chris@16
|
33
|
Chris@16
|
34 weak_ptr() BOOST_NOEXCEPT : px(0), pn() // never throws in 1.30+
|
Chris@16
|
35 {
|
Chris@16
|
36 }
|
Chris@16
|
37
|
Chris@16
|
38 // generated copy constructor, assignment, destructor are fine...
|
Chris@16
|
39
|
Chris@16
|
40 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
Chris@16
|
41
|
Chris@16
|
42 // ... except in C++0x, move disables the implicit copy
|
Chris@16
|
43
|
Chris@16
|
44 weak_ptr( weak_ptr const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
|
Chris@16
|
45 {
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 weak_ptr & operator=( weak_ptr const & r ) BOOST_NOEXCEPT
|
Chris@16
|
49 {
|
Chris@16
|
50 px = r.px;
|
Chris@16
|
51 pn = r.pn;
|
Chris@16
|
52 return *this;
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 #endif
|
Chris@16
|
56
|
Chris@16
|
57 //
|
Chris@16
|
58 // The "obvious" converting constructor implementation:
|
Chris@16
|
59 //
|
Chris@16
|
60 // template<class Y>
|
Chris@16
|
61 // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
|
Chris@16
|
62 // {
|
Chris@16
|
63 // }
|
Chris@16
|
64 //
|
Chris@16
|
65 // has a serious problem.
|
Chris@16
|
66 //
|
Chris@16
|
67 // r.px may already have been invalidated. The px(r.px)
|
Chris@16
|
68 // conversion may require access to *r.px (virtual inheritance).
|
Chris@16
|
69 //
|
Chris@16
|
70 // It is not possible to avoid spurious access violations since
|
Chris@16
|
71 // in multithreaded programs r.px may be invalidated at any point.
|
Chris@16
|
72 //
|
Chris@16
|
73
|
Chris@16
|
74 template<class Y>
|
Chris@16
|
75 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
Chris@16
|
76
|
Chris@16
|
77 weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
|
Chris@16
|
78
|
Chris@16
|
79 #else
|
Chris@16
|
80
|
Chris@16
|
81 weak_ptr( weak_ptr<Y> const & r )
|
Chris@16
|
82
|
Chris@16
|
83 #endif
|
Chris@16
|
84 BOOST_NOEXCEPT : px(r.lock().get()), pn(r.pn)
|
Chris@16
|
85 {
|
Chris@16
|
86 boost::detail::sp_assert_convertible< Y, T >();
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
Chris@16
|
90
|
Chris@16
|
91 template<class Y>
|
Chris@16
|
92 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
Chris@16
|
93
|
Chris@16
|
94 weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
|
Chris@16
|
95
|
Chris@16
|
96 #else
|
Chris@16
|
97
|
Chris@16
|
98 weak_ptr( weak_ptr<Y> && r )
|
Chris@16
|
99
|
Chris@16
|
100 #endif
|
Chris@16
|
101 BOOST_NOEXCEPT : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
|
Chris@16
|
102 {
|
Chris@16
|
103 boost::detail::sp_assert_convertible< Y, T >();
|
Chris@16
|
104 r.px = 0;
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 // for better efficiency in the T == Y case
|
Chris@16
|
108 weak_ptr( weak_ptr && r )
|
Chris@16
|
109 BOOST_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
|
Chris@16
|
110 {
|
Chris@16
|
111 r.px = 0;
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 // for better efficiency in the T == Y case
|
Chris@16
|
115 weak_ptr & operator=( weak_ptr && r ) BOOST_NOEXCEPT
|
Chris@16
|
116 {
|
Chris@16
|
117 this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
|
Chris@16
|
118 return *this;
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121
|
Chris@16
|
122 #endif
|
Chris@16
|
123
|
Chris@16
|
124 template<class Y>
|
Chris@16
|
125 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
Chris@16
|
126
|
Chris@16
|
127 weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
|
Chris@16
|
128
|
Chris@16
|
129 #else
|
Chris@16
|
130
|
Chris@16
|
131 weak_ptr( shared_ptr<Y> const & r )
|
Chris@16
|
132
|
Chris@16
|
133 #endif
|
Chris@16
|
134 BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
|
Chris@16
|
135 {
|
Chris@16
|
136 boost::detail::sp_assert_convertible< Y, T >();
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
|
Chris@16
|
140
|
Chris@16
|
141 template<class Y>
|
Chris@16
|
142 weak_ptr & operator=( weak_ptr<Y> const & r ) BOOST_NOEXCEPT
|
Chris@16
|
143 {
|
Chris@16
|
144 boost::detail::sp_assert_convertible< Y, T >();
|
Chris@16
|
145
|
Chris@16
|
146 px = r.lock().get();
|
Chris@16
|
147 pn = r.pn;
|
Chris@16
|
148
|
Chris@16
|
149 return *this;
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
Chris@16
|
153
|
Chris@16
|
154 template<class Y>
|
Chris@16
|
155 weak_ptr & operator=( weak_ptr<Y> && r ) BOOST_NOEXCEPT
|
Chris@16
|
156 {
|
Chris@16
|
157 this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
|
Chris@16
|
158 return *this;
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 #endif
|
Chris@16
|
162
|
Chris@16
|
163 template<class Y>
|
Chris@16
|
164 weak_ptr & operator=( shared_ptr<Y> const & r ) BOOST_NOEXCEPT
|
Chris@16
|
165 {
|
Chris@16
|
166 boost::detail::sp_assert_convertible< Y, T >();
|
Chris@16
|
167
|
Chris@16
|
168 px = r.px;
|
Chris@16
|
169 pn = r.pn;
|
Chris@16
|
170
|
Chris@16
|
171 return *this;
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 #endif
|
Chris@16
|
175
|
Chris@16
|
176 shared_ptr<T> lock() const BOOST_NOEXCEPT
|
Chris@16
|
177 {
|
Chris@16
|
178 return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 long use_count() const BOOST_NOEXCEPT
|
Chris@16
|
182 {
|
Chris@16
|
183 return pn.use_count();
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 bool expired() const BOOST_NOEXCEPT
|
Chris@16
|
187 {
|
Chris@16
|
188 return pn.use_count() == 0;
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 bool _empty() const // extension, not in std::weak_ptr
|
Chris@16
|
192 {
|
Chris@16
|
193 return pn.empty();
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 void reset() BOOST_NOEXCEPT // never throws in 1.30+
|
Chris@16
|
197 {
|
Chris@16
|
198 this_type().swap(*this);
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 void swap(this_type & other) BOOST_NOEXCEPT
|
Chris@16
|
202 {
|
Chris@16
|
203 std::swap(px, other.px);
|
Chris@16
|
204 pn.swap(other.pn);
|
Chris@16
|
205 }
|
Chris@16
|
206
|
Chris@16
|
207 template<typename Y>
|
Chris@16
|
208 void _internal_aliasing_assign(weak_ptr<Y> const & r, element_type * px2)
|
Chris@16
|
209 {
|
Chris@16
|
210 px = px2;
|
Chris@16
|
211 pn = r.pn;
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
|
Chris@16
|
215 {
|
Chris@16
|
216 return pn < rhs.pn;
|
Chris@16
|
217 }
|
Chris@16
|
218
|
Chris@16
|
219 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
|
Chris@16
|
220 {
|
Chris@16
|
221 return pn < rhs.pn;
|
Chris@16
|
222 }
|
Chris@16
|
223
|
Chris@16
|
224 // Tasteless as this may seem, making all members public allows member templates
|
Chris@16
|
225 // to work in the absence of member template friends. (Matthew Langston)
|
Chris@16
|
226
|
Chris@16
|
227 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
Chris@16
|
228
|
Chris@16
|
229 private:
|
Chris@16
|
230
|
Chris@16
|
231 template<class Y> friend class weak_ptr;
|
Chris@16
|
232 template<class Y> friend class shared_ptr;
|
Chris@16
|
233
|
Chris@16
|
234 #endif
|
Chris@16
|
235
|
Chris@16
|
236 element_type * px; // contained pointer
|
Chris@16
|
237 boost::detail::weak_count pn; // reference counter
|
Chris@16
|
238
|
Chris@16
|
239 }; // weak_ptr
|
Chris@16
|
240
|
Chris@16
|
241 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) BOOST_NOEXCEPT
|
Chris@16
|
242 {
|
Chris@16
|
243 return a.owner_before( b );
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) BOOST_NOEXCEPT
|
Chris@16
|
247 {
|
Chris@16
|
248 a.swap(b);
|
Chris@16
|
249 }
|
Chris@16
|
250
|
Chris@16
|
251 } // namespace boost
|
Chris@16
|
252
|
Chris@16
|
253 #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
|