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>
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>
383 operator==(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
384 {
return __a.get() == __b.get(); }
386 template<
typename _Tp>
388 operator==(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
391 template<
typename _Tp>
393 operator==(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
396 template<
typename _Tp,
typename _Up>
398 operator!=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
399 {
return __a.get() != __b.get(); }
401 template<
typename _Tp>
403 operator!=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
404 {
return (
bool)__a; }
406 template<
typename _Tp>
408 operator!=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
409 {
return (
bool)__a; }
411 template<
typename _Tp,
typename _Up>
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>
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>
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>
439 operator<=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
440 {
return !(__b < __a); }
442 template<
typename _Tp>
444 operator<=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
445 {
return !(
nullptr < __a); }
447 template<
typename _Tp>
449 operator<=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
450 {
return !(__a <
nullptr); }
452 template<
typename _Tp,
typename _Up>
454 operator>(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
455 {
return (__b < __a); }
457 template<
typename _Tp>
459 operator>(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
460 {
return nullptr < __a; }
462 template<
typename _Tp>
464 operator>(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
465 {
return __a <
nullptr; }
467 template<
typename _Tp,
typename _Up>
469 operator>=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
470 {
return !(__a < __b); }
472 template<
typename _Tp>
474 operator>=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
475 {
return !(__a <
nullptr); }
477 template<
typename _Tp>
479 operator>=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
480 {
return !(
nullptr < __a); }
482 template<
typename _Tp>
483 struct less<shared_ptr<_Tp>> :
public _Sp_less<shared_ptr<_Tp>>
487 template<
typename _Tp>
489 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
493 template<
typename _Tp,
typename _Up>
494 inline shared_ptr<_Tp>
495 static_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
497 using _Sp = shared_ptr<_Tp>;
498 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
501 template<
typename _Tp,
typename _Up>
502 inline shared_ptr<_Tp>
503 const_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
505 using _Sp = shared_ptr<_Tp>;
506 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
509 template<
typename _Tp,
typename _Up>
510 inline shared_ptr<_Tp>
511 dynamic_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
513 using _Sp = shared_ptr<_Tp>;
514 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
515 return _Sp(__r, __p);
519 #if __cplusplus > 201402L 520 template<
typename _Tp,
typename _Up>
521 inline shared_ptr<_Tp>
522 reinterpret_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
524 using _Sp = shared_ptr<_Tp>;
525 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
534 template<
typename _Tp>
537 template<
typename _Arg>
538 using _Constructible =
typename enable_if<
542 template<
typename _Arg>
548 constexpr
weak_ptr() noexcept =
default;
550 template<
typename _Yp,
551 typename = _Constructible<const shared_ptr<_Yp>&>>
553 : __weak_ptr<_Tp>(__r) { }
557 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
559 : __weak_ptr<_Tp>(__r) { }
563 template<
typename _Yp,
typename = _Constructible<weak_ptr<_Yp>>>
565 : __weak_ptr<_Tp>(std::move(__r)) { }
568 operator=(
const weak_ptr& __r) noexcept =
default;
570 template<
typename _Yp>
571 _Assignable<const weak_ptr<_Yp>&>
574 this->__weak_ptr<_Tp>::operator=(__r);
578 template<
typename _Yp>
579 _Assignable<const shared_ptr<_Yp>&>
582 this->__weak_ptr<_Tp>::operator=(__r);
587 operator=(
weak_ptr&& __r) noexcept =
default;
589 template<
typename _Yp>
590 _Assignable<weak_ptr<_Yp>>
593 this->__weak_ptr<_Tp>::operator=(std::move(__r));
598 lock()
const noexcept
602 #if __cpp_deduction_guides >= 201606 603 template<
typename _Tp>
608 template<
typename _Tp>
615 template<
typename _Tp =
void>
624 template<
typename _Tp>
626 :
public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
630 template<
typename _Tp>
632 :
public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
638 template<
typename _Tp>
658 shared_from_this()
const 661 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 662 #define __cpp_lib_enable_shared_from_this 201603 664 weak_from_this() noexcept
665 {
return this->_M_weak_this; }
668 weak_from_this()
const noexcept
669 {
return this->_M_weak_this; }
673 template<
typename _Tp1>
675 _M_weak_assign(_Tp1* __p,
const __shared_count<>& __n)
const noexcept
676 { _M_weak_this._M_assign(__p, __n); }
680 __enable_shared_from_this_base(
const __shared_count<>&,
684 template<
typename, _Lock_policy>
685 friend class __shared_ptr;
701 template<
typename _Tp,
typename _Alloc,
typename... _Args>
706 std::forward<_Args>(__args)...);
716 template<
typename _Tp,
typename... _Args>
717 inline shared_ptr<_Tp>
720 typedef typename std::remove_cv<_Tp>::type _Tp_nc;
722 std::forward<_Args>(__args)...);
726 template<
typename _Tp>
728 :
public __hash_base<size_t, shared_ptr<_Tp>>
739 _GLIBCXX_END_NAMESPACE_VERSION
742 #endif // _SHARED_PTR_H friend shared_ptr< _Yp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
A smart pointer with weak semantics.
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
20.7.1.2 unique_ptr for single objects.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
A simple smart pointer providing strict ownership semantics.
The standard allocator, as per [20.4].
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.
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...
Define a member typedef type only if a boolean constant is true.
constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
Primary class template hash.
Primary template owner_less.
shared_ptr(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer and the deleter __d.
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...
ISO C++ entities toplevel namespace is std.
shared_ptr(_Yp *__p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
Template class basic_ostream.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
shared_ptr(const shared_ptr< _Yp > &__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
A smart pointer with reference-counted copy semantics.
shared_ptr(shared_ptr< _Yp > &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns a null pointer and the deleter __d.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
shared_ptr(_Yp *__p)
Construct a shared_ptr that owns the pointer __p.