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_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>
364 friend shared_ptr<_Yp>
368 shared_ptr(
const weak_ptr<_Tp>& __r, std::nothrow_t)
369 : __shared_ptr<_Tp>(__r,
std::nothrow) { }
371 friend class weak_ptr<_Tp>;
374 #if __cpp_deduction_guides >= 201606 375 template<
typename _Tp>
376 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
377 template<
typename _Tp,
typename _Del>
378 shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
382 template<
typename _Tp,
typename _Up>
384 operator==(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
385 {
return __a.get() == __b.get(); }
387 template<
typename _Tp>
389 operator==(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
392 template<
typename _Tp>
394 operator==(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
397 template<
typename _Tp,
typename _Up>
399 operator!=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
400 {
return __a.get() != __b.get(); }
402 template<
typename _Tp>
404 operator!=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
405 {
return (
bool)__a; }
407 template<
typename _Tp>
409 operator!=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
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;
418 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::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;
427 return less<_Tp_elt*>()(__a.get(),
nullptr);
430 template<
typename _Tp>
432 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
434 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
435 return less<_Tp_elt*>()(
nullptr, __a.get());
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>
455 operator>(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
456 {
return (__b < __a); }
458 template<
typename _Tp>
460 operator>(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
461 {
return nullptr < __a; }
463 template<
typename _Tp>
465 operator>(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
466 {
return __a <
nullptr; }
468 template<
typename _Tp,
typename _Up>
470 operator>=(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
471 {
return !(__a < __b); }
473 template<
typename _Tp>
475 operator>=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
476 {
return !(__a <
nullptr); }
478 template<
typename _Tp>
480 operator>=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
481 {
return !(
nullptr < __a); }
483 template<
typename _Tp>
484 struct less<shared_ptr<_Tp>> :
public _Sp_less<shared_ptr<_Tp>>
488 template<
typename _Tp>
490 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
494 template<
typename _Tp,
typename _Up>
495 inline shared_ptr<_Tp>
496 static_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
498 using _Sp = shared_ptr<_Tp>;
499 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
502 template<
typename _Tp,
typename _Up>
503 inline shared_ptr<_Tp>
504 const_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
506 using _Sp = shared_ptr<_Tp>;
507 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
510 template<
typename _Tp,
typename _Up>
511 inline shared_ptr<_Tp>
512 dynamic_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
514 using _Sp = shared_ptr<_Tp>;
515 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
516 return _Sp(__r, __p);
520 #if __cplusplus > 201402L 521 template<
typename _Tp,
typename _Up>
522 inline shared_ptr<_Tp>
523 reinterpret_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
525 using _Sp = shared_ptr<_Tp>;
526 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
535 template<
typename _Tp>
538 template<
typename _Arg>
539 using _Constructible =
typename enable_if<
543 template<
typename _Arg>
549 constexpr
weak_ptr() noexcept =
default;
551 template<
typename _Yp,
552 typename = _Constructible<const shared_ptr<_Yp>&>>
554 : __weak_ptr<_Tp>(__r) { }
558 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
560 : __weak_ptr<_Tp>(__r) { }
564 template<
typename _Yp,
typename = _Constructible<weak_ptr<_Yp>>>
566 : __weak_ptr<_Tp>(std::move(__r)) { }
569 operator=(
const weak_ptr& __r) noexcept =
default;
571 template<
typename _Yp>
572 _Assignable<const weak_ptr<_Yp>&>
575 this->__weak_ptr<_Tp>::operator=(__r);
579 template<
typename _Yp>
580 _Assignable<const shared_ptr<_Yp>&>
583 this->__weak_ptr<_Tp>::operator=(__r);
588 operator=(
weak_ptr&& __r) noexcept =
default;
590 template<
typename _Yp>
591 _Assignable<weak_ptr<_Yp>>
594 this->__weak_ptr<_Tp>::operator=(std::move(__r));
599 lock()
const noexcept
603 #if __cpp_deduction_guides >= 201606 604 template<
typename _Tp>
609 template<
typename _Tp>
616 template<
typename _Tp =
void>
625 template<
typename _Tp>
627 :
public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
631 template<
typename _Tp>
633 :
public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
639 template<
typename _Tp>
659 shared_from_this()
const 662 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 663 #define __cpp_lib_enable_shared_from_this 201603 665 weak_from_this() noexcept
666 {
return this->_M_weak_this; }
669 weak_from_this()
const noexcept
670 {
return this->_M_weak_this; }
674 template<
typename _Tp1>
676 _M_weak_assign(_Tp1* __p,
const __shared_count<>& __n)
const noexcept
677 { _M_weak_this._M_assign(__p, __n); }
681 __enable_shared_from_this_base(
const __shared_count<>&,
685 template<
typename, _Lock_policy>
686 friend class __shared_ptr;
702 template<
typename _Tp,
typename _Alloc,
typename... _Args>
707 std::forward<_Args>(__args)...);
717 template<
typename _Tp,
typename... _Args>
718 inline shared_ptr<_Tp>
721 typedef typename std::remove_const<_Tp>::type _Tp_nc;
723 std::forward<_Args>(__args)...);
727 template<
typename _Tp>
729 :
public __hash_base<size_t, shared_ptr<_Tp>>
740 _GLIBCXX_END_NAMESPACE_VERSION
743 #endif // _SHARED_PTR_H
shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
A smart pointer with weak semantics.
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns a null pointer and the deleter __d.
Define a member typedef type only if a boolean constant is true.
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
Primary class template hash.
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.
shared_ptr(_Yp *__p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
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...
shared_ptr(const shared_ptr< _Yp > &__r, element_type *__p) noexcept
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
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.
ISO C++ entities toplevel namespace is std.
The standard allocator, as per [20.4].
shared_ptr(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer and the deleter __d.
shared_ptr(shared_ptr< _Yp > &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
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(_Yp *__p, _Deleter __d)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
20.7.1.2 unique_ptr for single objects.
constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
A simple smart pointer providing strict ownership semantics.
Primary template owner_less.
A smart pointer with reference-counted copy semantics.
Template class basic_ostream.