Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
blob0a69d2b2411b6ed9c74e209c1510075494f86b5b
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 // Forward declarations.
55 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
56 class __shared_ptr;
58 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
59 class __weak_ptr;
61 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
62 class __enable_shared_from_this;
64 template<typename _Tp>
65 class shared_ptr;
67 template<typename _Tp>
68 class weak_ptr;
70 template<typename _Tp>
71 struct owner_less;
73 template<typename _Tp>
74 class enable_shared_from_this;
76 template<_Lock_policy _Lp = __default_lock_policy>
77 class __weak_count;
79 template<_Lock_policy _Lp = __default_lock_policy>
80 class __shared_count;
83 // Counted ptr with no deleter or allocator support
84 template<typename _Ptr, _Lock_policy _Lp>
85 class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
87 public:
88 explicit
89 _Sp_counted_ptr(_Ptr __p)
90 : _M_ptr(__p) { }
92 virtual void
93 _M_dispose() // nothrow
94 { delete _M_ptr; }
96 virtual void
97 _M_destroy() // nothrow
98 { delete this; }
100 virtual void*
101 _M_get_deleter(const std::type_info&)
102 { return 0; }
104 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
105 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
107 protected:
108 _Ptr _M_ptr; // copy constructor must not throw
111 template<>
112 inline void
113 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() { }
115 template<>
116 inline void
117 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() { }
119 template<>
120 inline void
121 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() { }
123 // Support for custom deleter and/or allocator
124 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
125 class _Sp_counted_deleter : public _Sp_counted_ptr<_Ptr, _Lp>
127 typedef typename _Alloc::template
128 rebind<_Sp_counted_deleter>::other _My_alloc_type;
130 // Helper class that stores the Deleter and also acts as an allocator.
131 // Used to dispose of the owned pointer and the internal refcount
132 // Requires that copies of _Alloc can free each other's memory.
133 struct _My_Deleter
134 : public _My_alloc_type // copy constructor must not throw
136 _Deleter _M_del; // copy constructor must not throw
137 _My_Deleter(_Deleter __d, const _Alloc& __a)
138 : _My_alloc_type(__a), _M_del(__d) { }
141 protected:
142 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
144 public:
145 // __d(__p) must not throw.
146 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
147 : _Base_type(__p), _M_del(__d, _Alloc()) { }
149 // __d(__p) must not throw.
150 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
151 : _Base_type(__p), _M_del(__d, __a) { }
153 virtual void
154 _M_dispose() // nothrow
155 { _M_del._M_del(_Base_type::_M_ptr); }
157 virtual void
158 _M_destroy() // nothrow
160 _My_alloc_type __a(_M_del);
161 this->~_Sp_counted_deleter();
162 __a.deallocate(this, 1);
165 virtual void*
166 _M_get_deleter(const std::type_info& __ti)
168 #ifdef __GXX_RTTI
169 return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
170 #else
171 return 0;
172 #endif
175 protected:
176 _My_Deleter _M_del; // copy constructor must not throw
179 // helpers for make_shared / allocate_shared
181 template<typename _Tp>
182 struct _Sp_destroy_inplace
184 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
187 struct _Sp_make_shared_tag { };
189 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
190 class _Sp_counted_ptr_inplace
191 : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
193 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
194 _Base_type;
196 public:
197 explicit
198 _Sp_counted_ptr_inplace(_Alloc __a)
199 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
200 , _M_storage()
202 void* __p = &_M_storage;
203 ::new (__p) _Tp(); // might throw
204 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
207 template<typename... _Args>
208 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
209 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
210 , _M_storage()
212 void* __p = &_M_storage;
213 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
214 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
217 // Override because the allocator needs to know the dynamic type
218 virtual void
219 _M_destroy() // nothrow
221 typedef typename _Alloc::template
222 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
223 _My_alloc_type __a(_Base_type::_M_del);
224 this->~_Sp_counted_ptr_inplace();
225 __a.deallocate(this, 1);
228 // Sneaky trick so __shared_ptr can get the managed pointer
229 virtual void*
230 _M_get_deleter(const std::type_info& __ti)
232 #ifdef __GXX_RTTI
233 return __ti == typeid(_Sp_make_shared_tag)
234 ? static_cast<void*>(&_M_storage)
235 : _Base_type::_M_get_deleter(__ti);
236 #else
237 return 0;
238 #endif
241 private:
242 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
243 _M_storage;
246 template<_Lock_policy _Lp>
247 class __shared_count
249 public:
250 __shared_count() : _M_pi(0) // nothrow
253 template<typename _Ptr>
254 explicit
255 __shared_count(_Ptr __p) : _M_pi(0)
257 __try
259 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
261 __catch(...)
263 delete __p;
264 __throw_exception_again;
268 template<typename _Ptr, typename _Deleter>
269 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
271 // The allocator's value_type doesn't matter, will rebind it anyway.
272 typedef std::allocator<int> _Alloc;
273 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
274 typedef std::allocator<_Sp_cd_type> _Alloc2;
275 _Alloc2 __a2;
276 __try
278 _M_pi = __a2.allocate(1);
279 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
281 __catch(...)
283 __d(__p); // Call _Deleter on __p.
284 if (_M_pi)
285 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
286 __throw_exception_again;
290 template<typename _Ptr, typename _Deleter, typename _Alloc>
291 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
293 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
294 typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
295 _Alloc2 __a2(__a);
296 __try
298 _M_pi = __a2.allocate(1);
299 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
301 __catch(...)
303 __d(__p); // Call _Deleter on __p.
304 if (_M_pi)
305 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
306 __throw_exception_again;
310 template<typename _Tp, typename _Alloc, typename... _Args>
311 __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
312 : _M_pi(0)
314 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
315 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
316 _Alloc2 __a2(__a);
317 __try
319 _M_pi = __a2.allocate(1);
320 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
321 std::forward<_Args>(__args)...);
323 __catch(...)
325 if (_M_pi)
326 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
327 __throw_exception_again;
331 #if _GLIBCXX_DEPRECATED
332 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
333 template<typename _Tp>
334 explicit
335 __shared_count(std::auto_ptr<_Tp>&& __r)
336 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
337 { __r.release(); }
338 #endif
340 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
341 template<typename _Tp, typename _Del>
342 explicit
343 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
344 : _M_pi(_S_create_from_up(std::move(__r)))
345 { __r.release(); }
347 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
348 explicit __shared_count(const __weak_count<_Lp>& __r);
350 ~__shared_count() // nothrow
352 if (_M_pi != 0)
353 _M_pi->_M_release();
356 __shared_count(const __shared_count& __r)
357 : _M_pi(__r._M_pi) // nothrow
359 if (_M_pi != 0)
360 _M_pi->_M_add_ref_copy();
363 __shared_count&
364 operator=(const __shared_count& __r) // nothrow
366 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
367 if (__tmp != _M_pi)
369 if (__tmp != 0)
370 __tmp->_M_add_ref_copy();
371 if (_M_pi != 0)
372 _M_pi->_M_release();
373 _M_pi = __tmp;
375 return *this;
378 void
379 _M_swap(__shared_count& __r) // nothrow
381 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
382 __r._M_pi = _M_pi;
383 _M_pi = __tmp;
386 long
387 _M_get_use_count() const // nothrow
388 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
390 bool
391 _M_unique() const // nothrow
392 { return this->_M_get_use_count() == 1; }
394 void*
395 _M_get_deleter(const std::type_info& __ti) const
396 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
398 bool
399 _M_less(const __shared_count& __rhs) const
400 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
402 bool
403 _M_less(const __weak_count<_Lp>& __rhs) const
404 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
406 // Friend function injected into enclosing namespace and found by ADL
407 friend inline bool
408 operator==(const __shared_count& __a, const __shared_count& __b)
409 { return __a._M_pi == __b._M_pi; }
411 private:
412 friend class __weak_count<_Lp>;
414 template<typename _Tp, typename _Del>
415 static _Sp_counted_base<_Lp>*
416 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
417 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
419 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
420 _Lp>(__r.get(), __r.get_deleter());
423 template<typename _Tp, typename _Del>
424 static _Sp_counted_base<_Lp>*
425 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
426 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
428 typedef typename std::remove_reference<_Del>::type _Del1;
429 typedef std::reference_wrapper<_Del1> _Del2;
430 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
431 _Lp>(__r.get(), std::ref(__r.get_deleter()));
434 _Sp_counted_base<_Lp>* _M_pi;
438 template<_Lock_policy _Lp>
439 class __weak_count
441 public:
442 __weak_count() : _M_pi(0) // nothrow
445 __weak_count(const __shared_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
447 if (_M_pi != 0)
448 _M_pi->_M_weak_add_ref();
451 __weak_count(const __weak_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
453 if (_M_pi != 0)
454 _M_pi->_M_weak_add_ref();
457 ~__weak_count() // nothrow
459 if (_M_pi != 0)
460 _M_pi->_M_weak_release();
463 __weak_count<_Lp>&
464 operator=(const __shared_count<_Lp>& __r) // nothrow
466 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
467 if (__tmp != 0)
468 __tmp->_M_weak_add_ref();
469 if (_M_pi != 0)
470 _M_pi->_M_weak_release();
471 _M_pi = __tmp;
472 return *this;
475 __weak_count<_Lp>&
476 operator=(const __weak_count<_Lp>& __r) // nothrow
478 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
479 if (__tmp != 0)
480 __tmp->_M_weak_add_ref();
481 if (_M_pi != 0)
482 _M_pi->_M_weak_release();
483 _M_pi = __tmp;
484 return *this;
487 void
488 _M_swap(__weak_count<_Lp>& __r) // nothrow
490 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
491 __r._M_pi = _M_pi;
492 _M_pi = __tmp;
495 long
496 _M_get_use_count() const // nothrow
497 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
499 bool
500 _M_less(const __weak_count& __rhs) const
501 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
503 bool
504 _M_less(const __shared_count<_Lp>& __rhs) const
505 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
507 // Friend function injected into enclosing namespace and found by ADL
508 friend inline bool
509 operator==(const __weak_count& __a, const __weak_count& __b)
510 { return __a._M_pi == __b._M_pi; }
512 private:
513 friend class __shared_count<_Lp>;
515 _Sp_counted_base<_Lp>* _M_pi;
518 // Now that __weak_count is defined we can define this constructor:
519 template<_Lock_policy _Lp>
520 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
521 : _M_pi(__r._M_pi)
523 if (_M_pi != 0)
524 _M_pi->_M_add_ref_lock();
525 else
526 __throw_bad_weak_ptr();
530 // Support for enable_shared_from_this.
532 // Friend of __enable_shared_from_this.
533 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
534 void
535 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
536 const __enable_shared_from_this<_Tp1,
537 _Lp>*, const _Tp2*);
539 // Friend of enable_shared_from_this.
540 template<typename _Tp1, typename _Tp2>
541 void
542 __enable_shared_from_this_helper(const __shared_count<>&,
543 const enable_shared_from_this<_Tp1>*,
544 const _Tp2*);
546 template<_Lock_policy _Lp>
547 inline void
548 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
552 template<typename _Tp, _Lock_policy _Lp>
553 class __shared_ptr
555 public:
556 typedef _Tp element_type;
558 __shared_ptr() : _M_ptr(0), _M_refcount() // never throws
561 template<typename _Tp1>
562 explicit __shared_ptr(_Tp1* __p) : _M_ptr(__p), _M_refcount(__p)
564 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
565 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
566 __enable_shared_from_this_helper(_M_refcount, __p, __p);
569 template<typename _Tp1, typename _Deleter>
570 __shared_ptr(_Tp1* __p, _Deleter __d)
571 : _M_ptr(__p), _M_refcount(__p, __d)
573 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
574 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
575 __enable_shared_from_this_helper(_M_refcount, __p, __p);
578 template<typename _Tp1, typename _Deleter, typename _Alloc>
579 __shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
580 : _M_ptr(__p), _M_refcount(__p, __d, __a)
582 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
583 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
584 __enable_shared_from_this_helper(_M_refcount, __p, __p);
587 template<typename _Deleter>
588 __shared_ptr(nullptr_t __p, _Deleter __d)
589 : _M_ptr(0), _M_refcount(__p, __d)
592 template<typename _Deleter, typename _Alloc>
593 __shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
594 : _M_ptr(0), _M_refcount(__p, __d, __a)
597 template<typename _Tp1>
598 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
599 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
602 // generated copy constructor, assignment, destructor are fine.
604 template<typename _Tp1>
605 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
606 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
607 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
609 __shared_ptr(__shared_ptr&& __r)
610 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
612 _M_refcount._M_swap(__r._M_refcount);
613 __r._M_ptr = 0;
616 template<typename _Tp1>
617 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
618 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
620 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
621 _M_refcount._M_swap(__r._M_refcount);
622 __r._M_ptr = 0;
625 template<typename _Tp1>
626 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
627 : _M_refcount(__r._M_refcount) // may throw
629 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
631 // It is now safe to copy __r._M_ptr, as
632 // _M_refcount(__r._M_refcount) did not throw.
633 _M_ptr = __r._M_ptr;
636 // If an exception is thrown this constructor has no effect.
637 template<typename _Tp1, typename _Del>
638 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
639 : _M_ptr(__r.get()), _M_refcount()
641 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
642 _Tp1* __tmp = __r.get();
643 _M_refcount = __shared_count<_Lp>(std::move(__r));
644 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
647 #if _GLIBCXX_DEPRECATED
648 // Postcondition: use_count() == 1 and __r.get() == 0
649 template<typename _Tp1>
650 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
651 : _M_ptr(__r.get()), _M_refcount()
653 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
654 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
655 _Tp1* __tmp = __r.get();
656 _M_refcount = __shared_count<_Lp>(std::move(__r));
657 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
659 #endif
661 /* TODO: use delegating constructor */
662 __shared_ptr(nullptr_t) : _M_ptr(0), _M_refcount() // never throws
665 template<typename _Tp1>
666 __shared_ptr&
667 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
669 _M_ptr = __r._M_ptr;
670 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
671 return *this;
674 #if _GLIBCXX_DEPRECATED
675 template<typename _Tp1>
676 __shared_ptr&
677 operator=(std::auto_ptr<_Tp1>&& __r)
679 __shared_ptr(std::move(__r)).swap(*this);
680 return *this;
682 #endif
684 __shared_ptr&
685 operator=(__shared_ptr&& __r)
687 __shared_ptr(std::move(__r)).swap(*this);
688 return *this;
691 template<class _Tp1>
692 __shared_ptr&
693 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
695 __shared_ptr(std::move(__r)).swap(*this);
696 return *this;
699 template<typename _Tp1, typename _Del>
700 __shared_ptr&
701 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
703 __shared_ptr(std::move(__r)).swap(*this);
704 return *this;
707 void
708 reset() // never throws
709 { __shared_ptr().swap(*this); }
711 template<typename _Tp1>
712 void
713 reset(_Tp1* __p) // _Tp1 must be complete.
715 // Catch self-reset errors.
716 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
717 __shared_ptr(__p).swap(*this);
720 template<typename _Tp1, typename _Deleter>
721 void
722 reset(_Tp1* __p, _Deleter __d)
723 { __shared_ptr(__p, __d).swap(*this); }
725 template<typename _Tp1, typename _Deleter, typename _Alloc>
726 void
727 reset(_Tp1* __p, _Deleter __d, const _Alloc& __a)
728 { __shared_ptr(__p, __d, __a).swap(*this); }
730 // Allow class instantiation when _Tp is [cv-qual] void.
731 typename std::add_lvalue_reference<_Tp>::type
732 operator*() const // never throws
734 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
735 return *_M_ptr;
738 _Tp*
739 operator->() const // never throws
741 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
742 return _M_ptr;
745 _Tp*
746 get() const // never throws
747 { return _M_ptr; }
749 explicit operator bool() const // never throws
750 { return _M_ptr == 0 ? false : true; }
752 bool
753 unique() const // never throws
754 { return _M_refcount._M_unique(); }
756 long
757 use_count() const // never throws
758 { return _M_refcount._M_get_use_count(); }
760 void
761 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
763 std::swap(_M_ptr, __other._M_ptr);
764 _M_refcount._M_swap(__other._M_refcount);
767 template<typename _Tp1>
768 bool
769 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
770 { return _M_refcount._M_less(__rhs._M_refcount); }
772 template<typename _Tp1>
773 bool
774 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
775 { return _M_refcount._M_less(__rhs._M_refcount); }
777 #ifdef __GXX_RTTI
778 protected:
779 // This constructor is non-standard, it is used by allocate_shared.
780 template<typename _Alloc, typename... _Args>
781 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
782 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
783 std::forward<_Args>(__args)...)
785 // _M_ptr needs to point to the newly constructed object.
786 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
787 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
788 _M_ptr = static_cast<_Tp*>(__p);
789 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
791 #else
792 template<typename _Alloc>
793 struct _Deleter
795 void operator()(_Tp* __ptr)
797 _M_alloc.destroy(__ptr);
798 _M_alloc.deallocate(__ptr, 1);
800 _Alloc _M_alloc;
803 template<typename _Alloc, typename... _Args>
804 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
805 : _M_ptr(), _M_refcount()
807 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
808 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
809 _M_ptr = __del._M_alloc.allocate(1);
810 __try
812 __del._M_alloc.construct(_M_ptr, std::forward<_Args>(__args)...);
814 __catch(...)
816 __del._M_alloc.deallocate(_M_ptr, 1);
817 __throw_exception_again;
819 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
820 _M_refcount._M_swap(__count);
821 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
823 #endif
825 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
826 typename... _Args>
827 friend __shared_ptr<_Tp1, _Lp1>
828 __allocate_shared(_Alloc __a, _Args&&... __args);
830 private:
831 void*
832 _M_get_deleter(const std::type_info& __ti) const
833 { return _M_refcount._M_get_deleter(__ti); }
835 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
836 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
838 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
839 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
841 _Tp* _M_ptr; // Contained pointer.
842 __shared_count<_Lp> _M_refcount; // Reference counter.
846 // 20.8.13.2.7 shared_ptr comparisons
847 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
848 inline bool
849 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
850 const __shared_ptr<_Tp2, _Lp>& __b)
851 { return __a.get() == __b.get(); }
853 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
854 inline bool
855 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
856 const __shared_ptr<_Tp2, _Lp>& __b)
857 { return __a.get() != __b.get(); }
859 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
860 inline bool
861 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
862 const __shared_ptr<_Tp2, _Lp>& __b)
863 { return __a.get() < __b.get(); }
865 template<typename _Sp>
866 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
868 bool
869 operator()(const _Sp& __lhs, const _Sp& __rhs) const
871 typedef typename _Sp::element_type element_type;
872 return std::less<element_type*>()(__lhs.get(), __rhs.get());
876 template<typename _Tp, _Lock_policy _Lp>
877 struct less<__shared_ptr<_Tp, _Lp>>
878 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
879 { };
881 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
882 template<typename _Tp, _Lock_policy _Lp>
883 inline bool
884 operator>(const __shared_ptr<_Tp, _Lp>& __a,
885 const __shared_ptr<_Tp, _Lp>& __b)
886 { return __a.get() > __b.get(); }
888 template<typename _Tp, _Lock_policy _Lp>
889 inline bool
890 operator>=(const __shared_ptr<_Tp, _Lp>& __a,
891 const __shared_ptr<_Tp, _Lp>& __b)
892 { return __a.get() >= __b.get(); }
894 template<typename _Tp, _Lock_policy _Lp>
895 inline bool
896 operator<=(const __shared_ptr<_Tp, _Lp>& __a,
897 const __shared_ptr<_Tp, _Lp>& __b)
898 { return __a.get() <= __b.get(); }
900 // 2.2.3.8 shared_ptr specialized algorithms.
901 template<typename _Tp, _Lock_policy _Lp>
902 inline void
903 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
904 { __a.swap(__b); }
906 // 2.2.3.9 shared_ptr casts
908 // The seemingly equivalent code:
909 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
910 // will eventually result in undefined behaviour, attempting to
911 // delete the same object twice.
912 /// static_pointer_cast
913 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
914 inline __shared_ptr<_Tp, _Lp>
915 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
916 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
918 // The seemingly equivalent code:
919 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
920 // will eventually result in undefined behaviour, attempting to
921 // delete the same object twice.
922 /// const_pointer_cast
923 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
924 inline __shared_ptr<_Tp, _Lp>
925 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
926 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
928 // The seemingly equivalent code:
929 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
930 // will eventually result in undefined behaviour, attempting to
931 // delete the same object twice.
932 /// dynamic_pointer_cast
933 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
934 inline __shared_ptr<_Tp, _Lp>
935 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
937 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
938 return __shared_ptr<_Tp, _Lp>(__r, __p);
939 return __shared_ptr<_Tp, _Lp>();
943 template<typename _Tp, _Lock_policy _Lp>
944 class __weak_ptr
946 public:
947 typedef _Tp element_type;
949 __weak_ptr() : _M_ptr(0), _M_refcount() // never throws
952 // Generated copy constructor, assignment, destructor are fine.
954 // The "obvious" converting constructor implementation:
956 // template<typename _Tp1>
957 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
958 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
959 // { }
961 // has a serious problem.
963 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
964 // conversion may require access to *__r._M_ptr (virtual inheritance).
966 // It is not possible to avoid spurious access violations since
967 // in multithreaded programs __r._M_ptr may be invalidated at any point.
968 template<typename _Tp1>
969 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
970 : _M_refcount(__r._M_refcount) // never throws
972 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
973 _M_ptr = __r.lock().get();
976 template<typename _Tp1>
977 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
978 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
979 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
981 template<typename _Tp1>
982 __weak_ptr&
983 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
985 _M_ptr = __r.lock().get();
986 _M_refcount = __r._M_refcount;
987 return *this;
990 template<typename _Tp1>
991 __weak_ptr&
992 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
994 _M_ptr = __r._M_ptr;
995 _M_refcount = __r._M_refcount;
996 return *this;
999 __shared_ptr<_Tp, _Lp>
1000 lock() const // never throws
1002 #ifdef __GTHREADS
1003 // Optimization: avoid throw overhead.
1004 if (expired())
1005 return __shared_ptr<element_type, _Lp>();
1007 __try
1009 return __shared_ptr<element_type, _Lp>(*this);
1011 __catch(const bad_weak_ptr&)
1013 // Q: How can we get here?
1014 // A: Another thread may have invalidated r after the
1015 // use_count test above.
1016 return __shared_ptr<element_type, _Lp>();
1019 #else
1020 // Optimization: avoid try/catch overhead when single threaded.
1021 return expired() ? __shared_ptr<element_type, _Lp>()
1022 : __shared_ptr<element_type, _Lp>(*this);
1024 #endif
1025 } // XXX MT
1027 long
1028 use_count() const // never throws
1029 { return _M_refcount._M_get_use_count(); }
1031 bool
1032 expired() const // never throws
1033 { return _M_refcount._M_get_use_count() == 0; }
1035 template<typename _Tp1>
1036 bool
1037 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1038 { return _M_refcount._M_less(__rhs._M_refcount); }
1040 template<typename _Tp1>
1041 bool
1042 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1043 { return _M_refcount._M_less(__rhs._M_refcount); }
1045 void
1046 reset() // never throws
1047 { __weak_ptr().swap(*this); }
1049 void
1050 swap(__weak_ptr& __s) // never throws
1052 std::swap(_M_ptr, __s._M_ptr);
1053 _M_refcount._M_swap(__s._M_refcount);
1056 private:
1057 // Used by __enable_shared_from_this.
1058 void
1059 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
1061 _M_ptr = __ptr;
1062 _M_refcount = __refcount;
1065 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1066 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1067 friend class __enable_shared_from_this<_Tp, _Lp>;
1068 friend class enable_shared_from_this<_Tp>;
1070 _Tp* _M_ptr; // Contained pointer.
1071 __weak_count<_Lp> _M_refcount; // Reference counter.
1074 // 20.8.13.3.7 weak_ptr specialized algorithms.
1075 template<typename _Tp, _Lock_policy _Lp>
1076 inline void
1077 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1078 { __a.swap(__b); }
1080 template<typename _Tp, typename _Tp1>
1081 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1083 bool
1084 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1085 { return __lhs.owner_before(__rhs); }
1087 bool
1088 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1089 { return __lhs.owner_before(__rhs); }
1091 bool
1092 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1093 { return __lhs.owner_before(__rhs); }
1096 template<typename _Tp, _Lock_policy _Lp>
1097 struct owner_less<__shared_ptr<_Tp, _Lp>>
1098 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1099 { };
1101 template<typename _Tp, _Lock_policy _Lp>
1102 struct owner_less<__weak_ptr<_Tp, _Lp>>
1103 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1104 { };
1107 template<typename _Tp, _Lock_policy _Lp>
1108 class __enable_shared_from_this
1110 protected:
1111 __enable_shared_from_this() { }
1113 __enable_shared_from_this(const __enable_shared_from_this&) { }
1115 __enable_shared_from_this&
1116 operator=(const __enable_shared_from_this&)
1117 { return *this; }
1119 ~__enable_shared_from_this() { }
1121 public:
1122 __shared_ptr<_Tp, _Lp>
1123 shared_from_this()
1124 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1126 __shared_ptr<const _Tp, _Lp>
1127 shared_from_this() const
1128 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1130 private:
1131 template<typename _Tp1>
1132 void
1133 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
1134 { _M_weak_this._M_assign(__p, __n); }
1136 template<typename _Tp1>
1137 friend void
1138 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1139 const __enable_shared_from_this* __pe,
1140 const _Tp1* __px)
1142 if (__pe != 0)
1143 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1146 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1150 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1151 inline __shared_ptr<_Tp, _Lp>
1152 __allocate_shared(_Alloc __a, _Args&&... __args)
1154 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(),
1155 std::forward<_Alloc>(__a), std::forward<_Args>(__args)...);
1158 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1159 inline __shared_ptr<_Tp, _Lp>
1160 __make_shared(_Args&&... __args)
1162 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1163 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1164 std::forward<_Args>(__args)...);
1167 /// std::hash specialization for __shared_ptr.
1168 template<typename _Tp, _Lock_policy _Lp>
1169 struct hash<__shared_ptr<_Tp, _Lp>>
1170 : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t>
1172 size_t
1173 operator()(const __shared_ptr<_Tp, _Lp>& __s) const
1174 { return std::hash<_Tp*>()(__s.get()); }
1177 _GLIBCXX_END_NAMESPACE
1179 #endif // _SHARED_PTR_BASE_H