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