libstdc++

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