From 1a5f3c9c54a3de6444304ce4d0985879ea6d7c6f Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Fri, 6 Jan 2023 19:14:35 +0800 Subject: [PATCH] refactor(templates): modify the TReferenceWrapper to be like std --- .../Public/Templates/ReferenceWrapper.h | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/Redcraft.Utility/Source/Public/Templates/ReferenceWrapper.h b/Redcraft.Utility/Source/Public/Templates/ReferenceWrapper.h index 924f921..6a3c610 100644 --- a/Redcraft.Utility/Source/Public/Templates/ReferenceWrapper.h +++ b/Redcraft.Utility/Source/Public/Templates/ReferenceWrapper.h @@ -23,31 +23,30 @@ public: using Type = ReferencedType; /** Constructs a new reference wrapper. */ - template requires (CConvertibleTo) + template requires (CConvertibleTo && !CSameAs>) FORCEINLINE constexpr TReferenceWrapper(T&& Object) - { - ReferencedType& Reference = Forward(Object); - Pointer = AddressOf(Reference); - } + : Pointer(AddressOf(static_cast(Forward(Object)))) + { } - /** Copies/moves content of other into a new instance. */ + /** Copies content of other into a new instance. */ FORCEINLINE constexpr TReferenceWrapper(const TReferenceWrapper&) = default; - FORCEINLINE constexpr TReferenceWrapper(TReferenceWrapper&&) = default; - + /** Converting copy constructor. */ template requires (CConvertibleTo) FORCEINLINE constexpr TReferenceWrapper(const TReferenceWrapper& InValue) : Pointer(InValue.Pointer) { } - /** Assign a value to the referenced object. */ - template requires (CAssignableFrom) - FORCEINLINE constexpr TReferenceWrapper& operator=(T&& Object) { Get() = Forward(Object); return *this; } + /** Assigns by copying the content of other. */ + FORCEINLINE constexpr TReferenceWrapper& operator=(const TReferenceWrapper&) = default; + + /** Assigns by copying the content of other. */ + template requires (CConvertibleTo) + FORCEINLINE constexpr TReferenceWrapper& operator=(const TReferenceWrapper& InValue) + { + Pointer = InValue.Pointer; + } - /** Remove the assignment operator, as rebinding is not allowed. */ - FORCEINLINE constexpr TReferenceWrapper& operator=(const TReferenceWrapper&) = delete; - FORCEINLINE constexpr TReferenceWrapper& operator=(TReferenceWrapper&&) = delete; - /** @return The stored reference. */ FORCEINLINE constexpr ReferencedType& Get() const { return *Pointer; } FORCEINLINE constexpr operator ReferencedType&() const { return *Pointer; } @@ -65,12 +64,6 @@ public: return GetTypeHash(A.Get()); } - /** Overloads the Swap algorithm for TReferenceWrapper. */ - friend FORCEINLINE constexpr void Swap(TReferenceWrapper A, TReferenceWrapper B) requires (CSwappable) - { - Swap(A.Get(), B.Get()); - } - private: ReferencedType* Pointer;