]>
Commit | Line | Data |
---|---|---|
b758b22a BK |
1 | // <tr1/boost_shared_ptr.h> -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2005 Free Software Foundation, Inc. | |
4 | // | |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 2, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 18 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
b758b22a BK |
19 | // USA. |
20 | ||
21 | // As a special exception, you may use this file as part of a free software | |
22 | // library without restriction. Specifically, if other files instantiate | |
23 | // templates or use macros or inline functions from this file, or you compile | |
24 | // this file and link it with other files to produce an executable, this | |
25 | // file does not by itself cause the resulting executable to be covered by | |
26 | // the GNU General Public License. This exception does not however | |
27 | // invalidate any other reasons why the executable file might be covered by | |
28 | // the GNU General Public License. | |
29 | ||
30 | // shared_count.hpp | |
31 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. | |
32 | ||
33 | // shared_ptr.hpp | |
34 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. | |
35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov | |
36 | ||
37 | // weak_ptr.hpp | |
38 | // Copyright (C) 2001, 2002, 2003 Peter Dimov | |
39 | ||
40 | // enable_shared_from_this.hpp | |
41 | // Copyright (C) 2002 Peter Dimov | |
42 | ||
43 | // Distributed under the Boost Software License, Version 1.0. (See | |
44 | // accompanying file LICENSE_1_0.txt or copy at | |
45 | // http://www.boost.org/LICENSE_1_0.txt) | |
46 | ||
47 | // GCC Note: based on version 1.32.0 of the Boost library. | |
48 | ||
49 | /** @file boost_memory.h | |
50 | * This is an internal header file, included by other library headers. | |
51 | * You should not attempt to use it directly. | |
52 | */ | |
53 | ||
54 | #ifndef _BOOST_SHARED_PTR_H | |
55 | #define _BOOST_SHARED_PTR_H 1 | |
56 | ||
57 | // namespace std::tr1 | |
58 | namespace std | |
59 | { | |
3cbc7af0 | 60 | _GLIBCXX_BEGIN_NAMESPACE(tr1) |
b758b22a BK |
61 | |
62 | class bad_weak_ptr : public std::exception | |
63 | { | |
64 | public: | |
65 | ||
81809516 PC |
66 | virtual char const* |
67 | what() const throw() | |
68 | { return "tr1::bad_weak_ptr"; } | |
b758b22a BK |
69 | }; |
70 | ||
71 | // Helper for exception objects in <tr1/memory> | |
72 | // TODO this should be defined in a different file. | |
73 | inline void | |
74 | __throw_bad_weak_ptr() | |
75 | { | |
76 | #if __EXCEPTIONS | |
77 | throw bad_weak_ptr(); | |
78 | #else | |
79 | std::abort(); | |
80 | #endif | |
81 | } | |
82 | ||
83 | ||
81809516 | 84 | template<typename _Tp> |
b758b22a BK |
85 | struct _Sp_deleter |
86 | { | |
87 | typedef void result_type; | |
88 | typedef _Tp* argument_type; | |
89 | ||
90 | void | |
91 | operator()(_Tp* p) const | |
92 | { delete p; } | |
93 | }; | |
94 | ||
95 | ||
96 | class _Sp_counted_base | |
97 | { | |
98 | public: | |
99 | ||
100 | _Sp_counted_base() | |
101 | : _M_use_count(1), _M_weak_count(1) | |
7010f39e JW |
102 | { |
103 | // For the case of __GTHREAD_MUTEX_INIT we haven't initialised | |
104 | // the mutex yet, so do it now. | |
105 | #if defined(__GTHREADS) && defined(__GTHREAD_MUTEX_INIT) | |
106 | __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT; | |
107 | _M_mutex = __tmp; | |
108 | #endif | |
109 | } | |
b758b22a BK |
110 | |
111 | virtual | |
112 | ~_Sp_counted_base() // nothrow | |
113 | { } | |
114 | ||
115 | // dispose() is called when _M_use_count drops to zero, to release | |
116 | // the resources managed by *this. | |
117 | virtual void | |
118 | dispose() = 0; // nothrow | |
119 | ||
120 | // destroy() is called when _M_weak_count drops to zero. | |
121 | virtual void | |
122 | destroy() // nothrow | |
123 | { | |
124 | delete this; | |
125 | } | |
126 | ||
127 | virtual void* | |
128 | get_deleter(const std::type_info&) = 0; | |
129 | ||
130 | void | |
131 | add_ref_copy() | |
132 | { | |
b7ee72de | 133 | __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); |
b758b22a BK |
134 | } |
135 | ||
136 | void | |
137 | add_ref_lock() | |
138 | { | |
fcec20a7 | 139 | __gnu_cxx::lock lock(_M_mutex); |
b7ee72de | 140 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
81809516 PC |
141 | { |
142 | _M_use_count = 0; | |
143 | __throw_bad_weak_ptr(); | |
144 | } | |
b758b22a BK |
145 | } |
146 | ||
147 | void | |
148 | release() // nothrow | |
149 | { | |
b7ee72de | 150 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) |
81809516 PC |
151 | { |
152 | dispose(); | |
a15024e6 | 153 | #ifdef __GTHREADS |
7fd60218 PC |
154 | _GLIBCXX_READ_MEM_BARRIER; |
155 | _GLIBCXX_WRITE_MEM_BARRIER; | |
a15024e6 | 156 | #endif |
b7ee72de | 157 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
7fd60218 | 158 | destroy(); |
81809516 | 159 | } |
b758b22a BK |
160 | } |
161 | ||
162 | void | |
163 | weak_add_ref() // nothrow | |
164 | { | |
b7ee72de | 165 | __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); |
b758b22a BK |
166 | } |
167 | ||
168 | void | |
169 | weak_release() // nothrow | |
170 | { | |
b7ee72de | 171 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
81809516 | 172 | { |
a15024e6 | 173 | #ifdef __GTHREADS |
7fd60218 PC |
174 | _GLIBCXX_READ_MEM_BARRIER; |
175 | _GLIBCXX_WRITE_MEM_BARRIER; | |
a15024e6 | 176 | #endif |
81809516 PC |
177 | destroy(); |
178 | } | |
b758b22a BK |
179 | } |
180 | ||
181 | long | |
182 | use_count() const // nothrow | |
183 | { | |
184 | return _M_use_count; // XXX is this MT safe? | |
185 | } | |
186 | ||
187 | private: | |
188 | ||
189 | _Sp_counted_base(_Sp_counted_base const&); | |
81809516 | 190 | _Sp_counted_base& operator=(_Sp_counted_base const&); |
b758b22a BK |
191 | |
192 | _Atomic_word _M_use_count; // #shared | |
193 | _Atomic_word _M_weak_count; // #weak + (#shared != 0) | |
fcec20a7 | 194 | __gnu_cxx::mutex_type _M_mutex; |
b758b22a BK |
195 | }; |
196 | ||
81809516 PC |
197 | template<typename _Ptr, typename _Deleter> |
198 | class _Sp_counted_base_impl | |
199 | : public _Sp_counted_base | |
b758b22a | 200 | { |
81809516 | 201 | public: |
b758b22a | 202 | |
81809516 PC |
203 | /** |
204 | * @brief | |
205 | * @pre d(p) must not throw. | |
206 | */ | |
207 | _Sp_counted_base_impl(_Ptr __p, _Deleter __d) | |
208 | : _M_ptr(__p), _M_del(__d) | |
209 | { } | |
210 | ||
211 | virtual void | |
212 | dispose() // nothrow | |
213 | { | |
214 | _M_del(_M_ptr); | |
215 | } | |
216 | ||
217 | virtual void* | |
218 | get_deleter(const std::type_info& __ti) | |
219 | { | |
220 | return __ti == typeid(_Deleter) ? &_M_del : 0; | |
221 | } | |
222 | ||
223 | private: | |
224 | _Sp_counted_base_impl(const _Sp_counted_base_impl&); | |
225 | _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&); | |
226 | ||
227 | _Ptr _M_ptr; // copy constructor must not throw | |
228 | _Deleter _M_del; // copy constructor must not throw | |
229 | }; | |
b758b22a BK |
230 | |
231 | class weak_count; | |
232 | ||
233 | class shared_count | |
234 | { | |
235 | private: | |
236 | ||
237 | _Sp_counted_base* _M_pi; | |
238 | ||
239 | friend class weak_count; | |
240 | ||
241 | public: | |
242 | ||
243 | shared_count() | |
244 | : _M_pi(0) // nothrow | |
245 | { } | |
246 | ||
81809516 | 247 | template<typename _Ptr, typename _Deleter> |
b758b22a BK |
248 | shared_count(_Ptr __p, _Deleter __d) |
249 | : _M_pi(0) | |
250 | { | |
251 | try | |
81809516 PC |
252 | { |
253 | _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d); | |
254 | } | |
b758b22a | 255 | catch(...) |
81809516 PC |
256 | { |
257 | __d(__p); // delete __p | |
258 | __throw_exception_again; | |
259 | } | |
b758b22a BK |
260 | } |
261 | ||
262 | // auto_ptr<_Tp> is special cased to provide the strong guarantee | |
263 | ||
81809516 | 264 | template<typename _Tp> |
b758b22a | 265 | explicit shared_count(std::auto_ptr<_Tp>& __r) |
81809516 PC |
266 | : _M_pi(new _Sp_counted_base_impl<_Tp*, |
267 | _Sp_deleter<_Tp> >(__r.get(), _Sp_deleter<_Tp>())) | |
b758b22a BK |
268 | { __r.release(); } |
269 | ||
270 | // throws bad_weak_ptr when __r.use_count() == 0 | |
271 | explicit shared_count(const weak_count& __r); | |
272 | ||
273 | ~shared_count() // nothrow | |
274 | { | |
275 | if (_M_pi != 0) | |
276 | _M_pi->release(); | |
277 | } | |
278 | ||
279 | shared_count(const shared_count& __r) | |
280 | : _M_pi(__r._M_pi) // nothrow | |
281 | { | |
282 | if (_M_pi != 0) | |
283 | _M_pi->add_ref_copy(); | |
284 | } | |
285 | ||
286 | shared_count& | |
287 | operator=(const shared_count& __r) // nothrow | |
288 | { | |
289 | _Sp_counted_base* __tmp = __r._M_pi; | |
290 | ||
291 | if(__tmp != _M_pi) | |
81809516 PC |
292 | { |
293 | if(__tmp != 0) | |
294 | __tmp->add_ref_copy(); | |
295 | if(_M_pi != 0) | |
296 | _M_pi->release(); | |
297 | _M_pi = __tmp; | |
298 | } | |
b758b22a BK |
299 | return *this; |
300 | } | |
301 | ||
302 | void swap(shared_count& __r) // nothrow | |
303 | { | |
304 | _Sp_counted_base* __tmp = __r._M_pi; | |
305 | __r._M_pi = _M_pi; | |
306 | _M_pi = __tmp; | |
307 | } | |
308 | ||
309 | long | |
310 | use_count() const // nothrow | |
311 | { return _M_pi != 0 ? _M_pi->use_count() : 0; } | |
312 | ||
313 | bool | |
314 | unique() const // nothrow | |
315 | { return this->use_count() == 1; } | |
316 | ||
317 | friend inline bool | |
318 | operator==(const shared_count& __a, const shared_count& __b) | |
319 | { return __a._M_pi == __b._M_pi; } | |
320 | ||
321 | friend inline bool | |
322 | operator<(const shared_count& __a, const shared_count& __b) | |
323 | { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); } | |
324 | ||
325 | void* | |
326 | get_deleter(const std::type_info& __ti) const | |
327 | { return _M_pi ? _M_pi->get_deleter(__ti) : 0; } | |
328 | }; | |
329 | ||
b758b22a BK |
330 | class weak_count |
331 | { | |
332 | private: | |
333 | ||
7fd60218 | 334 | _Sp_counted_base* _M_pi; |
b758b22a BK |
335 | |
336 | friend class shared_count; | |
337 | ||
338 | public: | |
339 | ||
340 | weak_count() | |
341 | : _M_pi(0) // nothrow | |
342 | { } | |
343 | ||
344 | weak_count(const shared_count& __r) | |
345 | : _M_pi(__r._M_pi) // nothrow | |
346 | { | |
347 | if (_M_pi != 0) | |
348 | _M_pi->weak_add_ref(); | |
349 | } | |
350 | ||
351 | weak_count(const weak_count& __r) | |
352 | : _M_pi(__r._M_pi) // nothrow | |
353 | { | |
354 | if (_M_pi != 0) | |
355 | _M_pi->weak_add_ref(); | |
356 | } | |
357 | ||
358 | ~weak_count() // nothrow | |
359 | { | |
360 | if (_M_pi != 0) | |
361 | _M_pi->weak_release(); | |
362 | } | |
363 | ||
364 | weak_count& | |
365 | operator=(const shared_count& __r) // nothrow | |
366 | { | |
367 | _Sp_counted_base* __tmp = __r._M_pi; | |
368 | if (__tmp != 0) | |
369 | __tmp->weak_add_ref(); | |
370 | if (_M_pi != 0) | |
371 | _M_pi->weak_release(); | |
372 | _M_pi = __tmp; | |
373 | ||
374 | return *this; | |
375 | } | |
376 | ||
377 | weak_count& | |
378 | operator=(const weak_count& __r) // nothrow | |
379 | { | |
380 | _Sp_counted_base * __tmp = __r._M_pi; | |
381 | if (__tmp != 0) | |
382 | __tmp->weak_add_ref(); | |
383 | if (_M_pi != 0) | |
384 | _M_pi->weak_release(); | |
385 | _M_pi = __tmp; | |
386 | ||
387 | return *this; | |
388 | } | |
389 | ||
390 | void | |
391 | swap(weak_count& __r) // nothrow | |
392 | { | |
393 | _Sp_counted_base * __tmp = __r._M_pi; | |
394 | __r._M_pi = _M_pi; | |
395 | _M_pi = __tmp; | |
396 | } | |
397 | ||
398 | long | |
399 | use_count() const // nothrow | |
400 | { return _M_pi != 0 ? _M_pi->use_count() : 0; } | |
401 | ||
402 | friend inline bool | |
403 | operator==(const weak_count& __a, const weak_count& __b) | |
404 | { return __a._M_pi == __b._M_pi; } | |
405 | ||
406 | friend inline bool | |
407 | operator<(const weak_count& __a, const weak_count& __b) | |
408 | { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); } | |
409 | }; | |
410 | ||
411 | inline | |
412 | shared_count::shared_count(const weak_count& __r) | |
413 | : _M_pi(__r._M_pi) | |
414 | { | |
415 | if (_M_pi != 0) | |
b758b22a | 416 | _M_pi->add_ref_lock(); |
b758b22a | 417 | else |
b758b22a | 418 | __throw_bad_weak_ptr(); |
b758b22a BK |
419 | } |
420 | ||
a15024e6 | 421 | |
b758b22a | 422 | // fwd decls |
81daf4ba PC |
423 | template<typename _Tp> |
424 | class shared_ptr; | |
425 | ||
81809516 PC |
426 | template<typename _Tp> |
427 | class weak_ptr; | |
428 | ||
429 | template<typename _Tp> | |
430 | class enable_shared_from_this; | |
b758b22a BK |
431 | |
432 | struct __static_cast_tag {}; | |
433 | struct __const_cast_tag {}; | |
434 | struct __dynamic_cast_tag {}; | |
435 | struct __polymorphic_cast_tag {}; | |
436 | ||
81809516 PC |
437 | template<class _Tp> |
438 | struct shared_ptr_traits | |
439 | { typedef _Tp& reference; }; | |
b758b22a | 440 | |
81809516 PC |
441 | template<> |
442 | struct shared_ptr_traits<void> | |
443 | { typedef void reference; }; | |
b758b22a | 444 | |
81809516 PC |
445 | template<> |
446 | struct shared_ptr_traits<void const> | |
447 | { typedef void reference; }; | |
b758b22a | 448 | |
81809516 PC |
449 | template<> |
450 | struct shared_ptr_traits<void volatile> | |
451 | { typedef void reference; }; | |
b758b22a | 452 | |
81809516 PC |
453 | template<> |
454 | struct shared_ptr_traits<void const volatile> | |
455 | { typedef void reference; }; | |
b758b22a BK |
456 | |
457 | ||
458 | // enable_shared_from_this support | |
459 | ||
460 | // friend of enable_shared_from_this | |
81809516 | 461 | template<typename _Tp1, typename _Tp2> |
b758b22a | 462 | void |
81809516 PC |
463 | __enable_shared_from_this(const shared_count& __pn, |
464 | const enable_shared_from_this<_Tp1>* __pe, | |
465 | const _Tp2* __px ); | |
b758b22a BK |
466 | |
467 | inline void | |
468 | __enable_shared_from_this(const shared_count&, ...) | |
469 | { } | |
470 | ||
81daf4ba PC |
471 | |
472 | // get_deleter must be declared before friend declaration by shared_ptr. | |
473 | template<typename _Del, typename _Tp> | |
474 | _Del* get_deleter(const shared_ptr<_Tp>&); | |
475 | ||
b758b22a BK |
476 | /** |
477 | * @class shared_ptr <tr1/memory> | |
478 | * | |
479 | * A smart pointer with reference-counted copy semantics. | |
480 | * The object pointed to is deleted when the last shared_ptr pointing to it | |
481 | * is destroyed or reset. | |
482 | */ | |
81809516 | 483 | template<typename _Tp> |
b758b22a BK |
484 | class shared_ptr |
485 | { | |
486 | typedef typename shared_ptr_traits<_Tp>::reference _Reference; | |
487 | ||
488 | public: | |
489 | ||
490 | typedef _Tp element_type; | |
491 | ||
492 | /** @brief Construct an empty %shared_ptr. | |
493 | * @post use_count()==0 && get()==0 | |
494 | */ | |
81809516 PC |
495 | shared_ptr() |
496 | : _M_ptr(0), _M_refcount() // never throws | |
b758b22a BK |
497 | { } |
498 | ||
499 | /** @brief Construct a %shared_ptr that owns the pointer @a p. | |
500 | * @param p A pointer that is convertible to element_type*. | |
81809516 | 501 | * @post use_count() == 1 && get() == p |
b758b22a BK |
502 | * @throw std::bad_alloc, in which case @c delete @a p is called. |
503 | */ | |
81809516 | 504 | template<typename _Tp1> |
b758b22a BK |
505 | explicit shared_ptr(_Tp1* __p) |
506 | : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>()) | |
507 | { | |
508 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
509 | // __glibcxx_function_requires(_CompleteConcept<_Tp1*>) | |
510 | ||
511 | __enable_shared_from_this( _M_refcount, __p, __p ); | |
512 | } | |
513 | ||
514 | // | |
515 | // Requirements: D's copy constructor and destructor must not throw | |
516 | // | |
517 | // shared_ptr will release p by calling d(p) | |
518 | // | |
519 | /** @brief Construct a %shared_ptr that owns the pointer @a p | |
520 | * and the deleter @a d. | |
521 | * @param p A pointer. | |
522 | * @param d A deleter. | |
81809516 | 523 | * @post use_count() == 1 && get() == p |
b758b22a BK |
524 | * @throw std::bad_alloc, in which case @a d(p) is called. |
525 | */ | |
81809516 | 526 | template<typename _Tp1, typename _Deleter> |
b758b22a BK |
527 | shared_ptr(_Tp1* __p, _Deleter __d) |
528 | : _M_ptr(__p), _M_refcount(__p, __d) | |
529 | { | |
530 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
531 | // TODO requires D is CopyConstructible and d(p) well-formed | |
532 | ||
533 | __enable_shared_from_this( _M_refcount, __p, __p ); | |
534 | } | |
535 | ||
536 | // generated copy constructor, assignment, destructor are fine. | |
537 | ||
538 | /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise | |
539 | * construct a %shared_ptr that shares ownership with @a r. | |
540 | * @param r A %shared_ptr. | |
81809516 | 541 | * @post get() == r.get() && use_count() == r.use_count() |
b758b22a BK |
542 | * @throw std::bad_alloc, in which case |
543 | */ | |
81809516 | 544 | template<typename _Tp1> |
b758b22a BK |
545 | shared_ptr(const shared_ptr<_Tp1>& __r) |
546 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws | |
547 | { | |
548 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
549 | } | |
550 | ||
551 | /** @brief Constructs a %shared_ptr that shares ownership with @a r | |
552 | * and stores a copy of the pointer stored in @a r. | |
553 | * @param r A weak_ptr. | |
81809516 | 554 | * @post use_count() == r.use_count() |
b758b22a BK |
555 | * @throw bad_weak_ptr when r.expired(), |
556 | * in which case the constructor has no effect. | |
557 | */ | |
81809516 | 558 | template<typename _Tp1> |
b758b22a BK |
559 | explicit shared_ptr(const weak_ptr<_Tp1>& __r) |
560 | : _M_refcount(__r._M_refcount) // may throw | |
561 | { | |
562 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
563 | // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount) | |
564 | // did not throw | |
565 | _M_ptr = __r._M_ptr; | |
566 | } | |
567 | ||
568 | /** | |
81809516 | 569 | * @post use_count() == 1 and r.get() == 0 |
b758b22a | 570 | */ |
81809516 | 571 | template<typename _Tp1> |
b758b22a BK |
572 | explicit shared_ptr(std::auto_ptr<_Tp1>& __r) |
573 | : _M_ptr(__r.get()), _M_refcount() | |
574 | { | |
575 | // TODO requires r.release() convertible to _Tp*, Tp1 is complete, | |
576 | // delete r.release() well-formed | |
577 | _Tp1 * __tmp = __r.get(); | |
578 | _M_refcount = shared_count(__r); | |
579 | ||
580 | __enable_shared_from_this( _M_refcount, __tmp, __tmp ); | |
581 | } | |
582 | ||
81809516 | 583 | template<typename _Tp1> |
b758b22a | 584 | shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag) |
81809516 PC |
585 | : _M_ptr(static_cast<element_type*>(__r._M_ptr)), |
586 | _M_refcount(__r._M_refcount) | |
b758b22a BK |
587 | { } |
588 | ||
81809516 | 589 | template<typename _Tp1> |
b758b22a | 590 | shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag) |
81809516 PC |
591 | : _M_ptr(const_cast<element_type*>(__r._M_ptr)), |
592 | _M_refcount(__r._M_refcount) | |
b758b22a BK |
593 | { } |
594 | ||
81809516 | 595 | template<typename _Tp1> |
b758b22a | 596 | shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag) |
81809516 PC |
597 | : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)), |
598 | _M_refcount(__r._M_refcount) | |
b758b22a BK |
599 | { |
600 | if (_M_ptr == 0) // need to allocate new counter -- the cast failed | |
b758b22a | 601 | _M_refcount = shared_count(); |
b758b22a BK |
602 | } |
603 | ||
81809516 | 604 | template<typename _Tp1> |
b758b22a BK |
605 | shared_ptr& |
606 | operator=(const shared_ptr<_Tp1>& __r) // never throws | |
607 | { | |
608 | _M_ptr = __r._M_ptr; | |
609 | _M_refcount = __r._M_refcount; // shared_count::op= doesn't throw | |
610 | return *this; | |
611 | } | |
612 | ||
81809516 | 613 | template<typename _Tp1> |
b758b22a BK |
614 | shared_ptr& |
615 | operator=(std::auto_ptr<_Tp1>& __r) | |
616 | { | |
617 | shared_ptr(__r).swap(*this); | |
618 | return *this; | |
619 | } | |
620 | ||
621 | void | |
622 | reset() // never throws | |
623 | { shared_ptr().swap(*this); } | |
624 | ||
81809516 | 625 | template<typename _Tp1> |
b758b22a BK |
626 | void |
627 | reset(_Tp1* __p) // _Tp1 must be complete | |
628 | { | |
81809516 PC |
629 | _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset |
630 | // errors | |
b758b22a BK |
631 | shared_ptr(__p).swap(*this); |
632 | } | |
633 | ||
81809516 | 634 | template<typename _Tp1, typename _Deleter> |
b758b22a BK |
635 | void |
636 | reset(_Tp1 * __p, _Deleter __d) | |
637 | { shared_ptr(__p, __d).swap(*this); } | |
638 | ||
639 | // error to instantiate if _Tp is [cv-qual] void | |
640 | _Reference | |
641 | operator*() const // never throws | |
642 | { | |
643 | _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0); | |
644 | return *_M_ptr; | |
645 | } | |
646 | ||
647 | _Tp* | |
648 | operator->() const // never throws | |
649 | { | |
650 | _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0); | |
651 | return _M_ptr; | |
652 | } | |
653 | ||
654 | _Tp* | |
655 | get() const // never throws | |
656 | { return _M_ptr; } | |
657 | ||
658 | // implicit conversion to "bool" | |
659 | private: | |
660 | typedef _Tp* shared_ptr::*__unspecified_bool_type; | |
661 | ||
662 | public: | |
663 | operator __unspecified_bool_type() const // never throws | |
664 | { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; } | |
665 | ||
666 | bool | |
667 | unique() const // never throws | |
668 | { return _M_refcount.unique(); } | |
669 | ||
670 | long | |
671 | use_count() const // never throws | |
672 | { return _M_refcount.use_count(); } | |
673 | ||
674 | void | |
675 | swap(shared_ptr<_Tp>& __other) // never throws | |
676 | { | |
677 | std::swap(_M_ptr, __other._M_ptr); | |
678 | _M_refcount.swap(__other._M_refcount); | |
679 | } | |
680 | ||
81daf4ba | 681 | private: |
27473454 PC |
682 | void* |
683 | _M_get_deleter(const std::type_info& __ti) const | |
684 | { return _M_refcount.get_deleter(__ti); } | |
685 | ||
81809516 | 686 | template<typename _Tp1> |
b758b22a BK |
687 | bool |
688 | _M_less(const shared_ptr<_Tp1>& __rhs) const | |
689 | { return _M_refcount < __rhs._M_refcount; } | |
690 | ||
7fd60218 PC |
691 | template<typename _Tp1> friend class shared_ptr; |
692 | template<typename _Tp1> friend class weak_ptr; | |
b758b22a | 693 | |
81daf4ba PC |
694 | template<typename _Del, typename _Tp1> |
695 | friend _Del* get_deleter(const shared_ptr<_Tp1>&); | |
696 | ||
b758b22a | 697 | // friends injected into enclosing namespace and found by ADL: |
81809516 | 698 | template<typename _Tp1> |
b758b22a BK |
699 | friend inline bool |
700 | operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) | |
701 | { return __a.get() == __b.get(); } | |
702 | ||
81809516 | 703 | template<typename _Tp1> |
b758b22a BK |
704 | friend inline bool |
705 | operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) | |
706 | { return __a.get() != __b.get(); } | |
707 | ||
81809516 | 708 | template<typename _Tp1> |
b758b22a BK |
709 | friend inline bool |
710 | operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) | |
711 | { return __a._M_less(__b); } | |
712 | ||
713 | _Tp* _M_ptr; // contained pointer | |
714 | shared_count _M_refcount; // reference counter | |
715 | }; // shared_ptr | |
716 | ||
77633f4c PC |
717 | // 2.2.3.8 shared_ptr specialized algorithms. |
718 | template<typename _Tp> | |
719 | inline void | |
720 | swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) | |
721 | { __a.swap(__b); } | |
b758b22a | 722 | |
77633f4c | 723 | // 2.2.3.9 shared_ptr casts |
b758b22a BK |
724 | /** @warning The seemingly equivalent |
725 | * <code>shared_ptr<T>(static_cast<T*>(r.get()))</code> | |
726 | * will eventually result in undefined behaviour, | |
727 | * attempting to delete the same object twice. | |
728 | */ | |
81809516 | 729 | template<typename _Tp, typename _Tp1> |
b758b22a BK |
730 | shared_ptr<_Tp> |
731 | static_pointer_cast(const shared_ptr<_Tp1>& __r) | |
732 | { | |
733 | return shared_ptr<_Tp>(__r, __static_cast_tag()); | |
734 | } | |
735 | ||
736 | /** @warning The seemingly equivalent | |
737 | * <code>shared_ptr<T>(const_cast<T*>(r.get()))</code> | |
738 | * will eventually result in undefined behaviour, | |
739 | * attempting to delete the same object twice. | |
740 | */ | |
81809516 | 741 | template<typename _Tp, typename _Tp1> |
b758b22a BK |
742 | shared_ptr<_Tp> |
743 | const_pointer_cast(const shared_ptr<_Tp1>& __r) | |
744 | { | |
745 | return shared_ptr<_Tp>(__r, __const_cast_tag()); | |
746 | } | |
747 | ||
748 | /** @warning The seemingly equivalent | |
749 | * <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code> | |
750 | * will eventually result in undefined behaviour, | |
751 | * attempting to delete the same object twice. | |
752 | */ | |
81809516 | 753 | template<typename _Tp, typename _Tp1> |
b758b22a BK |
754 | shared_ptr<_Tp> |
755 | dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) | |
756 | { | |
757 | return shared_ptr<_Tp>(__r, __dynamic_cast_tag()); | |
758 | } | |
759 | ||
27473454 | 760 | // 2.2.3.7 shared_ptr I/O |
81809516 | 761 | template<typename _Ch, typename _Tr, typename _Tp> |
27473454 PC |
762 | std::basic_ostream<_Ch, _Tr>& |
763 | operator<<(std::basic_ostream<_Ch, _Tr>& __os, const shared_ptr<_Tp>& __p) | |
b758b22a BK |
764 | { |
765 | __os << __p.get(); | |
766 | return __os; | |
767 | } | |
768 | ||
27473454 | 769 | // 2.2.3.10 shared_ptr get_deleter (experimental) |
81809516 | 770 | template<typename _Del, typename _Tp> |
27473454 PC |
771 | inline _Del* |
772 | get_deleter(const shared_ptr<_Tp>& __p) | |
773 | { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); } | |
774 | ||
b758b22a | 775 | |
81809516 | 776 | template<typename _Tp> |
b758b22a BK |
777 | class weak_ptr |
778 | { | |
779 | public: | |
780 | ||
781 | typedef _Tp element_type; | |
782 | ||
783 | weak_ptr() | |
784 | : _M_ptr(0), _M_refcount() // never throws | |
785 | { } | |
786 | ||
81809516 PC |
787 | // generated copy constructor, assignment, destructor are fine |
788 | ||
789 | // | |
790 | // The "obvious" converting constructor implementation: | |
791 | // | |
792 | // template<class Y> | |
793 | // weak_ptr(weak_ptr<Y> const & r) | |
794 | // : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws | |
795 | // { } | |
796 | // | |
797 | // has a serious problem. | |
798 | // | |
799 | // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr) | |
800 | // conversion may require access to *r._M_ptr (virtual inheritance). | |
801 | // | |
802 | // It is not possible to avoid spurious access violations since | |
803 | // in multithreaded programs r._M_ptr may be invalidated at any point. | |
804 | // | |
805 | ||
806 | template<typename _Tp1> | |
b758b22a BK |
807 | weak_ptr(const weak_ptr<_Tp1>& r) |
808 | : _M_refcount(r._M_refcount) // never throws | |
809 | { | |
810 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
811 | _M_ptr = r.lock().get(); | |
812 | } | |
813 | ||
81809516 | 814 | template<typename _Tp1> |
b758b22a BK |
815 | weak_ptr(const shared_ptr<_Tp1>& r) |
816 | : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws | |
817 | { | |
818 | __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) | |
819 | } | |
820 | ||
81809516 | 821 | template<typename _Tp1> |
b758b22a BK |
822 | weak_ptr& |
823 | operator=(const weak_ptr<_Tp1>& r) // never throws | |
824 | { | |
825 | _M_ptr = r.lock().get(); | |
826 | _M_refcount = r._M_refcount; | |
827 | return *this; | |
828 | } | |
829 | ||
81809516 | 830 | template<typename _Tp1> |
b758b22a BK |
831 | weak_ptr& |
832 | operator=(const shared_ptr<_Tp1>& r) // never throws | |
833 | { | |
834 | _M_ptr = r._M_ptr; | |
835 | _M_refcount = r._M_refcount; | |
836 | return *this; | |
837 | } | |
838 | ||
839 | shared_ptr<_Tp> | |
840 | lock() const // never throws | |
841 | { | |
842 | #ifdef __GTHREADS | |
843 | ||
844 | // optimization: avoid throw overhead | |
845 | if (expired()) | |
81809516 PC |
846 | return shared_ptr<element_type>(); |
847 | ||
b758b22a | 848 | try |
81809516 PC |
849 | { |
850 | return shared_ptr<element_type>(*this); | |
851 | } | |
b758b22a | 852 | catch (const bad_weak_ptr&) |
81809516 PC |
853 | { |
854 | // Q: how can we get here? | |
855 | // A: another thread may have invalidated r after the | |
856 | // use_count test above. | |
857 | return shared_ptr<element_type>(); | |
858 | } | |
b758b22a BK |
859 | |
860 | #else | |
861 | ||
862 | // optimization: avoid try/catch overhead when single threaded | |
81809516 PC |
863 | return expired() ? shared_ptr<element_type>() |
864 | : shared_ptr<element_type>(*this); | |
b758b22a BK |
865 | |
866 | #endif | |
867 | } // XXX MT | |
868 | ||
b758b22a BK |
869 | long |
870 | use_count() const // never throws | |
871 | { return _M_refcount.use_count(); } | |
872 | ||
873 | bool | |
874 | expired() const // never throws | |
875 | { return _M_refcount.use_count() == 0; } | |
876 | ||
877 | void | |
878 | reset() // never throws | |
879 | { weak_ptr().swap(*this); } | |
880 | ||
881 | void | |
882 | swap(weak_ptr& __s) // never throws | |
883 | { | |
884 | std::swap(_M_ptr, __s._M_ptr); | |
885 | _M_refcount.swap(__s._M_refcount); | |
886 | } | |
887 | ||
888 | private: | |
889 | ||
81809516 | 890 | template<typename _Tp1> |
b758b22a BK |
891 | bool |
892 | _M_less(const weak_ptr<_Tp1>& __rhs) const | |
893 | { return _M_refcount < __rhs._M_refcount; } | |
894 | ||
895 | // used by __enable_shared_from_this | |
896 | void | |
897 | _M_assign(_Tp* __ptr, const shared_count& __refcount) | |
898 | { | |
899 | _M_ptr = __ptr; | |
900 | _M_refcount = __refcount; | |
901 | } | |
902 | ||
903 | // friend injected into namespace and found by ADL | |
904 | ||
81809516 | 905 | template<typename _Tp1> |
b758b22a BK |
906 | friend inline bool |
907 | operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs) | |
908 | { return __lhs._M_less(__rhs); } | |
909 | ||
81809516 PC |
910 | template<typename _Tp1> friend class weak_ptr; |
911 | template<typename _Tp1> friend class shared_ptr; | |
b758b22a BK |
912 | friend class enable_shared_from_this<_Tp>; |
913 | ||
914 | _Tp* _M_ptr; // contained pointer | |
915 | weak_count _M_refcount; // reference counter | |
916 | ||
917 | }; // weak_ptr | |
918 | ||
77633f4c PC |
919 | // 2.2.4.7 weak_ptr specialized algorithms. |
920 | template<typename _Tp> | |
921 | void | |
922 | swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) | |
923 | { __a.swap(__b); } | |
924 | ||
b758b22a | 925 | |
81809516 | 926 | template<typename _Tp> |
b758b22a BK |
927 | class enable_shared_from_this |
928 | { | |
929 | protected: | |
930 | ||
931 | enable_shared_from_this() | |
932 | { } | |
933 | ||
934 | enable_shared_from_this(const enable_shared_from_this&) | |
935 | { } | |
936 | ||
937 | enable_shared_from_this& | |
938 | operator=(const enable_shared_from_this&) | |
939 | { return *this; } | |
940 | ||
941 | ~enable_shared_from_this() | |
942 | { } | |
943 | ||
944 | public: | |
945 | ||
946 | shared_ptr<_Tp> | |
947 | shared_from_this() | |
948 | { | |
81809516 PC |
949 | shared_ptr<_Tp> __p(this->_M_weak_this); |
950 | return __p; | |
b758b22a BK |
951 | } |
952 | ||
953 | shared_ptr<const _Tp> | |
954 | shared_from_this() const | |
955 | { | |
81809516 PC |
956 | shared_ptr<const _Tp> __p(this->_M_weak_this); |
957 | return __p; | |
b758b22a BK |
958 | } |
959 | ||
960 | private: | |
81809516 | 961 | template<typename _Tp1> |
b758b22a BK |
962 | void |
963 | _M_weak_assign(_Tp1* __p, const shared_count& __n) const | |
964 | { _M_weak_this._M_assign(__p, __n); } | |
965 | ||
81809516 | 966 | template<typename _Tp1> |
b758b22a | 967 | friend void |
81809516 PC |
968 | __enable_shared_from_this(const shared_count& __pn, |
969 | const enable_shared_from_this* __pe, | |
970 | const _Tp1* __px) | |
b758b22a BK |
971 | { |
972 | if(__pe != 0) | |
973 | __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); | |
974 | } | |
975 | ||
976 | mutable weak_ptr<_Tp> _M_weak_this; | |
977 | }; | |
978 | ||
3cbc7af0 | 979 | _GLIBCXX_END_NAMESPACE |
b758b22a BK |
980 | } // namespace std |
981 | ||
982 | #endif |