123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746 |
- // -*- C++ -*-
- //===----------------------------------------------------------------------===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP___MEMORY_SHARED_PTR_H
- #define _LIBCPP___MEMORY_SHARED_PTR_H
- #include <__availability>
- #include <__config>
- #include <__functional/binary_function.h>
- #include <__functional/operations.h>
- #include <__functional/reference_wrapper.h>
- #include <__memory/addressof.h>
- #include <__memory/allocation_guard.h>
- #include <__memory/allocator.h>
- #include <__memory/allocator_traits.h>
- #include <__memory/compressed_pair.h>
- #include <__memory/pointer_traits.h>
- #include <__memory/unique_ptr.h>
- #include <__utility/forward.h>
- #include <__utility/move.h>
- #include <cstddef>
- #include <cstdlib> // abort
- #include <iosfwd>
- #include <stdexcept>
- #include <type_traits>
- #include <typeinfo>
- #if !defined(_LIBCPP_HAS_NO_THREADS) // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
- # include <atomic>
- #endif
- #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
- # include <__memory/auto_ptr.h>
- #endif
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- # pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- template <class _Alloc>
- class __allocator_destructor
- {
- typedef _LIBCPP_NODEBUG allocator_traits<_Alloc> __alloc_traits;
- public:
- typedef _LIBCPP_NODEBUG typename __alloc_traits::pointer pointer;
- typedef _LIBCPP_NODEBUG typename __alloc_traits::size_type size_type;
- private:
- _Alloc& __alloc_;
- size_type __s_;
- public:
- _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
- _NOEXCEPT
- : __alloc_(__a), __s_(__s) {}
- _LIBCPP_INLINE_VISIBILITY
- void operator()(pointer __p) _NOEXCEPT
- {__alloc_traits::deallocate(__alloc_, __p, __s_);}
- };
- // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
- // should be sufficient for thread safety.
- // See https://llvm.org/PR22803
- #if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
- && defined(__ATOMIC_RELAXED) \
- && defined(__ATOMIC_ACQ_REL)
- # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
- #elif defined(_LIBCPP_COMPILER_GCC)
- # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
- #endif
- template <class _ValueType>
- inline _LIBCPP_INLINE_VISIBILITY
- _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
- #if !defined(_LIBCPP_HAS_NO_THREADS) && \
- defined(__ATOMIC_RELAXED) && \
- (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
- return __atomic_load_n(__value, __ATOMIC_RELAXED);
- #else
- return *__value;
- #endif
- }
- template <class _ValueType>
- inline _LIBCPP_INLINE_VISIBILITY
- _ValueType __libcpp_acquire_load(_ValueType const* __value) {
- #if !defined(_LIBCPP_HAS_NO_THREADS) && \
- defined(__ATOMIC_ACQUIRE) && \
- (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
- return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
- #else
- return *__value;
- #endif
- }
- #ifndef _LIBCPP_COMPILER_MSVC
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _Tp
- __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
- {
- #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
- return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
- #else
- return __t += 1;
- #endif
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _Tp
- __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
- {
- #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
- return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
- #else
- return __t -= 1;
- #endif
- }
- #endif
- class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
- : public std::exception
- {
- public:
- bad_weak_ptr() _NOEXCEPT = default;
- bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
- virtual ~bad_weak_ptr() _NOEXCEPT;
- virtual const char* what() const _NOEXCEPT;
- };
- _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
- void __throw_bad_weak_ptr()
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw bad_weak_ptr();
- #else
- _VSTD::abort();
- #endif
- }
- template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
- class _LIBCPP_TYPE_VIS __shared_count
- {
- __shared_count(const __shared_count&);
- __shared_count& operator=(const __shared_count&);
- protected:
- #ifdef _LIBCPP_HAS_NO_THREADS
- typedef long __atomic_count;
- #else
- typedef atomic<long> __atomic_count;
- #endif
- __atomic_count __shared_owners_;
- virtual ~__shared_count();
- private:
- virtual void __on_zero_shared() _NOEXCEPT = 0;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __shared_count(long __refs = 0) _NOEXCEPT
- : __shared_owners_(__refs) {}
- #if defined(_LIBCPP_BUILDING_LIBRARY) && \
- defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
- void __add_shared() _NOEXCEPT;
- bool __release_shared() _NOEXCEPT;
- #else
- _LIBCPP_INLINE_VISIBILITY
- void __add_shared() _NOEXCEPT {
- #ifdef _LIBCPP_HAS_NO_THREADS
- __libcpp_atomic_refcount_increment(__shared_owners_);
- #else
- __shared_owners_++;
- #endif
- }
- _LIBCPP_INLINE_VISIBILITY
- bool __release_shared() _NOEXCEPT {
- #ifdef _LIBCPP_HAS_NO_THREADS
- if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
- #else
- if (--__shared_owners_ == -1) {
- #endif
- __on_zero_shared();
- return true;
- }
- return false;
- }
- #endif
- _LIBCPP_INLINE_VISIBILITY
- long use_count() const _NOEXCEPT {
- #ifdef _LIBCPP_HAS_NO_THREADS
- return __libcpp_relaxed_load(&__shared_owners_) + 1;
- #else
- return __shared_owners_.load(memory_order_relaxed) + 1;
- #endif
- }
- };
- class _LIBCPP_TYPE_VIS __shared_weak_count
- : private __shared_count
- {
- #ifdef _LIBCPP_HAS_NO_THREADS
- typedef long __atomic_count;
- #else
- typedef atomic<long> __atomic_count;
- #endif
- __atomic_count __shared_weak_owners_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
- : __shared_count(__refs),
- __shared_weak_owners_(__refs) {}
- protected:
- virtual ~__shared_weak_count();
- public:
- #if defined(_LIBCPP_BUILDING_LIBRARY) && \
- defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
- void __add_shared() _NOEXCEPT;
- void __add_weak() _NOEXCEPT;
- void __release_shared() _NOEXCEPT;
- #else
- _LIBCPP_INLINE_VISIBILITY
- void __add_shared() _NOEXCEPT {
- __shared_count::__add_shared();
- }
- _LIBCPP_INLINE_VISIBILITY
- void __add_weak() _NOEXCEPT {
- #ifdef _LIBCPP_HAS_NO_THREADS
- __libcpp_atomic_refcount_increment(__shared_weak_owners_);
- #else
- __shared_weak_owners_++;
- #endif
- }
- _LIBCPP_INLINE_VISIBILITY
- void __release_shared() _NOEXCEPT {
- if (__shared_count::__release_shared())
- __release_weak();
- }
- #endif
- void __release_weak() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- long use_count() const _NOEXCEPT {return __shared_count::use_count();}
- __shared_weak_count* lock() _NOEXCEPT;
- virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
- private:
- virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
- };
- template <class _Tp, class _Dp, class _Alloc>
- class __shared_ptr_pointer
- : public __shared_weak_count
- {
- __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
- : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
- #ifndef _LIBCPP_NO_RTTI
- virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
- #endif
- private:
- virtual void __on_zero_shared() _NOEXCEPT;
- virtual void __on_zero_shared_weak() _NOEXCEPT;
- };
- #ifndef _LIBCPP_NO_RTTI
- template <class _Tp, class _Dp, class _Alloc>
- const void*
- __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
- {
- return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
- }
- #endif // _LIBCPP_NO_RTTI
- template <class _Tp, class _Dp, class _Alloc>
- void
- __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
- {
- __data_.first().second()(__data_.first().first());
- __data_.first().second().~_Dp();
- }
- template <class _Tp, class _Dp, class _Alloc>
- void
- __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
- {
- typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
- typedef allocator_traits<_Al> _ATraits;
- typedef pointer_traits<typename _ATraits::pointer> _PTraits;
- _Al __a(__data_.second());
- __data_.second().~_Alloc();
- __a.deallocate(_PTraits::pointer_to(*this), 1);
- }
- template <class _Tp, class _Alloc>
- struct __shared_ptr_emplace
- : __shared_weak_count
- {
- template<class ..._Args>
- _LIBCPP_HIDE_FROM_ABI
- explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
- : __storage_(_VSTD::move(__a))
- {
- #if _LIBCPP_STD_VER > 17
- using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
- _TpAlloc __tmp(*__get_alloc());
- allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
- #else
- ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
- #endif
- }
- _LIBCPP_HIDE_FROM_ABI
- _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
- _LIBCPP_HIDE_FROM_ABI
- _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
- private:
- virtual void __on_zero_shared() _NOEXCEPT {
- #if _LIBCPP_STD_VER > 17
- using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
- _TpAlloc __tmp(*__get_alloc());
- allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
- #else
- __get_elem()->~_Tp();
- #endif
- }
- virtual void __on_zero_shared_weak() _NOEXCEPT {
- using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
- using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
- _ControlBlockAlloc __tmp(*__get_alloc());
- __storage_.~_Storage();
- allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
- pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
- }
- // This class implements the control block for non-array shared pointers created
- // through `std::allocate_shared` and `std::make_shared`.
- //
- // In previous versions of the library, we used a compressed pair to store
- // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
- // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
- // we now use a properly aligned char buffer while making sure that we maintain
- // the same layout that we had when we used a compressed pair.
- using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
- struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
- char __blob_[sizeof(_CompressedPair)];
- _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
- ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
- }
- _LIBCPP_HIDE_FROM_ABI ~_Storage() {
- __get_alloc()->~_Alloc();
- }
- _Alloc* __get_alloc() _NOEXCEPT {
- _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
- typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
- _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
- return __alloc;
- }
- _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
- _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
- typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
- _Tp *__elem = reinterpret_cast<_Tp*>(__second);
- return __elem;
- }
- };
- static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
- static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
- _Storage __storage_;
- };
- struct __shared_ptr_dummy_rebind_allocator_type;
- template <>
- class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
- {
- public:
- template <class _Other>
- struct rebind
- {
- typedef allocator<_Other> other;
- };
- };
- template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
- template<class _Tp, class _Up>
- struct __compatible_with
- #if _LIBCPP_STD_VER > 14
- : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
- #else
- : is_convertible<_Tp*, _Up*> {};
- #endif // _LIBCPP_STD_VER > 14
- template <class _Ptr, class = void>
- struct __is_deletable : false_type { };
- template <class _Ptr>
- struct __is_deletable<_Ptr, decltype(delete declval<_Ptr>())> : true_type { };
- template <class _Ptr, class = void>
- struct __is_array_deletable : false_type { };
- template <class _Ptr>
- struct __is_array_deletable<_Ptr, decltype(delete[] declval<_Ptr>())> : true_type { };
- template <class _Dp, class _Pt,
- class = decltype(declval<_Dp>()(declval<_Pt>()))>
- static true_type __well_formed_deleter_test(int);
- template <class, class>
- static false_type __well_formed_deleter_test(...);
- template <class _Dp, class _Pt>
- struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
- template<class _Dp, class _Tp, class _Yp>
- struct __shared_ptr_deleter_ctor_reqs
- {
- static const bool value = __compatible_with<_Tp, _Yp>::value &&
- is_move_constructible<_Dp>::value &&
- __well_formed_deleter<_Dp, _Tp*>::value;
- };
- #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
- # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
- #else
- # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
- #endif
- template<class _Tp>
- class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
- {
- public:
- #if _LIBCPP_STD_VER > 14
- typedef weak_ptr<_Tp> weak_type;
- typedef remove_extent_t<_Tp> element_type;
- #else
- typedef _Tp element_type;
- #endif
- private:
- element_type* __ptr_;
- __shared_weak_count* __cntrl_;
- public:
- _LIBCPP_HIDE_FROM_ABI
- _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT
- : __ptr_(nullptr),
- __cntrl_(nullptr)
- { }
- _LIBCPP_HIDE_FROM_ABI
- _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT
- : __ptr_(nullptr),
- __cntrl_(nullptr)
- { }
- template<class _Yp, class = __enable_if_t<
- _And<
- __compatible_with<_Yp, _Tp>
- // In C++03 we get errors when trying to do SFINAE with the
- // delete operator, so we always pretend that it's deletable.
- // The same happens on GCC.
- #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)
- , _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >
- #endif
- >::value
- > >
- explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
- unique_ptr<_Yp> __hold(__p);
- typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
- typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
- __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
- __hold.release();
- __enable_weak_this(__p, __p);
- }
- template<class _Yp, class _Dp, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(_Yp* __p, _Dp __d)
- : __ptr_(__p)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
- typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
- #ifndef _LIBCPP_CXX03_LANG
- __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
- #else
- __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
- #endif // not _LIBCPP_CXX03_LANG
- __enable_weak_this(__p, __p);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __d(__p);
- throw;
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
- : __ptr_(__p)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
- typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
- typedef __allocator_destructor<_A2> _D2;
- _A2 __a2(__a);
- unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
- ::new ((void*)_VSTD::addressof(*__hold2.get()))
- #ifndef _LIBCPP_CXX03_LANG
- _CntrlBlk(__p, _VSTD::move(__d), __a);
- #else
- _CntrlBlk(__p, __d, __a);
- #endif // not _LIBCPP_CXX03_LANG
- __cntrl_ = _VSTD::addressof(*__hold2.release());
- __enable_weak_this(__p, __p);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __d(__p);
- throw;
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class _Dp>
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(nullptr_t __p, _Dp __d)
- : __ptr_(nullptr)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
- typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
- #ifndef _LIBCPP_CXX03_LANG
- __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
- #else
- __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
- #endif // not _LIBCPP_CXX03_LANG
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __d(__p);
- throw;
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class _Dp, class _Alloc>
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
- : __ptr_(nullptr)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
- typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
- typedef __allocator_destructor<_A2> _D2;
- _A2 __a2(__a);
- unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
- ::new ((void*)_VSTD::addressof(*__hold2.get()))
- #ifndef _LIBCPP_CXX03_LANG
- _CntrlBlk(__p, _VSTD::move(__d), __a);
- #else
- _CntrlBlk(__p, __d, __a);
- #endif // not _LIBCPP_CXX03_LANG
- __cntrl_ = _VSTD::addressof(*__hold2.release());
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __d(__p);
- throw;
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- template<class _Yp>
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
- : __ptr_(__p),
- __cntrl_(__r.__cntrl_)
- {
- if (__cntrl_)
- __cntrl_->__add_shared();
- }
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(const shared_ptr& __r) _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- if (__cntrl_)
- __cntrl_->__add_shared();
- }
- template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- if (__cntrl_)
- __cntrl_->__add_shared();
- }
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(shared_ptr&& __r) _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- __r.__ptr_ = nullptr;
- __r.__cntrl_ = nullptr;
- }
- template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- __r.__ptr_ = nullptr;
- __r.__cntrl_ = nullptr;
- }
- template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- explicit shared_ptr(const weak_ptr<_Yp>& __r)
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
- {
- if (__cntrl_ == nullptr)
- __throw_bad_weak_ptr();
- }
- #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
- template<class _Yp, class = __enable_if_t<is_convertible<_Yp*, element_type*>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(auto_ptr<_Yp>&& __r)
- : __ptr_(__r.get())
- {
- typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
- __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
- __enable_weak_this(__r.get(), __r.get());
- __r.release();
- }
- #endif
- template <class _Yp, class _Dp, class = __enable_if_t<
- !is_lvalue_reference<_Dp>::value &&
- is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
- : __ptr_(__r.get())
- {
- #if _LIBCPP_STD_VER > 11
- if (__ptr_ == nullptr)
- __cntrl_ = nullptr;
- else
- #endif
- {
- typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
- typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
- __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
- __enable_weak_this(__r.get(), __r.get());
- }
- __r.release();
- }
- template <class _Yp, class _Dp, class = void, class = __enable_if_t<
- is_lvalue_reference<_Dp>::value &&
- is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
- : __ptr_(__r.get())
- {
- #if _LIBCPP_STD_VER > 11
- if (__ptr_ == nullptr)
- __cntrl_ = nullptr;
- else
- #endif
- {
- typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
- typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
- reference_wrapper<typename remove_reference<_Dp>::type>,
- _AllocT > _CntrlBlk;
- __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
- __enable_weak_this(__r.get(), __r.get());
- }
- __r.release();
- }
- _LIBCPP_HIDE_FROM_ABI
- ~shared_ptr()
- {
- if (__cntrl_)
- __cntrl_->__release_shared();
- }
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT
- {
- shared_ptr(__r).swap(*this);
- return *this;
- }
- template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
- {
- shared_ptr(__r).swap(*this);
- return *this;
- }
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT
- {
- shared_ptr(_VSTD::move(__r)).swap(*this);
- return *this;
- }
- template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r)
- {
- shared_ptr(_VSTD::move(__r)).swap(*this);
- return *this;
- }
- #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
- template<class _Yp, class = __enable_if_t<
- !is_array<_Yp>::value &&
- is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r)
- {
- shared_ptr(_VSTD::move(__r)).swap(*this);
- return *this;
- }
- #endif
- template <class _Yp, class _Dp, class = __enable_if_t<
- is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r)
- {
- shared_ptr(_VSTD::move(__r)).swap(*this);
- return *this;
- }
- _LIBCPP_HIDE_FROM_ABI
- void swap(shared_ptr& __r) _NOEXCEPT
- {
- _VSTD::swap(__ptr_, __r.__ptr_);
- _VSTD::swap(__cntrl_, __r.__cntrl_);
- }
- _LIBCPP_HIDE_FROM_ABI
- void reset() _NOEXCEPT
- {
- shared_ptr().swap(*this);
- }
- template<class _Yp, class = __enable_if_t<
- __compatible_with<_Yp, _Tp>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- void reset(_Yp* __p)
- {
- shared_ptr(__p).swap(*this);
- }
- template<class _Yp, class _Dp, class = __enable_if_t<
- __compatible_with<_Yp, _Tp>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- void reset(_Yp* __p, _Dp __d)
- {
- shared_ptr(__p, __d).swap(*this);
- }
- template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<
- __compatible_with<_Yp, _Tp>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- void reset(_Yp* __p, _Dp __d, _Alloc __a)
- {
- shared_ptr(__p, __d, __a).swap(*this);
- }
- _LIBCPP_HIDE_FROM_ABI
- element_type* get() const _NOEXCEPT
- {
- return __ptr_;
- }
- _LIBCPP_HIDE_FROM_ABI
- typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
- {
- return *__ptr_;
- }
- _LIBCPP_HIDE_FROM_ABI
- element_type* operator->() const _NOEXCEPT
- {
- static_assert(!is_array<_Tp>::value,
- "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
- return __ptr_;
- }
- _LIBCPP_HIDE_FROM_ABI
- long use_count() const _NOEXCEPT
- {
- return __cntrl_ ? __cntrl_->use_count() : 0;
- }
- _LIBCPP_HIDE_FROM_ABI
- bool unique() const _NOEXCEPT
- {
- return use_count() == 1;
- }
- _LIBCPP_HIDE_FROM_ABI
- explicit operator bool() const _NOEXCEPT
- {
- return get() != nullptr;
- }
- template <class _Up>
- _LIBCPP_HIDE_FROM_ABI
- bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
- {
- return __cntrl_ < __p.__cntrl_;
- }
- template <class _Up>
- _LIBCPP_HIDE_FROM_ABI
- bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
- {
- return __cntrl_ < __p.__cntrl_;
- }
- _LIBCPP_HIDE_FROM_ABI
- bool __owner_equivalent(const shared_ptr& __p) const
- {
- return __cntrl_ == __p.__cntrl_;
- }
- #if _LIBCPP_STD_VER > 14
- _LIBCPP_HIDE_FROM_ABI
- typename add_lvalue_reference<element_type>::type operator[](ptrdiff_t __i) const
- {
- static_assert(is_array<_Tp>::value,
- "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
- return __ptr_[__i];
- }
- #endif
- #ifndef _LIBCPP_NO_RTTI
- template <class _Dp>
- _LIBCPP_HIDE_FROM_ABI
- _Dp* __get_deleter() const _NOEXCEPT
- {
- return static_cast<_Dp*>(__cntrl_
- ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
- : nullptr);
- }
- #endif // _LIBCPP_NO_RTTI
- template<class _Yp, class _CntrlBlk>
- _LIBCPP_HIDE_FROM_ABI
- static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
- {
- shared_ptr<_Tp> __r;
- __r.__ptr_ = __p;
- __r.__cntrl_ = __cntrl;
- __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
- return __r;
- }
- private:
- template <class _Yp, bool = is_function<_Yp>::value>
- struct __shared_ptr_default_allocator
- {
- typedef allocator<_Yp> type;
- };
- template <class _Yp>
- struct __shared_ptr_default_allocator<_Yp, true>
- {
- typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
- };
- template <class _Yp, class _OrigPtr, class = __enable_if_t<
- is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value
- > >
- _LIBCPP_HIDE_FROM_ABI
- void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT
- {
- typedef typename remove_cv<_Yp>::type _RawYp;
- if (__e && __e->__weak_this_.expired())
- {
- __e->__weak_this_ = shared_ptr<_RawYp>(*this,
- const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
- }
- }
- _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT { }
- template <class, class _Yp>
- struct __shared_ptr_default_delete
- : default_delete<_Yp>
- { };
- template <class _Yp, class _Un, size_t _Sz>
- struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
- : default_delete<_Yp[]>
- { };
- template <class _Yp, class _Un>
- struct __shared_ptr_default_delete<_Yp[], _Un>
- : default_delete<_Yp[]>
- { };
- template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
- template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
- };
- #if _LIBCPP_STD_VER > 14
- template<class _Tp>
- shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
- template<class _Tp, class _Dp>
- shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
- #endif
- //
- // std::allocate_shared and std::make_shared
- //
- template<class _Tp, class _Alloc, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
- {
- using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
- using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
- __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
- ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
- auto __control_block = __guard.__release_ptr();
- return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
- }
- template<class _Tp, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value> >
- _LIBCPP_HIDE_FROM_ABI
- shared_ptr<_Tp> make_shared(_Args&& ...__args)
- {
- return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
- {
- return __x.get() == __y.get();
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
- {
- return !(__x == __y);
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
- {
- #if _LIBCPP_STD_VER <= 11
- typedef typename common_type<_Tp*, _Up*>::type _Vp;
- return less<_Vp>()(__x.get(), __y.get());
- #else
- return less<>()(__x.get(), __y.get());
- #endif
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
- {
- return __y < __x;
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
- {
- return !(__y < __x);
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
- {
- return !(__x < __y);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
- {
- return !__x;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
- {
- return !__x;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
- {
- return static_cast<bool>(__x);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
- {
- return static_cast<bool>(__x);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
- {
- return less<_Tp*>()(__x.get(), nullptr);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
- {
- return less<_Tp*>()(nullptr, __x.get());
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
- {
- return nullptr < __x;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
- {
- return __x < nullptr;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
- {
- return !(nullptr < __x);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
- {
- return !(__x < nullptr);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
- {
- return !(__x < nullptr);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
- {
- return !(nullptr < __x);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
- {
- __x.swap(__y);
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- shared_ptr<_Tp>
- static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
- {
- return shared_ptr<_Tp>(__r,
- static_cast<
- typename shared_ptr<_Tp>::element_type*>(__r.get()));
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- shared_ptr<_Tp>
- dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
- {
- typedef typename shared_ptr<_Tp>::element_type _ET;
- _ET* __p = dynamic_cast<_ET*>(__r.get());
- return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
- }
- template<class _Tp, class _Up>
- shared_ptr<_Tp>
- const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
- {
- typedef typename shared_ptr<_Tp>::element_type _RTp;
- return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
- }
- template<class _Tp, class _Up>
- shared_ptr<_Tp>
- reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
- {
- return shared_ptr<_Tp>(__r,
- reinterpret_cast<
- typename shared_ptr<_Tp>::element_type*>(__r.get()));
- }
- #ifndef _LIBCPP_NO_RTTI
- template<class _Dp, class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _Dp*
- get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
- {
- return __p.template __get_deleter<_Dp>();
- }
- #endif // _LIBCPP_NO_RTTI
- template<class _Tp>
- class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
- {
- public:
- #if _LIBCPP_STD_VER > 14
- typedef remove_extent_t<_Tp> element_type;
- #else
- typedef _Tp element_type;
- #endif
- private:
- element_type* __ptr_;
- __shared_weak_count* __cntrl_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
- template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
- typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
- _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- weak_ptr(weak_ptr const& __r) _NOEXCEPT;
- template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
- typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
- _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- weak_ptr(weak_ptr&& __r) _NOEXCEPT;
- template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
- typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
- _NOEXCEPT;
- ~weak_ptr();
- _LIBCPP_INLINE_VISIBILITY
- weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
- template<class _Yp>
- typename enable_if
- <
- __compatible_with<_Yp, _Tp>::value,
- weak_ptr&
- >::type
- _LIBCPP_INLINE_VISIBILITY
- operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
- template<class _Yp>
- typename enable_if
- <
- __compatible_with<_Yp, _Tp>::value,
- weak_ptr&
- >::type
- _LIBCPP_INLINE_VISIBILITY
- operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
- template<class _Yp>
- typename enable_if
- <
- __compatible_with<_Yp, _Tp>::value,
- weak_ptr&
- >::type
- _LIBCPP_INLINE_VISIBILITY
- operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void swap(weak_ptr& __r) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void reset() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- long use_count() const _NOEXCEPT
- {return __cntrl_ ? __cntrl_->use_count() : 0;}
- _LIBCPP_INLINE_VISIBILITY
- bool expired() const _NOEXCEPT
- {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
- shared_ptr<_Tp> lock() const _NOEXCEPT;
- template<class _Up>
- _LIBCPP_INLINE_VISIBILITY
- bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
- {return __cntrl_ < __r.__cntrl_;}
- template<class _Up>
- _LIBCPP_INLINE_VISIBILITY
- bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
- {return __cntrl_ < __r.__cntrl_;}
- template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
- template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
- };
- #if _LIBCPP_STD_VER > 14
- template<class _Tp>
- weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
- #endif
- template<class _Tp>
- inline
- _LIBCPP_CONSTEXPR
- weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
- : __ptr_(nullptr),
- __cntrl_(nullptr)
- {
- }
- template<class _Tp>
- inline
- weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- if (__cntrl_)
- __cntrl_->__add_weak();
- }
- template<class _Tp>
- template<class _Yp>
- inline
- weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
- typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
- _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- if (__cntrl_)
- __cntrl_->__add_weak();
- }
- template<class _Tp>
- template<class _Yp>
- inline
- weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
- typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
- _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- if (__cntrl_)
- __cntrl_->__add_weak();
- }
- template<class _Tp>
- inline
- weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- __r.__ptr_ = nullptr;
- __r.__cntrl_ = nullptr;
- }
- template<class _Tp>
- template<class _Yp>
- inline
- weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
- typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
- _NOEXCEPT
- : __ptr_(__r.__ptr_),
- __cntrl_(__r.__cntrl_)
- {
- __r.__ptr_ = nullptr;
- __r.__cntrl_ = nullptr;
- }
- template<class _Tp>
- weak_ptr<_Tp>::~weak_ptr()
- {
- if (__cntrl_)
- __cntrl_->__release_weak();
- }
- template<class _Tp>
- inline
- weak_ptr<_Tp>&
- weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
- {
- weak_ptr(__r).swap(*this);
- return *this;
- }
- template<class _Tp>
- template<class _Yp>
- inline
- typename enable_if
- <
- __compatible_with<_Yp, _Tp>::value,
- weak_ptr<_Tp>&
- >::type
- weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
- {
- weak_ptr(__r).swap(*this);
- return *this;
- }
- template<class _Tp>
- inline
- weak_ptr<_Tp>&
- weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
- {
- weak_ptr(_VSTD::move(__r)).swap(*this);
- return *this;
- }
- template<class _Tp>
- template<class _Yp>
- inline
- typename enable_if
- <
- __compatible_with<_Yp, _Tp>::value,
- weak_ptr<_Tp>&
- >::type
- weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
- {
- weak_ptr(_VSTD::move(__r)).swap(*this);
- return *this;
- }
- template<class _Tp>
- template<class _Yp>
- inline
- typename enable_if
- <
- __compatible_with<_Yp, _Tp>::value,
- weak_ptr<_Tp>&
- >::type
- weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
- {
- weak_ptr(__r).swap(*this);
- return *this;
- }
- template<class _Tp>
- inline
- void
- weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
- {
- _VSTD::swap(__ptr_, __r.__ptr_);
- _VSTD::swap(__cntrl_, __r.__cntrl_);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
- {
- __x.swap(__y);
- }
- template<class _Tp>
- inline
- void
- weak_ptr<_Tp>::reset() _NOEXCEPT
- {
- weak_ptr().swap(*this);
- }
- template<class _Tp>
- shared_ptr<_Tp>
- weak_ptr<_Tp>::lock() const _NOEXCEPT
- {
- shared_ptr<_Tp> __r;
- __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
- if (__r.__cntrl_)
- __r.__ptr_ = __ptr_;
- return __r;
- }
- #if _LIBCPP_STD_VER > 14
- template <class _Tp = void> struct owner_less;
- #else
- template <class _Tp> struct owner_less;
- #endif
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- template <class _Tp>
- struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
- #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
- : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
- #endif
- {
- _LIBCPP_SUPPRESS_DEPRECATED_POP
- #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
- _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
- _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> first_argument_type;
- _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> second_argument_type;
- #endif
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- };
- _LIBCPP_SUPPRESS_DEPRECATED_PUSH
- template <class _Tp>
- struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
- #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
- : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
- #endif
- {
- _LIBCPP_SUPPRESS_DEPRECATED_POP
- #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
- _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
- _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> first_argument_type;
- _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> second_argument_type;
- #endif
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- _LIBCPP_INLINE_VISIBILITY
- bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- };
- #if _LIBCPP_STD_VER > 14
- template <>
- struct _LIBCPP_TEMPLATE_VIS owner_less<void>
- {
- template <class _Tp, class _Up>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- template <class _Tp, class _Up>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- template <class _Tp, class _Up>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- template <class _Tp, class _Up>
- _LIBCPP_INLINE_VISIBILITY
- bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
- {return __x.owner_before(__y);}
- typedef void is_transparent;
- };
- #endif
- template<class _Tp>
- class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
- {
- mutable weak_ptr<_Tp> __weak_this_;
- protected:
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
- enable_shared_from_this() _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY
- enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY
- enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
- {return *this;}
- _LIBCPP_INLINE_VISIBILITY
- ~enable_shared_from_this() {}
- public:
- _LIBCPP_INLINE_VISIBILITY
- shared_ptr<_Tp> shared_from_this()
- {return shared_ptr<_Tp>(__weak_this_);}
- _LIBCPP_INLINE_VISIBILITY
- shared_ptr<_Tp const> shared_from_this() const
- {return shared_ptr<const _Tp>(__weak_this_);}
- #if _LIBCPP_STD_VER > 14
- _LIBCPP_INLINE_VISIBILITY
- weak_ptr<_Tp> weak_from_this() _NOEXCEPT
- { return __weak_this_; }
- _LIBCPP_INLINE_VISIBILITY
- weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
- { return __weak_this_; }
- #endif // _LIBCPP_STD_VER > 14
- template <class _Up> friend class shared_ptr;
- };
- template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
- template <class _Tp>
- struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
- {
- #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
- _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;
- _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
- #endif
- _LIBCPP_INLINE_VISIBILITY
- size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT
- {
- return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
- }
- };
- template<class _CharT, class _Traits, class _Yp>
- inline _LIBCPP_INLINE_VISIBILITY
- basic_ostream<_CharT, _Traits>&
- operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
- #if !defined(_LIBCPP_HAS_NO_THREADS)
- class _LIBCPP_TYPE_VIS __sp_mut
- {
- void* __lx;
- public:
- void lock() _NOEXCEPT;
- void unlock() _NOEXCEPT;
- private:
- _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
- __sp_mut(const __sp_mut&);
- __sp_mut& operator=(const __sp_mut&);
- friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
- };
- _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- __sp_mut& __get_sp_mut(const void*);
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- bool
- atomic_is_lock_free(const shared_ptr<_Tp>*)
- {
- return false;
- }
- template <class _Tp>
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- shared_ptr<_Tp>
- atomic_load(const shared_ptr<_Tp>* __p)
- {
- __sp_mut& __m = __get_sp_mut(__p);
- __m.lock();
- shared_ptr<_Tp> __q = *__p;
- __m.unlock();
- return __q;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- shared_ptr<_Tp>
- atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
- {
- return atomic_load(__p);
- }
- template <class _Tp>
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- void
- atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
- {
- __sp_mut& __m = __get_sp_mut(__p);
- __m.lock();
- __p->swap(__r);
- __m.unlock();
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- void
- atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
- {
- atomic_store(__p, __r);
- }
- template <class _Tp>
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- shared_ptr<_Tp>
- atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
- {
- __sp_mut& __m = __get_sp_mut(__p);
- __m.lock();
- __p->swap(__r);
- __m.unlock();
- return __r;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- shared_ptr<_Tp>
- atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
- {
- return atomic_exchange(__p, __r);
- }
- template <class _Tp>
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- bool
- atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
- {
- shared_ptr<_Tp> __temp;
- __sp_mut& __m = __get_sp_mut(__p);
- __m.lock();
- if (__p->__owner_equivalent(*__v))
- {
- _VSTD::swap(__temp, *__p);
- *__p = __w;
- __m.unlock();
- return true;
- }
- _VSTD::swap(__temp, *__v);
- *__v = *__p;
- __m.unlock();
- return false;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- bool
- atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
- {
- return atomic_compare_exchange_strong(__p, __v, __w);
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- bool
- atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
- shared_ptr<_Tp> __w, memory_order, memory_order)
- {
- return atomic_compare_exchange_strong(__p, __v, __w);
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
- bool
- atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
- shared_ptr<_Tp> __w, memory_order, memory_order)
- {
- return atomic_compare_exchange_weak(__p, __v, __w);
- }
- #endif // !defined(_LIBCPP_HAS_NO_THREADS)
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP___MEMORY_SHARED_PTR_H
|