refactor(templates): optimize TAny and TFunction and fix TFunction alignment problem
This commit is contained in:
parent
096150a305
commit
9ff3b1c583
@ -1233,6 +1233,8 @@ void TestFunction()
|
||||
TFunction<bool(bool)> Identity = [](bool In) { return In; };
|
||||
TFunction<bool(bool)> NotIdentity = NotFn(Identity);
|
||||
|
||||
always_check(NotFn(Identity)(false));
|
||||
|
||||
always_check(Identity(true));
|
||||
always_check(NotIdentity(false));
|
||||
}
|
||||
|
@ -13,31 +13,73 @@ NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
inline constexpr size_t ANY_DEFAULT_INLINE_SIZE = 64 - sizeof(uintptr);
|
||||
inline constexpr size_t ANY_DEFAULT_INLINE_ALIGNMENT = 16;
|
||||
// TAny's CustomStorage concept, see FAnyDefaultStorage.
|
||||
template <typename T>
|
||||
concept CAnyStorage = true; // TODO
|
||||
|
||||
template <size_t InlineSize, size_t InlineAlignment = ANY_DEFAULT_INLINE_ALIGNMENT> requires (Memory::IsValidAlignment(InlineAlignment))
|
||||
struct alignas(InlineAlignment) TAny
|
||||
// TAny's default storage structure.
|
||||
struct alignas(16) FAnyDefaultStorage
|
||||
{
|
||||
constexpr TAny() : TypeInfo(0) { }
|
||||
// The built-in copy/move operators are disabled and CopyCustom/MoveCustom is used instead of them.
|
||||
|
||||
// You can add custom variables like this.
|
||||
//Type Variable;
|
||||
|
||||
//~ Begin CAnyStorage Interface
|
||||
inline static constexpr size_t InlineSize = 64 - sizeof(uintptr);
|
||||
inline static constexpr size_t InlineAlignment = 16;
|
||||
constexpr void* InlineAllocation() { return &InlineAllocationImpl; }
|
||||
constexpr const void* InlineAllocation() const { return &InlineAllocationImpl; }
|
||||
constexpr void*& HeapAllocation() { return HeapAllocationImpl; }
|
||||
constexpr void* HeapAllocation() const { return HeapAllocationImpl; }
|
||||
constexpr uintptr& TypeInfo() { return TypeInfoImpl; }
|
||||
constexpr uintptr TypeInfo() const { return TypeInfoImpl; }
|
||||
constexpr void CopyCustom(const FAnyDefaultStorage& InValue) { /* Variable = InValue.Variable; */ } // You just need to copy the custom variables.
|
||||
constexpr void MoveCustom( FAnyDefaultStorage&& InValue) { /* Variable = MoveTemp(InValue.Variable); */ } // You just need to move the custom variables.
|
||||
//~ End CAnyStorage Interface
|
||||
|
||||
union
|
||||
{
|
||||
uint8 InlineAllocationImpl[InlineSize];
|
||||
void* HeapAllocationImpl;
|
||||
};
|
||||
|
||||
uintptr TypeInfoImpl;
|
||||
|
||||
};
|
||||
|
||||
static_assert(CAnyStorage<FAnyDefaultStorage>);
|
||||
|
||||
// You can add custom storage area through CustomStorage, such as TFunction.
|
||||
// It is not recommended to use this, FAny is recommended.
|
||||
template <CAnyStorage CustomStorage = FAnyDefaultStorage>
|
||||
struct TAny
|
||||
{
|
||||
inline static constexpr size_t InlineSize = CustomStorage::InlineSize;
|
||||
inline static constexpr size_t InlineAlignment = CustomStorage::InlineAlignment;
|
||||
|
||||
constexpr TAny() { Storage.TypeInfo() = 0; }
|
||||
|
||||
constexpr TAny(FInvalid) : TAny() { }
|
||||
|
||||
|
||||
FORCEINLINE TAny(const TAny& InValue)
|
||||
: TypeInfo(InValue.TypeInfo)
|
||||
{
|
||||
Storage.CopyCustom(InValue.Storage);
|
||||
|
||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
||||
|
||||
if (!IsValid()) return;
|
||||
|
||||
switch (GetRepresentation())
|
||||
{
|
||||
case ERepresentation::Trivial:
|
||||
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
||||
break;
|
||||
case ERepresentation::Small:
|
||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
case ERepresentation::Big:
|
||||
HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
|
||||
Storage.HeapAllocation() = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
|
||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
default: check_no_entry();
|
||||
@ -45,21 +87,24 @@ struct alignas(InlineAlignment) TAny
|
||||
}
|
||||
|
||||
FORCEINLINE TAny(TAny&& InValue)
|
||||
: TypeInfo(InValue.TypeInfo)
|
||||
{
|
||||
Storage.MoveCustom(MoveTemp(InValue.Storage));
|
||||
|
||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
||||
|
||||
if (!IsValid()) return;
|
||||
|
||||
switch (GetRepresentation())
|
||||
{
|
||||
case ERepresentation::Trivial:
|
||||
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
||||
break;
|
||||
case ERepresentation::Small:
|
||||
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
case ERepresentation::Big:
|
||||
HeapAllocation = InValue.HeapAllocation;
|
||||
InValue.TypeInfo = 0;
|
||||
Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
|
||||
InValue.Storage.TypeInfo() = 0;
|
||||
break;
|
||||
default: check_no_entry();
|
||||
}
|
||||
@ -87,6 +132,8 @@ struct alignas(InlineAlignment) TAny
|
||||
{
|
||||
if (&InValue == this) return *this;
|
||||
|
||||
Storage.CopyCustom(InValue.Storage);
|
||||
|
||||
if (!InValue.IsValid())
|
||||
{
|
||||
Reset();
|
||||
@ -96,7 +143,7 @@ struct alignas(InlineAlignment) TAny
|
||||
switch (GetRepresentation())
|
||||
{
|
||||
case ERepresentation::Trivial:
|
||||
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
||||
break;
|
||||
case ERepresentation::Small:
|
||||
case ERepresentation::Big:
|
||||
@ -109,18 +156,18 @@ struct alignas(InlineAlignment) TAny
|
||||
{
|
||||
ResetImpl();
|
||||
|
||||
TypeInfo = InValue.TypeInfo;
|
||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
||||
|
||||
switch (GetRepresentation())
|
||||
{
|
||||
case ERepresentation::Trivial:
|
||||
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
||||
break;
|
||||
case ERepresentation::Small:
|
||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
case ERepresentation::Big:
|
||||
HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
|
||||
Storage.HeapAllocation() = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
|
||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
default: check_no_entry();
|
||||
@ -134,6 +181,8 @@ struct alignas(InlineAlignment) TAny
|
||||
{
|
||||
if (&InValue == this) return *this;
|
||||
|
||||
Storage.MoveCustom(MoveTemp(InValue.Storage));
|
||||
|
||||
if (!InValue.IsValid())
|
||||
{
|
||||
Reset();
|
||||
@ -143,15 +192,15 @@ struct alignas(InlineAlignment) TAny
|
||||
switch (GetRepresentation())
|
||||
{
|
||||
case ERepresentation::Trivial:
|
||||
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
||||
break;
|
||||
case ERepresentation::Small:
|
||||
GetTypeInfoImpl().MoveAssignImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
case ERepresentation::Big:
|
||||
ResetImpl();
|
||||
HeapAllocation = InValue.HeapAllocation;
|
||||
InValue.TypeInfo = 0;
|
||||
Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
|
||||
InValue.Storage.TypeInfo() = 0;
|
||||
break;
|
||||
default: check_no_entry();
|
||||
}
|
||||
@ -160,19 +209,19 @@ struct alignas(InlineAlignment) TAny
|
||||
{
|
||||
ResetImpl();
|
||||
|
||||
TypeInfo = InValue.TypeInfo;
|
||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
||||
|
||||
switch (GetRepresentation())
|
||||
{
|
||||
case ERepresentation::Trivial:
|
||||
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
||||
break;
|
||||
case ERepresentation::Small:
|
||||
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
|
||||
break;
|
||||
case ERepresentation::Big:
|
||||
HeapAllocation = InValue.HeapAllocation;
|
||||
InValue.TypeInfo = 0;
|
||||
Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
|
||||
InValue.Storage.TypeInfo() = 0;
|
||||
break;
|
||||
default: check_no_entry();
|
||||
}
|
||||
@ -211,10 +260,10 @@ struct alignas(InlineAlignment) TAny
|
||||
return GetValue<SelectedType>();
|
||||
}
|
||||
|
||||
constexpr const type_info& GetTypeInfo() const { return IsValid() ? *GetTypeInfoImpl().TypeInfo : typeid(void); }
|
||||
constexpr const type_info& GetTypeInfo() const { return IsValid() ? *GetTypeInfoImpl().NativeTypeInfo : typeid(void); }
|
||||
|
||||
constexpr bool IsValid() const { return TypeInfo != 0; }
|
||||
constexpr explicit operator bool() const { return TypeInfo != 0; }
|
||||
constexpr bool IsValid() const { return Storage.TypeInfo() != 0; }
|
||||
constexpr explicit operator bool() const { return Storage.TypeInfo() != 0; }
|
||||
|
||||
template <typename T> constexpr bool HoldsAlternative() const { return IsValid() ? GetTypeInfo() == typeid(T) : false; }
|
||||
|
||||
@ -236,10 +285,13 @@ struct alignas(InlineAlignment) TAny
|
||||
template <typename T> requires CSameAs<T, TDecay<T>>&& CDestructible<TDecay<T>>
|
||||
constexpr const T& Get(const T& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
||||
|
||||
constexpr CustomStorage& GetCustomStorage() requires (!CSameAs<CustomStorage, FAnyDefaultStorage>) { return Storage; }
|
||||
constexpr const CustomStorage& GetCustomStorage() const requires (!CSameAs<CustomStorage, FAnyDefaultStorage>) { return Storage; }
|
||||
|
||||
FORCEINLINE void Reset()
|
||||
{
|
||||
ResetImpl();
|
||||
TypeInfo = 0;
|
||||
Storage.TypeInfo() = 0;
|
||||
}
|
||||
|
||||
FORCEINLINE size_t GetTypeHash() const
|
||||
@ -280,6 +332,8 @@ struct alignas(InlineAlignment) TAny
|
||||
|
||||
private:
|
||||
|
||||
CustomStorage Storage;
|
||||
|
||||
static constexpr uintptr_t RepresentationMask = 3;
|
||||
|
||||
enum class ERepresentation : uint8
|
||||
@ -291,7 +345,7 @@ private:
|
||||
|
||||
struct FTypeInfoImpl
|
||||
{
|
||||
const type_info* TypeInfo;
|
||||
const type_info* NativeTypeInfo;
|
||||
|
||||
const size_t TypeSize;
|
||||
const size_t TypeAlignment;
|
||||
@ -321,9 +375,9 @@ private:
|
||||
template <typename T>
|
||||
constexpr FTypeInfoImpl(TInPlaceType<T>)
|
||||
|
||||
: TypeInfo (&typeid(T))
|
||||
, TypeSize ( sizeof(T))
|
||||
, TypeAlignment (alignof(T))
|
||||
: NativeTypeInfo (&typeid(T))
|
||||
, TypeSize ( sizeof(T))
|
||||
, TypeAlignment (alignof(T))
|
||||
|
||||
, CopyConstructImpl ([](void* A, const void* B) { if constexpr (requires(T* A, const T* B) { Memory::CopyConstruct (A, B); }) Memory::CopyConstruct (reinterpret_cast<T*>(A), reinterpret_cast<const T*>(B)); else checkf(false, TEXT("The type '%s' is not copy constructible."), typeid(Types).name()); })
|
||||
, MoveConstructImpl ([](void* A, void* B) { if constexpr (requires(T* A, T* B) { Memory::MoveConstruct (A, B); }) Memory::MoveConstruct (reinterpret_cast<T*>(A), reinterpret_cast< T*>(B)); else checkf(false, TEXT("The type '%s' is not move constructible."), typeid(Types).name()); })
|
||||
@ -339,43 +393,35 @@ private:
|
||||
{ }
|
||||
};
|
||||
|
||||
union
|
||||
{
|
||||
TAlignedStorage<InlineSize, 1> InlineAllocation;
|
||||
void* HeapAllocation;
|
||||
};
|
||||
constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(Storage.TypeInfo() & RepresentationMask); }
|
||||
constexpr const FTypeInfoImpl& GetTypeInfoImpl() const { return *reinterpret_cast<const FTypeInfoImpl*>(Storage.TypeInfo() & ~RepresentationMask); }
|
||||
|
||||
uintptr TypeInfo;
|
||||
|
||||
constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(TypeInfo & RepresentationMask); }
|
||||
constexpr const FTypeInfoImpl& GetTypeInfoImpl() const { return *reinterpret_cast<const FTypeInfoImpl*>(TypeInfo & ~RepresentationMask); }
|
||||
|
||||
constexpr void* GetAllocation() { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? &InlineAllocation : HeapAllocation; }
|
||||
constexpr const void* GetAllocation() const { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? &InlineAllocation : HeapAllocation; }
|
||||
constexpr void* GetAllocation() { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? Storage.InlineAllocation() : Storage.HeapAllocation(); }
|
||||
constexpr const void* GetAllocation() const { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? Storage.InlineAllocation() : Storage.HeapAllocation(); }
|
||||
|
||||
template <typename SelectedType, typename... Types>
|
||||
FORCEINLINE void EmplaceImpl(Types&&... Args)
|
||||
{
|
||||
static constexpr const FTypeInfoImpl SelectedTypeInfo(InPlaceType<SelectedType>);
|
||||
TypeInfo = reinterpret_cast<uintptr>(&SelectedTypeInfo);
|
||||
Storage.TypeInfo() = reinterpret_cast<uintptr>(&SelectedTypeInfo);
|
||||
|
||||
constexpr bool bIsInlineStorable = sizeof(SelectedType) <= InlineSize && alignof(SelectedType) <= InlineAlignment;
|
||||
constexpr bool bIsInlineStorable = sizeof(SelectedType) <= Storage.InlineSize && alignof(SelectedType) <= Storage.InlineAlignment;
|
||||
constexpr bool bIsTriviallyStorable = bIsInlineStorable && CTrivial<SelectedType> && CTriviallyCopyable<SelectedType>;
|
||||
|
||||
if constexpr (bIsTriviallyStorable)
|
||||
{
|
||||
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...);
|
||||
TypeInfo |= static_cast<uintptr>(ERepresentation::Trivial);
|
||||
new(Storage.InlineAllocation()) SelectedType(Forward<Types>(Args)...);
|
||||
Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Trivial);
|
||||
}
|
||||
else if constexpr (bIsInlineStorable)
|
||||
{
|
||||
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...);
|
||||
TypeInfo |= static_cast<uintptr>(ERepresentation::Small);
|
||||
new(Storage.InlineAllocation()) SelectedType(Forward<Types>(Args)...);
|
||||
Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Small);
|
||||
}
|
||||
else
|
||||
{
|
||||
HeapAllocation = new SelectedType(Forward<Types>(Args)...);
|
||||
TypeInfo |= static_cast<uintptr>(ERepresentation::Big);
|
||||
Storage.HeapAllocation() = new SelectedType(Forward<Types>(Args)...);
|
||||
Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Big);
|
||||
}
|
||||
}
|
||||
|
||||
@ -392,7 +438,7 @@ private:
|
||||
break;
|
||||
case ERepresentation::Big:
|
||||
GetTypeInfoImpl().DestroyImpl(GetAllocation());
|
||||
Memory::Free(HeapAllocation);
|
||||
Memory::Free(Storage.HeapAllocation());
|
||||
break;
|
||||
default: check_no_entry();
|
||||
}
|
||||
@ -411,32 +457,32 @@ private:
|
||||
if (LHS.IsValid() == false) return partial_ordering::equivalent;
|
||||
return LHS.GetTypeInfoImpl().SynthThreeWayCompareImpl(LHS.GetAllocation(), RHS.GetAllocation());;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
template <typename T, size_t InlineSize, size_t InlineAlignment>
|
||||
constexpr bool operator==(const TAny<InlineSize, InlineAlignment>& LHS, const T& RHS)
|
||||
template <typename T, CAnyStorage StorageType>
|
||||
constexpr bool operator==(const TAny<StorageType>& LHS, const T& RHS)
|
||||
{
|
||||
return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() == RHS : false;
|
||||
}
|
||||
|
||||
template <size_t InlineSize, size_t InlineAlignment>
|
||||
constexpr bool operator==(const TAny<InlineSize, InlineAlignment>& LHS, FInvalid)
|
||||
template <CAnyStorage StorageType>
|
||||
constexpr bool operator==(const TAny<StorageType>& LHS, FInvalid)
|
||||
{
|
||||
return !LHS.IsValid();
|
||||
}
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
|
||||
template <typename T> struct TIsTAny : FFalse { };
|
||||
template <size_t InlineSize, size_t InlineAlignment> struct TIsTAny<TAny<InlineSize, InlineAlignment>> : FTrue { };
|
||||
template <typename T> struct TIsTAny : FFalse { };
|
||||
template <CAnyStorage StorageType> struct TIsTAny<TAny<StorageType>> : FTrue { };
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
template <typename T>
|
||||
concept CTAny = NAMESPACE_PRIVATE::TIsTAny<T>::Value;
|
||||
|
||||
using FAny = TAny<ANY_DEFAULT_INLINE_SIZE>;
|
||||
using FAny = TAny<>;
|
||||
|
||||
static_assert(sizeof(FAny) == 64, "The byte size of FAny is unexpected");
|
||||
|
||||
|
@ -9,23 +9,17 @@
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
#include "Miscellaneous/AssertionMacros.h"
|
||||
|
||||
// NOTE: Disable alignment limit warning
|
||||
#pragma warning(disable : 4359)
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
inline constexpr size_t FUNCTION_DEFAULT_INLINE_SIZE = ANY_DEFAULT_INLINE_SIZE - sizeof(uintptr);
|
||||
inline constexpr size_t FUNCTION_DEFAULT_INLINE_ALIGNMENT = ANY_DEFAULT_INLINE_ALIGNMENT;
|
||||
|
||||
template <typename F> requires CFunction<F>
|
||||
struct TFunctionRef;
|
||||
|
||||
template <typename F, size_t InlineSize, size_t InlineAlignment> requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
||||
template <typename F> requires CFunction<F>
|
||||
struct TFunction;
|
||||
|
||||
template <typename F, size_t InlineSize, size_t InlineAlignment> requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
||||
template <typename F> requires CFunction<F>
|
||||
struct TUniqueFunction;
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
@ -33,11 +27,11 @@ NAMESPACE_PRIVATE_BEGIN
|
||||
template <typename T> struct TIsTFunctionRef : FFalse { };
|
||||
template <typename F> struct TIsTFunctionRef<TFunctionRef<F>> : FTrue { };
|
||||
|
||||
template <typename T> struct TIsTFunction : FFalse { };
|
||||
template <typename F, size_t I, size_t J> struct TIsTFunction<TFunction<F, I, J>> : FTrue { };
|
||||
template <typename T> struct TIsTFunction : FFalse { };
|
||||
template <typename F> struct TIsTFunction<TFunction<F>> : FTrue { };
|
||||
|
||||
template <typename T> struct TIsTUniqueFunction : FFalse { };
|
||||
template <typename F, size_t I, size_t J> struct TIsTUniqueFunction<TUniqueFunction<F, I, J>> : FTrue { };
|
||||
template <typename T> struct TIsTUniqueFunction : FFalse { };
|
||||
template <typename F> struct TIsTUniqueFunction<TUniqueFunction<F>> : FTrue { };
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
@ -86,16 +80,47 @@ template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) co
|
||||
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) const& > { using Fn = Ret(Types...); using CVRef = const int&; };
|
||||
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) const&&> { using Fn = Ret(Types...); using CVRef = const int&&; };
|
||||
|
||||
template <typename F, typename CVRef, size_t InlineSize, size_t InlineAlignment, bool bIsRef> struct TFunctionImpl;
|
||||
template <typename CallableType>
|
||||
struct alignas(16) FFunctionStorage
|
||||
{
|
||||
//~ Begin CAnyStorage Interface
|
||||
inline static constexpr size_t InlineSize = 64 - sizeof(uintptr) - sizeof(CallableType);
|
||||
inline static constexpr size_t InlineAlignment = 16;
|
||||
constexpr void* InlineAllocation() { return &InlineAllocationImpl; }
|
||||
constexpr const void* InlineAllocation() const { return &InlineAllocationImpl; }
|
||||
constexpr void*& HeapAllocation() { return HeapAllocationImpl; }
|
||||
constexpr void* HeapAllocation() const { return HeapAllocationImpl; }
|
||||
constexpr uintptr& TypeInfo() { return TypeInfoImpl; }
|
||||
constexpr uintptr TypeInfo() const { return TypeInfoImpl; }
|
||||
constexpr void CopyCustom(const FFunctionStorage& InValue) { Callable = InValue.Callable; }
|
||||
constexpr void MoveCustom( FFunctionStorage&& InValue) { Callable = InValue.Callable; }
|
||||
//~ End CAnyStorage Interface
|
||||
|
||||
template <typename Ret, typename... Types, typename CVRef, size_t InlineSize, size_t InlineAlignment, bool bIsRef>
|
||||
struct alignas(InlineAlignment) TFunctionImpl<Ret(Types...), CVRef, InlineSize, InlineAlignment, bIsRef>
|
||||
union
|
||||
{
|
||||
uint8 InlineAllocationImpl[InlineSize];
|
||||
void* HeapAllocationImpl;
|
||||
};
|
||||
|
||||
uintptr TypeInfoImpl;
|
||||
|
||||
CallableType Callable;
|
||||
|
||||
template <CAnyStorage T>
|
||||
friend struct TAny;
|
||||
|
||||
};
|
||||
|
||||
template <typename F, typename CVRef, bool bIsRef> struct TFunctionImpl;
|
||||
|
||||
template <typename Ret, typename... Types, typename CVRef, bool bIsRef>
|
||||
struct TFunctionImpl<Ret(Types...), CVRef, bIsRef>
|
||||
{
|
||||
public:
|
||||
|
||||
using ResultType = Ret;
|
||||
using ArgumentType = TTuple<Types...>;
|
||||
|
||||
|
||||
TFunctionImpl() = default;
|
||||
TFunctionImpl(const TFunctionImpl&) = default;
|
||||
TFunctionImpl(TFunctionImpl&& InValue) = default;
|
||||
@ -110,8 +135,8 @@ public:
|
||||
FORCEINLINE ResultType operator()(Types... Args) const& requires (CSameAs<CVRef, const int& >) { return CallImpl(Forward<Types>(Args)...); }
|
||||
FORCEINLINE ResultType operator()(Types... Args) const&& requires (CSameAs<CVRef, const int&&>) { return CallImpl(Forward<Types>(Args)...); }
|
||||
|
||||
constexpr bool IsValid() const { return Callable != nullptr; }
|
||||
constexpr explicit operator bool() const { return Callable != nullptr; }
|
||||
constexpr bool IsValid() const { return GetCallableImpl() != nullptr; }
|
||||
constexpr explicit operator bool() const { return GetCallableImpl() != nullptr; }
|
||||
|
||||
FORCEINLINE const type_info& TargetType() const requires (!bIsRef) { return IsValid() ? Storage.GetTypeInfo() : typeid(void); };
|
||||
|
||||
@ -140,30 +165,49 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
Swap(Callable, InValue.Callable);
|
||||
Swap(Storage, InValue.Storage);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
using StorageType = TConditional<bIsRef, TCopyConst<CVRef, void>*, TAny<InlineSize, 1>>;
|
||||
using StorageRef = TConditional<bIsRef, TCopyConst<CVRef, void>*, TCopyCVRef<CVRef, StorageType>&>;
|
||||
using StoragePtrType = TCopyConst<CVRef, void>*;
|
||||
using CallableType = ResultType(*)(StoragePtrType, Types&&...);
|
||||
|
||||
using CallFunc = ResultType(*)(StorageRef, Types&&...);
|
||||
struct FunctionRefStorage
|
||||
{
|
||||
StoragePtrType Ptr;
|
||||
CallableType Callable;
|
||||
};
|
||||
|
||||
using FunctionStorage = TAny<FFunctionStorage<CallableType>>;
|
||||
using StorageType = TConditional<bIsRef, FunctionRefStorage, FunctionStorage>;
|
||||
|
||||
StorageType Storage;
|
||||
CallFunc Callable;
|
||||
|
||||
FORCEINLINE CallableType& GetCallableImpl()
|
||||
{
|
||||
if constexpr (bIsRef) return Storage.Callable;
|
||||
else return Storage.GetCustomStorage().Callable;
|
||||
}
|
||||
|
||||
FORCEINLINE CallableType GetCallableImpl() const
|
||||
{
|
||||
if constexpr (bIsRef) return Storage.Callable;
|
||||
else return Storage.GetCustomStorage().Callable;
|
||||
}
|
||||
|
||||
FORCEINLINE ResultType CallImpl(Types&&... Args)
|
||||
{
|
||||
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
||||
return Callable(Storage, Forward<Types>(Args)...);
|
||||
if constexpr (bIsRef) return GetCallableImpl()(Storage.Ptr, Forward<Types>(Args)...);
|
||||
else return GetCallableImpl()(&Storage, Forward<Types>(Args)...);
|
||||
}
|
||||
|
||||
FORCEINLINE ResultType CallImpl(Types&&... Args) const
|
||||
{
|
||||
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
||||
return Callable(Storage, Forward<Types>(Args)...);
|
||||
if constexpr (bIsRef) return GetCallableImpl()(Storage.Ptr, Forward<Types>(Args)...);
|
||||
else return GetCallableImpl()(&Storage, Forward<Types>(Args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -171,23 +215,23 @@ protected:
|
||||
template <typename DecayedType, typename... ArgTypes>
|
||||
FORCEINLINE void EmplaceImpl(ArgTypes&&... Args)
|
||||
{
|
||||
using CallableType = TCopyConst<TRemoveReference<CVRef>, DecayedType>;
|
||||
using FuncType = TCopyConst<TRemoveReference<CVRef>, DecayedType>;
|
||||
|
||||
if constexpr (bIsRef) Storage = ((reinterpret_cast<StorageType>(AddressOf(Args))), ...);
|
||||
if constexpr (bIsRef) Storage.Ptr = (AddressOf(Args), ...);
|
||||
else Storage.template Emplace<DecayedType>(Forward<ArgTypes>(Args)...);
|
||||
|
||||
Callable = [](StorageRef Storage, Types&&... Args) -> ResultType
|
||||
GetCallableImpl() = [](StoragePtrType Storage, Types&&... Args) -> ResultType
|
||||
{
|
||||
using InvokeType = TConditional<
|
||||
CReference<CVRef>,
|
||||
TCopyCVRef<CVRef, CallableType>,
|
||||
TCopyCVRef<CVRef, CallableType>&
|
||||
TCopyCVRef<CVRef, FuncType>,
|
||||
TCopyCVRef<CVRef, FuncType>&
|
||||
>;
|
||||
|
||||
const auto GetFunc = [&Storage]() -> InvokeType
|
||||
const auto GetFunc = [Storage]() -> InvokeType
|
||||
{
|
||||
if constexpr (!bIsRef) return Storage.template GetValue<DecayedType>();
|
||||
else return static_cast<InvokeType>(*reinterpret_cast<CallableType*>(Storage));
|
||||
if constexpr (!bIsRef) return static_cast<TCopyConst<CVRef, FunctionStorage>*>(Storage)->template GetValue<DecayedType>();
|
||||
else return static_cast<InvokeType>(*reinterpret_cast<FuncType*>(Storage));
|
||||
};
|
||||
|
||||
return InvokeResult<ResultType>(GetFunc(), Forward<Types>(Args)...);
|
||||
@ -196,11 +240,7 @@ protected:
|
||||
|
||||
FORCEINLINE void AssignImpl(const TFunctionImpl& InValue)
|
||||
{
|
||||
if (InValue.IsValid())
|
||||
{
|
||||
Callable = InValue.Callable;
|
||||
Storage = InValue.Storage;
|
||||
}
|
||||
if (InValue.IsValid()) Storage = InValue.Storage;
|
||||
else ResetImpl();
|
||||
}
|
||||
|
||||
@ -208,14 +248,13 @@ protected:
|
||||
{
|
||||
if (InValue.IsValid())
|
||||
{
|
||||
Callable = InValue.Callable;
|
||||
Storage = MoveTemp(InValue.Storage);
|
||||
InValue.ResetImpl();
|
||||
}
|
||||
else ResetImpl();
|
||||
}
|
||||
|
||||
constexpr void ResetImpl() { Callable = nullptr; }
|
||||
constexpr void ResetImpl() { GetCallableImpl() = nullptr; }
|
||||
|
||||
};
|
||||
|
||||
@ -226,8 +265,6 @@ struct TFunctionRef
|
||||
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
||||
FUNCTION_DEFAULT_INLINE_SIZE,
|
||||
FUNCTION_DEFAULT_INLINE_ALIGNMENT,
|
||||
true>
|
||||
{
|
||||
private:
|
||||
@ -235,8 +272,6 @@ private:
|
||||
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
||||
FUNCTION_DEFAULT_INLINE_SIZE,
|
||||
FUNCTION_DEFAULT_INLINE_ALIGNMENT,
|
||||
true>;
|
||||
|
||||
public:
|
||||
@ -263,14 +298,11 @@ public:
|
||||
|
||||
};
|
||||
|
||||
template <typename F, size_t InlineSize = FUNCTION_DEFAULT_INLINE_SIZE, size_t InlineAlignment = FUNCTION_DEFAULT_INLINE_ALIGNMENT>
|
||||
requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
||||
template <typename F> requires CFunction<F>
|
||||
struct TFunction
|
||||
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
||||
InlineSize,
|
||||
InlineAlignment,
|
||||
false>
|
||||
{
|
||||
private:
|
||||
@ -278,8 +310,6 @@ private:
|
||||
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
||||
InlineSize,
|
||||
InlineAlignment,
|
||||
false>;
|
||||
|
||||
public:
|
||||
@ -349,14 +379,11 @@ public:
|
||||
|
||||
};
|
||||
|
||||
template <typename F, size_t InlineSize = FUNCTION_DEFAULT_INLINE_SIZE, size_t InlineAlignment = FUNCTION_DEFAULT_INLINE_ALIGNMENT>
|
||||
requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
||||
template <typename F> requires CFunction<F>
|
||||
struct TUniqueFunction
|
||||
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
||||
InlineSize,
|
||||
InlineAlignment,
|
||||
false>
|
||||
{
|
||||
private:
|
||||
@ -364,8 +391,6 @@ private:
|
||||
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
||||
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
||||
InlineSize,
|
||||
InlineAlignment,
|
||||
false>;
|
||||
|
||||
public:
|
||||
@ -383,23 +408,23 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCEINLINE TUniqueFunction(const TFunction<F, InlineSize, InlineAlignment>& InValue)
|
||||
FORCEINLINE TUniqueFunction(const TFunction<F>& InValue)
|
||||
: Super(*reinterpret_cast<const TUniqueFunction*>(&InValue))
|
||||
{ }
|
||||
|
||||
FORCEINLINE TUniqueFunction(TFunction<F, InlineSize, InlineAlignment>&& InValue)
|
||||
FORCEINLINE TUniqueFunction(TFunction<F>&& InValue)
|
||||
: Super(MoveTemp(*reinterpret_cast<const TUniqueFunction*>(&InValue)))
|
||||
{
|
||||
InValue.Reset();
|
||||
}
|
||||
|
||||
FORCEINLINE TUniqueFunction& operator=(const TFunction<F, InlineSize, InlineAlignment>& InValue)
|
||||
FORCEINLINE TUniqueFunction& operator=(const TFunction<F>& InValue)
|
||||
{
|
||||
Super::AssignImpl(*reinterpret_cast<const TUniqueFunction*>(&InValue));
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCEINLINE TUniqueFunction& operator=(TFunction<F, InlineSize, InlineAlignment>&& InValue)
|
||||
FORCEINLINE TUniqueFunction& operator=(TFunction<F>&& InValue)
|
||||
{
|
||||
Super::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue)));
|
||||
return *this;
|
||||
|
Loading…
Reference in New Issue
Block a user