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()) { }
309 shared_ptr& operator=(
const shared_ptr&) noexcept =
default;
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>>
326 this->__shared_ptr<_Tp>::operator=(std::move(__r));
329 #pragma GCC diagnostic pop 333 operator=(shared_ptr&& __r) noexcept
335 this->__shared_ptr<_Tp>::operator=(std::move(__r));
340 _Assignable<shared_ptr<_Yp>>
343 this->__shared_ptr<_Tp>::operator=(std::move(__r));
347 template<
typename _Yp,
typename _Del>
348 _Assignable<unique_ptr<_Yp, _Del>>
351 this->__shared_ptr<_Tp>::operator=(std::move(__r));
357 template<
typename _Alloc,
typename... _Args>
358 shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
360 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
363 template<
typename _Yp,
typename _Alloc,
typename... _Args>
369 : __shared_ptr<_Tp>(__r, std::nothrow) { }
374 #if __cpp_deduction_guides >= 201606 375 template<
typename _Tp>
377 template<
typename _Tp,
typename _Del>
382 template<
typename _Tp,
typename _Up>
385 {
return __a.get() == __b.get(); }
387 template<
typename _Tp>
392 template<
typename _Tp>
397 template<
typename _Tp,
typename _Up>
400 {
return __a.get() != __b.get(); }
402 template<
typename _Tp>
405 {
return (
bool)__a; }
407 template<
typename _Tp>
410 {
return (
bool)__a; }
412 template<
typename _Tp,
typename _Up>
414 operator<(const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
416 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
417 using _Up_elt =
typename shared_ptr<_Up>::element_type;
419 return less<_Vp>()(__a.get(), __b.get());
422 template<
typename _Tp>
424 operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
426 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
430 template<
typename _Tp>
432 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
434 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
438 template<
typename _Tp,
typename _Up>
440 operator<=(const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
441 {
return !(__b < __a); }
443 template<
typename _Tp>
445 operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
446 {
return !(
nullptr < __a); }
448 template<
typename _Tp>
450 operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
451 {
return !(__a <
nullptr); }
453 template<
typename _Tp,
typename _Up>
456 {
return (__b < __a); }
458 template<
typename _Tp>
461 {
return nullptr < __a; }
463 template<
typename _Tp>
466 {
return __a <
nullptr; }
468 template<
typename _Tp,
typename _Up>
471 {
return !(__a < __b); }
473 template<
typename _Tp>
476 {
return !(__a <
nullptr); }
478 template<
typename _Tp>
481 {
return !(
nullptr < __a); }
484 template<
typename _Tp>
490 template<
typename _Tp,
typename _Up>
495 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
498 template<
typename _Tp,
typename _Up>
503 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
506 template<
typename _Tp,
typename _Up>
511 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
512 return _Sp(__r, __p);
516 #if __cplusplus > 201402L 517 template<
typename _Tp,
typename _Up>
522 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
531 template<
typename _Tp>
534 template<
typename _Arg>
535 using _Constructible =
typename enable_if<
539 template<
typename _Arg>
545 constexpr
weak_ptr() noexcept =
default;
547 template<
typename _Yp,
548 typename = _Constructible<const shared_ptr<_Yp>&>>
550 : __weak_ptr<_Tp>(__r) { }
554 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
556 : __weak_ptr<_Tp>(__r) { }
560 template<
typename _Yp,
typename = _Constructible<weak_ptr<_Yp>>>
562 : __weak_ptr<_Tp>(std::move(__r)) { }
565 operator=(
const weak_ptr& __r) noexcept =
default;
567 template<
typename _Yp>
568 _Assignable<const weak_ptr<_Yp>&>
571 this->__weak_ptr<_Tp>::operator=(__r);
575 template<
typename _Yp>
576 _Assignable<const shared_ptr<_Yp>&>
579 this->__weak_ptr<_Tp>::operator=(__r);
584 operator=(
weak_ptr&& __r) noexcept =
default;
586 template<
typename _Yp>
587 _Assignable<weak_ptr<_Yp>>
590 this->__weak_ptr<_Tp>::operator=(std::move(__r));
595 lock()
const noexcept
599 #if __cpp_deduction_guides >= 201606 600 template<
typename _Tp>
605 template<
typename _Tp>
612 template<
typename _Tp =
void>
621 template<
typename _Tp>
623 :
public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
627 template<
typename _Tp>
629 :
public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
635 template<
typename _Tp>
655 shared_from_this()
const 658 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 659 #define __cpp_lib_enable_shared_from_this 201603 661 weak_from_this() noexcept
662 {
return this->_M_weak_this; }
665 weak_from_this()
const noexcept
666 {
return this->_M_weak_this; }
670 template<
typename _Tp1>
672 _M_weak_assign(_Tp1* __p,
const __shared_count<>& __n)
const noexcept
673 { _M_weak_this._M_assign(__p, __n); }
677 __enable_shared_from_this_base(
const __shared_count<>&,
681 template<
typename, _Lock_policy>
682 friend class __shared_ptr;
698 template<
typename _Tp,
typename _Alloc,
typename... _Args>
703 std::forward<_Args>(__args)...);
713 template<
typename _Tp,
typename... _Args>
717 typedef typename std::remove_cv<_Tp>::type _Tp_nc;
719 std::forward<_Args>(__args)...);
723 template<
typename _Tp>
725 :
public __hash_base<size_t, shared_ptr<_Tp>>
736 _GLIBCXX_END_NAMESPACE_VERSION
739 #endif // _SHARED_PTR_H constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
Define a member typedef type only if a boolean constant is true.
shared_ptr(_Yp *__p)
Construct a shared_ptr that owns the pointer __p.
shared_ptr(_Yp *__p, _Deleter __d)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
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...
Primary template owner_less.
Primary class template hash.
ISO C++ entities toplevel namespace is std.
One of the comparison functors.
The standard allocator, as per [20.4].
shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
20.7.1.2 unique_ptr for single objects.
A smart pointer with weak semantics.
shared_ptr(const shared_ptr< _Yp > &__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
Template class basic_ostream.
shared_ptr(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer and the deleter __d.
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
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, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
friend shared_ptr< _Yp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
A simple smart pointer providing strict ownership semantics.
A smart pointer with reference-counted copy semantics.
shared_ptr(shared_ptr< _Yp > &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
Base class allowing use of member function shared_from_this.
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...