19#include "pw_allocator/internal/control_block.h"
20#include "pw_allocator/internal/managed_ptr.h"
44class SharedPtr final :
public ::pw::allocator::internal::ManagedPtr<T> {
46 using Base = ::pw::allocator::internal::ManagedPtr<T>;
47 using ControlBlock = ::pw::allocator::internal::ControlBlock;
50 using element_type =
typename Base::element_type;
89 *
this = std::move(other);
106 template <
typename U>
115 template <
typename U>
128 static_assert(std::is_array_v<T>,
129 "size() cannot be called on non-array types");
130 return control_block_ ==
nullptr ? 0 : control_block_->size();
136 return control_block_ ==
nullptr ? 0 : control_block_->num_shared();
144 void reset() noexcept;
151 template <typename PtrType>
153 return control_block_ < other.control_block();
175 SharedPtr(element_type* value, ControlBlock* control_block)
176 : Base(value), control_block_(control_block) {}
178 ControlBlock* control_block()
const {
return control_block_; }
181 template <
typename U>
182 void CopyFrom(
const SharedPtr<U>& other);
189 template <
typename U>
190 constexpr void CheckArrayTypes();
192 ControlBlock* control_block_ =
nullptr;
200 const SharedPtr<U>& other)
noexcept {
201 CheckArrayTypes<U>();
203 if (control_block_ !=
nullptr) {
204 control_block_->IncrementShared();
212 CheckArrayTypes<U>();
227 if (*
this ==
nullptr) {
230 auto action = control_block_->DecrementShared();
231 if (action == ControlBlock::Action::kNone) {
238 Allocator* allocator = control_block_->allocator();
239 if (!Base::HasCapability(allocator, allocator::kSkipsDestroy)) {
240 if constexpr (std::is_array_v<T>) {
241 Base::Destroy(control_block_->size());
247 if (action == ControlBlock::Action::kExpire) {
250 Base::Resize(allocator, control_block_,
sizeof(ControlBlock));
253 Base::Deallocate(allocator, control_block_);
262 std::swap(control_block_, other.control_block_);
268 CheckArrayTypes<U>();
269 Base::CopyFrom(other);
270 control_block_ = other.control_block_;
274void SharedPtr<T>::Release() {
276 control_block_ =
nullptr;
281constexpr void SharedPtr<T>::CheckArrayTypes() {
282 if constexpr (std::is_array_v<T>) {
283 static_assert(std::is_array_v<U>,
284 "non-array type used with SharedPtr<T[]>");
286 static_assert(!std::is_array_v<U>,
"array type used with SharedPtr<T>");
Definition: allocator.h:34
Definition: shared_ptr.h:44
SharedPtr(SharedPtr< U > &&other) noexcept
Definition: shared_ptr.h:88
SharedPtr & operator=(SharedPtr< U > &&other) noexcept
bool owner_before(const PtrType &other) const noexcept
Definition: shared_ptr.h:152
size_t size() const
Definition: shared_ptr.h:127
void swap(SharedPtr &other) noexcept
Swaps the managed pointer and deallocator of this and another object.
Definition: shared_ptr.h:260
size_t use_count() const
Definition: shared_ptr.h:135
~SharedPtr()
Releases any currently-held value.
Definition: shared_ptr.h:54
constexpr SharedPtr & operator=(const SharedPtr< U > &other) noexcept
constexpr SharedPtr() noexcept=default
constexpr SharedPtr(const SharedPtr &other) noexcept
Copy-constructs a SharedPtr<T> from a SharedPtr<T>.
Definition: shared_ptr.h:69
constexpr SharedPtr & operator=(const SharedPtr &other) noexcept
Definition: shared_ptr.h:95
constexpr SharedPtr(const SharedPtr< U > &other) noexcept
Definition: shared_ptr.h:78
void reset() noexcept
Definition: shared_ptr.h:226
Definition: weak_ptr.h:30
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27