20#include "pw_allocator/config.h"
21#include "pw_allocator/internal/managed_ptr.h"
22#include "pw_allocator/layout.h"
23#include "pw_preprocessor/compiler.h"
41class UniquePtr :
public ::pw::allocator::internal::ManagedPtr<T> {
43 using Base = ::pw::allocator::internal::ManagedPtr<T>;
44 using Empty = ::pw::allocator::internal::Empty;
47 using pointer =
typename Base::element_type*;
48 using element_type =
typename Base::element_type;
55 if constexpr (std::is_array_v<T>) {
82 static_assert(!allocator::internal::is_unbounded_array_v<T>,
83 "UniquePtr for unbounded array type must provide size");
84 if constexpr (allocator::internal::is_bounded_array_v<T>) {
85 size_ = std::extent_v<T>;
92 allocator::internal::is_unbounded_array_v<T>,
93 "UniquePtr must not provide size unless type is an unbounded array");
102 template <
typename U>
104 *
this = std::move(other);
116 template <
typename U>
129 static_assert(std::is_array_v<T>,
130 "size() cannot be called with a non-array type");
141 element_type*
Release() noexcept;
147 void Reset() noexcept;
161 template <typename U>
162 void CopyFrom(const
UniquePtr<U>& other);
168 std::conditional_t<std::is_array_v<T>,
size_t, Empty> size_;
179using UniquePtr = PW_ALLOCATOR_DEPRECATED ::pw::UniquePtr<T>;
202 element_type* value = Base::Release();
203 deallocator_ =
nullptr;
209 if (*
this ==
nullptr) {
212 if (!Base::HasCapability(deallocator_, allocator::kSkipsDestroy)) {
213 if constexpr (std::is_array_v<T>) {
214 Base::Destroy(size_);
220 auto* ptr =
const_cast<std::remove_const_t<element_type>*
>(
Release());
227 if constexpr (std::is_array_v<T>) {
228 std::swap(size_, other.size_);
230 std::swap(deallocator_, other.deallocator_);
236 static_assert(std::is_array_v<T> == std::is_array_v<U>);
237 Base::CopyFrom(other);
238 if constexpr (std::is_array_v<T>) {
241 deallocator_ = other.deallocator_;
Abstract interface for releasing memory.
Definition: deallocator.h:27
Definition: unique_ptr.h:41
UniquePtr(UniquePtr< U > &&other) noexcept
Definition: unique_ptr.h:103
element_type * Release() noexcept
Definition: unique_ptr.h:201
Deallocator * deallocator() const
Returns a pointer to the object that can destroy the value.
Definition: unique_ptr.h:135
void Reset() noexcept
Definition: unique_ptr.h:208
UniquePtr(element_type *value, Deallocator &deallocator)
Definition: unique_ptr.h:80
constexpr UniquePtr() noexcept
Definition: unique_ptr.h:54
size_t size() const
Definition: unique_ptr.h:128
UniquePtr & operator=(UniquePtr< U > &&other) noexcept
~UniquePtr()
Frees any currently-held value.
Definition: unique_ptr.h:108
constexpr UniquePtr(std::nullptr_t) noexcept
Definition: unique_ptr.h:64
void Swap(UniquePtr &other)
Swaps the managed pointer and deallocator of this and another object.
Definition: unique_ptr.h:225
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27