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