50 #define _SHARED_PTR_H 1 54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<
typename _Ch,
typename _Tr,
typename _Tp, _Lock_policy _Lp>
66 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
67 const __shared_ptr<_Tp, _Lp>& __p)
73 template<
typename _Del,
typename _Tp, _Lock_policy _Lp>
75 get_deleter(
const __shared_ptr<_Tp, _Lp>& __p) noexcept
78 return static_cast<_Del*
>(__p._M_get_deleter(
typeid(_Del)));
85 template<
typename _Del,
typename _Tp>
90 return static_cast<_Del*
>(__p._M_get_deleter(
typeid(_Del)));
102 template<
typename _Tp>
105 template<
typename... _Args>
106 using _Constructible =
typename enable_if<
110 template<
typename _Arg>
117 using element_type =
typename __shared_ptr<_Tp>::element_type;
119 #if __cplusplus > 201402L 120 # define __cpp_lib_shared_ptr_weak_type 201606 137 template<
typename _Yp,
typename = _Constructible<_Yp*>>
154 template<
typename _Yp,
typename _Deleter,
155 typename = _Constructible<_Yp*, _Deleter>>
157 : __shared_ptr<_Tp>(__p,
std::move(__d)) { }
172 template<
typename _Deleter>
174 : __shared_ptr<_Tp>(__p,
std::move(__d)) { }
191 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
192 typename = _Constructible<_Yp*, _Deleter, _Alloc>>
194 : __shared_ptr<_Tp>(__p,
std::move(__d),
std::move(__a)) { }
211 template<
typename _Deleter,
typename _Alloc>
213 : __shared_ptr<_Tp>(__p,
std::move(__d),
std::move(__a)) { }
233 template<
typename _Yp>
235 : __shared_ptr<_Tp>(__r, __p) { }
244 template<
typename _Yp,
245 typename = _Constructible<const shared_ptr<_Yp>&>>
247 : __shared_ptr<_Tp>(__r) { }
255 : __shared_ptr<_Tp>(std::move(__r)) { }
262 template<
typename _Yp,
typename = _Constructible<shared_ptr<_Yp>>>
264 : __shared_ptr<_Tp>(std::move(__r)) { }
274 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
276 : __shared_ptr<_Tp>(__r) { }
278 #if _GLIBCXX_USE_DEPRECATED 279 #pragma GCC diagnostic push 280 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 281 template<
typename _Yp,
typename = _Constructible<auto_ptr<_Yp>>>
283 #pragma GCC diagnostic pop 288 template<
typename _Yp,
typename _Del,
289 typename = _Constructible<unique_ptr<_Yp, _Del>>>
291 : __shared_ptr<_Tp>(
std::move(__r)) { }
293 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED 297 template<
typename _Yp,
typename _Del,
298 _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
300 : __shared_ptr<_Tp>(
std::move(__r), __sp_array_delete()) { }
311 template<
typename _Yp>
312 _Assignable<const shared_ptr<_Yp>&>
315 this->__shared_ptr<_Tp>::operator=(__r);
319 #if _GLIBCXX_USE_DEPRECATED 320 #pragma GCC diagnostic push 321 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 322 template<
typename _Yp>
323 _Assignable<auto_ptr<_Yp>>
324 operator=(auto_ptr<_Yp>&& __r)
326 this->__shared_ptr<_Tp>::operator=(std::move(__r));
329 #pragma GCC diagnostic pop 335 this->__shared_ptr<_Tp>::operator=(std::move(__r));
340 _Assignable<shared_ptr<_Yp>>
341 operator=(shared_ptr<_Yp>&& __r) noexcept
343 this->__shared_ptr<_Tp>::operator=(std::move(__r));
347 template<
typename _Yp,
typename _Del>
348 _Assignable<unique_ptr<_Yp, _Del>>
349 operator=(unique_ptr<_Yp, _Del>&& __r)
351 this->__shared_ptr<_Tp>::operator=(std::move(__r));
357 template<
typename _Alloc,
typename... _Args>
358 shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
359 : __shared_ptr<_Tp>(__tag,
std::
forward<_Args>(__args)...)
362 template<
typename _Yp,
typename _Alloc,
typename... _Args>
363 friend shared_ptr<_Yp>
367 shared_ptr(
const weak_ptr<_Tp>& __r, std::nothrow_t)
368 : __shared_ptr<_Tp>(__r,
std::nothrow) { }
370 friend class weak_ptr<_Tp>;
373 #if __cpp_deduction_guides >= 201606 374 template<
typename _Tp>
375 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
376 template<
typename _Tp,
typename _Del>
377 shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
381 template<
typename _Tp,
typename _Up>
382 _GLIBCXX_NODISCARD
inline bool 383 operator==(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
384 {
return __a.get() == __b.get(); }
386 template<
typename _Tp>
387 _GLIBCXX_NODISCARD
inline bool 388 operator==(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
391 template<
typename _Tp>
392 _GLIBCXX_NODISCARD
inline bool 393 operator==(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
396 template<
typename _Tp,
typename _Up>
397 _GLIBCXX_NODISCARD
inline bool 398 operator!=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
399 {
return __a.get() != __b.get(); }
401 template<
typename _Tp>
402 _GLIBCXX_NODISCARD
inline bool 403 operator!=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
404 {
return (
bool)__a; }
406 template<
typename _Tp>
407 _GLIBCXX_NODISCARD
inline bool 408 operator!=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
409 {
return (
bool)__a; }
411 template<
typename _Tp,
typename _Up>
412 _GLIBCXX_NODISCARD
inline bool 413 operator<(const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
415 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
416 using _Up_elt =
typename shared_ptr<_Up>::element_type;
417 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
418 return less<_Vp>()(__a.get(), __b.get());
421 template<
typename _Tp>
422 _GLIBCXX_NODISCARD
inline bool 423 operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
425 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
426 return less<_Tp_elt*>()(__a.get(),
nullptr);
429 template<
typename _Tp>
430 _GLIBCXX_NODISCARD
inline bool 431 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
433 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
434 return less<_Tp_elt*>()(
nullptr, __a.get());
437 template<
typename _Tp,
typename _Up>
438 _GLIBCXX_NODISCARD
inline bool 439 operator<=(const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
440 {
return !(__b < __a); }
442 template<
typename _Tp>
443 _GLIBCXX_NODISCARD
inline bool 444 operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
445 {
return !(
nullptr < __a); }
447 template<
typename _Tp>
448 _GLIBCXX_NODISCARD
inline bool 449 operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
450 {
return !(__a <
nullptr); }
452 template<
typename _Tp,
typename _Up>
453 _GLIBCXX_NODISCARD
inline bool 454 operator>(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
455 {
return (__b < __a); }
457 template<
typename _Tp>
458 _GLIBCXX_NODISCARD
inline bool 459 operator>(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
460 {
return nullptr < __a; }
462 template<
typename _Tp>
463 _GLIBCXX_NODISCARD
inline bool 464 operator>(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
465 {
return __a <
nullptr; }
467 template<
typename _Tp,
typename _Up>
468 _GLIBCXX_NODISCARD
inline bool 469 operator>=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
470 {
return !(__a < __b); }
472 template<
typename _Tp>
473 _GLIBCXX_NODISCARD
inline bool 474 operator>=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
475 {
return !(__a <
nullptr); }
477 template<
typename _Tp>
478 _GLIBCXX_NODISCARD
inline bool 479 operator>=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
480 {
return !(
nullptr < __a); }
483 template<
typename _Tp>
485 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
489 template<
typename _Tp,
typename _Up>
490 inline shared_ptr<_Tp>
491 static_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
493 using _Sp = shared_ptr<_Tp>;
494 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
497 template<
typename _Tp,
typename _Up>
498 inline shared_ptr<_Tp>
499 const_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
501 using _Sp = shared_ptr<_Tp>;
502 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
505 template<
typename _Tp,
typename _Up>
506 inline shared_ptr<_Tp>
507 dynamic_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
509 using _Sp = shared_ptr<_Tp>;
510 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
511 return _Sp(__r, __p);
515 #if __cplusplus > 201402L 516 template<
typename _Tp,
typename _Up>
517 inline shared_ptr<_Tp>
518 reinterpret_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
520 using _Sp = shared_ptr<_Tp>;
521 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
530 template<
typename _Tp>
533 template<
typename _Arg>
534 using _Constructible =
typename enable_if<
538 template<
typename _Arg>
544 constexpr
weak_ptr() noexcept =
default;
546 template<
typename _Yp,
547 typename = _Constructible<const shared_ptr<_Yp>&>>
549 : __weak_ptr<_Tp>(__r) { }
553 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
555 : __weak_ptr<_Tp>(__r) { }
559 template<
typename _Yp,
typename = _Constructible<weak_ptr<_Yp>>>
561 : __weak_ptr<_Tp>(std::move(__r)) { }
564 operator=(
const weak_ptr& __r) noexcept =
default;
566 template<
typename _Yp>
567 _Assignable<const weak_ptr<_Yp>&>
570 this->__weak_ptr<_Tp>::operator=(__r);
574 template<
typename _Yp>
575 _Assignable<const shared_ptr<_Yp>&>
578 this->__weak_ptr<_Tp>::operator=(__r);
583 operator=(
weak_ptr&& __r) noexcept =
default;
585 template<
typename _Yp>
586 _Assignable<weak_ptr<_Yp>>
589 this->__weak_ptr<_Tp>::operator=(std::move(__r));
594 lock()
const noexcept
598 #if __cpp_deduction_guides >= 201606 599 template<
typename _Tp>
604 template<
typename _Tp>
611 template<
typename _Tp =
void>
620 template<
typename _Tp>
622 :
public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
626 template<
typename _Tp>
628 :
public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
634 template<
typename _Tp>
654 shared_from_this()
const 657 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 658 #define __cpp_lib_enable_shared_from_this 201603 660 weak_from_this() noexcept
661 {
return this->_M_weak_this; }
664 weak_from_this()
const noexcept
665 {
return this->_M_weak_this; }
669 template<
typename _Tp1>
671 _M_weak_assign(_Tp1* __p,
const __shared_count<>& __n)
const noexcept
672 { _M_weak_this._M_assign(__p, __n); }
676 __enable_shared_from_this_base(
const __shared_count<>&,
680 template<
typename, _Lock_policy>
681 friend class __shared_ptr;
697 template<
typename _Tp,
typename _Alloc,
typename... _Args>
702 std::forward<_Args>(__args)...);
712 template<
typename _Tp,
typename... _Args>
713 inline shared_ptr<_Tp>
716 typedef typename std::remove_cv<_Tp>::type _Tp_nc;
718 std::forward<_Args>(__args)...);
722 template<
typename _Tp>
724 :
public __hash_base<size_t, shared_ptr<_Tp>>
735 #if __cplusplus >= 201703L 736 namespace __detail::__variant
738 template<
typename>
struct _Never_valueless_alt;
742 template<
typename _Tp>
749 template<
typename _Tp>
750 struct _Never_valueless_alt<
std::weak_ptr<_Tp>>
756 _GLIBCXX_END_NAMESPACE_VERSION
759 #endif // _SHARED_PTR_H shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
Base class allowing use of member function shared_from_this.
friend shared_ptr< _Yp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
ISO C++ entities toplevel namespace is std.
A smart pointer with reference-counted copy semantics.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
Template class basic_ostream.
The standard allocator, as per [20.4].
shared_ptr(_Yp *__p, _Deleter __d)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
shared_ptr(_Yp *__p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
Primary class template hash.
A simple smart pointer providing strict ownership semantics.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
shared_ptr(shared_ptr< _Yp > &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
shared_ptr(const shared_ptr< _Yp > &__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
shared_ptr(const weak_ptr< _Yp > &__r)
Constructs a shared_ptr that shares ownership with __r and stores a copy of the pointer stored in __r...
shared_ptr(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer and the deleter __d.
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns a null pointer and the deleter __d.
shared_ptr(_Yp *__p)
Construct a shared_ptr that owns the pointer __p.
Primary template owner_less.
shared_ptr(const shared_ptr< _Yp > &__r) noexcept
If __r is empty, constructs an empty shared_ptr; otherwise construct a shared_ptr that shares ownersh...
20.7.1.2 unique_ptr for single objects.
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
A smart pointer with weak semantics.
Define a member typedef type only if a boolean constant is true.