1 | #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
|
---|
2 | #define BOOST_SHARED_PTR_HPP_INCLUDED
|
---|
3 |
|
---|
4 | //
|
---|
5 | // shared_ptr.hpp
|
---|
6 | //
|
---|
7 | // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
---|
8 | // Copyright (c) 2001, 2002, 2003 Peter Dimov
|
---|
9 | //
|
---|
10 | // Distributed under the Boost Software License, Version 1.0. (See
|
---|
11 | // accompanying file LICENSE_1_0.txt or copy at
|
---|
12 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
13 | //
|
---|
14 | // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
|
---|
15 | //
|
---|
16 |
|
---|
17 | #include <boost/config.hpp> // for broken compiler workarounds
|
---|
18 |
|
---|
19 | #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
|
---|
20 | #include <boost/detail/shared_ptr_nmt.hpp>
|
---|
21 | #else
|
---|
22 |
|
---|
23 | #include <boost/assert.hpp>
|
---|
24 | #include <boost/checked_delete.hpp>
|
---|
25 | #include <boost/throw_exception.hpp>
|
---|
26 | #include <boost/detail/shared_count.hpp>
|
---|
27 | #include <boost/detail/workaround.hpp>
|
---|
28 |
|
---|
29 | #include <memory> // for std::auto_ptr
|
---|
30 | #include <algorithm> // for std::swap
|
---|
31 | #include <functional> // for std::less
|
---|
32 | #include <typeinfo> // for std::bad_cast
|
---|
33 | #include <iosfwd> // for std::basic_ostream
|
---|
34 |
|
---|
35 | #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
|
---|
36 | # pragma warning(push)
|
---|
37 | # pragma warning(disable:4284) // odd return type for operator->
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | namespace boost
|
---|
41 | {
|
---|
42 |
|
---|
43 | template<class T> class weak_ptr;
|
---|
44 | template<class T> class enable_shared_from_this;
|
---|
45 |
|
---|
46 | namespace detail
|
---|
47 | {
|
---|
48 |
|
---|
49 | struct static_cast_tag {};
|
---|
50 | struct const_cast_tag {};
|
---|
51 | struct dynamic_cast_tag {};
|
---|
52 | struct polymorphic_cast_tag {};
|
---|
53 |
|
---|
54 | template<class T> struct shared_ptr_traits
|
---|
55 | {
|
---|
56 | typedef T & reference;
|
---|
57 | };
|
---|
58 |
|
---|
59 | template<> struct shared_ptr_traits<void>
|
---|
60 | {
|
---|
61 | typedef void reference;
|
---|
62 | };
|
---|
63 |
|
---|
64 | #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
|
---|
65 |
|
---|
66 | template<> struct shared_ptr_traits<void const>
|
---|
67 | {
|
---|
68 | typedef void reference;
|
---|
69 | };
|
---|
70 |
|
---|
71 | template<> struct shared_ptr_traits<void volatile>
|
---|
72 | {
|
---|
73 | typedef void reference;
|
---|
74 | };
|
---|
75 |
|
---|
76 | template<> struct shared_ptr_traits<void const volatile>
|
---|
77 | {
|
---|
78 | typedef void reference;
|
---|
79 | };
|
---|
80 |
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | // enable_shared_from_this support
|
---|
84 |
|
---|
85 | template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this<T> const * pe, Y const * px )
|
---|
86 | {
|
---|
87 | if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
|
---|
88 | }
|
---|
89 |
|
---|
90 | inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
|
---|
91 | {
|
---|
92 | }
|
---|
93 |
|
---|
94 | } // namespace detail
|
---|
95 |
|
---|
96 |
|
---|
97 | //
|
---|
98 | // shared_ptr
|
---|
99 | //
|
---|
100 | // An enhanced relative of scoped_ptr with reference counted copy semantics.
|
---|
101 | // The object pointed to is deleted when the last shared_ptr pointing to it
|
---|
102 | // is destroyed or reset.
|
---|
103 | //
|
---|
104 |
|
---|
105 | template<class T> class shared_ptr
|
---|
106 | {
|
---|
107 | private:
|
---|
108 |
|
---|
109 | // Borland 5.5.1 specific workaround
|
---|
110 | typedef shared_ptr<T> this_type;
|
---|
111 |
|
---|
112 | public:
|
---|
113 |
|
---|
114 | typedef T element_type;
|
---|
115 | typedef T value_type;
|
---|
116 | typedef T * pointer;
|
---|
117 | typedef typename detail::shared_ptr_traits<T>::reference reference;
|
---|
118 |
|
---|
119 | shared_ptr(): px(0), pn() // never throws in 1.30+
|
---|
120 | {
|
---|
121 | }
|
---|
122 |
|
---|
123 | template<class Y>
|
---|
124 | explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
|
---|
125 | {
|
---|
126 | detail::sp_enable_shared_from_this( pn, p, p );
|
---|
127 | }
|
---|
128 |
|
---|
129 | //
|
---|
130 | // Requirements: D's copy constructor must not throw
|
---|
131 | //
|
---|
132 | // shared_ptr will release p by calling d(p)
|
---|
133 | //
|
---|
134 |
|
---|
135 | template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
|
---|
136 | {
|
---|
137 | detail::sp_enable_shared_from_this( pn, p, p );
|
---|
138 | }
|
---|
139 |
|
---|
140 | // generated copy constructor, assignment, destructor are fine...
|
---|
141 |
|
---|
142 | // except that Borland C++ has a bug, and g++ with -Wsynth warns
|
---|
143 | #if defined(__BORLANDC__) || defined(__GNUC__)
|
---|
144 |
|
---|
145 | shared_ptr & operator=(shared_ptr const & r) // never throws
|
---|
146 | {
|
---|
147 | px = r.px;
|
---|
148 | pn = r.pn; // shared_count::op= doesn't throw
|
---|
149 | return *this;
|
---|
150 | }
|
---|
151 |
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | template<class Y>
|
---|
155 | explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
|
---|
156 | {
|
---|
157 | // it is now safe to copy r.px, as pn(r.pn) did not throw
|
---|
158 | px = r.px;
|
---|
159 | }
|
---|
160 |
|
---|
161 | template<class Y>
|
---|
162 | shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
|
---|
163 | {
|
---|
164 | }
|
---|
165 |
|
---|
166 | template<class Y>
|
---|
167 | shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
|
---|
168 | {
|
---|
169 | }
|
---|
170 |
|
---|
171 | template<class Y>
|
---|
172 | shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
|
---|
173 | {
|
---|
174 | }
|
---|
175 |
|
---|
176 | template<class Y>
|
---|
177 | shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
|
---|
178 | {
|
---|
179 | if(px == 0) // need to allocate new counter -- the cast failed
|
---|
180 | {
|
---|
181 | pn = detail::shared_count();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | template<class Y>
|
---|
186 | shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
|
---|
187 | {
|
---|
188 | if(px == 0)
|
---|
189 | {
|
---|
190 | boost::throw_exception(std::bad_cast());
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | #ifndef BOOST_NO_AUTO_PTR
|
---|
195 |
|
---|
196 | template<class Y>
|
---|
197 | explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
|
---|
198 | {
|
---|
199 | Y * tmp = r.get();
|
---|
200 | pn = detail::shared_count(r);
|
---|
201 | detail::sp_enable_shared_from_this( pn, tmp, tmp );
|
---|
202 | }
|
---|
203 |
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
|
---|
207 |
|
---|
208 | template<class Y>
|
---|
209 | shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
|
---|
210 | {
|
---|
211 | px = r.px;
|
---|
212 | pn = r.pn; // shared_count::op= doesn't throw
|
---|
213 | return *this;
|
---|
214 | }
|
---|
215 |
|
---|
216 | #endif
|
---|
217 |
|
---|
218 | #ifndef BOOST_NO_AUTO_PTR
|
---|
219 |
|
---|
220 | template<class Y>
|
---|
221 | shared_ptr & operator=(std::auto_ptr<Y> & r)
|
---|
222 | {
|
---|
223 | this_type(r).swap(*this);
|
---|
224 | return *this;
|
---|
225 | }
|
---|
226 |
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | void reset() // never throws in 1.30+
|
---|
230 | {
|
---|
231 | this_type().swap(*this);
|
---|
232 | }
|
---|
233 |
|
---|
234 | template<class Y> void reset(Y * p) // Y must be complete
|
---|
235 | {
|
---|
236 | BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
|
---|
237 | this_type(p).swap(*this);
|
---|
238 | }
|
---|
239 |
|
---|
240 | template<class Y, class D> void reset(Y * p, D d)
|
---|
241 | {
|
---|
242 | this_type(p, d).swap(*this);
|
---|
243 | }
|
---|
244 |
|
---|
245 | reference operator* () const // never throws
|
---|
246 | {
|
---|
247 | BOOST_ASSERT(px != 0);
|
---|
248 | return *px;
|
---|
249 | }
|
---|
250 |
|
---|
251 | T * operator-> () const // never throws
|
---|
252 | {
|
---|
253 | BOOST_ASSERT(px != 0);
|
---|
254 | return px;
|
---|
255 | }
|
---|
256 |
|
---|
257 | T * get() const // never throws
|
---|
258 | {
|
---|
259 | return px;
|
---|
260 | }
|
---|
261 |
|
---|
262 | // implicit conversion to "bool"
|
---|
263 |
|
---|
264 | #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
|
---|
265 |
|
---|
266 | operator bool () const
|
---|
267 | {
|
---|
268 | return px != 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | #elif \
|
---|
272 | ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
|
---|
273 | ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) )
|
---|
274 |
|
---|
275 | typedef T * (this_type::*unspecified_bool_type)() const;
|
---|
276 |
|
---|
277 | operator unspecified_bool_type() const // never throws
|
---|
278 | {
|
---|
279 | return px == 0? 0: &this_type::get;
|
---|
280 | }
|
---|
281 |
|
---|
282 | #else
|
---|
283 |
|
---|
284 | typedef T * this_type::*unspecified_bool_type;
|
---|
285 |
|
---|
286 | operator unspecified_bool_type() const // never throws
|
---|
287 | {
|
---|
288 | return px == 0? 0: &this_type::px;
|
---|
289 | }
|
---|
290 |
|
---|
291 | #endif
|
---|
292 |
|
---|
293 | // operator! is redundant, but some compilers need it
|
---|
294 |
|
---|
295 | bool operator! () const // never throws
|
---|
296 | {
|
---|
297 | return px == 0;
|
---|
298 | }
|
---|
299 |
|
---|
300 | bool unique() const // never throws
|
---|
301 | {
|
---|
302 | return pn.unique();
|
---|
303 | }
|
---|
304 |
|
---|
305 | long use_count() const // never throws
|
---|
306 | {
|
---|
307 | return pn.use_count();
|
---|
308 | }
|
---|
309 |
|
---|
310 | void swap(shared_ptr<T> & other) // never throws
|
---|
311 | {
|
---|
312 | std::swap(px, other.px);
|
---|
313 | pn.swap(other.pn);
|
---|
314 | }
|
---|
315 |
|
---|
316 | template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
|
---|
317 | {
|
---|
318 | return pn < rhs.pn;
|
---|
319 | }
|
---|
320 |
|
---|
321 | void * _internal_get_deleter(std::type_info const & ti) const
|
---|
322 | {
|
---|
323 | return pn.get_deleter(ti);
|
---|
324 | }
|
---|
325 |
|
---|
326 | // Tasteless as this may seem, making all members public allows member templates
|
---|
327 | // to work in the absence of member template friends. (Matthew Langston)
|
---|
328 |
|
---|
329 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
---|
330 |
|
---|
331 | private:
|
---|
332 |
|
---|
333 | template<class Y> friend class shared_ptr;
|
---|
334 | template<class Y> friend class weak_ptr;
|
---|
335 |
|
---|
336 |
|
---|
337 | #endif
|
---|
338 |
|
---|
339 | T * px; // contained pointer
|
---|
340 | detail::shared_count pn; // reference counter
|
---|
341 |
|
---|
342 | }; // shared_ptr
|
---|
343 |
|
---|
344 | template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
|
---|
345 | {
|
---|
346 | return a.get() == b.get();
|
---|
347 | }
|
---|
348 |
|
---|
349 | template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
|
---|
350 | {
|
---|
351 | return a.get() != b.get();
|
---|
352 | }
|
---|
353 |
|
---|
354 | #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
|
---|
355 |
|
---|
356 | // Resolve the ambiguity between our op!= and the one in rel_ops
|
---|
357 |
|
---|
358 | template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
|
---|
359 | {
|
---|
360 | return a.get() != b.get();
|
---|
361 | }
|
---|
362 |
|
---|
363 | #endif
|
---|
364 |
|
---|
365 | template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
|
---|
366 | {
|
---|
367 | return a._internal_less(b);
|
---|
368 | }
|
---|
369 |
|
---|
370 | template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
|
---|
371 | {
|
---|
372 | a.swap(b);
|
---|
373 | }
|
---|
374 |
|
---|
375 | template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
|
---|
376 | {
|
---|
377 | return shared_ptr<T>(r, detail::static_cast_tag());
|
---|
378 | }
|
---|
379 |
|
---|
380 | template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
|
---|
381 | {
|
---|
382 | return shared_ptr<T>(r, detail::const_cast_tag());
|
---|
383 | }
|
---|
384 |
|
---|
385 | template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
|
---|
386 | {
|
---|
387 | return shared_ptr<T>(r, detail::dynamic_cast_tag());
|
---|
388 | }
|
---|
389 |
|
---|
390 | // shared_*_cast names are deprecated. Use *_pointer_cast instead.
|
---|
391 |
|
---|
392 | template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
|
---|
393 | {
|
---|
394 | return shared_ptr<T>(r, detail::static_cast_tag());
|
---|
395 | }
|
---|
396 |
|
---|
397 | template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
|
---|
398 | {
|
---|
399 | return shared_ptr<T>(r, detail::dynamic_cast_tag());
|
---|
400 | }
|
---|
401 |
|
---|
402 | template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
|
---|
403 | {
|
---|
404 | return shared_ptr<T>(r, detail::polymorphic_cast_tag());
|
---|
405 | }
|
---|
406 |
|
---|
407 | template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
|
---|
408 | {
|
---|
409 | BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
|
---|
410 | return shared_static_cast<T>(r);
|
---|
411 | }
|
---|
412 |
|
---|
413 | // get_pointer() enables boost::mem_fn to recognize shared_ptr
|
---|
414 |
|
---|
415 | template<class T> inline T * get_pointer(shared_ptr<T> const & p)
|
---|
416 | {
|
---|
417 | return p.get();
|
---|
418 | }
|
---|
419 |
|
---|
420 | // operator<<
|
---|
421 |
|
---|
422 | #if defined(__GNUC__) && (__GNUC__ < 3)
|
---|
423 |
|
---|
424 | template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
|
---|
425 | {
|
---|
426 | os << p.get();
|
---|
427 | return os;
|
---|
428 | }
|
---|
429 |
|
---|
430 | #else
|
---|
431 |
|
---|
432 | # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
|
---|
433 | // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
|
---|
434 | using std::basic_ostream;
|
---|
435 | template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
|
---|
436 | # else
|
---|
437 | template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
|
---|
438 | # endif
|
---|
439 | {
|
---|
440 | os << p.get();
|
---|
441 | return os;
|
---|
442 | }
|
---|
443 |
|
---|
444 | #endif
|
---|
445 |
|
---|
446 | // get_deleter (experimental)
|
---|
447 |
|
---|
448 | #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
|
---|
449 | ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
|
---|
450 | ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
|
---|
451 |
|
---|
452 | // g++ 2.9x doesn't allow static_cast<X const *>(void *)
|
---|
453 | // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
|
---|
454 |
|
---|
455 | template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
|
---|
456 | {
|
---|
457 | void const * q = p._internal_get_deleter(typeid(D));
|
---|
458 | return const_cast<D *>(static_cast<D const *>(q));
|
---|
459 | }
|
---|
460 |
|
---|
461 | #else
|
---|
462 |
|
---|
463 | template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
|
---|
464 | {
|
---|
465 | return static_cast<D *>(p._internal_get_deleter(typeid(D)));
|
---|
466 | }
|
---|
467 |
|
---|
468 | #endif
|
---|
469 |
|
---|
470 | } // namespace boost
|
---|
471 |
|
---|
472 | #ifdef BOOST_MSVC
|
---|
473 | # pragma warning(pop)
|
---|
474 | #endif
|
---|
475 |
|
---|
476 | #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
|
---|
477 |
|
---|
478 | #endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
|
---|