refactor(templates): modify the TReferenceWrapper to be like std

This commit is contained in:
_Redstone_c_ 2023-01-06 19:14:35 +08:00
parent 403f0721e1
commit 1a5f3c9c54

View File

@ -23,31 +23,30 @@ public:
using Type = ReferencedType; using Type = ReferencedType;
/** Constructs a new reference wrapper. */ /** Constructs a new reference wrapper. */
template <typename T = ReferencedType> requires (CConvertibleTo<T, ReferencedType&>) template <typename T = ReferencedType&> requires (CConvertibleTo<T&&, ReferencedType&> && !CSameAs<TReferenceWrapper, TRemoveCVRef<T>>)
FORCEINLINE constexpr TReferenceWrapper(T&& Object) FORCEINLINE constexpr TReferenceWrapper(T&& Object)
{ : Pointer(AddressOf(static_cast<ReferencedType&>(Forward<T>(Object))))
ReferencedType& Reference = Forward<T>(Object); { }
Pointer = AddressOf(Reference);
}
/** 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(const TReferenceWrapper&) = default;
FORCEINLINE constexpr TReferenceWrapper(TReferenceWrapper&&) = default;
/** Converting copy constructor. */ /** Converting copy constructor. */
template <typename T = ReferencedType> requires (CConvertibleTo<T&, ReferencedType&>) template <typename T = ReferencedType> requires (CConvertibleTo<T&, ReferencedType&>)
FORCEINLINE constexpr TReferenceWrapper(const TReferenceWrapper<T>& InValue) FORCEINLINE constexpr TReferenceWrapper(const TReferenceWrapper<T>& InValue)
: Pointer(InValue.Pointer) : Pointer(InValue.Pointer)
{ } { }
/** Assign a value to the referenced object. */ /** Assigns by copying the content of other. */
template <typename T = ReferencedType> requires (CAssignableFrom<ReferencedType&, T&&>) FORCEINLINE constexpr TReferenceWrapper& operator=(const TReferenceWrapper&) = default;
FORCEINLINE constexpr TReferenceWrapper& operator=(T&& Object) { Get() = Forward<T>(Object); return *this; }
/** Assigns by copying the content of other. */
template <typename T = ReferencedType> requires (CConvertibleTo<T&, ReferencedType&>)
FORCEINLINE constexpr TReferenceWrapper& operator=(const TReferenceWrapper<T>& 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. */ /** @return The stored reference. */
FORCEINLINE constexpr ReferencedType& Get() const { return *Pointer; } FORCEINLINE constexpr ReferencedType& Get() const { return *Pointer; }
FORCEINLINE constexpr operator ReferencedType&() const { return *Pointer; } FORCEINLINE constexpr operator ReferencedType&() const { return *Pointer; }
@ -65,12 +64,6 @@ public:
return GetTypeHash(A.Get()); return GetTypeHash(A.Get());
} }
/** Overloads the Swap algorithm for TReferenceWrapper. */
friend FORCEINLINE constexpr void Swap(TReferenceWrapper A, TReferenceWrapper B) requires (CSwappable<ReferencedType>)
{
Swap(A.Get(), B.Get());
}
private: private:
ReferencedType* Pointer; ReferencedType* Pointer;