Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
blob7e7dd4395b0797892cdb7e9cacfe9571ada18f33
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
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.
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.
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.
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/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
34 // weak_ptr.hpp
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
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)
44 /** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * You should not attempt to use it directly.
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
52 _GLIBCXX_BEGIN_NAMESPACE(std)
54 /**
55 * @brief Exception possibly thrown by @c shared_ptr.
56 * @ingroup exceptions
58 class bad_weak_ptr : public std::exception
60 public:
61 virtual char const*
62 what() const throw()
63 { return "std::bad_weak_ptr"; }
66 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
67 inline void
68 __throw_bad_weak_ptr()
70 #if __EXCEPTIONS
71 throw bad_weak_ptr();
72 #else
73 __builtin_abort();
74 #endif
77 using __gnu_cxx::_Lock_policy;
78 using __gnu_cxx::__default_lock_policy;
79 using __gnu_cxx::_S_single;
80 using __gnu_cxx::_S_mutex;
81 using __gnu_cxx::_S_atomic;
83 // Empty helper class except when the template argument is _S_mutex.
84 template<_Lock_policy _Lp>
85 class _Mutex_base
87 protected:
88 // The atomic policy uses fully-fenced builtins, single doesn't care.
89 enum { _S_need_barriers = 0 };
92 template<>
93 class _Mutex_base<_S_mutex>
94 : public __gnu_cxx::__mutex
96 protected:
97 // This policy is used when atomic builtins are not available.
98 // The replacement atomic operations might not have the necessary
99 // memory barriers.
100 enum { _S_need_barriers = 1 };
103 template<_Lock_policy _Lp = __default_lock_policy>
104 class _Sp_counted_base
105 : public _Mutex_base<_Lp>
107 public:
108 _Sp_counted_base()
109 : _M_use_count(1), _M_weak_count(1) { }
111 virtual
112 ~_Sp_counted_base() // nothrow
115 // Called when _M_use_count drops to zero, to release the resources
116 // managed by *this.
117 virtual void
118 _M_dispose() = 0; // nothrow
120 // Called when _M_weak_count drops to zero.
121 virtual void
122 _M_destroy() // nothrow
123 { delete this; }
125 virtual void*
126 _M_get_deleter(const std::type_info&) = 0;
128 void
129 _M_add_ref_copy()
130 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
132 void
133 _M_add_ref_lock();
135 void
136 _M_release() // nothrow
138 // Be race-detector-friendly. For more info see bits/c++config.
139 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
140 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
143 _M_dispose();
144 // There must be a memory barrier between dispose() and destroy()
145 // to ensure that the effects of dispose() are observed in the
146 // thread that runs destroy().
147 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
148 if (_Mutex_base<_Lp>::_S_need_barriers)
150 _GLIBCXX_READ_MEM_BARRIER;
151 _GLIBCXX_WRITE_MEM_BARRIER;
154 // Be race-detector-friendly. For more info see bits/c++config.
155 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
156 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
157 -1) == 1)
159 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
160 _M_destroy();
165 void
166 _M_weak_add_ref() // nothrow
167 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
169 void
170 _M_weak_release() // nothrow
172 // Be race-detector-friendly. For more info see bits/c++config.
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
174 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
177 if (_Mutex_base<_Lp>::_S_need_barriers)
179 // See _M_release(),
180 // destroy() must observe results of dispose()
181 _GLIBCXX_READ_MEM_BARRIER;
182 _GLIBCXX_WRITE_MEM_BARRIER;
184 _M_destroy();
188 long
189 _M_get_use_count() const // nothrow
191 // No memory barrier is used here so there is no synchronization
192 // with other threads.
193 return const_cast<const volatile _Atomic_word&>(_M_use_count);
196 private:
197 _Sp_counted_base(_Sp_counted_base const&);
198 _Sp_counted_base& operator=(_Sp_counted_base const&);
200 _Atomic_word _M_use_count; // #shared
201 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
204 template<>
205 inline void
206 _Sp_counted_base<_S_single>::
207 _M_add_ref_lock()
209 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
211 _M_use_count = 0;
212 __throw_bad_weak_ptr();
216 template<>
217 inline void
218 _Sp_counted_base<_S_mutex>::
219 _M_add_ref_lock()
221 __gnu_cxx::__scoped_lock sentry(*this);
222 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
224 _M_use_count = 0;
225 __throw_bad_weak_ptr();
229 template<>
230 inline void
231 _Sp_counted_base<_S_atomic>::
232 _M_add_ref_lock()
234 // Perform lock-free add-if-not-zero operation.
235 _Atomic_word __count;
238 __count = _M_use_count;
239 if (__count == 0)
240 __throw_bad_weak_ptr();
242 // Replace the current counter value with the old value + 1, as
243 // long as it's not changed meanwhile.
245 while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
246 __count + 1));
250 // Forward declarations.
251 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
252 class __shared_ptr;
254 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
255 class __weak_ptr;
257 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
258 class __enable_shared_from_this;
260 template<typename _Tp>
261 class shared_ptr;
263 template<typename _Tp>
264 class weak_ptr;
266 template<typename _Tp>
267 struct owner_less;
269 template<typename _Tp>
270 class enable_shared_from_this;
272 template<_Lock_policy _Lp = __default_lock_policy>
273 class __weak_count;
275 template<_Lock_policy _Lp = __default_lock_policy>
276 class __shared_count;
279 // Counted ptr with no deleter or allocator support
280 template<typename _Ptr, _Lock_policy _Lp>
281 class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
283 public:
284 explicit
285 _Sp_counted_ptr(_Ptr __p)
286 : _M_ptr(__p) { }
288 virtual void
289 _M_dispose() // nothrow
290 { delete _M_ptr; }
292 virtual void
293 _M_destroy() // nothrow
294 { delete this; }
296 virtual void*
297 _M_get_deleter(const std::type_info&)
298 { return 0; }
300 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
301 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
303 protected:
304 _Ptr _M_ptr; // copy constructor must not throw
307 template<>
308 inline void
309 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() { }
311 template<>
312 inline void
313 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() { }
315 template<>
316 inline void
317 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() { }
319 // Support for custom deleter and/or allocator
320 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
321 class _Sp_counted_deleter : public _Sp_counted_ptr<_Ptr, _Lp>
323 typedef typename _Alloc::template
324 rebind<_Sp_counted_deleter>::other _My_alloc_type;
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 _My_alloc_type // copy constructor must not throw
332 _Deleter _M_del; // copy constructor must not throw
333 _My_Deleter(_Deleter __d, const _Alloc& __a)
334 : _My_alloc_type(__a), _M_del(__d) { }
337 protected:
338 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
340 public:
341 // __d(__p) must not throw.
342 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
343 : _Base_type(__p), _M_del(__d, _Alloc()) { }
345 // __d(__p) must not throw.
346 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
347 : _Base_type(__p), _M_del(__d, __a) { }
349 virtual void
350 _M_dispose() // nothrow
351 { _M_del._M_del(_Base_type::_M_ptr); }
353 virtual void
354 _M_destroy() // nothrow
356 _My_alloc_type __a(_M_del);
357 this->~_Sp_counted_deleter();
358 __a.deallocate(this, 1);
361 virtual void*
362 _M_get_deleter(const std::type_info& __ti)
364 #ifdef __GXX_RTTI
365 return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
366 #else
367 return 0;
368 #endif
371 protected:
372 _My_Deleter _M_del; // copy constructor must not throw
375 // helpers for make_shared / allocate_shared
377 template<typename _Tp>
378 struct _Sp_destroy_inplace
380 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
383 struct _Sp_make_shared_tag { };
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>
389 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
390 _Base_type;
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()
398 void* __p = &_M_storage;
399 ::new (__p) _Tp(); // might throw
400 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
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()
408 void* __p = &_M_storage;
409 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
410 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
413 // Override because the allocator needs to know the dynamic type
414 virtual void
415 _M_destroy() // nothrow
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);
424 // Sneaky trick so __shared_ptr can get the managed pointer
425 virtual void*
426 _M_get_deleter(const std::type_info& __ti)
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
437 private:
438 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
439 _M_storage;
442 template<_Lock_policy _Lp>
443 class __shared_count
445 public:
446 constexpr __shared_count() : _M_pi(0) // nothrow
449 template<typename _Ptr>
450 explicit
451 __shared_count(_Ptr __p) : _M_pi(0)
453 __try
455 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
457 __catch(...)
459 delete __p;
460 __throw_exception_again;
464 template<typename _Ptr, typename _Deleter>
465 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
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
474 _M_pi = __a2.allocate(1);
475 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
477 __catch(...)
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;
486 template<typename _Ptr, typename _Deleter, typename _Alloc>
487 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
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
494 _M_pi = __a2.allocate(1);
495 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
497 __catch(...)
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;
506 template<typename _Tp, typename _Alloc, typename... _Args>
507 __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
508 : _M_pi(0)
510 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
511 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
512 _Alloc2 __a2(__a);
513 __try
515 _M_pi = __a2.allocate(1);
516 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
517 std::forward<_Args>(__args)...);
519 __catch(...)
521 if (_M_pi)
522 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
523 __throw_exception_again;
527 #if _GLIBCXX_DEPRECATED
528 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
529 template<typename _Tp>
530 explicit
531 __shared_count(std::auto_ptr<_Tp>&& __r)
532 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
533 { __r.release(); }
534 #endif
536 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
537 template<typename _Tp, typename _Del>
538 explicit
539 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
540 : _M_pi(_S_create_from_up(std::move(__r)))
541 { __r.release(); }
543 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
544 explicit __shared_count(const __weak_count<_Lp>& __r);
546 ~__shared_count() // nothrow
548 if (_M_pi != 0)
549 _M_pi->_M_release();
552 __shared_count(const __shared_count& __r)
553 : _M_pi(__r._M_pi) // nothrow
555 if (_M_pi != 0)
556 _M_pi->_M_add_ref_copy();
559 __shared_count&
560 operator=(const __shared_count& __r) // nothrow
562 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
563 if (__tmp != _M_pi)
565 if (__tmp != 0)
566 __tmp->_M_add_ref_copy();
567 if (_M_pi != 0)
568 _M_pi->_M_release();
569 _M_pi = __tmp;
571 return *this;
574 void
575 _M_swap(__shared_count& __r) // nothrow
577 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
578 __r._M_pi = _M_pi;
579 _M_pi = __tmp;
582 long
583 _M_get_use_count() const // nothrow
584 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
586 bool
587 _M_unique() const // nothrow
588 { return this->_M_get_use_count() == 1; }
590 void*
591 _M_get_deleter(const std::type_info& __ti) const
592 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
594 bool
595 _M_less(const __shared_count& __rhs) const
596 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
598 bool
599 _M_less(const __weak_count<_Lp>& __rhs) const
600 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
602 // Friend function injected into enclosing namespace and found by ADL
603 friend inline bool
604 operator==(const __shared_count& __a, const __shared_count& __b)
605 { return __a._M_pi == __b._M_pi; }
607 private:
608 friend class __weak_count<_Lp>;
610 template<typename _Tp, typename _Del>
611 static _Sp_counted_base<_Lp>*
612 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
613 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
615 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
616 _Lp>(__r.get(), __r.get_deleter());
619 template<typename _Tp, typename _Del>
620 static _Sp_counted_base<_Lp>*
621 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
622 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
624 typedef typename std::remove_reference<_Del>::type _Del1;
625 typedef std::reference_wrapper<_Del1> _Del2;
626 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
627 _Lp>(__r.get(), std::ref(__r.get_deleter()));
630 _Sp_counted_base<_Lp>* _M_pi;
634 template<_Lock_policy _Lp>
635 class __weak_count
637 public:
638 constexpr __weak_count() : _M_pi(0) // nothrow
641 __weak_count(const __shared_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
643 if (_M_pi != 0)
644 _M_pi->_M_weak_add_ref();
647 __weak_count(const __weak_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
649 if (_M_pi != 0)
650 _M_pi->_M_weak_add_ref();
653 ~__weak_count() // nothrow
655 if (_M_pi != 0)
656 _M_pi->_M_weak_release();
659 __weak_count<_Lp>&
660 operator=(const __shared_count<_Lp>& __r) // nothrow
662 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
663 if (__tmp != 0)
664 __tmp->_M_weak_add_ref();
665 if (_M_pi != 0)
666 _M_pi->_M_weak_release();
667 _M_pi = __tmp;
668 return *this;
671 __weak_count<_Lp>&
672 operator=(const __weak_count<_Lp>& __r) // nothrow
674 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
675 if (__tmp != 0)
676 __tmp->_M_weak_add_ref();
677 if (_M_pi != 0)
678 _M_pi->_M_weak_release();
679 _M_pi = __tmp;
680 return *this;
683 void
684 _M_swap(__weak_count<_Lp>& __r) // nothrow
686 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
687 __r._M_pi = _M_pi;
688 _M_pi = __tmp;
691 long
692 _M_get_use_count() const // nothrow
693 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
695 bool
696 _M_less(const __weak_count& __rhs) const
697 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
699 bool
700 _M_less(const __shared_count<_Lp>& __rhs) const
701 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
703 // Friend function injected into enclosing namespace and found by ADL
704 friend inline bool
705 operator==(const __weak_count& __a, const __weak_count& __b)
706 { return __a._M_pi == __b._M_pi; }
708 private:
709 friend class __shared_count<_Lp>;
711 _Sp_counted_base<_Lp>* _M_pi;
714 // Now that __weak_count is defined we can define this constructor:
715 template<_Lock_policy _Lp>
716 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
717 : _M_pi(__r._M_pi)
719 if (_M_pi != 0)
720 _M_pi->_M_add_ref_lock();
721 else
722 __throw_bad_weak_ptr();
726 // Support for enable_shared_from_this.
728 // Friend of __enable_shared_from_this.
729 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
730 void
731 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
732 const __enable_shared_from_this<_Tp1,
733 _Lp>*, const _Tp2*);
735 // Friend of enable_shared_from_this.
736 template<typename _Tp1, typename _Tp2>
737 void
738 __enable_shared_from_this_helper(const __shared_count<>&,
739 const enable_shared_from_this<_Tp1>*,
740 const _Tp2*);
742 template<_Lock_policy _Lp>
743 inline void
744 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
748 template<typename _Tp, _Lock_policy _Lp>
749 class __shared_ptr
751 public:
752 typedef _Tp element_type;
754 constexpr __shared_ptr()
755 : _M_ptr(0), _M_refcount() // never throws
758 template<typename _Tp1>
759 explicit __shared_ptr(_Tp1* __p)
760 : _M_ptr(__p), _M_refcount(__p)
762 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
763 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
764 __enable_shared_from_this_helper(_M_refcount, __p, __p);
767 template<typename _Tp1, typename _Deleter>
768 __shared_ptr(_Tp1* __p, _Deleter __d)
769 : _M_ptr(__p), _M_refcount(__p, __d)
771 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
772 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
773 __enable_shared_from_this_helper(_M_refcount, __p, __p);
776 template<typename _Tp1, typename _Deleter, typename _Alloc>
777 __shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
778 : _M_ptr(__p), _M_refcount(__p, __d, __a)
780 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
781 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
782 __enable_shared_from_this_helper(_M_refcount, __p, __p);
785 template<typename _Deleter>
786 __shared_ptr(nullptr_t __p, _Deleter __d)
787 : _M_ptr(0), _M_refcount(__p, __d)
790 template<typename _Deleter, typename _Alloc>
791 __shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
792 : _M_ptr(0), _M_refcount(__p, __d, __a)
795 template<typename _Tp1>
796 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
797 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
800 // generated copy constructor, assignment, destructor are fine.
802 template<typename _Tp1, typename = typename
803 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
804 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
805 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
808 __shared_ptr(__shared_ptr&& __r)
809 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
811 _M_refcount._M_swap(__r._M_refcount);
812 __r._M_ptr = 0;
815 template<typename _Tp1, typename = typename
816 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
817 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
818 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
820 _M_refcount._M_swap(__r._M_refcount);
821 __r._M_ptr = 0;
824 template<typename _Tp1>
825 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
826 : _M_refcount(__r._M_refcount) // may throw
828 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
830 // It is now safe to copy __r._M_ptr, as
831 // _M_refcount(__r._M_refcount) did not throw.
832 _M_ptr = __r._M_ptr;
835 // If an exception is thrown this constructor has no effect.
836 template<typename _Tp1, typename _Del>
837 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
838 : _M_ptr(__r.get()), _M_refcount()
840 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
841 _Tp1* __tmp = __r.get();
842 _M_refcount = __shared_count<_Lp>(std::move(__r));
843 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
846 #if _GLIBCXX_DEPRECATED
847 // Postcondition: use_count() == 1 and __r.get() == 0
848 template<typename _Tp1>
849 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
850 : _M_ptr(__r.get()), _M_refcount()
852 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
853 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
854 _Tp1* __tmp = __r.get();
855 _M_refcount = __shared_count<_Lp>(std::move(__r));
856 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
858 #endif
860 /* TODO: use delegating constructor */
861 constexpr __shared_ptr(nullptr_t)
862 : _M_ptr(0), _M_refcount() // never throws
865 template<typename _Tp1>
866 __shared_ptr&
867 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
869 _M_ptr = __r._M_ptr;
870 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
871 return *this;
874 #if _GLIBCXX_DEPRECATED
875 template<typename _Tp1>
876 __shared_ptr&
877 operator=(std::auto_ptr<_Tp1>&& __r)
879 __shared_ptr(std::move(__r)).swap(*this);
880 return *this;
882 #endif
884 __shared_ptr&
885 operator=(__shared_ptr&& __r)
887 __shared_ptr(std::move(__r)).swap(*this);
888 return *this;
891 template<class _Tp1>
892 __shared_ptr&
893 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
895 __shared_ptr(std::move(__r)).swap(*this);
896 return *this;
899 template<typename _Tp1, typename _Del>
900 __shared_ptr&
901 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
903 __shared_ptr(std::move(__r)).swap(*this);
904 return *this;
907 void
908 reset() // never throws
909 { __shared_ptr().swap(*this); }
911 template<typename _Tp1>
912 void
913 reset(_Tp1* __p) // _Tp1 must be complete.
915 // Catch self-reset errors.
916 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
917 __shared_ptr(__p).swap(*this);
920 template<typename _Tp1, typename _Deleter>
921 void
922 reset(_Tp1* __p, _Deleter __d)
923 { __shared_ptr(__p, __d).swap(*this); }
925 template<typename _Tp1, typename _Deleter, typename _Alloc>
926 void
927 reset(_Tp1* __p, _Deleter __d, const _Alloc& __a)
928 { __shared_ptr(__p, __d, __a).swap(*this); }
930 // Allow class instantiation when _Tp is [cv-qual] void.
931 typename std::add_lvalue_reference<_Tp>::type
932 operator*() const // never throws
934 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
935 return *_M_ptr;
938 _Tp*
939 operator->() const // never throws
941 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
942 return _M_ptr;
945 _Tp*
946 get() const // never throws
947 { return _M_ptr; }
949 explicit operator bool() const // never throws
950 { return _M_ptr == 0 ? false : true; }
952 bool
953 unique() const // never throws
954 { return _M_refcount._M_unique(); }
956 long
957 use_count() const // never throws
958 { return _M_refcount._M_get_use_count(); }
960 void
961 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
963 std::swap(_M_ptr, __other._M_ptr);
964 _M_refcount._M_swap(__other._M_refcount);
967 template<typename _Tp1>
968 bool
969 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
970 { return _M_refcount._M_less(__rhs._M_refcount); }
972 template<typename _Tp1>
973 bool
974 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
975 { return _M_refcount._M_less(__rhs._M_refcount); }
977 #ifdef __GXX_RTTI
978 protected:
979 // This constructor is non-standard, it is used by allocate_shared.
980 template<typename _Alloc, typename... _Args>
981 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
982 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
983 std::forward<_Args>(__args)...)
985 // _M_ptr needs to point to the newly constructed object.
986 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
987 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
988 _M_ptr = static_cast<_Tp*>(__p);
989 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
991 #else
992 template<typename _Alloc>
993 struct _Deleter
995 void operator()(_Tp* __ptr)
997 _M_alloc.destroy(__ptr);
998 _M_alloc.deallocate(__ptr, 1);
1000 _Alloc _M_alloc;
1003 template<typename _Alloc, typename... _Args>
1004 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
1005 : _M_ptr(), _M_refcount()
1007 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1008 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1009 _M_ptr = __del._M_alloc.allocate(1);
1010 __try
1012 __del._M_alloc.construct(_M_ptr, std::forward<_Args>(__args)...);
1014 __catch(...)
1016 __del._M_alloc.deallocate(_M_ptr, 1);
1017 __throw_exception_again;
1019 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1020 _M_refcount._M_swap(__count);
1021 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1023 #endif
1025 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1026 typename... _Args>
1027 friend __shared_ptr<_Tp1, _Lp1>
1028 __allocate_shared(_Alloc __a, _Args&&... __args);
1030 private:
1031 void*
1032 _M_get_deleter(const std::type_info& __ti) const
1033 { return _M_refcount._M_get_deleter(__ti); }
1035 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1036 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1038 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1039 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
1041 _Tp* _M_ptr; // Contained pointer.
1042 __shared_count<_Lp> _M_refcount; // Reference counter.
1046 // 20.8.13.2.7 shared_ptr comparisons
1047 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1048 inline bool
1049 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1050 const __shared_ptr<_Tp2, _Lp>& __b)
1051 { return __a.get() == __b.get(); }
1053 template<typename _Tp, _Lock_policy _Lp>
1054 inline bool
1055 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
1056 { return __a.get() == nullptr; }
1058 template<typename _Tp, _Lock_policy _Lp>
1059 inline bool
1060 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __b)
1061 { return nullptr == __b.get(); }
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)
1067 { return __a.get() != __b.get(); }
1069 template<typename _Tp, _Lock_policy _Lp>
1070 inline bool
1071 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
1072 { return __a.get() != nullptr; }
1074 template<typename _Tp, _Lock_policy _Lp>
1075 inline bool
1076 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __b)
1077 { return nullptr != __b.get(); }
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)
1083 { return __a.get() < __b.get(); }
1085 template<typename _Sp>
1086 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1088 bool
1089 operator()(const _Sp& __lhs, const _Sp& __rhs) const
1091 typedef typename _Sp::element_type element_type;
1092 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1096 template<typename _Tp, _Lock_policy _Lp>
1097 struct less<__shared_ptr<_Tp, _Lp>>
1098 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1099 { };
1101 // 2.2.3.8 shared_ptr specialized algorithms.
1102 template<typename _Tp, _Lock_policy _Lp>
1103 inline void
1104 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
1105 { __a.swap(__b); }
1107 // 2.2.3.9 shared_ptr casts
1109 // The seemingly equivalent code:
1110 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1111 // will eventually result in undefined behaviour, attempting to
1112 // delete the same object twice.
1113 /// static_pointer_cast
1114 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1115 inline __shared_ptr<_Tp, _Lp>
1116 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
1117 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
1119 // The seemingly equivalent code:
1120 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1121 // will eventually result in undefined behaviour, attempting to
1122 // delete the same object twice.
1123 /// const_pointer_cast
1124 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1125 inline __shared_ptr<_Tp, _Lp>
1126 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
1127 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
1129 // The seemingly equivalent code:
1130 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1131 // will eventually result in undefined behaviour, attempting to
1132 // delete the same object twice.
1133 /// dynamic_pointer_cast
1134 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1135 inline __shared_ptr<_Tp, _Lp>
1136 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
1138 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1139 return __shared_ptr<_Tp, _Lp>(__r, __p);
1140 return __shared_ptr<_Tp, _Lp>();
1144 template<typename _Tp, _Lock_policy _Lp>
1145 class __weak_ptr
1147 public:
1148 typedef _Tp element_type;
1150 constexpr __weak_ptr()
1151 : _M_ptr(0), _M_refcount() // never throws
1154 // Generated copy constructor, assignment, destructor are fine.
1156 // The "obvious" converting constructor implementation:
1158 // template<typename _Tp1>
1159 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1160 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1161 // { }
1163 // has a serious problem.
1165 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1166 // conversion may require access to *__r._M_ptr (virtual inheritance).
1168 // It is not possible to avoid spurious access violations since
1169 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1170 template<typename _Tp1, typename = typename
1171 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1172 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1173 : _M_refcount(__r._M_refcount) // never throws
1174 { _M_ptr = __r.lock().get(); }
1176 template<typename _Tp1, typename = typename
1177 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1178 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
1179 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1182 template<typename _Tp1>
1183 __weak_ptr&
1184 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
1186 _M_ptr = __r.lock().get();
1187 _M_refcount = __r._M_refcount;
1188 return *this;
1191 template<typename _Tp1>
1192 __weak_ptr&
1193 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
1195 _M_ptr = __r._M_ptr;
1196 _M_refcount = __r._M_refcount;
1197 return *this;
1200 __shared_ptr<_Tp, _Lp>
1201 lock() const // never throws
1203 #ifdef __GTHREADS
1204 // Optimization: avoid throw overhead.
1205 if (expired())
1206 return __shared_ptr<element_type, _Lp>();
1208 __try
1210 return __shared_ptr<element_type, _Lp>(*this);
1212 __catch(const bad_weak_ptr&)
1214 // Q: How can we get here?
1215 // A: Another thread may have invalidated r after the
1216 // use_count test above.
1217 return __shared_ptr<element_type, _Lp>();
1220 #else
1221 // Optimization: avoid try/catch overhead when single threaded.
1222 return expired() ? __shared_ptr<element_type, _Lp>()
1223 : __shared_ptr<element_type, _Lp>(*this);
1225 #endif
1226 } // XXX MT
1228 long
1229 use_count() const // never throws
1230 { return _M_refcount._M_get_use_count(); }
1232 bool
1233 expired() const // never throws
1234 { return _M_refcount._M_get_use_count() == 0; }
1236 template<typename _Tp1>
1237 bool
1238 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1239 { return _M_refcount._M_less(__rhs._M_refcount); }
1241 template<typename _Tp1>
1242 bool
1243 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1244 { return _M_refcount._M_less(__rhs._M_refcount); }
1246 void
1247 reset() // never throws
1248 { __weak_ptr().swap(*this); }
1250 void
1251 swap(__weak_ptr& __s) // never throws
1253 std::swap(_M_ptr, __s._M_ptr);
1254 _M_refcount._M_swap(__s._M_refcount);
1257 private:
1258 // Used by __enable_shared_from_this.
1259 void
1260 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
1262 _M_ptr = __ptr;
1263 _M_refcount = __refcount;
1266 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1267 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1268 friend class __enable_shared_from_this<_Tp, _Lp>;
1269 friend class enable_shared_from_this<_Tp>;
1271 _Tp* _M_ptr; // Contained pointer.
1272 __weak_count<_Lp> _M_refcount; // Reference counter.
1275 // 20.8.13.3.7 weak_ptr specialized algorithms.
1276 template<typename _Tp, _Lock_policy _Lp>
1277 inline void
1278 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1279 { __a.swap(__b); }
1281 template<typename _Tp, typename _Tp1>
1282 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1284 bool
1285 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1286 { return __lhs.owner_before(__rhs); }
1288 bool
1289 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1290 { return __lhs.owner_before(__rhs); }
1292 bool
1293 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1294 { return __lhs.owner_before(__rhs); }
1297 template<typename _Tp, _Lock_policy _Lp>
1298 struct owner_less<__shared_ptr<_Tp, _Lp>>
1299 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1300 { };
1302 template<typename _Tp, _Lock_policy _Lp>
1303 struct owner_less<__weak_ptr<_Tp, _Lp>>
1304 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1305 { };
1308 template<typename _Tp, _Lock_policy _Lp>
1309 class __enable_shared_from_this
1311 protected:
1312 constexpr __enable_shared_from_this() { }
1314 __enable_shared_from_this(const __enable_shared_from_this&) { }
1316 __enable_shared_from_this&
1317 operator=(const __enable_shared_from_this&)
1318 { return *this; }
1320 ~__enable_shared_from_this() { }
1322 public:
1323 __shared_ptr<_Tp, _Lp>
1324 shared_from_this()
1325 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1327 __shared_ptr<const _Tp, _Lp>
1328 shared_from_this() const
1329 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1331 private:
1332 template<typename _Tp1>
1333 void
1334 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
1335 { _M_weak_this._M_assign(__p, __n); }
1337 template<typename _Tp1>
1338 friend void
1339 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1340 const __enable_shared_from_this* __pe,
1341 const _Tp1* __px)
1343 if (__pe != 0)
1344 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1347 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1351 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1352 inline __shared_ptr<_Tp, _Lp>
1353 __allocate_shared(_Alloc __a, _Args&&... __args)
1355 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(),
1356 std::forward<_Alloc>(__a), std::forward<_Args>(__args)...);
1359 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1360 inline __shared_ptr<_Tp, _Lp>
1361 __make_shared(_Args&&... __args)
1363 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1364 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1365 std::forward<_Args>(__args)...);
1368 /// std::hash specialization for __shared_ptr.
1369 template<typename _Tp, _Lock_policy _Lp>
1370 struct hash<__shared_ptr<_Tp, _Lp>>
1371 : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t>
1373 size_t
1374 operator()(const __shared_ptr<_Tp, _Lp>& __s) const
1375 { return std::hash<_Tp*>()(__s.get()); }
1378 _GLIBCXX_END_NAMESPACE
1380 #endif // _SHARED_PTR_BASE_H