refactor(templates): optimize TAny and TFunction and fix TFunction alignment problem

This commit is contained in:
_Redstone_c_ 2022-11-13 22:21:00 +08:00
parent 096150a305
commit 9ff3b1c583
3 changed files with 194 additions and 121 deletions

View File

@ -1233,6 +1233,8 @@ void TestFunction()
TFunction<bool(bool)> Identity = [](bool In) { return In; }; TFunction<bool(bool)> Identity = [](bool In) { return In; };
TFunction<bool(bool)> NotIdentity = NotFn(Identity); TFunction<bool(bool)> NotIdentity = NotFn(Identity);
always_check(NotFn(Identity)(false));
always_check(Identity(true)); always_check(Identity(true));
always_check(NotIdentity(false)); always_check(NotIdentity(false));
} }

View File

@ -13,31 +13,73 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
inline constexpr size_t ANY_DEFAULT_INLINE_SIZE = 64 - sizeof(uintptr); // TAny's CustomStorage concept, see FAnyDefaultStorage.
inline constexpr size_t ANY_DEFAULT_INLINE_ALIGNMENT = 16; template <typename T>
concept CAnyStorage = true; // TODO
template <size_t InlineSize, size_t InlineAlignment = ANY_DEFAULT_INLINE_ALIGNMENT> requires (Memory::IsValidAlignment(InlineAlignment)) // TAny's default storage structure.
struct alignas(InlineAlignment) TAny 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() { } constexpr TAny(FInvalid) : TAny() { }
FORCEINLINE TAny(const TAny& InValue) FORCEINLINE TAny(const TAny& InValue)
: TypeInfo(InValue.TypeInfo)
{ {
Storage.CopyCustom(InValue.Storage);
Storage.TypeInfo() = InValue.Storage.TypeInfo();
if (!IsValid()) return; if (!IsValid()) return;
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case ERepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
break; break;
case ERepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case ERepresentation::Big: case ERepresentation::Big:
HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment); Storage.HeapAllocation() = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
default: check_no_entry(); default: check_no_entry();
@ -45,21 +87,24 @@ struct alignas(InlineAlignment) TAny
} }
FORCEINLINE TAny(TAny&& InValue) FORCEINLINE TAny(TAny&& InValue)
: TypeInfo(InValue.TypeInfo)
{ {
Storage.MoveCustom(MoveTemp(InValue.Storage));
Storage.TypeInfo() = InValue.Storage.TypeInfo();
if (!IsValid()) return; if (!IsValid()) return;
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case ERepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
break; break;
case ERepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case ERepresentation::Big: case ERepresentation::Big:
HeapAllocation = InValue.HeapAllocation; Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
InValue.TypeInfo = 0; InValue.Storage.TypeInfo() = 0;
break; break;
default: check_no_entry(); default: check_no_entry();
} }
@ -87,6 +132,8 @@ struct alignas(InlineAlignment) TAny
{ {
if (&InValue == this) return *this; if (&InValue == this) return *this;
Storage.CopyCustom(InValue.Storage);
if (!InValue.IsValid()) if (!InValue.IsValid())
{ {
Reset(); Reset();
@ -96,7 +143,7 @@ struct alignas(InlineAlignment) TAny
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case ERepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
break; break;
case ERepresentation::Small: case ERepresentation::Small:
case ERepresentation::Big: case ERepresentation::Big:
@ -109,18 +156,18 @@ struct alignas(InlineAlignment) TAny
{ {
ResetImpl(); ResetImpl();
TypeInfo = InValue.TypeInfo; Storage.TypeInfo() = InValue.Storage.TypeInfo();
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case ERepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
break; break;
case ERepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case ERepresentation::Big: case ERepresentation::Big:
HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment); Storage.HeapAllocation() = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
default: check_no_entry(); default: check_no_entry();
@ -134,6 +181,8 @@ struct alignas(InlineAlignment) TAny
{ {
if (&InValue == this) return *this; if (&InValue == this) return *this;
Storage.MoveCustom(MoveTemp(InValue.Storage));
if (!InValue.IsValid()) if (!InValue.IsValid())
{ {
Reset(); Reset();
@ -143,15 +192,15 @@ struct alignas(InlineAlignment) TAny
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case ERepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
break; break;
case ERepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().MoveAssignImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().MoveAssignImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case ERepresentation::Big: case ERepresentation::Big:
ResetImpl(); ResetImpl();
HeapAllocation = InValue.HeapAllocation; Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
InValue.TypeInfo = 0; InValue.Storage.TypeInfo() = 0;
break; break;
default: check_no_entry(); default: check_no_entry();
} }
@ -160,19 +209,19 @@ struct alignas(InlineAlignment) TAny
{ {
ResetImpl(); ResetImpl();
TypeInfo = InValue.TypeInfo; Storage.TypeInfo() = InValue.Storage.TypeInfo();
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case ERepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
break; break;
case ERepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case ERepresentation::Big: case ERepresentation::Big:
HeapAllocation = InValue.HeapAllocation; Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
InValue.TypeInfo = 0; InValue.Storage.TypeInfo() = 0;
break; break;
default: check_no_entry(); default: check_no_entry();
} }
@ -211,10 +260,10 @@ struct alignas(InlineAlignment) TAny
return GetValue<SelectedType>(); 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 bool IsValid() const { return Storage.TypeInfo() != 0; }
constexpr explicit operator bool() const { return TypeInfo != 0; } constexpr explicit operator bool() const { return Storage.TypeInfo() != 0; }
template <typename T> constexpr bool HoldsAlternative() const { return IsValid() ? GetTypeInfo() == typeid(T) : false; } 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>> 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 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() FORCEINLINE void Reset()
{ {
ResetImpl(); ResetImpl();
TypeInfo = 0; Storage.TypeInfo() = 0;
} }
FORCEINLINE size_t GetTypeHash() const FORCEINLINE size_t GetTypeHash() const
@ -280,6 +332,8 @@ struct alignas(InlineAlignment) TAny
private: private:
CustomStorage Storage;
static constexpr uintptr_t RepresentationMask = 3; static constexpr uintptr_t RepresentationMask = 3;
enum class ERepresentation : uint8 enum class ERepresentation : uint8
@ -291,7 +345,7 @@ private:
struct FTypeInfoImpl struct FTypeInfoImpl
{ {
const type_info* TypeInfo; const type_info* NativeTypeInfo;
const size_t TypeSize; const size_t TypeSize;
const size_t TypeAlignment; const size_t TypeAlignment;
@ -321,9 +375,9 @@ private:
template <typename T> template <typename T>
constexpr FTypeInfoImpl(TInPlaceType<T>) constexpr FTypeInfoImpl(TInPlaceType<T>)
: TypeInfo (&typeid(T)) : NativeTypeInfo (&typeid(T))
, TypeSize ( sizeof(T)) , TypeSize ( sizeof(T))
, TypeAlignment (alignof(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()); }) , 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()); }) , 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 constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(Storage.TypeInfo() & RepresentationMask); }
{ constexpr const FTypeInfoImpl& GetTypeInfoImpl() const { return *reinterpret_cast<const FTypeInfoImpl*>(Storage.TypeInfo() & ~RepresentationMask); }
TAlignedStorage<InlineSize, 1> InlineAllocation;
void* HeapAllocation;
};
uintptr TypeInfo; 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(); }
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; }
template <typename SelectedType, typename... Types> template <typename SelectedType, typename... Types>
FORCEINLINE void EmplaceImpl(Types&&... Args) FORCEINLINE void EmplaceImpl(Types&&... Args)
{ {
static constexpr const FTypeInfoImpl SelectedTypeInfo(InPlaceType<SelectedType>); 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>; constexpr bool bIsTriviallyStorable = bIsInlineStorable && CTrivial<SelectedType> && CTriviallyCopyable<SelectedType>;
if constexpr (bIsTriviallyStorable) if constexpr (bIsTriviallyStorable)
{ {
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...); new(Storage.InlineAllocation()) SelectedType(Forward<Types>(Args)...);
TypeInfo |= static_cast<uintptr>(ERepresentation::Trivial); Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Trivial);
} }
else if constexpr (bIsInlineStorable) else if constexpr (bIsInlineStorable)
{ {
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...); new(Storage.InlineAllocation()) SelectedType(Forward<Types>(Args)...);
TypeInfo |= static_cast<uintptr>(ERepresentation::Small); Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Small);
} }
else else
{ {
HeapAllocation = new SelectedType(Forward<Types>(Args)...); Storage.HeapAllocation() = new SelectedType(Forward<Types>(Args)...);
TypeInfo |= static_cast<uintptr>(ERepresentation::Big); Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Big);
} }
} }
@ -392,7 +438,7 @@ private:
break; break;
case ERepresentation::Big: case ERepresentation::Big:
GetTypeInfoImpl().DestroyImpl(GetAllocation()); GetTypeInfoImpl().DestroyImpl(GetAllocation());
Memory::Free(HeapAllocation); Memory::Free(Storage.HeapAllocation());
break; break;
default: check_no_entry(); default: check_no_entry();
} }
@ -414,29 +460,29 @@ private:
}; };
template <typename T, size_t InlineSize, size_t InlineAlignment> template <typename T, CAnyStorage StorageType>
constexpr bool operator==(const TAny<InlineSize, InlineAlignment>& LHS, const T& RHS) constexpr bool operator==(const TAny<StorageType>& LHS, const T& RHS)
{ {
return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() == RHS : false; return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() == RHS : false;
} }
template <size_t InlineSize, size_t InlineAlignment> template <CAnyStorage StorageType>
constexpr bool operator==(const TAny<InlineSize, InlineAlignment>& LHS, FInvalid) constexpr bool operator==(const TAny<StorageType>& LHS, FInvalid)
{ {
return !LHS.IsValid(); return !LHS.IsValid();
} }
NAMESPACE_PRIVATE_BEGIN NAMESPACE_PRIVATE_BEGIN
template <typename T> struct TIsTAny : FFalse { }; template <typename T> struct TIsTAny : FFalse { };
template <size_t InlineSize, size_t InlineAlignment> struct TIsTAny<TAny<InlineSize, InlineAlignment>> : FTrue { }; template <CAnyStorage StorageType> struct TIsTAny<TAny<StorageType>> : FTrue { };
NAMESPACE_PRIVATE_END NAMESPACE_PRIVATE_END
template <typename T> template <typename T>
concept CTAny = NAMESPACE_PRIVATE::TIsTAny<T>::Value; 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"); static_assert(sizeof(FAny) == 64, "The byte size of FAny is unexpected");

