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