2022-12-29 21:55:02 +08:00
|
|
|
|
#pragma once
|
2022-02-03 16:45:34 +08:00
|
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
2022-03-26 19:31:45 +08:00
|
|
|
|
#include "TypeTraits/CompositeType.h"
|
|
|
|
|
#include "TypeTraits/Miscellaneous.h"
|
|
|
|
|
#include "TypeTraits/SupportedOperations.h"
|
2022-02-03 16:45:34 +08:00
|
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Forms lvalue reference to const type of 'Ref'. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr const T& AsConst(T& Ref)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
|
|
|
|
return Ref;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** The const rvalue reference overload is deleted to disallow rvalue arguments. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
|
|
|
|
void AsConst(const T&& Ref) = delete;
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** MoveTemp will cast a reference to an rvalue reference. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr TRemoveReference<T>&& MoveTemp(T&& Obj)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-11-15 22:15:10 +08:00
|
|
|
|
using CastType = TRemoveReference<T>;
|
|
|
|
|
return static_cast<CastType&&>(Obj);
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** CopyTemp will enforce the creation of an rvalue which can bind to rvalue reference parameters. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T CopyTemp(T& Obj)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-11-15 22:15:10 +08:00
|
|
|
|
return const_cast<const T&>(Obj);
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** CopyTemp will enforce the creation of an rvalue which can bind to rvalue reference parameters. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T CopyTemp(const T& Obj)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-11-15 22:15:10 +08:00
|
|
|
|
return Obj;
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** CopyTemp will enforce the creation of an rvalue which can bind to rvalue reference parameters. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T&& CopyTemp(T&& Obj)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-12-29 21:55:02 +08:00
|
|
|
|
// If we already have an rvalue, just return it unchanged, rather than needlessly creating yet another rvalue from it.
|
2022-11-15 22:15:10 +08:00
|
|
|
|
return MoveTemp(Obj);
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Forwards lvalues as either lvalues or as rvalues, depending on T. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T&& Forward(TRemoveReference<T>& Obj)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-11-15 22:15:10 +08:00
|
|
|
|
return static_cast<T&&>(Obj);
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Forwards lvalues as either lvalues or as rvalues, depending on T. */
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T&& Forward(TRemoveReference<T>&& Obj)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-11-15 22:15:10 +08:00
|
|
|
|
return static_cast<T&&>(Obj);
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Exchanges the given values. */
|
2022-12-19 18:00:52 +08:00
|
|
|
|
template <typename T> requires (CMoveConstructible<T> && CMoveAssignable<T>)
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr void Swap(T& A, T& B)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-12-19 18:00:52 +08:00
|
|
|
|
T Temp = MoveTemp(A);
|
|
|
|
|
A = MoveTemp(B);
|
|
|
|
|
B = MoveTemp(Temp);
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 19:19:18 +08:00
|
|
|
|
/** Overloads the Swap algorithm for array. */
|
|
|
|
|
template <typename T, size_t N> requires (CMoveConstructible<T> && CMoveAssignable<T>)
|
|
|
|
|
FORCEINLINE constexpr void Swap(T(&A)[N], T(&B)[N])
|
|
|
|
|
{
|
|
|
|
|
for (size_t Index = 0; Index < N; ++Index)
|
|
|
|
|
{
|
|
|
|
|
T Temp = MoveTemp(A[Index]);
|
|
|
|
|
A[Index] = MoveTemp(B[Index]);
|
|
|
|
|
B[Index] = MoveTemp(Temp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Replaces the value of 'A' with 'B' and returns the old value of 'A'. */
|
2022-11-16 22:03:54 +08:00
|
|
|
|
template <typename T, typename U = T> requires (CMoveConstructible<T> && CAssignableFrom<T&, U>)
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T Exchange(T& A, U&& B)
|
2022-02-03 16:45:34 +08:00
|
|
|
|
{
|
2022-02-04 16:34:41 +08:00
|
|
|
|
T Temp = MoveTemp(A);
|
|
|
|
|
A = Forward<U>(B);
|
|
|
|
|
return Temp;
|
2022-02-03 16:45:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* Converts any type T to a reference type, making it possible to use member functions
|
|
|
|
|
* in decltype expressions without the need to go through constructors.
|
|
|
|
|
*/
|
2022-02-03 16:45:34 +08:00
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
TAddRValueReference<T> DeclVal();
|
2022-02-03 16:45:34 +08:00
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Obtains the actual address of the object or function arg, even in presence of overloaded operator&. */
|
2022-11-16 22:03:54 +08:00
|
|
|
|
template <typename T> requires (CObject<T>)
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T* AddressOf(T& Object)
|
2022-02-09 17:07:47 +08:00
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(Object)));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Obtains the actual address of the object or function arg, even in presence of overloaded operator&. */
|
2022-05-15 23:10:02 +08:00
|
|
|
|
template <typename T> requires (!CObject<T>)
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr T* AddressOf(T& Object)
|
2022-02-09 17:07:47 +08:00
|
|
|
|
{
|
|
|
|
|
return &Object;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/** Rvalue overload is deleted to prevent taking the address of const rvalues. */
|
|
|
|
|
template <typename T>
|
|
|
|
|
const T* AddressOf(const T&&) = delete;
|
|
|
|
|
|
2022-12-30 19:11:01 +08:00
|
|
|
|
struct FIgnore final
|
2022-03-31 09:39:30 +08:00
|
|
|
|
{
|
|
|
|
|
template <typename T>
|
2022-12-13 22:02:39 +08:00
|
|
|
|
FORCEINLINE constexpr void operator=(T&&) const { }
|
2022-03-31 09:39:30 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* An object of unspecified type such that any value can be assigned to it with no effect.
|
|
|
|
|
* Intended for use with Tie when unpacking a TTuple, as placeholders for unused arguments
|
|
|
|
|
* or using Ignore to avoid warnings about unused return values from NODISCARD functions.
|
|
|
|
|
*/
|
2022-03-31 09:39:30 +08:00
|
|
|
|
inline constexpr FIgnore Ignore;
|
|
|
|
|
|
2022-11-21 23:28:19 +08:00
|
|
|
|
// This macro is used in place of using type aliases, see Atomic.h, etc
|
|
|
|
|
#define STRONG_INHERIT(...) /* BaseClass */ \
|
|
|
|
|
/* struct DerivedClass : */ public __VA_ARGS__ \
|
|
|
|
|
{ \
|
|
|
|
|
private: \
|
|
|
|
|
\
|
|
|
|
|
using BaseClassTypedef = __VA_ARGS__; \
|
|
|
|
|
\
|
|
|
|
|
public: \
|
|
|
|
|
\
|
|
|
|
|
using BaseClassTypedef::BaseClassTypedef; \
|
|
|
|
|
using BaseClassTypedef::operator=; \
|
|
|
|
|
\
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 21:55:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* This class is used to create a set of overloaded functions.
|
|
|
|
|
*
|
|
|
|
|
* Visit(TOverloaded {
|
|
|
|
|
* [](auto A) { ... },
|
|
|
|
|
* [](double A) { ... },
|
|
|
|
|
* [](const FString& A) { ... },
|
|
|
|
|
* }, Target);
|
|
|
|
|
*/
|
2022-12-03 23:11:05 +08:00
|
|
|
|
template <typename... Ts>
|
2022-12-30 19:11:01 +08:00
|
|
|
|
struct TOverloaded final : Ts...
|
2022-12-03 23:11:05 +08:00
|
|
|
|
{
|
|
|
|
|
using Ts::operator()...;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename... Ts>
|
|
|
|
|
TOverloaded(Ts...) -> TOverloaded<Ts...>;
|
|
|
|
|
|
2022-02-03 16:45:34 +08:00
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
|
NAMESPACE_REDCRAFT_END
|