]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED |
2 | #define BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED | |
3 | ||
4 | // | |
5 | // intrusive_ptr.hpp | |
6 | // | |
7 | // Copyright (c) 2001, 2002 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/intrusive_ptr.html for documentation. | |
14 | // | |
15 | ||
16 | #include <boost/config.hpp> | |
17 | ||
18 | #include <boost/assert.hpp> | |
19 | #include <boost/detail/workaround.hpp> | |
20 | #include <boost/smart_ptr/detail/sp_convertible.hpp> | |
21 | #include <boost/smart_ptr/detail/sp_nullptr_t.hpp> | |
22 | ||
23 | #include <boost/config/no_tr1/functional.hpp> // for std::less | |
24 | ||
25 | #if !defined(BOOST_NO_IOSTREAM) | |
26 | #if !defined(BOOST_NO_IOSFWD) | |
27 | #include <iosfwd> // for std::basic_ostream | |
28 | #else | |
29 | #include <ostream> | |
30 | #endif | |
31 | #endif | |
32 | ||
33 | ||
34 | namespace boost | |
35 | { | |
36 | ||
37 | // | |
38 | // intrusive_ptr | |
39 | // | |
40 | // A smart pointer that uses intrusive reference counting. | |
41 | // | |
42 | // Relies on unqualified calls to | |
43 | // | |
44 | // void intrusive_ptr_add_ref(T * p); | |
45 | // void intrusive_ptr_release(T * p); | |
46 | // | |
47 | // (p != 0) | |
48 | // | |
49 | // The object is responsible for destroying itself. | |
50 | // | |
51 | ||
52 | template<class T> class intrusive_ptr | |
53 | { | |
54 | private: | |
55 | ||
56 | typedef intrusive_ptr this_type; | |
57 | ||
58 | public: | |
59 | ||
60 | typedef T element_type; | |
61 | ||
62 | BOOST_CONSTEXPR intrusive_ptr() BOOST_NOEXCEPT : px( 0 ) | |
63 | { | |
64 | } | |
65 | ||
66 | intrusive_ptr( T * p, bool add_ref = true ): px( p ) | |
67 | { | |
68 | if( px != 0 && add_ref ) intrusive_ptr_add_ref( px ); | |
69 | } | |
70 | ||
71 | #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES) | |
72 | ||
73 | template<class U> | |
74 | #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) | |
75 | ||
76 | intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() ) | |
77 | ||
78 | #else | |
79 | ||
80 | intrusive_ptr( intrusive_ptr<U> const & rhs ) | |
81 | ||
82 | #endif | |
83 | : px( rhs.get() ) | |
84 | { | |
85 | if( px != 0 ) intrusive_ptr_add_ref( px ); | |
86 | } | |
87 | ||
88 | #endif | |
89 | ||
90 | intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px ) | |
91 | { | |
92 | if( px != 0 ) intrusive_ptr_add_ref( px ); | |
93 | } | |
94 | ||
95 | ~intrusive_ptr() | |
96 | { | |
97 | if( px != 0 ) intrusive_ptr_release( px ); | |
98 | } | |
99 | ||
100 | #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES) | |
101 | ||
102 | template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs) | |
103 | { | |
104 | this_type(rhs).swap(*this); | |
105 | return *this; | |
106 | } | |
107 | ||
108 | #endif | |
109 | ||
110 | // Move support | |
111 | ||
112 | #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) | |
113 | ||
114 | intrusive_ptr(intrusive_ptr && rhs) BOOST_NOEXCEPT : px( rhs.px ) | |
115 | { | |
116 | rhs.px = 0; | |
117 | } | |
118 | ||
119 | intrusive_ptr & operator=(intrusive_ptr && rhs) BOOST_NOEXCEPT | |
120 | { | |
121 | this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this); | |
122 | return *this; | |
123 | } | |
124 | ||
125 | template<class U> friend class intrusive_ptr; | |
126 | ||
127 | template<class U> | |
128 | #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) | |
129 | ||
130 | intrusive_ptr(intrusive_ptr<U> && rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty()) | |
131 | ||
132 | #else | |
133 | ||
134 | intrusive_ptr(intrusive_ptr<U> && rhs) | |
135 | ||
136 | #endif | |
137 | : px( rhs.px ) | |
138 | { | |
139 | rhs.px = 0; | |
140 | } | |
141 | ||
142 | template<class U> | |
143 | intrusive_ptr & operator=(intrusive_ptr<U> && rhs) BOOST_NOEXCEPT | |
144 | { | |
145 | this_type( static_cast< intrusive_ptr<U> && >( rhs ) ).swap(*this); | |
146 | return *this; | |
147 | } | |
148 | ||
149 | #endif | |
150 | ||
151 | intrusive_ptr & operator=(intrusive_ptr const & rhs) | |
152 | { | |
153 | this_type(rhs).swap(*this); | |
154 | return *this; | |
155 | } | |
156 | ||
157 | intrusive_ptr & operator=(T * rhs) | |
158 | { | |
159 | this_type(rhs).swap(*this); | |
160 | return *this; | |
161 | } | |
162 | ||
163 | void reset() BOOST_NOEXCEPT | |
164 | { | |
165 | this_type().swap( *this ); | |
166 | } | |
167 | ||
168 | void reset( T * rhs ) | |
169 | { | |
170 | this_type( rhs ).swap( *this ); | |
171 | } | |
172 | ||
173 | void reset( T * rhs, bool add_ref ) | |
174 | { | |
175 | this_type( rhs, add_ref ).swap( *this ); | |
176 | } | |
177 | ||
178 | T * get() const BOOST_NOEXCEPT | |
179 | { | |
180 | return px; | |
181 | } | |
182 | ||
183 | T * detach() BOOST_NOEXCEPT | |
184 | { | |
185 | T * ret = px; | |
186 | px = 0; | |
187 | return ret; | |
188 | } | |
189 | ||
190 | T & operator*() const | |
191 | { | |
192 | BOOST_ASSERT( px != 0 ); | |
193 | return *px; | |
194 | } | |
195 | ||
196 | T * operator->() const | |
197 | { | |
198 | BOOST_ASSERT( px != 0 ); | |
199 | return px; | |
200 | } | |
201 | ||
202 | // implicit conversion to "bool" | |
203 | #include <boost/smart_ptr/detail/operator_bool.hpp> | |
204 | ||
205 | void swap(intrusive_ptr & rhs) BOOST_NOEXCEPT | |
206 | { | |
207 | T * tmp = px; | |
208 | px = rhs.px; | |
209 | rhs.px = tmp; | |
210 | } | |
211 | ||
212 | private: | |
213 | ||
214 | T * px; | |
215 | }; | |
216 | ||
217 | template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) | |
218 | { | |
219 | return a.get() == b.get(); | |
220 | } | |
221 | ||
222 | template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) | |
223 | { | |
224 | return a.get() != b.get(); | |
225 | } | |
226 | ||
227 | template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b) | |
228 | { | |
229 | return a.get() == b; | |
230 | } | |
231 | ||
232 | template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b) | |
233 | { | |
234 | return a.get() != b; | |
235 | } | |
236 | ||
237 | template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b) | |
238 | { | |
239 | return a == b.get(); | |
240 | } | |
241 | ||
242 | template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b) | |
243 | { | |
244 | return a != b.get(); | |
245 | } | |
246 | ||
247 | #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 | |
248 | ||
249 | // Resolve the ambiguity between our op!= and the one in rel_ops | |
250 | ||
251 | template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) | |
252 | { | |
253 | return a.get() != b.get(); | |
254 | } | |
255 | ||
256 | #endif | |
257 | ||
258 | #if !defined( BOOST_NO_CXX11_NULLPTR ) | |
259 | ||
260 | template<class T> inline bool operator==( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT | |
261 | { | |
262 | return p.get() == 0; | |
263 | } | |
264 | ||
265 | template<class T> inline bool operator==( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT | |
266 | { | |
267 | return p.get() == 0; | |
268 | } | |
269 | ||
270 | template<class T> inline bool operator!=( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT | |
271 | { | |
272 | return p.get() != 0; | |
273 | } | |
274 | ||
275 | template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT | |
276 | { | |
277 | return p.get() != 0; | |
278 | } | |
279 | ||
280 | #endif | |
281 | ||
282 | template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) | |
283 | { | |
284 | return std::less<T *>()(a.get(), b.get()); | |
285 | } | |
286 | ||
287 | template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs) | |
288 | { | |
289 | lhs.swap(rhs); | |
290 | } | |
291 | ||
292 | // mem_fn support | |
293 | ||
294 | template<class T> T * get_pointer(intrusive_ptr<T> const & p) | |
295 | { | |
296 | return p.get(); | |
297 | } | |
298 | ||
299 | template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p) | |
300 | { | |
301 | return static_cast<T *>(p.get()); | |
302 | } | |
303 | ||
304 | template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p) | |
305 | { | |
306 | return const_cast<T *>(p.get()); | |
307 | } | |
308 | ||
309 | template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p) | |
310 | { | |
311 | return dynamic_cast<T *>(p.get()); | |
312 | } | |
313 | ||
314 | // operator<< | |
315 | ||
316 | #if !defined(BOOST_NO_IOSTREAM) | |
317 | ||
318 | #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) ) | |
319 | ||
320 | template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p) | |
321 | { | |
322 | os << p.get(); | |
323 | return os; | |
324 | } | |
325 | ||
326 | #else | |
327 | ||
328 | // in STLport's no-iostreams mode no iostream symbols can be used | |
329 | #ifndef _STLP_NO_IOSTREAMS | |
330 | ||
331 | # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT) | |
332 | // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL | |
333 | using std::basic_ostream; | |
334 | template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p) | |
335 | # else | |
336 | template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p) | |
337 | # endif | |
338 | { | |
339 | os << p.get(); | |
340 | return os; | |
341 | } | |
342 | ||
343 | #endif // _STLP_NO_IOSTREAMS | |
344 | ||
345 | #endif // __GNUC__ < 3 | |
346 | ||
347 | #endif // !defined(BOOST_NO_IOSTREAM) | |
348 | ||
349 | // hash_value | |
350 | ||
351 | template< class T > struct hash; | |
352 | ||
353 | template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p ) | |
354 | { | |
355 | return boost::hash< T* >()( p.get() ); | |
356 | } | |
357 | ||
358 | } // namespace boost | |
359 | ||
360 | #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED |