]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/shared_ptr.h
re PR libstdc++/36962 ([C++0x] Add constructors / assignment operators from unique_pt...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / shared_ptr.h
1 // <bits/shared_ptr.h> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008 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
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 bits/shared_ptr.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 __GXX_EXPERIMENTAL_CXX0X__
55 # include <c++0x_warning.h>
56 #endif
57
58 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
59 # error C++0x header cannot be included from TR1 header
60 #endif
61
62 namespace std
63 {
64 // counted ptr with no deleter or allocator support
65 template<typename _Ptr, _Lock_policy _Lp>
66 class _Sp_counted_ptr
67 : public _Sp_counted_base<_Lp>
68 {
69 public:
70 _Sp_counted_ptr(_Ptr __p)
71 : _M_ptr(__p) { }
72
73 virtual void
74 _M_dispose() // nothrow
75 { delete _M_ptr; }
76
77 virtual void
78 _M_destroy() // nothrow
79 { delete this; }
80
81 virtual void*
82 _M_get_deleter(const std::type_info& __ti)
83 { return 0; }
84
85 private:
86 _Sp_counted_ptr(const _Sp_counted_ptr&);
87 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&);
88
89 protected:
90 _Ptr _M_ptr; // copy constructor must not throw
91 };
92
93 // support for custom deleter and/or allocator
94 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
95 class _Sp_counted_deleter
96 : public _Sp_counted_ptr<_Ptr, _Lp>
97 {
98 typedef typename _Alloc::template
99 rebind<_Sp_counted_deleter>::other _My_alloc_type;
100
101 // Helper class that stores the Deleter and also acts as an allocator.
102 // Used to dispose of the owned pointer and the internal refcount
103 // Requires that copies of _Alloc can free each other's memory.
104 struct _My_Deleter
105 : public _My_alloc_type // copy constructor must not throw
106 {
107 _Deleter _M_del; // copy constructor must not throw
108 _My_Deleter(_Deleter __d, const _Alloc& __a)
109 : _My_alloc_type(__a), _M_del(__d) { }
110 };
111
112 protected:
113 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
114
115 public:
116 /**
117 * @brief
118 * @pre __d(__p) must not throw.
119 */
120 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
121 : _Base_type(__p), _M_del(__d, _Alloc()) { }
122
123 /**
124 * @brief
125 * @pre __d(__p) must not throw.
126 */
127 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
128 : _Base_type(__p), _M_del(__d, __a) { }
129
130 virtual void
131 _M_dispose() // nothrow
132 { _M_del._M_del(_Base_type::_M_ptr); }
133
134 virtual void
135 _M_destroy() // nothrow
136 {
137 _My_alloc_type __a(_M_del);
138 this->~_Sp_counted_deleter();
139 __a.deallocate(this, 1);
140 }
141
142 virtual void*
143 _M_get_deleter(const std::type_info& __ti)
144 { return __ti == typeid(_Deleter) ? &_M_del._M_del : 0; }
145
146 private:
147 _Sp_counted_deleter(const _Sp_counted_deleter&);
148 _Sp_counted_deleter& operator=(const _Sp_counted_deleter&);
149
150 protected:
151 _My_Deleter _M_del; // copy constructor must not throw
152 };
153
154 // helpers for make_shared / allocate_shared
155
156 template<typename _Tp>
157 struct _Sp_destroy_inplace
158 {
159 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
160 };
161
162 struct _Sp_make_shared_tag { };
163
164 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
165 class _Sp_counted_ptr_inplace
166 : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
167 {
168 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
169 _Base_type;
170
171 public:
172 _Sp_counted_ptr_inplace(_Alloc __a)
173 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
174 , _M_storage()
175 {
176 void* __p = &_M_storage;
177 ::new (__p) _Tp(); // might throw
178 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
179 }
180
181 template<typename... _Args>
182 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
183 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
184 , _M_storage()
185 {
186 void* __p = &_M_storage;
187 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
188 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
189 }
190
191 // override because the allocator needs to know the dynamic type
192 virtual void
193 _M_destroy() // nothrow
194 {
195 typedef typename _Alloc::template
196 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
197 _My_alloc_type __a(_Base_type::_M_del);
198 this->~_Sp_counted_ptr_inplace();
199 __a.deallocate(this, 1);
200 }
201
202 // sneaky trick so __shared_ptr can get the managed pointer
203 virtual void*
204 _M_get_deleter(const std::type_info& __ti)
205 {
206 return __ti == typeid(_Sp_make_shared_tag)
207 ? static_cast<void*>(&_M_storage)
208 : _Base_type::_M_get_deleter(__ti);
209 }
210
211 private:
212 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
213 _M_storage;
214 };
215
216 template<_Lock_policy _Lp = __default_lock_policy>
217 class __weak_count;
218
219 template<_Lock_policy _Lp = __default_lock_policy>
220 class __shared_count
221 {
222 public:
223 __shared_count()
224 : _M_pi(0) // nothrow
225 { }
226
227 template<typename _Ptr>
228 __shared_count(_Ptr __p) : _M_pi(0)
229 {
230 try
231 {
232 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
233 }
234 catch(...)
235 {
236 delete __p;
237 __throw_exception_again;
238 }
239 }
240
241 template<typename _Ptr, typename _Deleter>
242 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
243 {
244 // allocator's value_type doesn't matter, will rebind it anyway
245 typedef std::allocator<int> _Alloc;
246 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
247 typedef std::allocator<_Sp_cd_type> _Alloc2;
248 _Alloc2 __a2;
249 try
250 {
251 _M_pi = __a2.allocate(1);
252 new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
253 }
254 catch(...)
255 {
256 __d(__p); // Call _Deleter on __p.
257 if (_M_pi)
258 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
259 __throw_exception_again;
260 }
261 }
262
263 template<typename _Ptr, typename _Deleter, typename _Alloc>
264 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
265 {
266 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
267 typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
268 _Alloc2 __a2(__a);
269 try
270 {
271 _M_pi = __a2.allocate(1);
272 new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
273 }
274 catch(...)
275 {
276 __d(__p); // Call _Deleter on __p.
277 if (_M_pi)
278 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
279 __throw_exception_again;
280 }
281 }
282
283 template<typename _Tp, typename _Alloc, typename... _Args>
284 __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
285 : _M_pi(0)
286 {
287 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
288 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
289 _Alloc2 __a2(__a);
290 try
291 {
292 _M_pi = __a2.allocate(1);
293 new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
294 std::forward<_Args>(__args)...);
295 }
296 catch(...)
297 {
298 if (_M_pi)
299 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
300 __throw_exception_again;
301 }
302 }
303
304 #if _GLIBCXX_DEPRECATED
305 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
306 template<typename _Tp>
307 explicit
308 __shared_count(std::auto_ptr<_Tp>&& __r)
309 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
310 { __r.release(); }
311 #endif
312
313 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
314 template<typename _Tp, typename _Del>
315 explicit
316 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
317 : _M_pi(_S_create_from_up(std::move(__r)))
318 { __r.release(); }
319
320 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
321 explicit
322 __shared_count(const __weak_count<_Lp>& __r);
323
324 ~__shared_count() // nothrow
325 {
326 if (_M_pi != 0)
327 _M_pi->_M_release();
328 }
329
330 __shared_count(const __shared_count& __r)
331 : _M_pi(__r._M_pi) // nothrow
332 {
333 if (_M_pi != 0)
334 _M_pi->_M_add_ref_copy();
335 }
336
337 __shared_count&
338 operator=(const __shared_count& __r) // nothrow
339 {
340 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
341 if (__tmp != _M_pi)
342 {
343 if (__tmp != 0)
344 __tmp->_M_add_ref_copy();
345 if (_M_pi != 0)
346 _M_pi->_M_release();
347 _M_pi = __tmp;
348 }
349 return *this;
350 }
351
352 void
353 _M_swap(__shared_count& __r) // nothrow
354 {
355 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
356 __r._M_pi = _M_pi;
357 _M_pi = __tmp;
358 }
359
360 long
361 _M_get_use_count() const // nothrow
362 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
363
364 bool
365 _M_unique() const // nothrow
366 { return this->_M_get_use_count() == 1; }
367
368 friend inline bool
369 operator==(const __shared_count& __a, const __shared_count& __b)
370 { return __a._M_pi == __b._M_pi; }
371
372 friend inline bool
373 operator<(const __shared_count& __a, const __shared_count& __b)
374 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
375
376 void*
377 _M_get_deleter(const std::type_info& __ti) const
378 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
379
380 private:
381 friend class __weak_count<_Lp>;
382
383 template<typename _Tp, typename _Del>
384 static _Sp_counted_base<_Lp>*
385 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
386 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
387 {
388 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
389 _Lp>(__r.get(), __r.get_deleter());
390 }
391
392 template<typename _Tp, typename _Del>
393 static _Sp_counted_base<_Lp>*
394 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
395 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
396 {
397 typedef typename std::remove_reference<_Del>::type _Del1;
398 typedef std::reference_wrapper<_Del1> _Del2;
399 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
400 _Lp>(__r.get(), std::ref(__r.get_deleter()));
401 }
402
403 _Sp_counted_base<_Lp>* _M_pi;
404 };
405
406
407 template<_Lock_policy _Lp>
408 class __weak_count
409 {
410 public:
411 __weak_count()
412 : _M_pi(0) // nothrow
413 { }
414
415 __weak_count(const __shared_count<_Lp>& __r)
416 : _M_pi(__r._M_pi) // nothrow
417 {
418 if (_M_pi != 0)
419 _M_pi->_M_weak_add_ref();
420 }
421
422 __weak_count(const __weak_count<_Lp>& __r)
423 : _M_pi(__r._M_pi) // nothrow
424 {
425 if (_M_pi != 0)
426 _M_pi->_M_weak_add_ref();
427 }
428
429 ~__weak_count() // nothrow
430 {
431 if (_M_pi != 0)
432 _M_pi->_M_weak_release();
433 }
434
435 __weak_count<_Lp>&
436 operator=(const __shared_count<_Lp>& __r) // nothrow
437 {
438 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
439 if (__tmp != 0)
440 __tmp->_M_weak_add_ref();
441 if (_M_pi != 0)
442 _M_pi->_M_weak_release();
443 _M_pi = __tmp;
444 return *this;
445 }
446
447 __weak_count<_Lp>&
448 operator=(const __weak_count<_Lp>& __r) // nothrow
449 {
450 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
451 if (__tmp != 0)
452 __tmp->_M_weak_add_ref();
453 if (_M_pi != 0)
454 _M_pi->_M_weak_release();
455 _M_pi = __tmp;
456 return *this;
457 }
458
459 void
460 _M_swap(__weak_count<_Lp>& __r) // nothrow
461 {
462 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
463 __r._M_pi = _M_pi;
464 _M_pi = __tmp;
465 }
466
467 long
468 _M_get_use_count() const // nothrow
469 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
470
471 friend inline bool
472 operator==(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
473 { return __a._M_pi == __b._M_pi; }
474
475 friend inline bool
476 operator<(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
477 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
478
479 private:
480 friend class __shared_count<_Lp>;
481
482 _Sp_counted_base<_Lp>* _M_pi;
483 };
484
485 // now that __weak_count is defined we can define this constructor:
486 template<_Lock_policy _Lp>
487 inline
488 __shared_count<_Lp>::
489 __shared_count(const __weak_count<_Lp>& __r)
490 : _M_pi(__r._M_pi)
491 {
492 if (_M_pi != 0)
493 _M_pi->_M_add_ref_lock();
494 else
495 __throw_bad_weak_ptr();
496 }
497
498 // Forward declarations.
499 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
500 class __shared_ptr;
501
502 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
503 class __weak_ptr;
504
505 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
506 class __enable_shared_from_this;
507
508 template<typename _Tp>
509 class shared_ptr;
510
511 template<typename _Tp>
512 class weak_ptr;
513
514 template<typename _Tp>
515 class enable_shared_from_this;
516
517 // Support for enable_shared_from_this.
518
519 // Friend of __enable_shared_from_this.
520 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
521 void
522 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
523 const __enable_shared_from_this<_Tp1,
524 _Lp>*, const _Tp2*);
525
526 // Friend of enable_shared_from_this.
527 template<typename _Tp1, typename _Tp2>
528 void
529 __enable_shared_from_this_helper(const __shared_count<>&,
530 const enable_shared_from_this<_Tp1>*,
531 const _Tp2*);
532
533 template<_Lock_policy _Lp>
534 inline void
535 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
536 { }
537
538
539 /**
540 * @class __shared_ptr
541 *
542 * A smart pointer with reference-counted copy semantics.
543 * The object pointed to is deleted when the last shared_ptr pointing to
544 * it is destroyed or reset.
545 */
546 template<typename _Tp, _Lock_policy _Lp>
547 class __shared_ptr
548 {
549 public:
550 typedef _Tp element_type;
551
552 /** @brief Construct an empty %__shared_ptr.
553 * @post use_count()==0 && get()==0
554 */
555 __shared_ptr()
556 : _M_ptr(0), _M_refcount() // never throws
557 { }
558
559 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
560 * @param __p A pointer that is convertible to element_type*.
561 * @post use_count() == 1 && get() == __p
562 * @throw std::bad_alloc, in which case @c delete @a __p is called.
563 */
564 template<typename _Tp1>
565 explicit
566 __shared_ptr(_Tp1* __p)
567 : _M_ptr(__p), _M_refcount(__p)
568 {
569 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
570 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
571 __enable_shared_from_this_helper(_M_refcount, __p, __p);
572 }
573
574 //
575 // Requirements: _Deleter's copy constructor and destructor must
576 // not throw
577 //
578 // __shared_ptr will release __p by calling __d(__p)
579 //
580 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
581 * and the deleter @a __d.
582 * @param __p A pointer.
583 * @param __d A deleter.
584 * @post use_count() == 1 && get() == __p
585 * @throw std::bad_alloc, in which case @a __d(__p) is called.
586 */
587 template<typename _Tp1, typename _Deleter>
588 __shared_ptr(_Tp1* __p, _Deleter __d)
589 : _M_ptr(__p), _M_refcount(__p, __d)
590 {
591 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
592 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
593 __enable_shared_from_this_helper(_M_refcount, __p, __p);
594 }
595
596 //
597 // Requirements: _Deleter's copy constructor and destructor must
598 // not throw _Alloc's copy constructor and destructor must not
599 // throw.
600 //
601 // __shared_ptr will release __p by calling __d(__p)
602 //
603 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
604 * and the deleter @a __d.
605 * @param __p A pointer.
606 * @param __d A deleter.
607 * @param __a An allocator.
608 * @post use_count() == 1 && get() == __p
609 * @throw std::bad_alloc, in which case @a __d(__p) is called.
610 */
611 template<typename _Tp1, typename _Deleter, typename _Alloc>
612 __shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
613 : _M_ptr(__p), _M_refcount(__p, __d, __a)
614 {
615 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
616 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
617 __enable_shared_from_this_helper(_M_refcount, __p, __p);
618 }
619
620 /** @brief Constructs a %__shared_ptr instance that stores @a __p
621 * and shares ownership with @a __r.
622 * @param __r A %__shared_ptr.
623 * @param __p A pointer that will remain valid while @a *__r is valid.
624 * @post get() == __p && use_count() == __r.use_count()
625 *
626 * This can be used to construct a @c shared_ptr to a sub-object
627 * of an object managed by an existing @c shared_ptr.
628 *
629 * @code
630 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
631 * shared_ptr<int> pi(pii, &pii->first);
632 * assert(pii.use_count() == 2);
633 * @endcode
634 */
635 template<typename _Tp1>
636 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
637 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
638 { }
639
640 // generated copy constructor, assignment, destructor are fine.
641
642 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
643 * otherwise construct a %__shared_ptr that shares ownership
644 * with @a __r.
645 * @param __r A %__shared_ptr.
646 * @post get() == __r.get() && use_count() == __r.use_count()
647 */
648 template<typename _Tp1>
649 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
650 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
651 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
652
653 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
654 * @param __r A %__shared_ptr rvalue.
655 * @post *this contains the old value of @a __r, @a __r is empty.
656 */
657 __shared_ptr(__shared_ptr&& __r)
658 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
659 {
660 _M_refcount._M_swap(__r._M_refcount);
661 __r._M_ptr = 0;
662 }
663
664 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
665 * @param __r A %__shared_ptr rvalue.
666 * @post *this contains the old value of @a __r, @a __r is empty.
667 */
668 template<typename _Tp1>
669 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
670 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
671 {
672 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
673 _M_refcount._M_swap(__r._M_refcount);
674 __r._M_ptr = 0;
675 }
676
677 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
678 * and stores a copy of the pointer stored in @a __r.
679 * @param __r A weak_ptr.
680 * @post use_count() == __r.use_count()
681 * @throw bad_weak_ptr when __r.expired(),
682 * in which case the constructor has no effect.
683 */
684 template<typename _Tp1>
685 explicit
686 __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
687 : _M_refcount(__r._M_refcount) // may throw
688 {
689 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
690 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
691 // did not throw.
692 _M_ptr = __r._M_ptr;
693 }
694
695 template<typename _Tp1, typename _Del>
696 explicit
697 __shared_ptr(const std::unique_ptr<_Tp1, _Del>&) = delete;
698
699 /**
700 * If an exception is thrown this constructor has no effect.
701 */
702 template<typename _Tp1, typename _Del>
703 explicit
704 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
705 : _M_ptr(__r.get()), _M_refcount()
706 {
707 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
708 _Tp1* __tmp = __r.get();
709 _M_refcount = __shared_count<_Lp>(std::move(__r));
710 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
711 }
712
713 #if _GLIBCXX_DEPRECATED
714 /**
715 * @post use_count() == 1 and __r.get() == 0
716 */
717 template<typename _Tp1>
718 explicit
719 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
720 : _M_ptr(__r.get()), _M_refcount()
721 {
722 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
723 // TODO requires _Tp1 is complete, delete __r.release() well-formed
724 _Tp1* __tmp = __r.get();
725 _M_refcount = __shared_count<_Lp>(std::move(__r));
726 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
727 }
728 #endif
729
730 template<typename _Tp1>
731 __shared_ptr&
732 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
733 {
734 _M_ptr = __r._M_ptr;
735 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
736 return *this;
737 }
738
739 #if _GLIBCXX_DEPRECATED
740 template<typename _Tp1>
741 __shared_ptr&
742 operator=(std::auto_ptr<_Tp1>&& __r)
743 {
744 __shared_ptr(std::move(__r)).swap(*this);
745 return *this;
746 }
747 #endif
748
749 __shared_ptr&
750 operator=(__shared_ptr&& __r)
751 {
752 __shared_ptr(std::move(__r)).swap(*this);
753 return *this;
754 }
755
756 template<class _Tp1>
757 __shared_ptr&
758 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
759 {
760 __shared_ptr(std::move(__r)).swap(*this);
761 return *this;
762 }
763
764 template<typename _Tp1, typename _Del>
765 __shared_ptr&
766 operator=(const std::unique_ptr<_Tp1, _Del>& __r) = delete;
767
768 template<typename _Tp1, typename _Del>
769 __shared_ptr&
770 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
771 {
772 __shared_ptr(std::move(__r)).swap(*this);
773 return *this;
774 }
775
776 void
777 reset() // never throws
778 { __shared_ptr().swap(*this); }
779
780 template<typename _Tp1>
781 void
782 reset(_Tp1* __p) // _Tp1 must be complete.
783 {
784 // Catch self-reset errors.
785 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
786 __shared_ptr(__p).swap(*this);
787 }
788
789 template<typename _Tp1, typename _Deleter>
790 void
791 reset(_Tp1* __p, _Deleter __d)
792 { __shared_ptr(__p, __d).swap(*this); }
793
794 template<typename _Tp1, typename _Deleter, typename _Alloc>
795 void
796 reset(_Tp1* __p, _Deleter __d, const _Alloc& __a)
797 { __shared_ptr(__p, __d, __a).swap(*this); }
798
799 // Allow class instantiation when _Tp is [cv-qual] void.
800 typename std::add_lvalue_reference<_Tp>::type
801 operator*() const // never throws
802 {
803 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
804 return *_M_ptr;
805 }
806
807 _Tp*
808 operator->() const // never throws
809 {
810 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
811 return _M_ptr;
812 }
813
814 _Tp*
815 get() const // never throws
816 { return _M_ptr; }
817
818 // Implicit conversion to "bool"
819 private:
820 typedef _Tp* __shared_ptr::*__unspecified_bool_type;
821
822 public:
823 operator __unspecified_bool_type() const // never throws
824 { return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
825
826 bool
827 unique() const // never throws
828 { return _M_refcount._M_unique(); }
829
830 long
831 use_count() const // never throws
832 { return _M_refcount._M_get_use_count(); }
833
834 void
835 swap(__shared_ptr<_Tp, _Lp>&& __other) // never throws
836 {
837 std::swap(_M_ptr, __other._M_ptr);
838 _M_refcount._M_swap(__other._M_refcount);
839 }
840
841 protected:
842 // This constructor is non-standard, it is used by allocate_shared.
843 template<typename _Alloc, typename... _Args>
844 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
845 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
846 std::forward<_Args>(__args)...)
847 {
848 // _M_ptr needs to point to the newly constructed object.
849 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
850 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
851 _M_ptr = static_cast<_Tp*>(__p);
852 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
853 }
854
855 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
856 typename... _Args>
857 friend __shared_ptr<_Tp1, _Lp1>
858 __allocate_shared(_Alloc __a, _Args&&... __args);
859
860 private:
861 void*
862 _M_get_deleter(const std::type_info& __ti) const
863 { return _M_refcount._M_get_deleter(__ti); }
864
865 template<typename _Tp1, _Lock_policy _Lp1>
866 bool
867 _M_less(const __shared_ptr<_Tp1, _Lp1>& __rhs) const
868 { return _M_refcount < __rhs._M_refcount; }
869
870 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
871 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
872
873 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
874 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
875
876 // Friends injected into enclosing namespace and found by ADL:
877 template<typename _Tp1>
878 friend inline bool
879 operator==(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
880 { return __a.get() == __b.get(); }
881
882 template<typename _Tp1>
883 friend inline bool
884 operator!=(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
885 { return __a.get() != __b.get(); }
886
887 template<typename _Tp1>
888 friend inline bool
889 operator<(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
890 { return __a._M_less(__b); }
891
892 _Tp* _M_ptr; // Contained pointer.
893 __shared_count<_Lp> _M_refcount; // Reference counter.
894 };
895
896 // 2.2.3.8 shared_ptr specialized algorithms.
897 template<typename _Tp, _Lock_policy _Lp>
898 inline void
899 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
900 { __a.swap(__b); }
901
902 template<typename _Tp, _Lock_policy _Lp>
903 inline void
904 swap(__shared_ptr<_Tp, _Lp>&& __a, __shared_ptr<_Tp, _Lp>& __b)
905 { __a.swap(__b); }
906
907 template<typename _Tp, _Lock_policy _Lp>
908 inline void
909 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>&& __b)
910 { __a.swap(__b); }
911
912 // 2.2.3.9 shared_ptr casts
913 /** @warning The seemingly equivalent
914 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
915 * will eventually result in undefined behaviour,
916 * attempting to delete the same object twice.
917 */
918 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
919 inline __shared_ptr<_Tp, _Lp>
920 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
921 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
922
923 /** @warning The seemingly equivalent
924 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
925 * will eventually result in undefined behaviour,
926 * attempting to delete the same object twice.
927 */
928 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
929 inline __shared_ptr<_Tp, _Lp>
930 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
931 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
932
933 /** @warning The seemingly equivalent
934 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
935 * will eventually result in undefined behaviour,
936 * attempting to delete the same object twice.
937 */
938 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
939 inline __shared_ptr<_Tp, _Lp>
940 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
941 {
942 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
943 return __shared_ptr<_Tp, _Lp>(__r, __p);
944 return __shared_ptr<_Tp, _Lp>();
945 }
946
947 // 2.2.3.7 shared_ptr I/O
948 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
949 std::basic_ostream<_Ch, _Tr>&
950 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
951 const __shared_ptr<_Tp, _Lp>& __p)
952 {
953 __os << __p.get();
954 return __os;
955 }
956
957 // 2.2.3.10 shared_ptr get_deleter (experimental)
958 template<typename _Del, typename _Tp, _Lock_policy _Lp>
959 inline _Del*
960 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
961 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
962
963
964 template<typename _Tp, _Lock_policy _Lp>
965 class __weak_ptr
966 {
967 public:
968 typedef _Tp element_type;
969
970 __weak_ptr()
971 : _M_ptr(0), _M_refcount() // never throws
972 { }
973
974 // Generated copy constructor, assignment, destructor are fine.
975
976 // The "obvious" converting constructor implementation:
977 //
978 // template<typename _Tp1>
979 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
980 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
981 // { }
982 //
983 // has a serious problem.
984 //
985 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
986 // conversion may require access to *__r._M_ptr (virtual inheritance).
987 //
988 // It is not possible to avoid spurious access violations since
989 // in multithreaded programs __r._M_ptr may be invalidated at any point.
990 template<typename _Tp1>
991 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
992 : _M_refcount(__r._M_refcount) // never throws
993 {
994 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
995 _M_ptr = __r.lock().get();
996 }
997
998 template<typename _Tp1>
999 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
1000 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1001 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
1002
1003 template<typename _Tp1>
1004 __weak_ptr&
1005 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
1006 {
1007 _M_ptr = __r.lock().get();
1008 _M_refcount = __r._M_refcount;
1009 return *this;
1010 }
1011
1012 template<typename _Tp1>
1013 __weak_ptr&
1014 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
1015 {
1016 _M_ptr = __r._M_ptr;
1017 _M_refcount = __r._M_refcount;
1018 return *this;
1019 }
1020
1021 __shared_ptr<_Tp, _Lp>
1022 lock() const // never throws
1023 {
1024 #ifdef __GTHREADS
1025 // Optimization: avoid throw overhead.
1026 if (expired())
1027 return __shared_ptr<element_type, _Lp>();
1028
1029 try
1030 {
1031 return __shared_ptr<element_type, _Lp>(*this);
1032 }
1033 catch(const bad_weak_ptr&)
1034 {
1035 // Q: How can we get here?
1036 // A: Another thread may have invalidated r after the
1037 // use_count test above.
1038 return __shared_ptr<element_type, _Lp>();
1039 }
1040
1041 #else
1042 // Optimization: avoid try/catch overhead when single threaded.
1043 return expired() ? __shared_ptr<element_type, _Lp>()
1044 : __shared_ptr<element_type, _Lp>(*this);
1045
1046 #endif
1047 } // XXX MT
1048
1049 long
1050 use_count() const // never throws
1051 { return _M_refcount._M_get_use_count(); }
1052
1053 bool
1054 expired() const // never throws
1055 { return _M_refcount._M_get_use_count() == 0; }
1056
1057 void
1058 reset() // never throws
1059 { __weak_ptr().swap(*this); }
1060
1061 void
1062 swap(__weak_ptr& __s) // never throws
1063 {
1064 std::swap(_M_ptr, __s._M_ptr);
1065 _M_refcount._M_swap(__s._M_refcount);
1066 }
1067
1068 private:
1069 // Used by __enable_shared_from_this.
1070 void
1071 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
1072 {
1073 _M_ptr = __ptr;
1074 _M_refcount = __refcount;
1075 }
1076
1077 template<typename _Tp1>
1078 bool
1079 _M_less(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1080 { return _M_refcount < __rhs._M_refcount; }
1081
1082 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1083 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1084 friend class __enable_shared_from_this<_Tp, _Lp>;
1085 friend class enable_shared_from_this<_Tp>;
1086
1087 // Friend injected into namespace and found by ADL.
1088 template<typename _Tp1>
1089 friend inline bool
1090 operator<(const __weak_ptr& __lhs, const __weak_ptr<_Tp1, _Lp>& __rhs)
1091 { return __lhs._M_less(__rhs); }
1092
1093 _Tp* _M_ptr; // Contained pointer.
1094 __weak_count<_Lp> _M_refcount; // Reference counter.
1095 };
1096
1097 // 2.2.4.7 weak_ptr specialized algorithms.
1098 template<typename _Tp, _Lock_policy _Lp>
1099 inline void
1100 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1101 { __a.swap(__b); }
1102
1103
1104 template<typename _Tp, _Lock_policy _Lp>
1105 class __enable_shared_from_this
1106 {
1107 protected:
1108 __enable_shared_from_this() { }
1109
1110 __enable_shared_from_this(const __enable_shared_from_this&) { }
1111
1112 __enable_shared_from_this&
1113 operator=(const __enable_shared_from_this&)
1114 { return *this; }
1115
1116 ~__enable_shared_from_this() { }
1117
1118 public:
1119 __shared_ptr<_Tp, _Lp>
1120 shared_from_this()
1121 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1122
1123 __shared_ptr<const _Tp, _Lp>
1124 shared_from_this() const
1125 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1126
1127 private:
1128 template<typename _Tp1>
1129 void
1130 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
1131 { _M_weak_this._M_assign(__p, __n); }
1132
1133 template<typename _Tp1>
1134 friend void
1135 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1136 const __enable_shared_from_this* __pe,
1137 const _Tp1* __px)
1138 {
1139 if (__pe != 0)
1140 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1141 }
1142
1143 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1144 };
1145
1146
1147 /// shared_ptr
1148 // The actual shared_ptr, with forwarding constructors and
1149 // assignment operators.
1150 template<typename _Tp>
1151 class shared_ptr
1152 : public __shared_ptr<_Tp>
1153 {
1154 public:
1155 shared_ptr()
1156 : __shared_ptr<_Tp>() { }
1157
1158 template<typename _Tp1>
1159 explicit
1160 shared_ptr(_Tp1* __p)
1161 : __shared_ptr<_Tp>(__p) { }
1162
1163 template<typename _Tp1, typename _Deleter>
1164 shared_ptr(_Tp1* __p, _Deleter __d)
1165 : __shared_ptr<_Tp>(__p, __d) { }
1166
1167 template<typename _Tp1, typename _Deleter, typename _Alloc>
1168 shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
1169 : __shared_ptr<_Tp>(__p, __d, __a) { }
1170
1171 // Aliasing constructor
1172 template<typename _Tp1>
1173 shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
1174 : __shared_ptr<_Tp>(__r, __p) { }
1175
1176 template<typename _Tp1>
1177 shared_ptr(const shared_ptr<_Tp1>& __r)
1178 : __shared_ptr<_Tp>(__r) { }
1179
1180 shared_ptr(shared_ptr&& __r)
1181 : __shared_ptr<_Tp>(std::move(__r)) { }
1182
1183 template<typename _Tp1>
1184 shared_ptr(shared_ptr<_Tp1>&& __r)
1185 : __shared_ptr<_Tp>(std::move(__r)) { }
1186
1187 template<typename _Tp1>
1188 explicit
1189 shared_ptr(const weak_ptr<_Tp1>& __r)
1190 : __shared_ptr<_Tp>(__r) { }
1191
1192 #if _GLIBCXX_DEPRECATED
1193 template<typename _Tp1>
1194 explicit
1195 shared_ptr(std::auto_ptr<_Tp1>&& __r)
1196 : __shared_ptr<_Tp>(std::move(__r)) { }
1197 #endif
1198
1199 template<typename _Tp1, typename _Del>
1200 explicit
1201 shared_ptr(const std::unique_ptr<_Tp1, _Del>&) = delete;
1202
1203 template<typename _Tp1, typename _Del>
1204 explicit
1205 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
1206 : __shared_ptr<_Tp>(std::move(__r)) { }
1207
1208 template<typename _Tp1>
1209 shared_ptr&
1210 operator=(const shared_ptr<_Tp1>& __r) // never throws
1211 {
1212 this->__shared_ptr<_Tp>::operator=(__r);
1213 return *this;
1214 }
1215
1216 #if _GLIBCXX_DEPRECATED
1217 template<typename _Tp1>
1218 shared_ptr&
1219 operator=(std::auto_ptr<_Tp1>&& __r)
1220 {
1221 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1222 return *this;
1223 }
1224 #endif
1225
1226 shared_ptr&
1227 operator=(shared_ptr&& __r)
1228 {
1229 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1230 return *this;
1231 }
1232
1233 template<class _Tp1>
1234 shared_ptr&
1235 operator=(shared_ptr<_Tp1>&& __r)
1236 {
1237 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1238 return *this;
1239 }
1240
1241 template<typename _Tp1, typename _Del>
1242 shared_ptr&
1243 operator=(const std::unique_ptr<_Tp1, _Del>& __r) = delete;
1244
1245 template<typename _Tp1, typename _Del>
1246 shared_ptr&
1247 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
1248 {
1249 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1250 return *this;
1251 }
1252
1253 private:
1254 // This constructor is non-standard, it is used by allocate_shared.
1255 template<typename _Alloc, typename... _Args>
1256 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
1257 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
1258 { }
1259
1260 template<typename _Tp1, typename _Alloc, typename... _Args>
1261 friend shared_ptr<_Tp1>
1262 allocate_shared(_Alloc __a, _Args&&... __args);
1263 };
1264
1265 // 20.7.12.2.9 shared_ptr specialized algorithms.
1266 template<typename _Tp>
1267 inline void
1268 swap(__shared_ptr<_Tp>& __a, __shared_ptr<_Tp>& __b)
1269 { __a.swap(__b); }
1270
1271 template<typename _Tp>
1272 inline void
1273 swap(__shared_ptr<_Tp>&& __a, __shared_ptr<_Tp>& __b)
1274 { __a.swap(__b); }
1275
1276 template<typename _Tp>
1277 inline void
1278 swap(__shared_ptr<_Tp>& __a, __shared_ptr<_Tp>&& __b)
1279 { __a.swap(__b); }
1280
1281 template<typename _Tp, typename _Tp1>
1282 inline shared_ptr<_Tp>
1283 static_pointer_cast(const shared_ptr<_Tp1>& __r)
1284 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
1285
1286 template<typename _Tp, typename _Tp1>
1287 inline shared_ptr<_Tp>
1288 const_pointer_cast(const shared_ptr<_Tp1>& __r)
1289 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
1290
1291 template<typename _Tp, typename _Tp1>
1292 inline shared_ptr<_Tp>
1293 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
1294 {
1295 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1296 return shared_ptr<_Tp>(__r, __p);
1297 return shared_ptr<_Tp>();
1298 }
1299
1300
1301 /// weak_ptr
1302 // The actual weak_ptr, with forwarding constructors and
1303 // assignment operators.
1304 template<typename _Tp>
1305 class weak_ptr
1306 : public __weak_ptr<_Tp>
1307 {
1308 public:
1309 weak_ptr()
1310 : __weak_ptr<_Tp>() { }
1311
1312 template<typename _Tp1>
1313 weak_ptr(const weak_ptr<_Tp1>& __r)
1314 : __weak_ptr<_Tp>(__r) { }
1315
1316 template<typename _Tp1>
1317 weak_ptr(const shared_ptr<_Tp1>& __r)
1318 : __weak_ptr<_Tp>(__r) { }
1319
1320 template<typename _Tp1>
1321 weak_ptr&
1322 operator=(const weak_ptr<_Tp1>& __r) // never throws
1323 {
1324 this->__weak_ptr<_Tp>::operator=(__r);
1325 return *this;
1326 }
1327
1328 template<typename _Tp1>
1329 weak_ptr&
1330 operator=(const shared_ptr<_Tp1>& __r) // never throws
1331 {
1332 this->__weak_ptr<_Tp>::operator=(__r);
1333 return *this;
1334 }
1335
1336 shared_ptr<_Tp>
1337 lock() const // never throws
1338 {
1339 #ifdef __GTHREADS
1340 if (this->expired())
1341 return shared_ptr<_Tp>();
1342
1343 try
1344 {
1345 return shared_ptr<_Tp>(*this);
1346 }
1347 catch(const bad_weak_ptr&)
1348 {
1349 return shared_ptr<_Tp>();
1350 }
1351 #else
1352 return this->expired() ? shared_ptr<_Tp>()
1353 : shared_ptr<_Tp>(*this);
1354 #endif
1355 }
1356 };
1357
1358 /// enable_shared_from_this
1359 template<typename _Tp>
1360 class enable_shared_from_this
1361 {
1362 protected:
1363 enable_shared_from_this() { }
1364
1365 enable_shared_from_this(const enable_shared_from_this&) { }
1366
1367 enable_shared_from_this&
1368 operator=(const enable_shared_from_this&)
1369 { return *this; }
1370
1371 ~enable_shared_from_this() { }
1372
1373 public:
1374 shared_ptr<_Tp>
1375 shared_from_this()
1376 { return shared_ptr<_Tp>(this->_M_weak_this); }
1377
1378 shared_ptr<const _Tp>
1379 shared_from_this() const
1380 { return shared_ptr<const _Tp>(this->_M_weak_this); }
1381
1382 private:
1383 template<typename _Tp1>
1384 void
1385 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
1386 { _M_weak_this._M_assign(__p, __n); }
1387
1388 template<typename _Tp1>
1389 friend void
1390 __enable_shared_from_this_helper(const __shared_count<>& __pn,
1391 const enable_shared_from_this* __pe,
1392 const _Tp1* __px)
1393 {
1394 if (__pe != 0)
1395 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1396 }
1397
1398 mutable weak_ptr<_Tp> _M_weak_this;
1399 };
1400
1401 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1402 inline __shared_ptr<_Tp, _Lp>
1403 __allocate_shared(_Alloc __a, _Args&&... __args)
1404 {
1405 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(),
1406 std::forward<_Alloc>(__a), std::forward<_Args>(__args)...);
1407 }
1408
1409 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1410 inline __shared_ptr<_Tp, _Lp>
1411 __make_shared(_Args&&... __args)
1412 {
1413 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1414 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1415 std::forward<_Args>(__args)...);
1416 }
1417
1418 /** @brief Create an object that is owned by a shared_ptr.
1419 * @param __a An allocator.
1420 * @param __args Arguments for the @a _Tp object's constructor.
1421 * @return A shared_ptr that owns the newly created object.
1422 * @throw An exception thrown from @a _Alloc::allocate or from the
1423 * constructor of @a _Tp.
1424 *
1425 * A copy of @a __a will be used to allocate memory for the shared_ptr
1426 * and the new object.
1427 */
1428 template<typename _Tp, typename _Alloc, typename... _Args>
1429 inline shared_ptr<_Tp>
1430 allocate_shared(_Alloc __a, _Args&&... __args)
1431 {
1432 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
1433 std::forward<_Args>(__args)...);
1434 }
1435
1436 /** @brief Create an object that is owned by a shared_ptr.
1437 * @param __args Arguments for the @a _Tp object's constructor.
1438 * @return A shared_ptr that owns the newly created object.
1439 * @throw std::bad_alloc, or an exception thrown from the
1440 * constructor of @a _Tp.
1441 */
1442 template<typename _Tp, typename... _Args>
1443 inline shared_ptr<_Tp>
1444 make_shared(_Args&&... __args)
1445 {
1446 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1447 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
1448 std::forward<_Args>(__args)...);
1449 }
1450
1451 }