2022-02-09 13:46:39 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
|
|
|
#include "Templates/Invoke.h"
|
|
|
|
#include "Templates/Utility.h"
|
2022-05-03 14:20:08 +00:00
|
|
|
#include "Templates/Optional.h"
|
2022-02-09 13:46:39 +00:00
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* TReferenceWrapper is a class template that wraps a reference object.
|
|
|
|
* It is frequently used as a mechanism to store references inside standard
|
|
|
|
* containers which cannot normally hold references.
|
|
|
|
*/
|
2022-05-15 15:10:02 +00:00
|
|
|
template <typename ReferencedType> requires (CObject<ReferencedType> || CFunction<ReferencedType>)
|
2022-12-30 11:11:01 +00:00
|
|
|
class TReferenceWrapper final
|
2022-02-09 13:46:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2022-05-03 14:20:08 +00:00
|
|
|
using Type = ReferencedType;
|
2022-02-09 13:46:39 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Constructs a new reference wrapper. */
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T = ReferencedType> requires (CConvertibleTo<T, ReferencedType&>)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper(T&& Object)
|
2022-11-15 14:15:10 +00:00
|
|
|
{
|
|
|
|
ReferencedType& Reference = Forward<T>(Object);
|
|
|
|
Pointer = AddressOf(Reference);
|
|
|
|
}
|
2022-02-09 13:46:39 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Copies/moves content of other into a new instance. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper(const TReferenceWrapper&) = default;
|
2022-12-13 14:11:10 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper(TReferenceWrapper&&) = default;
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
/** Converting copy constructor. */
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T = ReferencedType> requires (CConvertibleTo<T&, ReferencedType&>)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper(const TReferenceWrapper<T>& InValue)
|
2022-05-03 14:20:08 +00:00
|
|
|
: Pointer(InValue.Pointer)
|
|
|
|
{ }
|
2022-02-09 13:46:39 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Assign a value to the referenced object. */
|
2022-12-13 14:11:10 +00:00
|
|
|
template <typename T = ReferencedType> requires (CAssignableFrom<ReferencedType&, T&&>)
|
|
|
|
FORCEINLINE constexpr TReferenceWrapper& operator=(T&& Object) { Get() = Forward<T>(Object); return *this; }
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Remove the assignment operator, as rebinding is not allowed. */
|
2022-12-13 14:11:10 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper& operator=(const TReferenceWrapper&) = delete;
|
|
|
|
FORCEINLINE constexpr TReferenceWrapper& operator=(TReferenceWrapper&&) = delete;
|
2022-05-03 14:20:08 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** @return The stored reference. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr ReferencedType& Get() const { return *Pointer; }
|
2022-12-29 13:55:02 +00:00
|
|
|
FORCEINLINE constexpr operator ReferencedType&() const { return *Pointer; }
|
2022-02-09 13:46:39 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Calls the Callable object, reference to which is stored. */
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename... Ts>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TInvokeResult<ReferencedType&, Ts...> operator()(Ts&&... Args) const
|
2022-02-09 13:46:39 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return Invoke(Get(), Forward<Ts>(Args)...);
|
2022-02-09 13:46:39 +00:00
|
|
|
}
|
2022-05-03 14:20:08 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Overloads the GetTypeHash algorithm for TReferenceWrapper. */
|
|
|
|
NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(TReferenceWrapper A) requires (CHashable<ReferencedType>)
|
2022-05-03 14:20:08 +00:00
|
|
|
{
|
2022-12-19 10:00:52 +00:00
|
|
|
return GetTypeHash(A.Get());
|
2022-05-03 14:20:08 +00:00
|
|
|
}
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
/** Overloads the Swap algorithm for TReferenceWrapper. */
|
|
|
|
friend FORCEINLINE constexpr void Swap(TReferenceWrapper A, TReferenceWrapper B) requires (CSwappable<ReferencedType>)
|
|
|
|
{
|
|
|
|
Swap(A.Get(), B.Get());
|
|
|
|
}
|
|
|
|
|
2022-02-09 13:46:39 +00:00
|
|
|
private:
|
|
|
|
|
2022-05-03 14:20:08 +00:00
|
|
|
ReferencedType* Pointer;
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <typename T> requires (CObject<T> || CFunction<T>) friend class TReferenceWrapper;
|
2022-05-03 14:20:08 +00:00
|
|
|
|
2022-02-09 13:46:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TReferenceWrapper(T&) -> TReferenceWrapper<T>;
|
|
|
|
|
2022-04-24 15:08:00 +00:00
|
|
|
template <typename T>
|
|
|
|
void Ref(const T&&) = delete;
|
|
|
|
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper<T> Ref(T& InValue)
|
2022-04-24 15:08:00 +00:00
|
|
|
{
|
|
|
|
return TReferenceWrapper<T>(InValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper<T> Ref(TReferenceWrapper<T> InValue)
|
2022-04-24 15:08:00 +00:00
|
|
|
{
|
|
|
|
return Ref(InValue.Get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper<const T> Ref(const T& InValue)
|
2022-04-24 15:08:00 +00:00
|
|
|
{
|
|
|
|
return TReferenceWrapper<const T>(InValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TReferenceWrapper<const T> Ref(TReferenceWrapper<T> InValue)
|
2022-04-24 15:08:00 +00:00
|
|
|
{
|
|
|
|
return Ref(InValue.Get());
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
template <typename T> struct TIsTReferenceWrapperImpl : FFalse { };
|
|
|
|
template <typename T> struct TIsTReferenceWrapperImpl<TReferenceWrapper<T>> : FTrue { };
|
|
|
|
|
|
|
|
template <typename T> struct TUnwrapReferenceImpl { using Type = T; };
|
|
|
|
template <typename T> struct TUnwrapReferenceImpl<TReferenceWrapper<T>> { using Type = T&; };
|
|
|
|
|
|
|
|
template <typename T> struct TUnwrapRefDecayImpl { using Type = typename TUnwrapReferenceImpl<TDecay<T>>::Type; };
|
2022-03-31 09:36:48 +00:00
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
template <typename T>
|
2022-11-17 12:57:54 +00:00
|
|
|
concept CTReferenceWrapper = NAMESPACE_PRIVATE::TIsTReferenceWrapperImpl<TRemoveCV<T>>::Value;
|
2022-05-20 15:35:36 +00:00
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
template <typename T>
|
|
|
|
using TUnwrapReference = typename NAMESPACE_PRIVATE::TUnwrapReferenceImpl<T>::Type;
|
2022-03-26 11:33:28 +00:00
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
template <typename T>
|
|
|
|
using TUnwrapRefDecay = typename NAMESPACE_PRIVATE::TUnwrapRefDecayImpl<T>::Type;
|
2022-03-26 11:33:28 +00:00
|
|
|
|
2022-02-09 13:46:39 +00:00
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|