10 UniquePtr() : ptr(
nullptr), deleteFn() {}
12 explicit UniquePtr(T* ptr, std::function<
void(
void*)> newDeleteFn) : ptr(ptr), deleteFn(newDeleteFn) {}
14 UniquePtr(std::nullptr_t) : ptr(
nullptr), deleteFn() {}
17 UniquePtr(
const UniquePtr<T>& other) =
delete;
18 UniquePtr& operator=(
const UniquePtr<T>& other) =
delete;
20 UniquePtr& operator=(std::nullptr_t) {
25 UniquePtr(UniquePtr&& other) noexcept
26 : ptr(other.ptr), deleteFn(std::move(other.deleteFn)) {
28 other.deleteFn = std::function<void(
void*)>{};
32 UniquePtr& operator=(UniquePtr&& other)
noexcept {
33 UniquePtr tmp(std::move(other));
39 std::enable_if_t<std::is_convertible_v<U*, T*>,
int> = 0>
40 UniquePtr(UniquePtr<U>&& other) noexcept
41 : ptr(other.Get()), deleteFn(std::move(other.GetDeleter())) {
46 std::enable_if_t<std::is_convertible_v<U*, T*>,
int> = 0>
47 UniquePtr& operator=(UniquePtr<U>&& other)
noexcept {
48 if (
this !=
reinterpret_cast<UniquePtr<T>*
>(&other)) {
50 ptr = other.Release();
51 deleteFn = std::move(other.GetDeleter());
56 T* Release()
noexcept {
57 T* emptyPtr =
nullptr;
58 std::swap(emptyPtr, ptr);
59 deleteFn = std::function<void(
void*)>{};
64 void Reset()
noexcept {
75 deleteFn = std::function<void(
void*)>{};
78 void Swap(UniquePtr& other)
noexcept {
79 std::swap(ptr, other.ptr);
80 std::swap(deleteFn, other.deleteFn);
84 return ptr == other.ptr && deleteFn.target<void(
void)>() == other.deleteFn.target<
void(
void)>();
87 bool operator==(
const T* other)
const noexcept {
91 bool operator==(std::nullptr_t)
const noexcept {
92 return ptr ==
nullptr;
95 explicit operator T*()
const {
99 explicit operator bool()
const {
100 return ptr !=
nullptr;
107 const T* operator->()
const {
115 const T& operator*()
const {
123 const T* Get()
const {
127 const std::function<void(
void*)>& GetDeleter()
const {
128 return this->deleteFn;
136 bool operator==(
const UniquePtr<U>& other)
const noexcept {
137 static_assert(std::is_convertible_v<U*, T*> || std::is_convertible_v<T*, U*>,
138 "UniquePtr comparison requires compatible pointer types.");
139 return ptr == other.ptr && deleteFn == other.deleteFn;
144 std::function<void(
void*)> deleteFn;