From 0c54bbe2acee905d54f417ddeae3873cef0eeff3 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 13 Feb 2023 20:58:08 +0800 Subject: [PATCH] refactor(memory): remove FNoInit versions of MakeUnique() and MakeShared() --- .../Source/Private/Testing/MemoryTesting.cpp | 24 ---------- .../Source/Public/Memory/SharedPointer.h | 47 ++++++------------- .../Source/Public/Memory/UniquePointer.h | 22 +++++---- .../Source/Public/Templates/Optional.h | 4 +- 4 files changed, 29 insertions(+), 68 deletions(-) diff --git a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp index d849f92..7be45b2 100644 --- a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp @@ -379,12 +379,6 @@ void TestUniquePointer() always_check( FCounter::Num == 0); always_check(FArrayDeleter::Num == 4); - { - TUniquePtr Temp = MakeUnique(NoInit); - *Temp = 15; - always_check(*Temp.Get() = 15); - } - { TUniquePtr Temp = MakeUnique(); *Temp = 15; @@ -450,12 +444,6 @@ void TestUniquePointer() always_check(FCounter::Num == 0); always_check(FDeleter::Num == 4); - { - TUniquePtr Temp = MakeUnique(4, NoInit); - Temp[0] = 15; - always_check(Temp.Get()[0] = 15); - } - { TUniquePtr Temp = MakeUnique(4); Temp[0] = 15; @@ -581,12 +569,6 @@ void TestSharedPointer() always_check(FCounter::Num == 0); always_check(FDeleter::Num == 4); - { - TSharedRef Temp = MakeShared(4, NoInit); - Temp[0] = 15; - always_check(Temp.Get()[0] = 15); - } - { TSharedRef Temp = MakeShared(4); Temp[0] = 15; @@ -684,12 +666,6 @@ void TestSharedPointer() always_check(FCounter::Num == 0); always_check(FDeleter::Num == 5); - { - TSharedPtr Temp = MakeShared(4, NoInit); - Temp[0] = 15; - always_check(Temp.Get()[0] = 15); - } - { TSharedPtr Temp = MakeShared(4); Temp[0] = 15; diff --git a/Redcraft.Utility/Source/Public/Memory/SharedPointer.h b/Redcraft.Utility/Source/Public/Memory/SharedPointer.h index 7bbc91e..a337533 100644 --- a/Redcraft.Utility/Source/Public/Memory/SharedPointer.h +++ b/Redcraft.Utility/Source/Public/Memory/SharedPointer.h @@ -241,10 +241,18 @@ class TSharedControllerWithObject final : public FSharedController { public: - FORCEINLINE explicit TSharedControllerWithObject(FNoInit) requires (!CConstructibleFrom) { new (&Storage) T; } - - template requires (CConstructibleFrom) - FORCEINLINE explicit TSharedControllerWithObject(Ts&&... Args) { new (&Storage) T(Forward(Args)...); } + template + FORCEINLINE explicit TSharedControllerWithObject(Ts&&... Args) + { + if constexpr (sizeof...(Ts) == 0) + { + new (&Storage) T; + } + else + { + new (&Storage) T(Forward(Args)...); + } + } virtual ~TSharedControllerWithObject() = default; @@ -263,20 +271,11 @@ class TSharedControllerWithArray final : public FSharedController { public: - static TSharedControllerWithArray* New(size_t N, FNoInit) - { - void* Buffer = Memory::Malloc(sizeof(TSharedControllerWithArray) + sizeof(T) * (N - 1), alignof(TSharedControllerWithArray)); - const auto Controller = new (Buffer) TSharedControllerWithArray(N); - const T* ElementPtr = new (Controller->GetPointer()) T[N]; - check(ElementPtr == Controller->GetPointer()); - return Controller; - } - static TSharedControllerWithArray* New(size_t N) { void* Buffer = Memory::Malloc(sizeof(TSharedControllerWithArray) + sizeof(T) * (N - 1), alignof(TSharedControllerWithArray)); const auto Controller = new (Buffer) TSharedControllerWithArray(N); - const T* ElementPtr = new (Controller->GetPointer()) T[N](); + const T* ElementPtr = new (Controller->GetPointer()) T[N]; check(ElementPtr == Controller->GetPointer()); return Controller; } @@ -674,7 +673,7 @@ public: return ControllerWithDeleter != nullptr ? &ControllerWithDeleter->GetDeleter() : nullptr; } - /** @return The a reference or pointer to the object owned by *this, i.e. Get(). */ + /** @return The reference or pointer to the object owned by *this, i.e. Get(). */ NODISCARD FORCEINLINE constexpr T& operator*() const { return *Get(); } NODISCARD FORCEINLINE constexpr T* operator->() const { return Get(); } @@ -1143,7 +1142,7 @@ public: NODISCARD FORCEINLINE constexpr bool IsValid() const { return Get() != nullptr; } NODISCARD FORCEINLINE constexpr explicit operator bool() const { return Get() != nullptr; } - /** @return The a reference or pointer to the object owned by *this, i.e. Get(). */ + /** @return The reference or pointer to the object owned by *this, i.e. Get(). */ NODISCARD FORCEINLINE constexpr T& operator*() const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return *Get(); } NODISCARD FORCEINLINE constexpr T* operator->() const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return Get(); } @@ -1748,14 +1747,6 @@ private: }; -/** Constructs an object of type T and wraps it in a TSharedRef or TSharedPtr. Without initialization. */ -template requires (CObject && !CArray && !CConstructibleFrom && CDestructible) -NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy MakeShared(FNoInit) -{ - const auto Controller = new NAMESPACE_PRIVATE::TSharedControllerWithObject(NoInit); - return NAMESPACE_PRIVATE::TSharedProxy(Controller->GetPointer(), Controller); -} - /** Constructs an object of type T and wraps it in a TSharedRef or TSharedPtr. */ template requires (CObject && !CArray && CConstructibleFrom && CDestructible) NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy MakeShared(Ts&&... Args) @@ -1764,14 +1755,6 @@ NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy MakeShared(Ts&&... Args return NAMESPACE_PRIVATE::TSharedProxy(Controller->GetPointer(), Controller); } -/** Constructs an array of type T and wraps it in a TSharedRef or TSharedPtr. Without initialization. */ -template requires (CUnboundedArray && CDefaultConstructible> && CDestructible>) -NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy MakeShared(size_t N, FNoInit) -{ - const auto Controller = NAMESPACE_PRIVATE::TSharedControllerWithArray>::New(N, NoInit); - return NAMESPACE_PRIVATE::TSharedProxy(Controller->GetPointer(), Controller); -} - /** Constructs an array of type T and wraps it in a TSharedRef or TSharedPtr. */ template requires (CUnboundedArray && CDefaultConstructible> && CDestructible>) NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy MakeShared(size_t N) diff --git a/Redcraft.Utility/Source/Public/Memory/UniquePointer.h b/Redcraft.Utility/Source/Public/Memory/UniquePointer.h index 501df78..d7c137e 100644 --- a/Redcraft.Utility/Source/Public/Memory/UniquePointer.h +++ b/Redcraft.Utility/Source/Public/Memory/UniquePointer.h @@ -659,21 +659,23 @@ private: }; -/** Constructs an object of type T and wraps it in a TUniquePtr. Without initialization. */ -template requires (CObject && !CArray && !CConstructibleFrom && CDestructible) -NODISCARD FORCEINLINE constexpr TUniquePtr MakeUnique(FNoInit) { return TUniquePtr(new T); } - /** Constructs an object of type T and wraps it in a TUniquePtr. */ template requires (CObject && !CArray && CConstructibleFrom && CDestructible) -NODISCARD FORCEINLINE constexpr TUniquePtr MakeUnique(Ts&&... Args) { return TUniquePtr(new T(Forward(Args)...)); } - -/** Constructs an array of type T and wraps it in a TUniquePtr. Without initialization. */ -template requires (CUnboundedArray && CDefaultConstructible> && CDestructible>) -NODISCARD FORCEINLINE constexpr TUniquePtr MakeUnique(size_t N, FNoInit) { return TUniquePtr(new TRemoveExtent[N]); } +NODISCARD FORCEINLINE constexpr TUniquePtr MakeUnique(Ts&&... Args) +{ + if constexpr (sizeof...(Ts) == 0) + { + return TUniquePtr(new T); + } + else + { + return TUniquePtr(new T(Forward(Args)...)); + } +} /** Constructs an array of type T and wraps it in a TUniquePtr. */ template requires (CUnboundedArray && CDefaultConstructible> && CDestructible>) -NODISCARD FORCEINLINE constexpr TUniquePtr MakeUnique(size_t N) { return TUniquePtr(new TRemoveExtent[N]()); } +NODISCARD FORCEINLINE constexpr TUniquePtr MakeUnique(size_t N) { return TUniquePtr(new TRemoveExtent[N]); } /** Construction of arrays of known bound is disallowed. */ template requires (CBoundedArray) diff --git a/Redcraft.Utility/Source/Public/Templates/Optional.h b/Redcraft.Utility/Source/Public/Templates/Optional.h index 3987917..87b472d 100644 --- a/Redcraft.Utility/Source/Public/Templates/Optional.h +++ b/Redcraft.Utility/Source/Public/Templates/Optional.h @@ -245,14 +245,14 @@ public: } /** Check if the optional value is equivalent to 'InValue'. */ - template requires (!CTOptional&& CWeaklyEqualityComparable) + template requires (!CTOptional && CWeaklyEqualityComparable) NODISCARD FORCEINLINE constexpr bool operator==(const T& InValue) const& { return IsValid() ? GetValue() == InValue : false; } /** Check that the optional value is in ordered relationship with 'InValue'. */ - template requires (!CTOptional&& CSynthThreeWayComparable) + template requires (!CTOptional && CSynthThreeWayComparable) NODISCARD FORCEINLINE constexpr partial_ordering operator<=>(const T& InValue) const& { return IsValid() ? SynthThreeWayCompare(GetValue(), InValue) : partial_ordering::unordered;