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)
74 template<
typename _Del,
typename _Tp, _Lock_policy _Lp>
79 return static_cast<_Del*
>(__p._M_get_deleter(
typeid(_Del)));
92 template<
typename _Tp>
95 template<
typename... _Args>
96 using _Constructible =
typename enable_if<
97 is_constructible<__shared_ptr<_Tp>, _Args...>::value
100 template<
typename _Arg>
101 using _Assignable =
typename enable_if<
102 is_assignable<__shared_ptr<_Tp>&, _Arg>::value,
shared_ptr&
107 using element_type =
typename __shared_ptr<_Tp>::element_type;
109 #if __cplusplus > 201402L 110 # define __cpp_lib_shared_ptr_weak_type 201606 127 template<
typename _Yp,
typename = _Constructible<_Yp*>>
144 template<
typename _Yp,
typename _Deleter,
145 typename = _Constructible<_Yp*, _Deleter>>
147 : __shared_ptr<_Tp>(__p,
std::move(__d)) { }
162 template<
typename _Deleter>
164 : __shared_ptr<_Tp>(__p,
std::move(__d)) { }
181 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
182 typename = _Constructible<_Yp*, _Deleter, _Alloc>>
184 : __shared_ptr<_Tp>(__p,
std::move(__d),
std::move(__a)) { }
201 template<
typename _Deleter,
typename _Alloc>
203 : __shared_ptr<_Tp>(__p,
std::move(__d),
std::move(__a)) { }
223 template<
typename _Yp>
225 : __shared_ptr<_Tp>(__r, __p) { }
234 template<
typename _Yp,
235 typename = _Constructible<const shared_ptr<_Yp>&>>
237 : __shared_ptr<_Tp>(__r) { }
245 : __shared_ptr<_Tp>(std::move(__r)) { }
252 template<
typename _Yp,
typename = _Constructible<shared_ptr<_Yp>>>
254 : __shared_ptr<_Tp>(std::move(__r)) { }
264 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
266 : __shared_ptr<_Tp>(__r) { }
268 #if _GLIBCXX_USE_DEPRECATED 269 template<
typename _Yp,
typename = _Constructible<auto_ptr<_Yp>>>
275 template<
typename _Yp,
typename _Del,
276 typename = _Constructible<unique_ptr<_Yp, _Del>>>
278 : __shared_ptr<_Tp>(
std::move(__r)) { }
280 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED 284 template<
typename _Yp,
typename _Del,
285 _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
287 : __shared_ptr<_Tp>(
std::move(__r), __sp_array_delete()) { }
298 template<
typename _Yp>
299 _Assignable<const shared_ptr<_Yp>&>
302 this->__shared_ptr<_Tp>::operator=(__r);
306 #if _GLIBCXX_USE_DEPRECATED 307 template<
typename _Yp>
308 _Assignable<auto_ptr<_Yp>>
309 operator=(auto_ptr<_Yp>&& __r)
311 this->__shared_ptr<_Tp>::operator=(std::move(__r));
319 this->__shared_ptr<_Tp>::operator=(std::move(__r));
324 _Assignable<shared_ptr<_Yp>>
325 operator=(shared_ptr<_Yp>&& __r) noexcept
327 this->__shared_ptr<_Tp>::operator=(std::move(__r));
331 template<
typename _Yp,
typename _Del>
332 _Assignable<unique_ptr<_Yp, _Del>>
333 operator=(unique_ptr<_Yp, _Del>&& __r)
335 this->__shared_ptr<_Tp>::operator=(std::move(__r));
341 template<
typename _Alloc,
typename... _Args>
342 shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
344 : __shared_ptr<_Tp>(__tag, __a,
std::
forward<_Args>(__args)...)
347 template<
typename _Yp,
typename _Alloc,
typename... _Args>
348 friend shared_ptr<_Yp>
352 shared_ptr(
const weak_ptr<_Tp>& __r, std::nothrow_t)
353 : __shared_ptr<_Tp>(__r,
std::nothrow) { }
355 friend class weak_ptr<_Tp>;
358 #if __cpp_deduction_guides >= 201606 359 template<
typename _Tp>
360 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
361 template<
typename _Tp,
typename _Del>
362 shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
366 template<
typename _Tp,
typename _Up>
368 operator==(
const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
369 {
return __a.get() == __b.get(); }
371 template<
typename _Tp>
373 operator==(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
376 template<
typename _Tp>
378 operator==(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
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
389 {
return (
bool)__a; }
391 template<
typename _Tp>
393 operator!=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
394 {
return (
bool)__a; }
396 template<
typename _Tp,
typename _Up>
398 operator<(const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
400 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
401 using _Up_elt =
typename shared_ptr<_Up>::element_type;
402 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
403 return less<_Vp>()(__a.get(), __b.get());
406 template<
typename _Tp>
408 operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
410 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
411 return less<_Tp_elt*>()(__a.get(),
nullptr);
414 template<
typename _Tp>
416 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
418 using _Tp_elt =
typename shared_ptr<_Tp>::element_type;
419 return less<_Tp_elt*>()(
nullptr, __a.get());
422 template<
typename _Tp,
typename _Up>
424 operator<=(const shared_ptr<_Tp>& __a,
const shared_ptr<_Up>& __b) noexcept
425 {
return !(__b < __a); }
427 template<
typename _Tp>
429 operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
430 {
return !(
nullptr < __a); }
432 template<
typename _Tp>
434 operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
435 {
return !(__a <
nullptr); }
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 !(__a < __b); }
457 template<
typename _Tp>
459 operator>=(
const shared_ptr<_Tp>& __a, nullptr_t) noexcept
460 {
return !(__a <
nullptr); }
462 template<
typename _Tp>
464 operator>=(nullptr_t,
const shared_ptr<_Tp>& __a) noexcept
465 {
return !(
nullptr < __a); }
467 template<
typename _Tp>
468 struct less<shared_ptr<_Tp>> :
public _Sp_less<shared_ptr<_Tp>>
472 template<
typename _Tp>
474 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
478 template<
typename _Tp,
typename _Up>
479 inline shared_ptr<_Tp>
480 static_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
482 using _Sp = shared_ptr<_Tp>;
483 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
486 template<
typename _Tp,
typename _Up>
487 inline shared_ptr<_Tp>
488 const_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
490 using _Sp = shared_ptr<_Tp>;
491 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
494 template<
typename _Tp,
typename _Up>
495 inline shared_ptr<_Tp>
496 dynamic_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
498 using _Sp = shared_ptr<_Tp>;
499 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
500 return _Sp(__r, __p);
504 #if __cplusplus > 201402L 505 template<
typename _Tp,
typename _Up>
506 inline shared_ptr<_Tp>
507 reinterpret_pointer_cast(
const shared_ptr<_Up>& __r) noexcept
509 using _Sp = shared_ptr<_Tp>;
510 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
519 template<
typename _Tp>
522 template<
typename _Arg>
523 using _Constructible =
typename enable_if<
524 is_constructible<__weak_ptr<_Tp>, _Arg>::value
527 template<
typename _Arg>
528 using _Assignable =
typename enable_if<
529 is_assignable<__weak_ptr<_Tp>&, _Arg>::value,
weak_ptr&
533 constexpr
weak_ptr() noexcept =
default;
535 template<
typename _Yp,
536 typename = _Constructible<const shared_ptr<_Yp>&>>
538 : __weak_ptr<_Tp>(__r) { }
542 template<
typename _Yp,
typename = _Constructible<const weak_ptr<_Yp>&>>
544 : __weak_ptr<_Tp>(__r) { }
548 template<
typename _Yp,
typename = _Constructible<weak_ptr<_Yp>>>
550 : __weak_ptr<_Tp>(std::move(__r)) { }
553 operator=(
const weak_ptr& __r) noexcept =
default;
555 template<
typename _Yp>
556 _Assignable<const weak_ptr<_Yp>&>
559 this->__weak_ptr<_Tp>::operator=(__r);
563 template<
typename _Yp>
564 _Assignable<const shared_ptr<_Yp>&>
567 this->__weak_ptr<_Tp>::operator=(__r);
572 operator=(
weak_ptr&& __r) noexcept =
default;
574 template<
typename _Yp>
575 _Assignable<weak_ptr<_Yp>>
578 this->__weak_ptr<_Tp>::operator=(std::move(__r));
583 lock()
const noexcept
587 #if __cpp_deduction_guides >= 201606 588 template<
typename _Tp>
593 template<
typename _Tp>
600 template<
typename _Tp =
void>
609 template<
typename _Tp>
611 :
public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
615 template<
typename _Tp>
617 :
public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
623 template<
typename _Tp>
643 shared_from_this()
const 646 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 647 #define __cpp_lib_enable_shared_from_this 201603 649 weak_from_this() noexcept
650 {
return this->_M_weak_this; }
653 weak_from_this()
const noexcept
654 {
return this->_M_weak_this; }
658 template<
typename _Tp1>
660 _M_weak_assign(_Tp1* __p,
const __shared_count<>& __n)
const noexcept
661 { _M_weak_this._M_assign(__p, __n); }
665 __enable_shared_from_this_base(
const __shared_count<>&,
669 template<
typename, _Lock_policy>
670 friend class __shared_ptr;
686 template<
typename _Tp,
typename _Alloc,
typename... _Args>
691 std::forward<_Args>(__args)...);
701 template<
typename _Tp,
typename... _Args>
702 inline shared_ptr<_Tp>
705 typedef typename std::remove_const<_Tp>::type _Tp_nc;
707 std::forward<_Args>(__args)...);
711 template<
typename _Tp>
713 :
public __hash_base<size_t, shared_ptr<_Tp>>
724 _GLIBCXX_END_NAMESPACE_VERSION
727 #endif // _SHARED_PTR_H Base class allowing use of member function shared_from_this.
shared_ptr< _Tp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
shared_ptr(_Yp *__p)
Construct a shared_ptr that owns the pointer __p.
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< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
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(nullptr_t __p, _Deleter __d)
Construct a shared_ptr that owns a null pointer 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...
A simple smart pointer providing strict ownership semantics.
shared_ptr(_Yp *__p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns the pointer __p and the deleter __d.
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.
Primary template owner_less.
20.7.1.2 unique_ptr for single objects.
A smart pointer with reference-counted copy semantics.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
friend shared_ptr< _Yp > allocate_shared(const _Alloc &__a, _Args &&... __args)
Create an object that is owned by a shared_ptr.
Template class basic_ostream.
ISO C++ entities toplevel namespace is std.
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
Construct a shared_ptr that owns a null pointer and the deleter __d.
constexpr shared_ptr() noexcept
Construct an empty shared_ptr.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
constexpr shared_ptr(nullptr_t) noexcept
Construct an empty shared_ptr.
Primary class template hash.
shared_ptr(shared_ptr &&__r) noexcept
Move-constructs a shared_ptr instance from __r.
A smart pointer with weak semantics.