From c7e3ac32b47efed7c4d46b33d6b97b3a96c6eb71 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Sat, 7 Jan 2023 11:53:29 +0800 Subject: [PATCH] refactor(templates): TUniquePtr calls the deleter using Invoke() instead --- .../Source/Public/Templates/UniquePointer.h | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/Redcraft.Utility/Source/Public/Templates/UniquePointer.h b/Redcraft.Utility/Source/Public/Templates/UniquePointer.h index ec05de6..97e338e 100644 --- a/Redcraft.Utility/Source/Public/Templates/UniquePointer.h +++ b/Redcraft.Utility/Source/Public/Templates/UniquePointer.h @@ -1,6 +1,7 @@ #pragma once #include "CoreTypes.h" +#include "Templates/Invoke.h" #include "Templates/Utility.h" #include "Templates/Noncopyable.h" #include "TypeTraits/PrimaryType.h" @@ -123,7 +124,7 @@ struct TDefaultDelete }; /** This is essentially a reference version of TUniquePtr. */ -template *> E = TDefaultDelete> requires (CObject && !CBoundedArray && !CRValueReference) +template *> E = TDefaultDelete> requires (CObject && !CBoundedArray && (CDestructible || CLValueReference)) class TUniqueRef final : private FSingleton { public: @@ -151,7 +152,7 @@ public: } /** Destroy the owned object. */ - FORCEINLINE constexpr ~TUniqueRef() { GetDeleter()(Get()); } + FORCEINLINE constexpr ~TUniqueRef() { Invoke(GetDeleter(), Get()); } /** Compares the pointer values of two TUniqueRef. */ NODISCARD friend FORCEINLINE constexpr bool operator==(const TUniqueRef& LHS, const TUniqueRef& RHS) { return LHS.Get() == RHS.Get(); } @@ -172,7 +173,7 @@ public: FORCEINLINE constexpr void Reset(T* InPtr) { checkf(InPtr != nullptr, TEXT("TUniqueRef cannot be initialized by nullptr. Please use TUniquePtr.")); - GetDeleter()(Get()); + Invoke(GetDeleter(), Get()); Storage.GetPointer() = InPtr; } @@ -214,8 +215,8 @@ public: NODISCARD FORCEINLINE constexpr T* Get() const { return Storage.GetPointer(); } /** @return The deleter that is used for destruction of the managed object. */ - NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } - NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } /** @return The a reference or pointer to the object owned by *this, i.e. Get(). */ NODISCARD FORCEINLINE constexpr T& operator*() const { return *Get(); } @@ -271,7 +272,7 @@ public: } /** Destroy the owned array. */ - FORCEINLINE constexpr ~TUniqueRef() { GetDeleter()(Get()); } + FORCEINLINE constexpr ~TUniqueRef() { Invoke(GetDeleter(), Get()); } /** Compares the pointer values of two TUniqueRef. */ NODISCARD friend FORCEINLINE constexpr bool operator==(const TUniqueRef& LHS, const TUniqueRef& RHS) { return LHS.Get() == RHS.Get(); } @@ -295,7 +296,7 @@ public: FORCEINLINE constexpr void Reset(U InPtr) { checkf(InPtr != nullptr, TEXT("TUniqueRef cannot be initialized by nullptr. Please use TUniquePtr.")); - GetDeleter()(Get()); + Invoke(GetDeleter(), Get()); Storage.GetPointer() = InPtr; } @@ -340,8 +341,8 @@ public: NODISCARD FORCEINLINE constexpr T* Get() const { return Storage.GetPointer(); } /** @return The deleter that is used for destruction of the managed array. */ - NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } - NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } /** @return The element at index, i.e. Get()[Index]. */ NODISCARD FORCEINLINE constexpr T& operator[](size_t Index) const { return Get()[Index]; } @@ -363,7 +364,7 @@ private: }; /** Single-ownership smart pointer. Use this when you need an object's lifetime to be strictly bound to the lifetime of a single smart pointer. */ -template *> E = TDefaultDelete> requires (CObject && !CBoundedArray && !CRValueReference) +template *> E = TDefaultDelete> requires (CObject && !CBoundedArray && (CDestructible || CLValueReference)) class TUniquePtr final : private FNoncopyable { public: @@ -393,10 +394,10 @@ public: FORCEINLINE constexpr TUniquePtr(TUniquePtr&& InValue) : Storage(InValue.Release(), Forward(InValue.GetDeleter())) { } /** If !IsValid() there are no effects. Otherwise, the owned object is destroyed. */ - FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) GetDeleter()(Get()); } + FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) Invoke(GetDeleter(), Get()); } /** Move assignment operator. Transfers ownership from 'InValue' to *this. */ - FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) + FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) requires (!CReference && CAssignableFrom) { Reset(InValue.Release()); GetDeleter() = Forward(InValue.GetDeleter()); @@ -434,7 +435,7 @@ public: /** Replaces the managed object. */ FORCEINLINE constexpr void Reset(T* InPtr = nullptr) { - if (IsValid()) GetDeleter()(Get()); + if (IsValid()) Invoke(GetDeleter(), Get()); Storage.GetPointer() = InPtr; } @@ -464,8 +465,8 @@ public: NODISCARD FORCEINLINE constexpr T* Get() const { return Storage.GetPointer(); } /** @return The deleter that is used for destruction of the managed object. */ - NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } - NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } /** @return true if *this owns an object, false otherwise. */ NODISCARD FORCEINLINE constexpr bool IsValid() const { return Get() != nullptr; } @@ -489,7 +490,7 @@ private: NAMESPACE_PRIVATE::TUniqueStorage Storage; - template *> OtherE> requires (CObject && !CBoundedArray && !CRValueReference) + template *> OtherE> requires (CObject && !CBoundedArray && (CDestructible || CLValueReference)) friend class TUniquePtr; }; @@ -525,15 +526,15 @@ public: FORCEINLINE constexpr TUniquePtr(TUniquePtr&& InValue) : Storage(InValue.Release(), Forward(InValue.GetDeleter())) { } /** Constructs a TUniquePtr by transferring ownership from 'InValue' to *this and stores the nullptr in 'InValue'. */ - template requires (CConvertibleTo && CArray + template requires (CConvertibleTo && CArray && ((CReference && CSameAs) || (!CReference && CConvertibleTo))) FORCEINLINE constexpr TUniquePtr(TUniquePtr&& InValue) : Storage(InValue.Release(), Forward(InValue.GetDeleter())) { } /** If !IsValid() there are no effects. Otherwise, the owned array is destroyed. */ - FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) GetDeleter()(Get()); } + FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) Invoke(GetDeleter(), Get()); } /** Move assignment operator. Transfers ownership from 'InValue' to *this. */ - FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) + FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) requires (!CReference && CAssignableFrom) { Reset(InValue.Release()); GetDeleter() = Forward(InValue.GetDeleter()); @@ -541,7 +542,7 @@ public: } /** Move assignment operator. Transfers ownership from 'InValue' to *this. */ - template requires (CConvertibleTo + template requires (CConvertibleTo && CArray && !CReference && CAssignableFrom) FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) { @@ -574,7 +575,7 @@ public: template requires (CNullPointer || (CPointer && CConvertibleTo(*)[], T(*)[]>)) FORCEINLINE constexpr void Reset(U InPtr = nullptr) { - if (IsValid()) GetDeleter()(Get()); + if (IsValid()) Invoke(GetDeleter(), Get()); Storage.GetPointer() = InPtr; } @@ -607,8 +608,8 @@ public: NODISCARD FORCEINLINE constexpr T* Get() const { return Storage.GetPointer(); } /** @return The deleter that is used for destruction of the managed array. */ - NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } - NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr E& GetDeleter() { return Storage.GetDeleter(); } + NODISCARD FORCEINLINE constexpr const E& GetDeleter() const { return Storage.GetDeleter(); } /** @return true if *this owns an array, false otherwise. */ NODISCARD FORCEINLINE constexpr bool IsValid() const { return Get() != nullptr; } @@ -631,7 +632,7 @@ private: NAMESPACE_PRIVATE::TUniqueStorage Storage; - template *> OtherE> requires (CObject && !CBoundedArray && !CRValueReference) + template *> OtherE> requires (CObject && !CBoundedArray && (CDestructible || CLValueReference)) friend class TUniquePtr; };