16#include "pw_allocator/config.h"
19#if PW_ALLOCATOR_HAS_ATOMICS
24#include "pw_allocator/internal/control_block.h"
25#include "pw_allocator/internal/managed_ptr.h"
26#include "pw_allocator/shared_ptr.h"
37class WeakPtr final :
public ::pw::allocator::internal::WeakManagedPtr<T> {
39 using Base = ::pw::allocator::internal::WeakManagedPtr<T>;
40 using ControlBlock = ::pw::allocator::internal::ControlBlock;
43 using element_type =
typename Base::element_type;
101 template <
typename U>
108 template <
typename U>
115 void reset() noexcept;
119 std::swap(control_block_, other.control_block_);
125 return control_block_ ==
nullptr ? 0 : control_block_->num_shared();
138 template <typename PtrType>
140 return control_block_ < other.control_block();
148 ControlBlock* control_block()
const {
return control_block_; }
150 ControlBlock* control_block_ =
nullptr;
158 Base::template CheckAssignable<U>();
159 control_block_ = other.control_block_;
160 if (control_block_ !=
nullptr) {
161 control_block_->IncrementWeak();
169 Base::template CheckAssignable<U>();
170 control_block_ = other.control_block_;
171 if (control_block_ !=
nullptr) {
172 control_block_->IncrementWeak();
180 Base::template CheckAssignable<U>();
181 control_block_ = other.control_block_;
182 other.control_block_ =
nullptr;
188 if (control_block_ ==
nullptr ||
189 control_block_->DecrementWeak() != ControlBlock::Action::kFree) {
192 Allocator* allocator = control_block_->allocator();
193 Base::Deallocate(allocator, control_block_);
194 control_block_ =
nullptr;
199 if (control_block_ ==
nullptr || !control_block_->IncrementShared()) {
202 void* data = control_block_->data();
203 auto* t = std::launder(
reinterpret_cast<element_type*
>(data));
Definition: allocator.h:36
Definition: shared_ptr.h:59
Definition: weak_ptr.h:37
WeakPtr(WeakPtr< U > &&other) noexcept
Definition: weak_ptr.h:78
WeakPtr(const WeakPtr &other) noexcept
Copy-constructs a WeakPtr<T> from a WeakPtr<T>.
Definition: weak_ptr.h:52
WeakPtr & operator=(const SharedPtr< U > &other) noexcept
int32_t use_count() const noexcept
Definition: weak_ptr.h:124
WeakPtr(const WeakPtr< U > &other) noexcept
Definition: weak_ptr.h:59
WeakPtr & operator=(const WeakPtr< U > &other) noexcept
constexpr WeakPtr & operator=(const WeakPtr &other) noexcept
Copy-assigns a WeakPtr<T> from a WeakPtr<T>.
Definition: weak_ptr.h:85
WeakPtr(const SharedPtr< U > &other) noexcept
Definition: weak_ptr.h:68
constexpr WeakPtr() noexcept=default
Creates an empty (nullptr) instance.
bool expired() const noexcept
Definition: weak_ptr.h:130
WeakPtr & operator=(WeakPtr< U > &&other) noexcept
void swap(WeakPtr &other) noexcept
Swaps the managed pointer and deallocator of this and another object.
Definition: weak_ptr.h:118
bool owner_before(const PtrType &other) const noexcept
Definition: weak_ptr.h:139
void reset() noexcept
Definition: weak_ptr.h:187
SharedPtr< T > Lock() const noexcept
Definition: weak_ptr.h:198
The Pigweed namespace.
Definition: alignment.h:27