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"
35class WeakPtr final :
public ::pw::allocator::internal::WeakManagedPtr<T> {
37 using Base = ::pw::allocator::internal::WeakManagedPtr<T>;
38 using ControlBlock = ::pw::allocator::internal::ControlBlock;
41 using element_type =
typename Base::element_type;
106 template <
typename U>
113 void reset() noexcept;
117 std::swap(control_block_, other.control_block_);
123 return control_block_ ==
nullptr ? 0 : control_block_->num_shared();
136 template <typename PtrType>
138 return control_block_ < other.control_block();
146 ControlBlock* control_block()
const {
return control_block_; }
148 ControlBlock* control_block_ =
nullptr;
156 Base::template CheckAssignable<U>();
157 control_block_ = other.control_block_;
158 if (control_block_ !=
nullptr) {
159 control_block_->IncrementWeak();
167 Base::template CheckAssignable<U>();
168 control_block_ = other.control_block_;
169 if (control_block_ !=
nullptr) {
170 control_block_->IncrementWeak();
178 Base::template CheckAssignable<U>();
179 control_block_ = other.control_block_;
180 other.control_block_ =
nullptr;
186 if (control_block_ ==
nullptr ||
187 control_block_->DecrementWeak() != ControlBlock::Action::kFree) {
190 Allocator* allocator = control_block_->allocator();
191 Base::Deallocate(allocator, control_block_);
192 control_block_ =
nullptr;
197 if (control_block_ ==
nullptr || !control_block_->IncrementShared()) {
200 void* data = control_block_->data();
201 auto* t = std::launder(
reinterpret_cast<element_type*
>(data));
Definition: allocator.h:34
Definition: shared_ptr.h:57
Definition: weak_ptr.h:35
void reset() noexcept
Definition: weak_ptr.h:185
WeakPtr(WeakPtr< U > &&other) noexcept
Definition: weak_ptr.h:76
WeakPtr(const WeakPtr &other) noexcept
Copy-constructs a WeakPtr<T> from a WeakPtr<T>.
Definition: weak_ptr.h:50
WeakPtr & operator=(const SharedPtr< U > &other) noexcept
int32_t use_count() const noexcept
Definition: weak_ptr.h:122
WeakPtr(const WeakPtr< U > &other) noexcept
Definition: weak_ptr.h:57
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:83
SharedPtr< T > Lock() const noexcept
Definition: weak_ptr.h:196
WeakPtr(const SharedPtr< U > &other) noexcept
Definition: weak_ptr.h:66
constexpr WeakPtr() noexcept=default
Creates an empty (nullptr) instance.
bool expired() const noexcept
Definition: weak_ptr.h:128
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:116
bool owner_before(const PtrType &other) const noexcept
Definition: weak_ptr.h:137
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27