16#ifndef PW_ALLOCATOR_PUBLIC_PW_ALLOCATOR_SHARED_PTR_H_
17#define PW_ALLOCATOR_PUBLIC_PW_ALLOCATOR_SHARED_PTR_H_
19#include "pw_allocator/config.h"
22#if PW_ALLOCATOR_HAS_ATOMICS
28#include "pw_allocator/allocator.h"
29#include "pw_allocator/deallocator.h"
30#include "pw_allocator/internal/control_block.h"
31#include "pw_allocator/internal/managed_ptr.h"
32#include "pw_allocator/layout.h"
33#include "pw_allocator/unique_ptr.h"
34#include "pw_multibuf/v2/properties.h"
44template <
typename To,
typename From>
47template <
typename To,
typename From>
82class SharedPtr final : public ::pw::allocator::internal::ManagedPtr<T> {
84 using Base = ::pw::allocator::internal::ManagedPtr<T>;
85 using ControlBlock = ::pw::allocator::internal::ControlBlock;
88 using element_type =
typename Base::element_type;
104 template <
typename... Args>
130 constexpr
SharedPtr(element_type* value, ControlBlock* control_block)
131 : Base(value), control_block_(control_block) {}
148 template <
typename U,
149 typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
159 template <
typename U,
160 typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
162 *
this = std::move(other);
180 operator= <T>(other);
190 template <
typename U,
191 typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
200 template <
typename U,
201 typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
223 template <
typename U>
225 return static_pointer_cast<U>(*
this);
240 template <
typename To,
typename From>
254 template <
typename To,
typename From>
260 [[nodiscard]]
friend constexpr bool operator==(
const SharedPtr& lhs,
262 return lhs.Equals(
nullptr);
264 [[nodiscard]]
friend constexpr bool operator==(std::nullptr_t,
266 return rhs.Equals(
nullptr);
268 [[nodiscard]]
friend constexpr bool operator==(
const SharedPtr& lhs,
269 const SharedPtr& rhs) {
270 return lhs.Equals(rhs) && lhs.control_block_ == rhs.control_block_;
273 [[nodiscard]]
friend constexpr bool operator!=(
const SharedPtr& lhs,
275 return !lhs.Equals(
nullptr);
277 [[nodiscard]]
friend constexpr bool operator!=(std::nullptr_t,
278 const SharedPtr& rhs) {
279 return !rhs.Equals(
nullptr);
281 [[nodiscard]]
friend constexpr bool operator!=(
const SharedPtr& lhs,
282 const SharedPtr& rhs) {
283 return !(lhs == rhs);
291 constexpr size_t size()
const {
292 static_assert(std::is_array_v<T>,
293 "size() cannot be called on non-array types");
294 return control_block_ ==
nullptr
296 : (control_block_->size() /
sizeof(element_type));
302 return control_block_ ==
nullptr ? 0 : control_block_->num_shared();
307 template <
typename PtrType>
309 return control_block_ < other.control_block();
327 return control_block_ ==
nullptr ? nullptr : control_block_->allocator();
345 ControlBlock* control_block()
const {
return control_block_; }
348 template <
typename U,
349 typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
350 void CopyFrom(
const SharedPtr<U>& other);
357 template <
typename U>
358 constexpr void CheckArrayTypes();
360 ControlBlock* control_block_ =
nullptr;
369 size_t size =
sizeof(element_type);
370 if constexpr (std::is_array_v<T>) {
371 size *= owned.
size();
373 control_block_ = ControlBlock::Create(owned.deallocator(), owned.get(), size);
374 if (control_block_ !=
nullptr) {
375 Base::CopyFrom(owned);
381template <
typename... Args>
383 static_assert(!std::is_array_v<T>);
384 auto* control_block = ControlBlock::Create(allocator, Layout::Of<T>());
385 if (control_block ==
nullptr) {
388 auto* t =
new (control_block->data()) T(std::forward<Args>(args)...);
389 return SharedPtr<T>(t, control_block);
396 static_assert(allocator::internal::is_unbounded_array_v<T>);
397 Layout layout = Layout::Of<T>(count).Align(alignment);
398 auto* control_block = ControlBlock::Create(allocator, layout);
399 if (control_block ==
nullptr) {
402 auto* t =
new (control_block->data()) element_type[count];
407template <
typename U,
typename>
410 CheckArrayTypes<U>();
413 if (control_block_ !=
nullptr) {
414 control_block_->IncrementShared();
420template <
typename U,
typename>
422 CheckArrayTypes<U>();
435template <
typename To,
typename From>
437 using PtrType =
typename SharedPtr<To>::element_type*;
438 if (p.control_block_ !=
nullptr) {
439 p.control_block_->IncrementShared();
441 return SharedPtr<To>{
static_cast<PtrType
>(p.get()), p.control_block_};
444template <
typename To,
typename From>
446 using PtrType =
typename SharedPtr<To>::element_type*;
447 if (p.control_block_ !=
nullptr) {
448 p.control_block_->IncrementShared();
450 return SharedPtr<To>{
const_cast<PtrType
>(p.get()), p.control_block_};
455 if (*
this ==
nullptr) {
458 auto action = control_block_->DecrementShared();
459 if (action == ControlBlock::Action::kNone) {
466 Allocator* allocator = control_block_->allocator();
467 if (!Base::HasCapability(allocator, allocator::kSkipsDestroy)) {
468 if constexpr (std::is_array_v<T>) {
469 Base::Destroy(size());
475 if (action == ControlBlock::Action::kExpire) {
478 Base::Resize(allocator, control_block_,
sizeof(ControlBlock));
481 std::destroy_at(control_block_);
482 Base::Deallocate(allocator, control_block_);
491 std::swap(control_block_, other.control_block_);
495template <
typename U,
typename>
497 CheckArrayTypes<U>();
498 Base::CopyFrom(other);
499 control_block_ = other.control_block_;
503void SharedPtr<T>::Release() {
505 control_block_ =
nullptr;
510constexpr void SharedPtr<T>::CheckArrayTypes() {
511 if constexpr (std::is_array_v<T>) {
512 static_assert(std::is_array_v<U>,
513 "non-array type used with SharedPtr<T[]>");
515 static_assert(!std::is_array_v<U>,
"array type used with SharedPtr<T>");
Definition: allocator.h:45
Definition: shared_ptr.h:82
static SharedPtr Create(Allocator *allocator, size_t count, size_t alignment)
Definition: shared_ptr.h:393
bool owner_before(const PtrType &other) const noexcept
Definition: shared_ptr.h:308
SharedPtr(UniquePtr< T > &owned) noexcept
Definition: shared_ptr.h:368
constexpr SharedPtr(std::nullptr_t) noexcept
Definition: shared_ptr.h:137
friend constexpr SharedPtr< To > const_pointer_cast(const SharedPtr< From > &p) noexcept
Definition: shared_ptr.h:445
void swap(SharedPtr &other) noexcept
Swaps the managed pointer and deallocator of this and another object.
Definition: shared_ptr.h:489
constexpr int32_t use_count() const
Definition: shared_ptr.h:301
SharedPtr & operator=(std::nullptr_t) noexcept
Definition: shared_ptr.h:430
constexpr SharedPtr & operator=(const SharedPtr< U > &other) noexcept
SharedPtr & operator=(SharedPtr< U > &&other) noexcept
static SharedPtr Create(Allocator *allocator, Args &&... args)
SharedPtr(SharedPtr< U > &&other) noexcept
Definition: shared_ptr.h:161
~SharedPtr()
Releases any currently-held value.
Definition: shared_ptr.h:92
constexpr size_t size() const
Definition: shared_ptr.h:291
friend constexpr SharedPtr< To > static_pointer_cast(const SharedPtr< From > &p) noexcept
Definition: shared_ptr.h:436
constexpr SharedPtr() noexcept=default
constexpr SharedPtr(const SharedPtr &other) noexcept
Copy-constructs a SharedPtr<T> from a SharedPtr<T>.
Definition: shared_ptr.h:140
constexpr SharedPtr(const SharedPtr< U > &other) noexcept
Definition: shared_ptr.h:150
constexpr SharedPtr & operator=(const SharedPtr &other) noexcept
Definition: shared_ptr.h:179
void reset() noexcept
Definition: shared_ptr.h:454
Definition: unique_ptr.h:43
Definition: weak_ptr.h:37
Definition: multibuf.h:184
Definition: multibuf.h:194
constexpr SharedPtr< To > static_pointer_cast(const SharedPtr< From > &p) noexcept
Definition: shared_ptr.h:436
constexpr SharedPtr< To > const_pointer_cast(const SharedPtr< From > &p) noexcept
Definition: shared_ptr.h:445
Property
Basic properties of a MultiBuf.
Definition: properties.h:24
BasicMultiBuf< Property::kLayerable > MultiBuf
Definition: multibuf.h:62
The Pigweed namespace.
Definition: alignment.h:27