2022-12-29 13:55:02 +00:00
|
|
|
|
#pragma once
|
2022-02-03 08:45:34 +00:00
|
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
2022-03-26 11:31:45 +00:00
|
|
|
|
#include "TypeTraits/CompositeType.h"
|
|
|
|
|
#include "TypeTraits/Miscellaneous.h"
|
|
|
|
|
#include "TypeTraits/SupportedOperations.h"
|
2022-02-03 08:45:34 +00:00
|
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Forms lvalue reference to const type of 'Ref'. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr const T& AsConst(T& Ref)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
|
|
|
|
return Ref;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** The const rvalue reference overload is deleted to disallow rvalue arguments. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
void AsConst(const T&& Ref) = delete;
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** MoveTemp will cast a reference to an rvalue reference. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr TRemoveReference<T>&& MoveTemp(T&& Obj)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-11-15 14:15:10 +00:00
|
|
|
|
using CastType = TRemoveReference<T>;
|
|
|
|
|
return static_cast<CastType&&>(Obj);
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** CopyTemp will enforce the creation of an rvalue which can bind to rvalue reference parameters. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T CopyTemp(T& Obj)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-11-15 14:15:10 +00:00
|
|
|
|
return const_cast<const T&>(Obj);
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** CopyTemp will enforce the creation of an rvalue which can bind to rvalue reference parameters. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T CopyTemp(const T& Obj)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-11-15 14:15:10 +00:00
|
|
|
|
return Obj;
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** CopyTemp will enforce the creation of an rvalue which can bind to rvalue reference parameters. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T&& CopyTemp(T&& Obj)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-12-29 13:55:02 +00:00
|
|
|
|
// If we already have an rvalue, just return it unchanged, rather than needlessly creating yet another rvalue from it.
|
2022-11-15 14:15:10 +00:00
|
|
|
|
return MoveTemp(Obj);
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Forwards lvalues as either lvalues or as rvalues, depending on T. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T&& Forward(TRemoveReference<T>& Obj)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-11-15 14:15:10 +00:00
|
|
|
|
return static_cast<T&&>(Obj);
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Forwards lvalues as either lvalues or as rvalues, depending on T. */
|
2022-02-03 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T&& Forward(TRemoveReference<T>&& Obj)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-11-15 14:15:10 +00:00
|
|
|
|
return static_cast<T&&>(Obj);
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Exchanges the given values. */
|
2022-12-19 10:00:52 +00:00
|
|
|
|
template <typename T> requires (CMoveConstructible<T> && CMoveAssignable<T>)
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr void Swap(T& A, T& B)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-12-19 10:00:52 +00:00
|
|
|
|
T Temp = MoveTemp(A);
|
|
|
|
|
A = MoveTemp(B);
|
|
|
|
|
B = MoveTemp(Temp);
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Replaces the value of 'A' with 'B' and returns the old value of 'A'. */
|
2022-11-16 14:03:54 +00:00
|
|
|
|
template <typename T, typename U = T> requires (CMoveConstructible<T> && CAssignableFrom<T&, U>)
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T Exchange(T& A, U&& B)
|
2022-02-03 08:45:34 +00:00
|
|
|
|
{
|
2022-02-04 08:34:41 +00:00
|
|
|
|
T Temp = MoveTemp(A);
|
|
|
|
|
A = Forward<U>(B);
|
|
|
|
|
return Temp;
|
2022-02-03 08:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00: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 08:45:34 +00:00
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
TAddRValueReference<T> DeclVal();
|
2022-02-03 08:45:34 +00:00
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Obtains the actual address of the object or function arg, even in presence of overloaded operator&. */
|
2022-11-16 14:03:54 +00:00
|
|
|
|
template <typename T> requires (CObject<T>)
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T* AddressOf(T& Object)
|
2022-02-09 09:07:47 +00:00
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(Object)));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Obtains the actual address of the object or function arg, even in presence of overloaded operator&. */
|
2022-05-15 15:10:02 +00:00
|
|
|
|
template <typename T> requires (!CObject<T>)
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr T* AddressOf(T& Object)
|
2022-02-09 09:07:47 +00:00
|
|
|
|
{
|
|
|
|
|
return &Object;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Rvalue overload is deleted to prevent taking the address of const rvalues. */
|
|
|
|
|
template <typename T>
|
|
|
|
|
const T* AddressOf(const T&&) = delete;
|
|
|
|
|
|
2022-03-31 01:39:30 +00:00
|
|
|
|
struct FIgnore
|
|
|
|
|
{
|
|
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
|
FORCEINLINE constexpr void operator=(T&&) const { }
|
2022-03-31 01:39:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-12-29 13:55:02 +00: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 01:39:30 +00:00
|
|
|
|
inline constexpr FIgnore Ignore;
|
|
|
|
|
|
2022-11-21 15:28:19 +00: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 13:55:02 +00: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 15:11:05 +00:00
|
|
|
|
template <typename... Ts>
|
|
|
|
|
struct TOverloaded : Ts...
|
|
|
|
|
{
|
|
|
|
|
using Ts::operator()...;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename... Ts>
|
|
|
|
|
TOverloaded(Ts...) -> TOverloaded<Ts...>;
|
|
|
|
|
|
2022-02-03 08:45:34 +00:00
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
|
NAMESPACE_REDCRAFT_END
|