2022-04-07 07:57:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
2022-12-12 14:07:12 +00:00
|
|
|
#include "Memory/Memory.h"
|
2022-12-03 15:11:05 +00:00
|
|
|
#include "Templates/Meta.h"
|
2022-04-07 07:57:02 +00:00
|
|
|
#include "Templates/Invoke.h"
|
2022-04-08 03:44:36 +00:00
|
|
|
#include "Templates/Utility.h"
|
2022-04-07 07:57:02 +00:00
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
#include "Miscellaneous/AssertionMacros.h"
|
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
// NOTE: In the STL, the assignment operation of the std::any type uses the copy-and-swap idiom
|
|
|
|
// instead of directly calling the assignment operation of the contained value.
|
|
|
|
// But we don't follow the the copy-and-swap idiom, see "Templates/Any.h".
|
|
|
|
// This class implements assignment operations in a way that assumes no assignment operations of the type,
|
|
|
|
// because the assignment operations of TFunction are in most cases different between LHS and RHS.
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TFunctionRef;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TFunction;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TUniqueFunction;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename T> struct TIsTFunctionRef : FFalse { };
|
|
|
|
template <typename F> struct TIsTFunctionRef<TFunctionRef<F>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
template <typename T> struct TIsTFunction : FFalse { };
|
|
|
|
template <typename F> struct TIsTFunction<TFunction<F>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
template <typename T> struct TIsTUniqueFunction : FFalse { };
|
|
|
|
template <typename F> struct TIsTUniqueFunction<TUniqueFunction<F>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
2022-11-17 12:57:54 +00:00
|
|
|
template <typename T> concept CTFunctionRef = NAMESPACE_PRIVATE::TIsTFunctionRef<TRemoveCV<T>>::Value;
|
|
|
|
template <typename T> concept CTFunction = NAMESPACE_PRIVATE::TIsTFunction<TRemoveCV<T>>::Value;
|
|
|
|
template <typename T> concept CTUniqueFunction = NAMESPACE_PRIVATE::TIsTUniqueFunction<TRemoveCV<T>>::Value;
|
2022-05-20 15:35:36 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
template <bool bIsRef, bool bIsUnique>
|
|
|
|
class TFunctionStorage;
|
|
|
|
|
|
|
|
template <bool bIsUnique>
|
|
|
|
class TFunctionStorage<true, bIsUnique>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunctionStorage() = default;
|
|
|
|
FORCEINLINE constexpr TFunctionStorage(const TFunctionStorage&) = default;
|
|
|
|
FORCEINLINE constexpr TFunctionStorage(TFunctionStorage&&) = default;
|
|
|
|
FORCEINLINE constexpr TFunctionStorage& operator=(const TFunctionStorage&) = delete;
|
|
|
|
FORCEINLINE constexpr TFunctionStorage& operator=(TFunctionStorage&&) = delete;
|
|
|
|
FORCEINLINE constexpr ~TFunctionStorage() = default;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr uintptr GetValuePtr() const { return ValuePtr; }
|
|
|
|
FORCEINLINE constexpr uintptr GetCallable() const { return Callable; }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr bool IsValid() const { return ValuePtr != 0; }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
// Use Invalidate() to invalidate the storage or use Emplace<T>() to emplace a new object after destruction.
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Destroy() { }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
// Make sure you call this function after you have destroyed the held object using Destroy().
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Invalidate() { ValuePtr = 0; }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
// Make sure you call this function after you have destroyed the held object using Destroy().
|
|
|
|
template <typename T, typename U>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Emplace(intptr InCallable, U&& Args)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
static_assert(CSameAs<TDecay<T>, TDecay<U>>);
|
|
|
|
ValuePtr = reinterpret_cast<uintptr>(AddressOf(Args));
|
|
|
|
Callable = InCallable;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
uintptr ValuePtr;
|
|
|
|
uintptr Callable;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// For non-unique storage, the memory layout should be compatible with unique storage,
|
|
|
|
// i.e. it can be directly reinterpreted_cast.
|
|
|
|
template <bool bIsUnique>
|
|
|
|
class alignas(16) TFunctionStorage<false, bIsUnique>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunctionStorage() = default;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-16 15:23:05 +00:00
|
|
|
TFunctionStorage(const TFunctionStorage& InValue) requires (!bIsUnique)
|
2022-12-13 15:18:36 +00:00
|
|
|
: RTTI(InValue.RTTI)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
if (!IsValid()) return;
|
|
|
|
|
|
|
|
Callable = InValue.Callable;
|
|
|
|
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty:
|
|
|
|
break;
|
|
|
|
case ERepresentation::Trivial:
|
|
|
|
Memory::Memcpy(InternalStorage, InValue.InternalStorage);
|
|
|
|
break;
|
|
|
|
case ERepresentation::Small:
|
2022-12-13 15:18:36 +00:00
|
|
|
GetRTTI().CopyConstruct(GetStorage(), InValue.GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
case ERepresentation::Big:
|
2022-12-13 15:18:36 +00:00
|
|
|
ExternalStorage = Memory::Malloc(GetRTTI().TypeSize, GetRTTI().TypeAlignment);
|
|
|
|
GetRTTI().CopyConstruct(GetStorage(), InValue.GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
default: check_no_entry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:23:05 +00:00
|
|
|
TFunctionStorage(TFunctionStorage&& InValue)
|
2022-12-13 15:18:36 +00:00
|
|
|
: RTTI(InValue.RTTI)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
if (!IsValid()) return;
|
|
|
|
|
|
|
|
Callable = InValue.Callable;
|
|
|
|
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty:
|
|
|
|
break;
|
|
|
|
case ERepresentation::Trivial:
|
|
|
|
Memory::Memcpy(InternalStorage, InValue.InternalStorage);
|
|
|
|
break;
|
|
|
|
case ERepresentation::Small:
|
2022-12-13 15:18:36 +00:00
|
|
|
GetRTTI().MoveConstruct(GetStorage(), InValue.GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
case ERepresentation::Big:
|
|
|
|
ExternalStorage = InValue.ExternalStorage;
|
|
|
|
InValue.Invalidate();
|
|
|
|
break;
|
|
|
|
default: check_no_entry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE ~TFunctionStorage()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:23:05 +00:00
|
|
|
TFunctionStorage& operator=(const TFunctionStorage& InValue) requires (!bIsUnique)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
2022-12-29 13:55:02 +00:00
|
|
|
if (&InValue == this) UNLIKELY return *this;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
if (!InValue.IsValid())
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
RTTI = InValue.RTTI;
|
2022-12-12 14:07:12 +00:00
|
|
|
Callable = InValue.Callable;
|
|
|
|
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty:
|
|
|
|
break;
|
|
|
|
case ERepresentation::Trivial:
|
|
|
|
Memory::Memcpy(InternalStorage, InValue.InternalStorage);
|
|
|
|
break;
|
|
|
|
case ERepresentation::Small:
|
2022-12-13 15:18:36 +00:00
|
|
|
GetRTTI().CopyConstruct(GetStorage(), InValue.GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
case ERepresentation::Big:
|
2022-12-13 15:18:36 +00:00
|
|
|
ExternalStorage = Memory::Malloc(GetRTTI().TypeSize, GetRTTI().TypeAlignment);
|
|
|
|
GetRTTI().CopyConstruct(GetStorage(), InValue.GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
default: check_no_entry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:23:05 +00:00
|
|
|
TFunctionStorage& operator=(TFunctionStorage&& InValue)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
2022-12-29 13:55:02 +00:00
|
|
|
if (&InValue == this) UNLIKELY return *this;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
if (!InValue.IsValid())
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
RTTI = InValue.RTTI;
|
2022-12-12 14:07:12 +00:00
|
|
|
Callable = InValue.Callable;
|
|
|
|
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty:
|
|
|
|
break;
|
|
|
|
case ERepresentation::Trivial:
|
|
|
|
Memory::Memcpy(InternalStorage, InValue.InternalStorage);
|
|
|
|
break;
|
|
|
|
case ERepresentation::Small:
|
2022-12-13 15:18:36 +00:00
|
|
|
GetRTTI().MoveConstruct(GetStorage(), InValue.GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
case ERepresentation::Big:
|
|
|
|
ExternalStorage = InValue.ExternalStorage;
|
|
|
|
InValue.Invalidate();
|
|
|
|
break;
|
|
|
|
default: check_no_entry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr uintptr GetValuePtr() const { return reinterpret_cast<uintptr>(GetStorage()); }
|
|
|
|
FORCEINLINE constexpr uintptr GetCallable() const { return Callable; }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
FORCEINLINE constexpr bool IsValid() const { return RTTI != 0; }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
// Use Invalidate() to invalidate the storage or use Emplace<T>() to emplace a new object after destruction.
|
2022-12-16 15:23:05 +00:00
|
|
|
void Destroy()
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
if (!IsValid()) return;
|
|
|
|
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty:
|
|
|
|
case ERepresentation::Trivial:
|
|
|
|
break;
|
|
|
|
case ERepresentation::Small:
|
2022-12-13 15:18:36 +00:00
|
|
|
GetRTTI().Destruct(GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
break;
|
|
|
|
case ERepresentation::Big:
|
2022-12-13 15:18:36 +00:00
|
|
|
GetRTTI().Destruct(GetStorage());
|
2022-12-12 14:07:12 +00:00
|
|
|
Memory::Free(ExternalStorage);
|
|
|
|
break;
|
|
|
|
default: check_no_entry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure you call this function after you have destroyed the held object using Destroy().
|
2022-12-13 15:18:36 +00:00
|
|
|
FORCEINLINE constexpr void Invalidate() { RTTI = 0; }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
// Make sure you call this function after you have destroyed the held object using Destroy().
|
|
|
|
template <typename T, typename... Ts>
|
2022-12-16 15:23:05 +00:00
|
|
|
void Emplace(uintptr InCallable, Ts&&... Args)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
Callable = InCallable;
|
|
|
|
|
|
|
|
using DecayedType = TDecay<T>;
|
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
static constexpr const FRTTI SelectedRTTI(InPlaceType<DecayedType>);
|
|
|
|
RTTI = reinterpret_cast<uintptr>(&SelectedRTTI);
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-14 14:11:30 +00:00
|
|
|
if constexpr (CEmpty<DecayedType> && CTrivial<DecayedType>) return; // ERepresentation::Empty
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-14 14:11:30 +00:00
|
|
|
constexpr bool bIsTriviallyStorable = sizeof(DecayedType) <= sizeof(InternalStorage) && alignof(DecayedType) <= alignof(TFunctionStorage) && CTriviallyCopyable<DecayedType>;
|
|
|
|
constexpr bool bIsSmallStorable = sizeof(DecayedType) <= sizeof(InternalStorage) && alignof(DecayedType) <= alignof(TFunctionStorage);
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
if constexpr (bIsTriviallyStorable)
|
|
|
|
{
|
|
|
|
new (&InternalStorage) DecayedType(Forward<Ts>(Args)...);
|
2022-12-13 15:18:36 +00:00
|
|
|
RTTI |= static_cast<uintptr>(ERepresentation::Trivial);
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
2022-12-14 14:11:30 +00:00
|
|
|
else if constexpr (bIsSmallStorable)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
new (&InternalStorage) DecayedType(Forward<Ts>(Args)...);
|
2022-12-13 15:18:36 +00:00
|
|
|
RTTI |= static_cast<uintptr>(ERepresentation::Small);
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ExternalStorage = new DecayedType(Forward<Ts>(Args)...);
|
2022-12-13 15:18:36 +00:00
|
|
|
RTTI |= static_cast<uintptr>(ERepresentation::Big);
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:00:52 +00:00
|
|
|
friend void Swap(TFunctionStorage& A, TFunctionStorage& B)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
2022-12-19 10:00:52 +00:00
|
|
|
if (!A.IsValid() && !B.IsValid()) return;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-19 10:00:52 +00:00
|
|
|
if (A.IsValid() && !B.IsValid())
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
2022-12-19 10:00:52 +00:00
|
|
|
B = MoveTemp(A);
|
|
|
|
A.Destroy();
|
|
|
|
A.Invalidate();
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
2022-12-19 10:00:52 +00:00
|
|
|
else if (!A.IsValid() && B.IsValid())
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
2022-12-19 10:00:52 +00:00
|
|
|
A = MoveTemp(B);
|
|
|
|
B.Destroy();
|
|
|
|
B.Invalidate();
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-19 10:00:52 +00:00
|
|
|
TFunctionStorage Temp = MoveTemp(A);
|
|
|
|
A = MoveTemp(B);
|
|
|
|
B = MoveTemp(Temp);
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
struct FMovableRTTI
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
const size_t TypeSize;
|
|
|
|
const size_t TypeAlignment;
|
|
|
|
|
|
|
|
using FMoveConstruct = void(*)(void*, void*);
|
|
|
|
using FDestruct = void(*)(void* );
|
|
|
|
|
|
|
|
const FMoveConstruct MoveConstruct;
|
|
|
|
const FDestruct Destruct;
|
|
|
|
|
|
|
|
template <typename T>
|
2022-12-13 15:18:36 +00:00
|
|
|
FORCEINLINE constexpr FMovableRTTI(TInPlaceType<T>)
|
2022-12-12 14:07:12 +00:00
|
|
|
: TypeSize(sizeof(T)), TypeAlignment(alignof(T))
|
|
|
|
, MoveConstruct(
|
|
|
|
[](void* A, void* B)
|
|
|
|
{
|
2022-12-13 15:18:36 +00:00
|
|
|
new (A) T(MoveTemp(*reinterpret_cast<T*>(B)));
|
2022-12-12 14:07:12 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
, Destruct(
|
|
|
|
[](void* A)
|
|
|
|
{
|
|
|
|
reinterpret_cast<T*>(A)->~T();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
struct FCopyableRTTI : public FMovableRTTI
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
using FCopyConstruct = void(*)(void*, const void*);
|
|
|
|
|
|
|
|
const FCopyConstruct CopyConstruct;
|
|
|
|
|
|
|
|
template <typename T>
|
2022-12-13 15:18:36 +00:00
|
|
|
FORCEINLINE constexpr FCopyableRTTI(TInPlaceType<T>)
|
|
|
|
: FMovableRTTI(InPlaceType<T>)
|
2022-12-12 14:07:12 +00:00
|
|
|
, CopyConstruct(
|
|
|
|
[](void* A, const void* B)
|
|
|
|
{
|
|
|
|
new (A) T(*reinterpret_cast<const T*>(B));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
using FRTTI = TConditional<bIsUnique, FMovableRTTI, FCopyableRTTI>;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
static_assert(alignof(FRTTI) >= 4);
|
2022-12-12 14:07:12 +00:00
|
|
|
|
|
|
|
static constexpr uintptr_t RepresentationMask = 3;
|
|
|
|
|
|
|
|
enum class ERepresentation : uintptr
|
|
|
|
{
|
|
|
|
Empty = 0, // EmptyType
|
|
|
|
Trivial = 1, // Trivial & Internal
|
|
|
|
Small = 2, // InternalStorage
|
|
|
|
Big = 3, // ExternalStorage
|
|
|
|
};
|
2022-12-15 15:38:51 +00:00
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint8 InternalStorage[64 - sizeof(uintptr) - sizeof(uintptr)];
|
|
|
|
void* ExternalStorage;
|
|
|
|
};
|
|
|
|
|
|
|
|
uintptr RTTI;
|
|
|
|
uintptr Callable;
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-13 15:18:36 +00:00
|
|
|
FORCEINLINE constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(RTTI & RepresentationMask); }
|
|
|
|
FORCEINLINE constexpr const FRTTI& GetRTTI() const { return *reinterpret_cast<const FRTTI*>(RTTI & ~RepresentationMask); }
|
2022-12-12 14:07:12 +00:00
|
|
|
|
2022-12-14 14:11:30 +00:00
|
|
|
FORCEINLINE constexpr void* GetStorage()
|
|
|
|
{
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty: return nullptr;
|
|
|
|
case ERepresentation::Trivial: return &InternalStorage;
|
|
|
|
case ERepresentation::Small: return &InternalStorage;
|
|
|
|
case ERepresentation::Big: return ExternalStorage;
|
|
|
|
default: check_no_entry(); return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE constexpr const void* GetStorage() const
|
|
|
|
{
|
|
|
|
switch (GetRepresentation())
|
|
|
|
{
|
|
|
|
case ERepresentation::Empty: return nullptr;
|
|
|
|
case ERepresentation::Trivial: return &InternalStorage;
|
|
|
|
case ERepresentation::Small: return &InternalStorage;
|
|
|
|
case ERepresentation::Big: return ExternalStorage;
|
|
|
|
default: check_no_entry(); return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 14:07:12 +00:00
|
|
|
};
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename T>
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr bool FunctionIsBound(const T& Func)
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-20 15:35:36 +00:00
|
|
|
if constexpr (CPointer<T> || CMemberPointer<T> || CTFunctionRef<T> || CTFunction<T> || CTUniqueFunction<T>)
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return !!Func;
|
2022-04-07 07:57:02 +00:00
|
|
|
}
|
2022-05-14 14:52:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename Signature, typename F> struct TIsInvocableSignature : FFalse { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F>
|
|
|
|
struct TIsInvocableSignature<Ret(Ts...), F>
|
|
|
|
: TBoolConstant<CInvocableResult<Ret, F, Ts...> && CInvocableResult<Ret, F&, Ts...>>
|
2022-04-07 07:57:02 +00:00
|
|
|
{ };
|
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) & , F> : TBoolConstant<CInvocableResult<Ret, F&, Ts...>> { };
|
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) &&, F> : TBoolConstant<CInvocableResult<Ret, F , Ts...>> { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F>
|
|
|
|
struct TIsInvocableSignature<Ret(Ts...) const, F>
|
|
|
|
: TBoolConstant<CInvocableResult<Ret, const F, Ts...> && CInvocableResult<Ret, const F&, Ts...>>
|
2022-04-07 07:57:02 +00:00
|
|
|
{ };
|
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) const& , F> : TBoolConstant<CInvocableResult<Ret, const F&, Ts...>> { };
|
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) const&&, F> : TBoolConstant<CInvocableResult<Ret, const F , Ts...>> { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
template <typename F> struct TFunctionInfo;
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) > { using Fn = Ret(Ts...); using CVRef = int; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) & > { using Fn = Ret(Ts...); using CVRef = int&; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) && > { using Fn = Ret(Ts...); using CVRef = int&&; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) const > { using Fn = Ret(Ts...); using CVRef = const int; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) const& > { using Fn = Ret(Ts...); using CVRef = const int&; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) const&&> { using Fn = Ret(Ts...); using CVRef = const int&&; };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
template <typename F, typename CVRef, bool bIsRef, bool bIsUnique = false> class TFunctionImpl;
|
2022-11-13 14:21:00 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
template <typename Ret, typename... Ts, typename CVRef, bool bIsRef, bool bIsUnique>
|
|
|
|
class TFunctionImpl<Ret(Ts...), CVRef, bIsRef, bIsUnique>
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
using ResultType = Ret;
|
2022-12-03 15:11:05 +00:00
|
|
|
using ArgumentType = TTypeSequence<Ts...>;
|
2022-11-13 14:21:00 +00:00
|
|
|
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunctionImpl() = default;
|
|
|
|
FORCEINLINE constexpr TFunctionImpl(const TFunctionImpl&) = default;
|
|
|
|
FORCEINLINE constexpr TFunctionImpl(TFunctionImpl&&) = default;
|
|
|
|
FORCEINLINE constexpr TFunctionImpl& operator=(const TFunctionImpl&) = default;
|
|
|
|
FORCEINLINE constexpr TFunctionImpl& operator=(TFunctionImpl&&) = default;
|
|
|
|
FORCEINLINE constexpr ~TFunctionImpl() = default;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Invokes the stored callable function target with the parameters args. */
|
2022-11-16 11:13:37 +00:00
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) requires (CSameAs<CVRef, int >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) & requires (CSameAs<CVRef, int& >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) && requires (CSameAs<CVRef, int&&>) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) const requires (CSameAs<CVRef, const int >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) const& requires (CSameAs<CVRef, const int& >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) const&& requires (CSameAs<CVRef, const int&&>) { return CallImpl(Forward<Ts>(Args)...); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** @return false if instance stores a callable function target, true otherwise. */
|
2023-01-03 12:40:27 +00:00
|
|
|
NODISCARD FORCEINLINE constexpr bool operator==(nullptr_t) const& requires (!bIsRef) { return !IsValid(); }
|
2022-12-18 11:11:53 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** @return true if instance stores a callable function target, false otherwise. */
|
2023-01-03 12:40:27 +00:00
|
|
|
NODISCARD FORCEINLINE constexpr bool IsValid() const requires (!bIsRef) { return Storage.IsValid(); }
|
|
|
|
NODISCARD FORCEINLINE constexpr explicit operator bool() const requires (!bIsRef) { return Storage.IsValid(); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
using CallableType = ResultType(*)(uintptr, Ts&&...);
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
TFunctionStorage<bIsRef, bIsUnique> Storage;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
FORCEINLINE ResultType CallImpl(Ts&&... Args) const
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2023-01-03 12:40:27 +00:00
|
|
|
checkf(Storage.IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
2022-12-12 14:07:12 +00:00
|
|
|
CallableType Callable = reinterpret_cast<CallableType>(Storage.GetCallable());
|
|
|
|
return Callable(Storage.GetValuePtr(), Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
protected:
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
// Use Invalidate() to invalidate the storage or use Emplace<T>() to emplace a new object after destruction.
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Destroy() { Storage.Destroy(); }
|
2022-05-01 10:02:26 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
// Make sure you call this function after you have destroyed the held object using Destroy().
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Invalidate() { Storage.Invalidate(); }
|
2022-04-24 14:42:40 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
// Make sure you call this function after you have destroyed the held object using Destroy().
|
2022-12-31 11:19:30 +00:00
|
|
|
template <typename T, typename... Us>
|
|
|
|
FORCEINLINE constexpr TDecay<T>& Emplace(Us&&... Args)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
using DecayedType = TDecay<T>;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
// This add a l-value reference to a non-reference type, while preserving the r-value reference.
|
|
|
|
using ObjectType = TCopyCVRef<CVRef, DecayedType>;
|
|
|
|
using InvokeType = TConditional<CReference<ObjectType>, ObjectType, ObjectType&>;
|
2022-04-24 14:42:40 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
CallableType Callable = [](uintptr ObjectPtr, Ts&&... Args) -> ResultType
|
|
|
|
{
|
|
|
|
return InvokeResult<ResultType>(
|
|
|
|
static_cast<InvokeType>(*reinterpret_cast<DecayedType*>(ObjectPtr)),
|
|
|
|
Forward<Ts>(Args)...
|
|
|
|
);
|
2022-04-24 14:42:40 +00:00
|
|
|
};
|
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
Storage.template Emplace<DecayedType>(
|
|
|
|
reinterpret_cast<uintptr>(Callable),
|
2022-12-31 11:19:30 +00:00
|
|
|
Forward<Us>(Args)...
|
2022-12-12 14:07:12 +00:00
|
|
|
);
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
return *reinterpret_cast<DecayedType*>(Storage.GetValuePtr());
|
2022-04-07 07:57:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 10:00:52 +00:00
|
|
|
friend FORCEINLINE constexpr void Swap(TFunctionImpl& A, TFunctionImpl& B) requires (!bIsRef) { Swap(A.Storage, B.Storage); }
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
};
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
NAMESPACE_PRIVATE_END
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* A class which represents a reference to something callable. The important part here is *reference* - if
|
|
|
|
* you bind it to a lambda and the lambda goes out of scope, you will be left with an invalid reference.
|
|
|
|
*
|
|
|
|
* If you also want to take ownership of the callable thing, e.g. you want to return a lambda from a
|
|
|
|
* function, you should use TFunction. TFunctionRef does not concern itself with ownership because it's
|
|
|
|
* intended to be FAST.
|
|
|
|
*
|
|
|
|
* TFunctionRef is most useful when you want to parameterize a function with some caller-defined code
|
|
|
|
* without making it a template.
|
|
|
|
*/
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
2022-12-30 11:11:01 +00:00
|
|
|
class TFunctionRef final
|
2022-05-14 14:52:21 +00:00
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
true>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
|
2022-05-14 14:52:21 +00:00
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
true>;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
public:
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Remove the default initialization and disallow the construction of a TFunctionRef that does not store the function target. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunctionRef() = delete;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2023-02-26 13:55:36 +00:00
|
|
|
FORCEINLINE constexpr TFunctionRef(const TFunctionRef&) = default;
|
|
|
|
FORCEINLINE constexpr TFunctionRef(TFunctionRef&&) = default;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* We delete the assignment operators because we don't want it to be confused with being related to
|
|
|
|
* regular C++ reference assignment - i.e. calling the assignment operator of whatever the reference
|
|
|
|
* is bound to - because that's not what TFunctionRef does, nor is it even capable of doing that.
|
|
|
|
*/
|
2023-02-26 13:55:36 +00:00
|
|
|
FORCEINLINE constexpr TFunctionRef& operator=(const TFunctionRef&) = delete;
|
|
|
|
FORCEINLINE constexpr TFunctionRef& operator=(TFunctionRef&&) = delete;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Constructor which binds a TFunctionRef to a callable object. */
|
2022-12-12 14:07:12 +00:00
|
|
|
template <typename T> requires (!CTFunctionRef<TDecay<T>>
|
2022-11-16 14:03:54 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunctionRef(T&& InValue)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
|
|
|
checkf(NAMESPACE_PRIVATE::FunctionIsBound(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef"));
|
2022-12-12 14:07:12 +00:00
|
|
|
Impl::template Emplace<T>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-18 11:11:53 +00:00
|
|
|
template <typename T>
|
|
|
|
TFunctionRef(const T&& InValue) = delete;
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
};
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* A class which represents a copy of something callable.
|
|
|
|
*
|
|
|
|
* It takes a copy of whatever is bound to it, meaning you can return it from functions and store them in
|
|
|
|
* objects without caring about the lifetime of the original object being bound.
|
|
|
|
*/
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
2022-12-30 11:11:01 +00:00
|
|
|
class TFunction final
|
2022-05-14 14:52:21 +00:00
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
2022-12-12 14:07:12 +00:00
|
|
|
false, false>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
|
2022-05-14 14:52:21 +00:00
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
2022-12-12 14:07:12 +00:00
|
|
|
false, false>;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Default constructor. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunction(nullptr_t = nullptr) { Impl::Invalidate(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2023-02-26 13:55:36 +00:00
|
|
|
FORCEINLINE TFunction(const TFunction&) = default;
|
|
|
|
FORCEINLINE TFunction(TFunction&&) = default;
|
|
|
|
FORCEINLINE TFunction& operator=(const TFunction&) = default;
|
|
|
|
FORCEINLINE TFunction& operator=(TFunction&&) = default;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* Constructs an TFunction with initial content an function object of type TDecay<T>,
|
|
|
|
* direct-initialized from Forward<T>(InValue).
|
|
|
|
*/
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (!CTInPlaceType<TDecay<T>>
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
2022-05-22 14:52:47 +00:00
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CCopyConstructible<TDecay<T>>
|
2022-12-12 14:07:12 +00:00
|
|
|
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>
|
2022-11-16 14:03:54 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction(T&& InValue)
|
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::Invalidate();
|
|
|
|
else Impl::template Emplace<T>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an TFunction with initial content an function object of type TDecay<T>,
|
|
|
|
* direct-non-list-initialized from Forward<Ts>(Args)....
|
|
|
|
*/
|
2022-12-31 11:19:30 +00:00
|
|
|
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, Ts...> && CCopyConstructible<TDecay<T>>
|
2022-12-12 14:07:12 +00:00
|
|
|
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
2022-12-31 11:19:30 +00:00
|
|
|
FORCEINLINE explicit TFunction(TInPlaceType<T>, Ts&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-31 11:19:30 +00:00
|
|
|
Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an TFunction with initial content an function object of type TDecay<T>,
|
|
|
|
* direct-non-list-initialized from IL, Forward<Ts>(Args)....
|
|
|
|
*/
|
|
|
|
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CCopyConstructible<TDecay<T>>
|
|
|
|
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
|
|
|
FORCEINLINE explicit TFunction(TInPlaceType<T>, initializer_list<U> IL, Ts&&... Args)
|
|
|
|
{
|
|
|
|
Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Removes any bound callable from the TFunction, restoring it to the default empty state. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TFunction& operator=(nullptr_t) { Reset(); return *this; }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Assigns the type and value of 'InValue'. */
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
2022-12-12 14:07:12 +00:00
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CCopyConstructible<TDecay<T>>
|
|
|
|
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction& operator=(T&& InValue)
|
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Reset();
|
|
|
|
else Emplace<T>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* Changes the function object to one of type TDecay<T> constructed from the arguments.
|
|
|
|
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
|
|
|
* TDecay<T>, direct-non-list-initialized from Forward<Ts>(Args)..., as the function object.
|
|
|
|
*
|
2022-12-31 11:19:30 +00:00
|
|
|
* @param Args - The arguments to be passed to the constructor of the function object.
|
|
|
|
*
|
|
|
|
* @return A reference to the new function object.
|
|
|
|
*/
|
|
|
|
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, Ts...> && CCopyConstructible<TDecay<T>>
|
|
|
|
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
|
|
|
FORCEINLINE TDecay<T>& Emplace(Ts&&... Args)
|
|
|
|
{
|
|
|
|
Impl::Destroy();
|
|
|
|
return Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the function object to one of type TDecay<T> constructed from the arguments.
|
|
|
|
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
|
|
|
* TDecay<T>, direct-non-list-initialized from IL, Forward<Ts>(Args)..., as the function object.
|
|
|
|
*
|
|
|
|
* @param IL, Args - The arguments to be passed to the constructor of the function object.
|
2022-12-29 13:55:02 +00:00
|
|
|
*
|
|
|
|
* @return A reference to the new function object.
|
|
|
|
*/
|
2022-12-31 11:19:30 +00:00
|
|
|
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CCopyConstructible<TDecay<T>>
|
2022-12-12 14:07:12 +00:00
|
|
|
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
2022-12-31 11:19:30 +00:00
|
|
|
FORCEINLINE TDecay<T>& Emplace(initializer_list<U> IL, Ts&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
Impl::Destroy();
|
2022-12-31 11:19:30 +00:00
|
|
|
return Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Removes any bound callable from the TFunction, restoring it to the default empty state. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Reset() { Impl::Destroy(); Impl::Invalidate(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Overloads the Swap algorithm for TFunction. */
|
2022-12-19 10:00:52 +00:00
|
|
|
friend FORCEINLINE constexpr void Swap(TFunction& A, TFunction& B) { Swap(static_cast<Impl&>(A), static_cast<Impl&>(B)); }
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
};
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* A class which represents a copy of something callable, but is move-only.
|
|
|
|
*
|
|
|
|
* It takes a copy of whatever is bound to it, meaning you can return it from functions and store them in
|
|
|
|
* objects without caring about the lifetime of the original object being bound.
|
|
|
|
*/
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
2022-12-30 11:11:01 +00:00
|
|
|
class TUniqueFunction final
|
2022-05-14 14:52:21 +00:00
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
2022-12-12 14:07:12 +00:00
|
|
|
false, true>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
|
2022-05-14 14:52:21 +00:00
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
2022-12-12 14:07:12 +00:00
|
|
|
false, true>;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
public:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Default constructor. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TUniqueFunction(nullptr_t = nullptr) { Impl::Invalidate(); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2023-02-26 13:55:36 +00:00
|
|
|
FORCEINLINE TUniqueFunction(const TUniqueFunction&) = delete;
|
|
|
|
FORCEINLINE TUniqueFunction(TUniqueFunction&&) = default;
|
|
|
|
FORCEINLINE TUniqueFunction& operator=(const TUniqueFunction&) = delete;
|
|
|
|
FORCEINLINE TUniqueFunction& operator=(TUniqueFunction&&) = default;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Constructor from TFunction to TUniqueFunction. */
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction(const TFunction<F>& InValue)
|
2022-12-12 14:07:12 +00:00
|
|
|
{
|
|
|
|
new (this) TFunction<F>(InValue);
|
|
|
|
}
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Constructor from TFunction to TUniqueFunction. */
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction(TFunction<F>&& InValue)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
new (this) TFunction<F>(MoveTemp(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Assignment operator from TFunction to TUniqueFunction. */
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(const TFunction<F>& InValue)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
*reinterpret_cast<TFunction<F>*>(this) = InValue;
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Assignment operator from TFunction to TUniqueFunction. */
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(TFunction<F>&& InValue)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
*reinterpret_cast<TFunction<F>*>(this) = MoveTemp(InValue);
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* Constructs an TUniqueFunction with initial content an function object of type TDecay<T>,
|
|
|
|
* direct-initialized from Forward<T>(InValue).
|
|
|
|
*/
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (!CTInPlaceType<TDecay<T>>
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
2022-12-12 14:07:12 +00:00
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>
|
2022-11-16 14:03:54 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(T&& InValue)
|
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::Invalidate();
|
|
|
|
else Impl::template Emplace<T>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an TUniqueFunction with initial content an function object of type TDecay<T>,
|
|
|
|
* direct-non-list-initialized from Forward<Ts>(Args)....
|
|
|
|
*/
|
2022-12-31 11:19:30 +00:00
|
|
|
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
|
|
|
FORCEINLINE explicit TUniqueFunction(TInPlaceType<T>, Ts&&... Args)
|
|
|
|
{
|
|
|
|
Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an TUniqueFunction with initial content an function object of type TDecay<T>,
|
|
|
|
* direct-non-list-initialized from IL, Forward<Ts>(Args)....
|
|
|
|
*/
|
|
|
|
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
|
|
|
FORCEINLINE explicit TUniqueFunction(TInPlaceType<T>, initializer_list<U> IL, Ts&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-31 11:19:30 +00:00
|
|
|
Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Removes any bound callable from the TUniqueFunction, restoring it to the default empty state. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr TUniqueFunction& operator=(nullptr_t) { Impl::Destroy(); Impl::Invalidate(); return *this; }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
2022-12-12 14:07:12 +00:00
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(T&& InValue)
|
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Reset();
|
|
|
|
else Emplace<T>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2022-12-18 11:11:53 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/**
|
|
|
|
* Changes the function object to one of type TDecay<T> constructed from the arguments.
|
|
|
|
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
|
|
|
* TDecay<T>, direct-non-list-initialized from Forward<Ts>(Args)..., as the function object.
|
|
|
|
*
|
|
|
|
* @param Args - The arguments to be passed to the constructor of the function object.
|
|
|
|
*
|
|
|
|
* @return A reference to the new function object.
|
|
|
|
*/
|
2022-12-31 11:19:30 +00:00
|
|
|
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
|
|
|
FORCEINLINE TDecay<T>& Emplace(Ts&&... Args)
|
|
|
|
{
|
|
|
|
Impl::Destroy();
|
|
|
|
using DecayedType = TDecay<T>;
|
|
|
|
return Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the function object to one of type TDecay<T> constructed from the arguments.
|
|
|
|
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
|
|
|
* TDecay<T>, direct-non-list-initialized from IL, Forward<Ts>(Args)..., as the function object.
|
|
|
|
*
|
|
|
|
* @param IL, Args - The arguments to be passed to the constructor of the function object.
|
|
|
|
*
|
|
|
|
* @return A reference to the new function object.
|
|
|
|
*/
|
|
|
|
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
|
|
|
FORCEINLINE TDecay<T>& Emplace(initializer_list<U> IL, Ts&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-12-12 14:07:12 +00:00
|
|
|
Impl::Destroy();
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-12-31 11:19:30 +00:00
|
|
|
return Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Removes any bound callable from the TUniqueFunction, restoring it to the default empty state. */
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr void Reset() { Impl::Destroy(); Impl::Invalidate(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Overloads the Swap algorithm for TUniqueFunction. */
|
2022-12-19 10:00:52 +00:00
|
|
|
friend FORCEINLINE constexpr void Swap(TUniqueFunction& A, TUniqueFunction& B) { Swap(static_cast<Impl&>(A), static_cast<Impl&>(B)); }
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
};
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
static_assert(sizeof(TFunction<void()>) == 64, "The byte size of TFunction is unexpected");
|
|
|
|
static_assert(sizeof(TUniqueFunction<void()>) == 64, "The byte size of TUniqueFunction is unexpected");
|
|
|
|
|
2022-12-12 14:07:12 +00:00
|
|
|
static_assert(alignof(TFunction<void()>) == 16, "The byte alignment of TFunction is unexpected");
|
|
|
|
static_assert(alignof(TUniqueFunction<void()>) == 16, "The byte alignment of TUniqueFunction is unexpected");
|
|
|
|
|
2022-04-08 03:44:36 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
|
|
|
|
|
|
|
template <typename F>
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TNotFunction
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
F Storage;
|
2022-04-08 03:44:36 +00:00
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<F&, Ts&&...>)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr auto operator()(Ts&&... Args) &
|
2022-11-16 11:13:37 +00:00
|
|
|
-> decltype(!Invoke(Storage, Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(Storage, Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<F&&, Ts&&...>)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr auto operator()(Ts&&... Args) &&
|
2022-11-16 11:13:37 +00:00
|
|
|
-> decltype(!Invoke(MoveTemp(Storage), Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(MoveTemp(Storage), Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<const F&, Ts&&...>)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr auto operator()(Ts&&... Args) const&
|
2022-11-16 11:13:37 +00:00
|
|
|
-> decltype(!Invoke(Storage, Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(Storage, Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<const F&&, Ts&&...>)
|
2022-12-13 14:02:39 +00:00
|
|
|
FORCEINLINE constexpr auto operator()(Ts&&... Args) const&&
|
2022-11-16 11:13:37 +00:00
|
|
|
-> decltype(!Invoke(MoveTemp(Storage), Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(MoveTemp(Storage), Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
/** Creates a forwarding call wrapper that returns the negation of the callable object it holds. */
|
|
|
|
template <typename F> requires (CConstructibleFrom<F, F&&> && CMoveConstructible<F>)
|
|
|
|
NODISCARD FORCEINLINE constexpr NAMESPACE_PRIVATE::TNotFunction<TDecay<F>> NotFn(F&& Func)
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-12-13 14:02:39 +00:00
|
|
|
return { Forward<F>(Func) };
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|