1 // shared_ptr and weak_ptr implementation -*- C++ -*-
3 // Copyright (C) 2007-2015 Free Software Foundation, Inc.
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)
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.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
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.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
50 #define _SHARED_PTR_H 1
52 #include <bits/shared_ptr_base.h>
54 namespace std
_GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @addtogroup pointer_abstractions
63 /// 20.7.2.2.11 shared_ptr I/O
64 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
65 inline std::basic_ostream
<_Ch
, _Tr
>&
66 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
67 const __shared_ptr
<_Tp
, _Lp
>& __p
)
73 /// 20.7.2.2.10 shared_ptr get_deleter
74 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
76 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
) noexcept
79 return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
)));
87 * @brief A smart pointer with reference-counted copy semantics.
89 * The object pointed to is deleted when the last shared_ptr pointing to
90 * it is destroyed or reset.
92 template<typename _Tp
>
93 class shared_ptr
: public __shared_ptr
<_Tp
>
95 template<typename _Ptr
>
97 = typename enable_if
<is_convertible
<_Ptr
, _Tp
*>::value
>::type
;
101 * @brief Construct an empty %shared_ptr.
102 * @post use_count()==0 && get()==0
104 constexpr shared_ptr() noexcept
105 : __shared_ptr
<_Tp
>() { }
107 shared_ptr(const shared_ptr
&) noexcept
= default;
110 * @brief Construct a %shared_ptr that owns the pointer @a __p.
111 * @param __p A pointer that is convertible to element_type*.
112 * @post use_count() == 1 && get() == __p
113 * @throw std::bad_alloc, in which case @c delete @a __p is called.
115 template<typename _Tp1
>
116 explicit shared_ptr(_Tp1
* __p
)
117 : __shared_ptr
<_Tp
>(__p
) { }
120 * @brief Construct a %shared_ptr that owns the pointer @a __p
121 * and the deleter @a __d.
122 * @param __p A pointer.
123 * @param __d A deleter.
124 * @post use_count() == 1 && get() == __p
125 * @throw std::bad_alloc, in which case @a __d(__p) is called.
127 * Requirements: _Deleter's copy constructor and destructor must
130 * __shared_ptr will release __p by calling __d(__p)
132 template<typename _Tp1
, typename _Deleter
>
133 shared_ptr(_Tp1
* __p
, _Deleter __d
)
134 : __shared_ptr
<_Tp
>(__p
, __d
) { }
137 * @brief Construct a %shared_ptr that owns a null pointer
138 * and the deleter @a __d.
139 * @param __p A null pointer constant.
140 * @param __d A deleter.
141 * @post use_count() == 1 && get() == __p
142 * @throw std::bad_alloc, in which case @a __d(__p) is called.
144 * Requirements: _Deleter's copy constructor and destructor must
147 * The last owner will call __d(__p)
149 template<typename _Deleter
>
150 shared_ptr(nullptr_t __p
, _Deleter __d
)
151 : __shared_ptr
<_Tp
>(__p
, __d
) { }
154 * @brief Construct a %shared_ptr that owns the pointer @a __p
155 * and the deleter @a __d.
156 * @param __p A pointer.
157 * @param __d A deleter.
158 * @param __a An allocator.
159 * @post use_count() == 1 && get() == __p
160 * @throw std::bad_alloc, in which case @a __d(__p) is called.
162 * Requirements: _Deleter's copy constructor and destructor must
163 * not throw _Alloc's copy constructor and destructor must not
166 * __shared_ptr will release __p by calling __d(__p)
168 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
169 shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
170 : __shared_ptr
<_Tp
>(__p
, __d
, std::move(__a
)) { }
173 * @brief Construct a %shared_ptr that owns a null pointer
174 * and the deleter @a __d.
175 * @param __p A null pointer constant.
176 * @param __d A deleter.
177 * @param __a An allocator.
178 * @post use_count() == 1 && get() == __p
179 * @throw std::bad_alloc, in which case @a __d(__p) is called.
181 * Requirements: _Deleter's copy constructor and destructor must
182 * not throw _Alloc's copy constructor and destructor must not
185 * The last owner will call __d(__p)
187 template<typename _Deleter
, typename _Alloc
>
188 shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
189 : __shared_ptr
<_Tp
>(__p
, __d
, std::move(__a
)) { }
191 // Aliasing constructor
194 * @brief Constructs a %shared_ptr instance that stores @a __p
195 * and shares ownership with @a __r.
196 * @param __r A %shared_ptr.
197 * @param __p A pointer that will remain valid while @a *__r is valid.
198 * @post get() == __p && use_count() == __r.use_count()
200 * This can be used to construct a @c shared_ptr to a sub-object
201 * of an object managed by an existing @c shared_ptr.
204 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
205 * shared_ptr<int> pi(pii, &pii->first);
206 * assert(pii.use_count() == 2);
209 template<typename _Tp1
>
210 shared_ptr(const shared_ptr
<_Tp1
>& __r
, _Tp
* __p
) noexcept
211 : __shared_ptr
<_Tp
>(__r
, __p
) { }
214 * @brief If @a __r is empty, constructs an empty %shared_ptr;
215 * otherwise construct a %shared_ptr that shares ownership
217 * @param __r A %shared_ptr.
218 * @post get() == __r.get() && use_count() == __r.use_count()
220 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
221 shared_ptr(const shared_ptr
<_Tp1
>& __r
) noexcept
222 : __shared_ptr
<_Tp
>(__r
) { }
225 * @brief Move-constructs a %shared_ptr instance from @a __r.
226 * @param __r A %shared_ptr rvalue.
227 * @post *this contains the old value of @a __r, @a __r is empty.
229 shared_ptr(shared_ptr
&& __r
) noexcept
230 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
233 * @brief Move-constructs a %shared_ptr instance from @a __r.
234 * @param __r A %shared_ptr rvalue.
235 * @post *this contains the old value of @a __r, @a __r is empty.
237 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
238 shared_ptr(shared_ptr
<_Tp1
>&& __r
) noexcept
239 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
242 * @brief Constructs a %shared_ptr that shares ownership with @a __r
243 * and stores a copy of the pointer stored in @a __r.
244 * @param __r A weak_ptr.
245 * @post use_count() == __r.use_count()
246 * @throw bad_weak_ptr when __r.expired(),
247 * in which case the constructor has no effect.
249 template<typename _Tp1
>
250 explicit shared_ptr(const weak_ptr
<_Tp1
>& __r
)
251 : __shared_ptr
<_Tp
>(__r
) { }
253 #if _GLIBCXX_USE_DEPRECATED
254 template<typename _Tp1
>
255 shared_ptr(std::auto_ptr
<_Tp1
>&& __r
);
258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
259 // 2399. shared_ptr's constructor from unique_ptr should be constrained
260 template<typename _Tp1
, typename _Del
, typename
261 = _Convertible
<typename unique_ptr
<_Tp1
, _Del
>::pointer
>>
262 shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
263 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
266 * @brief Construct an empty %shared_ptr.
267 * @post use_count() == 0 && get() == nullptr
269 constexpr shared_ptr(nullptr_t
) noexcept
: shared_ptr() { }
271 shared_ptr
& operator=(const shared_ptr
&) noexcept
= default;
273 template<typename _Tp1
>
275 operator=(const shared_ptr
<_Tp1
>& __r
) noexcept
277 this->__shared_ptr
<_Tp
>::operator=(__r
);
281 #if _GLIBCXX_USE_DEPRECATED
282 template<typename _Tp1
>
284 operator=(std::auto_ptr
<_Tp1
>&& __r
)
286 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
292 operator=(shared_ptr
&& __r
) noexcept
294 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
300 operator=(shared_ptr
<_Tp1
>&& __r
) noexcept
302 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
306 template<typename _Tp1
, typename _Del
>
308 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
310 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
315 // This constructor is non-standard, it is used by allocate_shared.
316 template<typename _Alloc
, typename
... _Args
>
317 shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
319 : __shared_ptr
<_Tp
>(__tag
, __a
, std::forward
<_Args
>(__args
)...)
322 template<typename _Tp1
, typename _Alloc
, typename
... _Args
>
323 friend shared_ptr
<_Tp1
>
324 allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
326 // This constructor is non-standard, it is used by weak_ptr::lock().
327 shared_ptr(const weak_ptr
<_Tp
>& __r
, std::nothrow_t
)
328 : __shared_ptr
<_Tp
>(__r
, std::nothrow
) { }
330 friend class weak_ptr
<_Tp
>;
333 // 20.7.2.2.7 shared_ptr comparisons
334 template<typename _Tp1
, typename _Tp2
>
336 operator==(const shared_ptr
<_Tp1
>& __a
,
337 const shared_ptr
<_Tp2
>& __b
) noexcept
338 { return __a
.get() == __b
.get(); }
340 template<typename _Tp
>
342 operator==(const shared_ptr
<_Tp
>& __a
, nullptr_t
) noexcept
345 template<typename _Tp
>
347 operator==(nullptr_t
, const shared_ptr
<_Tp
>& __a
) noexcept
350 template<typename _Tp1
, typename _Tp2
>
352 operator!=(const shared_ptr
<_Tp1
>& __a
,
353 const shared_ptr
<_Tp2
>& __b
) noexcept
354 { return __a
.get() != __b
.get(); }
356 template<typename _Tp
>
358 operator!=(const shared_ptr
<_Tp
>& __a
, nullptr_t
) noexcept
359 { return (bool)__a
; }
361 template<typename _Tp
>
363 operator!=(nullptr_t
, const shared_ptr
<_Tp
>& __a
) noexcept
364 { return (bool)__a
; }
366 template<typename _Tp1
, typename _Tp2
>
368 operator<(const shared_ptr
<_Tp1
>& __a
,
369 const shared_ptr
<_Tp2
>& __b
) noexcept
371 typedef typename
std::common_type
<_Tp1
*, _Tp2
*>::type _CT
;
372 return std::less
<_CT
>()(__a
.get(), __b
.get());
375 template<typename _Tp
>
377 operator<(const shared_ptr
<_Tp
>& __a
, nullptr_t
) noexcept
378 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
380 template<typename _Tp
>
382 operator<(nullptr_t
, const shared_ptr
<_Tp
>& __a
) noexcept
383 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
385 template<typename _Tp1
, typename _Tp2
>
387 operator<=(const shared_ptr
<_Tp1
>& __a
,
388 const shared_ptr
<_Tp2
>& __b
) noexcept
389 { return !(__b
< __a
); }
391 template<typename _Tp
>
393 operator<=(const shared_ptr
<_Tp
>& __a
, nullptr_t
) noexcept
394 { return !(nullptr < __a
); }
396 template<typename _Tp
>
398 operator<=(nullptr_t
, const shared_ptr
<_Tp
>& __a
) noexcept
399 { return !(__a
< nullptr); }
401 template<typename _Tp1
, typename _Tp2
>
403 operator>(const shared_ptr
<_Tp1
>& __a
,
404 const shared_ptr
<_Tp2
>& __b
) noexcept
405 { return (__b
< __a
); }
407 template<typename _Tp
>
409 operator>(const shared_ptr
<_Tp
>& __a
, nullptr_t
) noexcept
410 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
412 template<typename _Tp
>
414 operator>(nullptr_t
, const shared_ptr
<_Tp
>& __a
) noexcept
415 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
417 template<typename _Tp1
, typename _Tp2
>
419 operator>=(const shared_ptr
<_Tp1
>& __a
,
420 const shared_ptr
<_Tp2
>& __b
) noexcept
421 { return !(__a
< __b
); }
423 template<typename _Tp
>
425 operator>=(const shared_ptr
<_Tp
>& __a
, nullptr_t
) noexcept
426 { return !(__a
< nullptr); }
428 template<typename _Tp
>
430 operator>=(nullptr_t
, const shared_ptr
<_Tp
>& __a
) noexcept
431 { return !(nullptr < __a
); }
433 template<typename _Tp
>
434 struct less
<shared_ptr
<_Tp
>> : public _Sp_less
<shared_ptr
<_Tp
>>
437 // 20.7.2.2.8 shared_ptr specialized algorithms.
438 template<typename _Tp
>
440 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>& __b
) noexcept
443 // 20.7.2.2.9 shared_ptr casts.
444 template<typename _Tp
, typename _Tp1
>
445 inline shared_ptr
<_Tp
>
446 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
) noexcept
447 { return shared_ptr
<_Tp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
449 template<typename _Tp
, typename _Tp1
>
450 inline shared_ptr
<_Tp
>
451 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
) noexcept
452 { return shared_ptr
<_Tp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
454 template<typename _Tp
, typename _Tp1
>
455 inline shared_ptr
<_Tp
>
456 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
) noexcept
458 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
459 return shared_ptr
<_Tp
>(__r
, __p
);
460 return shared_ptr
<_Tp
>();
465 * @brief A smart pointer with weak semantics.
467 * With forwarding constructors and assignment operators.
469 template<typename _Tp
>
470 class weak_ptr
: public __weak_ptr
<_Tp
>
472 template<typename _Ptr
>
474 = typename enable_if
<is_convertible
<_Ptr
, _Tp
*>::value
>::type
;
477 constexpr weak_ptr() noexcept
= default;
479 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
480 weak_ptr(const shared_ptr
<_Tp1
>& __r
) noexcept
481 : __weak_ptr
<_Tp
>(__r
) { }
483 weak_ptr(const weak_ptr
&) noexcept
= default;
485 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
486 weak_ptr(const weak_ptr
<_Tp1
>& __r
) noexcept
487 : __weak_ptr
<_Tp
>(__r
) { }
489 weak_ptr(weak_ptr
&&) noexcept
= default;
491 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
492 weak_ptr(weak_ptr
<_Tp1
>&& __r
) noexcept
493 : __weak_ptr
<_Tp
>(std::move(__r
)) { }
496 operator=(const weak_ptr
& __r
) noexcept
= default;
498 template<typename _Tp1
>
500 operator=(const weak_ptr
<_Tp1
>& __r
) noexcept
502 this->__weak_ptr
<_Tp
>::operator=(__r
);
506 template<typename _Tp1
>
508 operator=(const shared_ptr
<_Tp1
>& __r
) noexcept
510 this->__weak_ptr
<_Tp
>::operator=(__r
);
515 operator=(weak_ptr
&& __r
) noexcept
= default;
517 template<typename _Tp1
>
519 operator=(weak_ptr
<_Tp1
>&& __r
) noexcept
521 this->__weak_ptr
<_Tp
>::operator=(std::move(__r
));
526 lock() const noexcept
527 { return shared_ptr
<_Tp
>(*this, std::nothrow
); }
530 // 20.7.2.3.6 weak_ptr specialized algorithms.
531 template<typename _Tp
>
533 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
) noexcept
537 /// Primary template owner_less
538 template<typename _Tp
>
541 /// Partial specialization of owner_less for shared_ptr.
542 template<typename _Tp
>
543 struct owner_less
<shared_ptr
<_Tp
>>
544 : public _Sp_owner_less
<shared_ptr
<_Tp
>, weak_ptr
<_Tp
>>
547 /// Partial specialization of owner_less for weak_ptr.
548 template<typename _Tp
>
549 struct owner_less
<weak_ptr
<_Tp
>>
550 : public _Sp_owner_less
<weak_ptr
<_Tp
>, shared_ptr
<_Tp
>>
554 * @brief Base class allowing use of member function shared_from_this.
556 template<typename _Tp
>
557 class enable_shared_from_this
560 constexpr enable_shared_from_this() noexcept
{ }
562 enable_shared_from_this(const enable_shared_from_this
&) noexcept
{ }
564 enable_shared_from_this
&
565 operator=(const enable_shared_from_this
&) noexcept
568 ~enable_shared_from_this() { }
573 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
575 shared_ptr
<const _Tp
>
576 shared_from_this() const
577 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
580 template<typename _Tp1
>
582 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const noexcept
583 { _M_weak_this
._M_assign(__p
, __n
); }
585 template<typename _Tp1
>
587 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
588 const enable_shared_from_this
* __pe
,
589 const _Tp1
* __px
) noexcept
592 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
595 mutable weak_ptr
<_Tp
> _M_weak_this
;
599 * @brief Create an object that is owned by a shared_ptr.
600 * @param __a An allocator.
601 * @param __args Arguments for the @a _Tp object's constructor.
602 * @return A shared_ptr that owns the newly created object.
603 * @throw An exception thrown from @a _Alloc::allocate or from the
604 * constructor of @a _Tp.
606 * A copy of @a __a will be used to allocate memory for the shared_ptr
607 * and the new object.
609 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
610 inline shared_ptr
<_Tp
>
611 allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
613 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), __a
,
614 std::forward
<_Args
>(__args
)...);
618 * @brief Create an object that is owned by a shared_ptr.
619 * @param __args Arguments for the @a _Tp object's constructor.
620 * @return A shared_ptr that owns the newly created object.
621 * @throw std::bad_alloc, or an exception thrown from the
622 * constructor of @a _Tp.
624 template<typename _Tp
, typename
... _Args
>
625 inline shared_ptr
<_Tp
>
626 make_shared(_Args
&&... __args
)
628 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
629 return std::allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
630 std::forward
<_Args
>(__args
)...);
633 /// std::hash specialization for shared_ptr.
634 template<typename _Tp
>
635 struct hash
<shared_ptr
<_Tp
>>
636 : public __hash_base
<size_t, shared_ptr
<_Tp
>>
639 operator()(const shared_ptr
<_Tp
>& __s
) const noexcept
640 { return std::hash
<_Tp
*>()(__s
.get()); }
643 // @} group pointer_abstractions
645 _GLIBCXX_END_NAMESPACE_VERSION
648 #endif // _SHARED_PTR_H