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"
43class UniquePtr :
public ::pw::allocator::internal::ManagedPtr<T> {
45 using Base = ::pw::allocator::internal::ManagedPtr<T>;
46 using Empty = ::pw::allocator::internal::Empty;
49 using pointer =
typename Base::element_type*;
50 using element_type =
typename Base::element_type;
57 if constexpr (std::is_array_v<T>) {
84 static_assert(!allocator::internal::is_unbounded_array_v<T>,
85 "UniquePtr for unbounded array type must provide size");
86 if constexpr (allocator::internal::is_bounded_array_v<T>) {
87 size_ = std::extent_v<T>;
94 allocator::internal::is_unbounded_array_v<T>,
95 "UniquePtr must not provide size unless type is an unbounded array");
104 template <
typename U>
106 *
this = std::move(other);
118 template <
typename U>
131 static_assert(std::is_array_v<T>,
132 "size() cannot be called with a non-array type");
143 element_type*
Release() noexcept;
149 void Reset() noexcept;
163 template <typename U>
164 void CopyFrom(const
UniquePtr<U>& other);
170 std::conditional_t<std::is_array_v<T>,
size_t, Empty> size_;
181using UniquePtr = PW_ALLOCATOR_DEPRECATED ::pw::UniquePtr<T>;
206 element_type* value = Base::Release();
207 deallocator_ =
nullptr;
213 if (*
this ==
nullptr) {
216 if (!Base::HasCapability(deallocator_, allocator::kSkipsDestroy)) {
217 if constexpr (std::is_array_v<T>) {
218 Base::Destroy(size_);
225 auto* ptr =
const_cast<std::remove_const_t<element_type>*
>(
Release());
232 if constexpr (std::is_array_v<T>) {
233 std::swap(size_, other.size_);
235 std::swap(deallocator_, other.deallocator_);
241 static_assert(std::is_array_v<T> == std::is_array_v<U>);
242 Base::CopyFrom(other);
243 if constexpr (std::is_array_v<T>) {
246 deallocator_ = other.deallocator_;
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: unique_ptr.h:43
UniquePtr(UniquePtr< U > &&other) noexcept
Definition: unique_ptr.h:105
element_type * Release() noexcept
Definition: unique_ptr.h:205
Deallocator * deallocator() const
Returns a pointer to the object that can destroy the value.
Definition: unique_ptr.h:137
void Reset() noexcept
Definition: unique_ptr.h:212
UniquePtr(element_type *value, Deallocator &deallocator)
Definition: unique_ptr.h:82
constexpr UniquePtr() noexcept
Definition: unique_ptr.h:56
size_t size() const
Definition: unique_ptr.h:130
UniquePtr & operator=(UniquePtr< U > &&other) noexcept
~UniquePtr()
Frees any currently-held value.
Definition: unique_ptr.h:110
constexpr UniquePtr(std::nullptr_t) noexcept
Definition: unique_ptr.h:66
void Swap(UniquePtr &other)
Swaps the managed pointer and deallocator of this and another object.
Definition: unique_ptr.h:230
#define PW_NO_UNIQUE_ADDRESS
Definition: compiler.h:297
The Pigweed namespace.
Definition: alignment.h:27