View File

@ -9,23 +9,17 @@
#include "TypeTraits/TypeTraits.h" #include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/AssertionMacros.h"
// NOTE: Disable alignment limit warning
#pragma warning(disable : 4359)
NAMESPACE_REDCRAFT_BEGIN NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) 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> template <typename F> requires CFunction<F>
struct TFunctionRef; 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; 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; struct TUniqueFunction;
NAMESPACE_PRIVATE_BEGIN NAMESPACE_PRIVATE_BEGIN
@ -33,11 +27,11 @@ NAMESPACE_PRIVATE_BEGIN
template <typename T> struct TIsTFunctionRef : FFalse { }; template <typename T> struct TIsTFunctionRef : FFalse { };
template <typename F> struct TIsTFunctionRef<TFunctionRef<F>> : FTrue { }; template <typename F> struct TIsTFunctionRef<TFunctionRef<F>> : FTrue { };
template <typename T> struct TIsTFunction : FFalse { }; template <typename T> struct TIsTFunction : FFalse { };
template <typename F, size_t I, size_t J> struct TIsTFunction<TFunction<F, I, J>> : FTrue { }; template <typename F> struct TIsTFunction<TFunction<F>> : FTrue { };
template <typename T> struct TIsTUniqueFunction : FFalse { }; template <typename T> struct TIsTUniqueFunction : FFalse { };
template <typename F, size_t I, size_t J> struct TIsTUniqueFunction<TUniqueFunction<F, I, J>> : FTrue { }; template <typename F> struct TIsTUniqueFunction<TUniqueFunction<F>> : FTrue { };
NAMESPACE_PRIVATE_END NAMESPACE_PRIVATE_END
@ -86,10 +80,41 @@ 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 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> union
struct alignas(InlineAlignment) TFunctionImpl<Ret(Types...), CVRef, InlineSize, InlineAlignment, bIsRef> {
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: public:
@ -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)...); }
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 bool IsValid() const { return GetCallableImpl() != nullptr; }
constexpr explicit operator bool() const { return Callable != nullptr; } constexpr explicit operator bool() const { return GetCallableImpl() != nullptr; }
FORCEINLINE const type_info& TargetType() const requires (!bIsRef) { return IsValid() ? Storage.GetTypeInfo() : typeid(void); }; FORCEINLINE const type_info& TargetType() const requires (!bIsRef) { return IsValid() ? Storage.GetTypeInfo() : typeid(void); };
@ -140,30 +165,49 @@ public:
return; return;
} }
Swap(Callable, InValue.Callable);
Swap(Storage, InValue.Storage); Swap(Storage, InValue.Storage);
} }
private: private:
using StorageType = TConditional<bIsRef, TCopyConst<CVRef, void>*, TAny<InlineSize, 1>>; using StoragePtrType = TCopyConst<CVRef, void>*;
using StorageRef = TConditional<bIsRef, TCopyConst<CVRef, void>*, TCopyCVRef<CVRef, StorageType>&>; 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; 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) FORCEINLINE ResultType CallImpl(Types&&... Args)
{ {
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!")); 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 FORCEINLINE ResultType CallImpl(Types&&... Args) const
{ {
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!")); 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: protected:
@ -171,23 +215,23 @@ protected:
template <typename DecayedType, typename... ArgTypes> template <typename DecayedType, typename... ArgTypes>
FORCEINLINE void EmplaceImpl(ArgTypes&&... Args) 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)...); else Storage.template Emplace<DecayedType>(Forward<ArgTypes>(Args)...);
Callable = [](StorageRef Storage, Types&&... Args) -> ResultType GetCallableImpl() = [](StoragePtrType Storage, Types&&... Args) -> ResultType
{ {
using InvokeType = TConditional< using InvokeType = TConditional<
CReference<CVRef>, CReference<CVRef>,
TCopyCVRef<CVRef, CallableType>, TCopyCVRef<CVRef, FuncType>,
TCopyCVRef<CVRef, CallableType>& TCopyCVRef<CVRef, FuncType>&
>; >;
const auto GetFunc = [&Storage]() -> InvokeType const auto GetFunc = [Storage]() -> InvokeType
{ {
if constexpr (!bIsRef) return Storage.template GetValue<DecayedType>(); if constexpr (!bIsRef) return static_cast<TCopyConst<CVRef, FunctionStorage>*>(Storage)->template GetValue<DecayedType>();
else return static_cast<InvokeType>(*reinterpret_cast<CallableType*>(Storage)); else return static_cast<InvokeType>(*reinterpret_cast<FuncType*>(Storage));
}; };
return InvokeResult<ResultType>(GetFunc(), Forward<Types>(Args)...); return InvokeResult<ResultType>(GetFunc(), Forward<Types>(Args)...);
@ -196,11 +240,7 @@ protected:
FORCEINLINE void AssignImpl(const TFunctionImpl& InValue) FORCEINLINE void AssignImpl(const TFunctionImpl& InValue)
{ {
if (InValue.IsValid()) if (InValue.IsValid()) Storage = InValue.Storage;
{
Callable = InValue.Callable;
Storage = InValue.Storage;
}
else ResetImpl(); else ResetImpl();
} }
@ -208,14 +248,13 @@ protected:
{ {
if (InValue.IsValid()) if (InValue.IsValid())
{ {
Callable = InValue.Callable;
Storage = MoveTemp(InValue.Storage); Storage = MoveTemp(InValue.Storage);
InValue.ResetImpl(); InValue.ResetImpl();
} }
else ResetImpl(); else ResetImpl();
} }
constexpr void ResetImpl() { Callable = nullptr; } constexpr void ResetImpl() { GetCallableImpl() = nullptr; }
}; };
@ -226,8 +265,6 @@ struct TFunctionRef
: public NAMESPACE_PRIVATE::TFunctionImpl< : public NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
FUNCTION_DEFAULT_INLINE_SIZE,
FUNCTION_DEFAULT_INLINE_ALIGNMENT,
true> true>
{ {
private: private:
@ -235,8 +272,6 @@ private:
using Super = NAMESPACE_PRIVATE::TFunctionImpl< using Super = NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
FUNCTION_DEFAULT_INLINE_SIZE,
FUNCTION_DEFAULT_INLINE_ALIGNMENT,
true>; true>;
public: public:
@ -263,14 +298,11 @@ public:
}; };
template <typename F, size_t InlineSize = FUNCTION_DEFAULT_INLINE_SIZE, size_t InlineAlignment = FUNCTION_DEFAULT_INLINE_ALIGNMENT> template <typename F> requires CFunction<F>
requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
struct TFunction struct TFunction
: public NAMESPACE_PRIVATE::TFunctionImpl< : public NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
InlineSize,
InlineAlignment,
false> false>
{ {
private: private:
@ -278,8 +310,6 @@ private:
using Super = NAMESPACE_PRIVATE::TFunctionImpl< using Super = NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
InlineSize,
InlineAlignment,
false>; false>;
public: public:
@ -349,14 +379,11 @@ public:
}; };
template <typename F, size_t InlineSize = FUNCTION_DEFAULT_INLINE_SIZE, size_t InlineAlignment = FUNCTION_DEFAULT_INLINE_ALIGNMENT> template <typename F> requires CFunction<F>
requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
struct TUniqueFunction struct TUniqueFunction
: public NAMESPACE_PRIVATE::TFunctionImpl< : public NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
InlineSize,
InlineAlignment,
false> false>
{ {
private: private:
@ -364,8 +391,6 @@ private:
using Super = NAMESPACE_PRIVATE::TFunctionImpl< using Super = NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef, typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
InlineSize,
InlineAlignment,
false>; false>;
public: public:
@ -383,23 +408,23 @@ public:
return *this; return *this;
} }
FORCEINLINE TUniqueFunction(const TFunction<F, InlineSize, InlineAlignment>& InValue) FORCEINLINE TUniqueFunction(const TFunction<F>& InValue)
: Super(*reinterpret_cast<const TUniqueFunction*>(&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))) : Super(MoveTemp(*reinterpret_cast<const TUniqueFunction*>(&InValue)))
{ {
InValue.Reset(); 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)); Super::AssignImpl(*reinterpret_cast<const TUniqueFunction*>(&InValue));
return *this; return *this;
} }
FORCEINLINE TUniqueFunction& operator=(TFunction<F, InlineSize, InlineAlignment>&& InValue) FORCEINLINE TUniqueFunction& operator=(TFunction<F>&& InValue)
{ {
Super::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue))); Super::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue)));
return *this; return *this;