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