File indexing completed on 2026-05-03 08:13:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
0011 #define _LIBCPP___CXX03___STOP_TOKEN_INTRUSIVE_SHARED_PTR_H
0012
0013 #include <__cxx03/__atomic/atomic.h>
0014 #include <__cxx03/__atomic/memory_order.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/__type_traits/is_reference.h>
0017 #include <__cxx03/__utility/move.h>
0018 #include <__cxx03/__utility/swap.h>
0019 #include <__cxx03/cstddef>
0020
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 # pragma GCC system_header
0023 #endif
0024
0025 _LIBCPP_PUSH_MACROS
0026 #include <__cxx03/__undef_macros>
0027
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029
0030 #if _LIBCPP_STD_VER >= 20
0031
0032
0033
0034
0035
0036
0037
0038 template <class _Tp>
0039 struct __intrusive_shared_ptr_traits;
0040
0041
0042
0043
0044 template <class _Tp>
0045 struct __intrusive_shared_ptr {
0046 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr() = default;
0047
0048 _LIBCPP_HIDE_FROM_ABI explicit __intrusive_shared_ptr(_Tp* __raw_ptr) : __raw_ptr_(__raw_ptr) {
0049 if (__raw_ptr_)
0050 __increment_ref_count(*__raw_ptr_);
0051 }
0052
0053 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(const __intrusive_shared_ptr& __other) noexcept
0054 : __raw_ptr_(__other.__raw_ptr_) {
0055 if (__raw_ptr_)
0056 __increment_ref_count(*__raw_ptr_);
0057 }
0058
0059 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr(__intrusive_shared_ptr&& __other) noexcept
0060 : __raw_ptr_(__other.__raw_ptr_) {
0061 __other.__raw_ptr_ = nullptr;
0062 }
0063
0064 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(const __intrusive_shared_ptr& __other) noexcept {
0065 if (__other.__raw_ptr_ != __raw_ptr_) {
0066 if (__other.__raw_ptr_) {
0067 __increment_ref_count(*__other.__raw_ptr_);
0068 }
0069 if (__raw_ptr_) {
0070 __decrement_ref_count(*__raw_ptr_);
0071 }
0072 __raw_ptr_ = __other.__raw_ptr_;
0073 }
0074 return *this;
0075 }
0076
0077 _LIBCPP_HIDE_FROM_ABI __intrusive_shared_ptr& operator=(__intrusive_shared_ptr&& __other) noexcept {
0078 __intrusive_shared_ptr(std::move(__other)).swap(*this);
0079 return *this;
0080 }
0081
0082 _LIBCPP_HIDE_FROM_ABI ~__intrusive_shared_ptr() {
0083 if (__raw_ptr_) {
0084 __decrement_ref_count(*__raw_ptr_);
0085 }
0086 }
0087
0088 _LIBCPP_HIDE_FROM_ABI _Tp* operator->() const noexcept { return __raw_ptr_; }
0089 _LIBCPP_HIDE_FROM_ABI _Tp& operator*() const noexcept { return *__raw_ptr_; }
0090 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __raw_ptr_ != nullptr; }
0091
0092 _LIBCPP_HIDE_FROM_ABI void swap(__intrusive_shared_ptr& __other) { std::swap(__raw_ptr_, __other.__raw_ptr_); }
0093
0094 _LIBCPP_HIDE_FROM_ABI friend void swap(__intrusive_shared_ptr& __lhs, __intrusive_shared_ptr& __rhs) {
0095 __lhs.swap(__rhs);
0096 }
0097
0098 _LIBCPP_HIDE_FROM_ABI friend bool constexpr
0099 operator==(const __intrusive_shared_ptr&, const __intrusive_shared_ptr&) = default;
0100
0101 _LIBCPP_HIDE_FROM_ABI friend bool constexpr operator==(const __intrusive_shared_ptr& __ptr, std::nullptr_t) {
0102 return __ptr.__raw_ptr_ == nullptr;
0103 }
0104
0105 private:
0106 _Tp* __raw_ptr_ = nullptr;
0107
0108
0109
0110 _LIBCPP_HIDE_FROM_ABI static void __increment_ref_count(_Tp& __obj) {
0111 __get_atomic_ref_count(__obj).fetch_add(1, std::memory_order_relaxed);
0112 }
0113
0114 _LIBCPP_HIDE_FROM_ABI static void __decrement_ref_count(_Tp& __obj) {
0115 if (__get_atomic_ref_count(__obj).fetch_sub(1, std::memory_order_acq_rel) == 1) {
0116 delete &__obj;
0117 }
0118 }
0119
0120 _LIBCPP_HIDE_FROM_ABI static decltype(auto) __get_atomic_ref_count(_Tp& __obj) {
0121 using __ret_type = decltype(__intrusive_shared_ptr_traits<_Tp>::__get_atomic_ref_count(__obj));
0122 static_assert(
0123 std::is_reference_v<__ret_type>, "__get_atomic_ref_count should return a reference to the atomic counter");
0124 return __intrusive_shared_ptr_traits<_Tp>::__get_atomic_ref_count(__obj);
0125 }
0126 };
0127
0128 #endif
0129
0130 _LIBCPP_END_NAMESPACE_STD
0131
0132 _LIBCPP_POP_MACROS
0133
0134 #endif