19#include "pw_allocator/internal/control_block.h"
20#include "pw_allocator/internal/managed_ptr.h"
21#include "pw_allocator/shared_ptr.h"
30class WeakPtr final :
public ::pw::allocator::internal::WeakManagedPtr<T> {
32 using Base = ::pw::allocator::internal::WeakManagedPtr<T>;
33 using ControlBlock = ::pw::allocator::internal::ControlBlock;
36 using element_type =
typename Base::element_type;
101 template <
typename U>
108 void reset() noexcept;
112 std::swap(control_block_, other.control_block_);
118 return control_block_ ==
nullptr ? 0 : control_block_->num_shared();
131 template <typename PtrType>
133 return control_block_ < other.control_block();
141 ControlBlock* control_block()
const {
return control_block_; }
143 ControlBlock* control_block_ =
nullptr;
151 Base::template CheckAssignable<U>();
152 control_block_ = other.control_block_;
153 if (control_block_ !=
nullptr) {
154 control_block_->IncrementWeak();
162 Base::template CheckAssignable<U>();
163 control_block_ = other.control_block_;
164 if (control_block_ !=
nullptr) {
165 control_block_->IncrementWeak();
173 Base::template CheckAssignable<U>();
174 control_block_ = other.control_block_;
175 other.control_block_ =
nullptr;
181 if (control_block_ ==
nullptr ||
182 control_block_->DecrementWeak() != ControlBlock::Action::kFree) {
185 Allocator* allocator = control_block_->allocator();
186 Base::Deallocate(allocator, control_block_);
187 control_block_ =
nullptr;
192 if (control_block_ ==
nullptr || !control_block_->IncrementShared()) {
195 void* data = control_block_->data();
196 auto* t = std::launder(
reinterpret_cast<element_type*
>(data));
Definition: allocator.h:34
Definition: shared_ptr.h:44
Definition: weak_ptr.h:30
void reset() noexcept
Definition: weak_ptr.h:180
WeakPtr(WeakPtr< U > &&other) noexcept
Definition: weak_ptr.h:71
uint32_t use_count() const noexcept
Definition: weak_ptr.h:117
WeakPtr(const WeakPtr &other) noexcept
Copy-constructs a WeakPtr<T> from a WeakPtr<T>.
Definition: weak_ptr.h:45
WeakPtr & operator=(const SharedPtr< U > &other) noexcept
WeakPtr(const WeakPtr< U > &other) noexcept
Definition: weak_ptr.h:52
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:78
SharedPtr< T > Lock() const noexcept
Definition: weak_ptr.h:191
WeakPtr(const SharedPtr< U > &other) noexcept
Definition: weak_ptr.h:61
constexpr WeakPtr() noexcept=default
Creates an empty (nullptr) instance.
bool expired() const noexcept
Definition: weak_ptr.h:123
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:111
bool owner_before(const PtrType &other) const noexcept
Definition: weak_ptr.h:132
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27