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