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