refactor(templates): optimize TAny implementation, increase inline storage size
This commit is contained in:
parent
59d3a9eac2
commit
eb814e37d3
@ -9,193 +9,74 @@
|
|||||||
#include "Miscellaneous/TypeInfo.h"
|
#include "Miscellaneous/TypeInfo.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)
|
||||||
|
|
||||||
NAMESPACE_PRIVATE_BEGIN
|
NAMESPACE_PRIVATE_BEGIN
|
||||||
|
|
||||||
template <typename T>
|
enum class EAnyRepresentation : uint8
|
||||||
void* AnyCopyNew(const void* Source)
|
|
||||||
{
|
{
|
||||||
if constexpr (!TIsCopyConstructible<T>::Value) check_no_entry();
|
Trivial, // Trivial
|
||||||
else return new T(*reinterpret_cast<const T*>(Source));
|
// Inline, // InlineAllocation
|
||||||
return nullptr;
|
Small, // Trivial & Inline
|
||||||
}
|
Big, // HeapAllocation
|
||||||
|
|
||||||
using FAnyCopyNewFunc = void* (*)(const void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void* AnyMoveNew(void* Source)
|
|
||||||
{
|
|
||||||
if constexpr (!TIsMoveConstructible<T>::Value) check_no_entry();
|
|
||||||
else return new T(MoveTemp(*reinterpret_cast<T*>(Source)));
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyMoveNewFunc = void* (*)(void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void AnyDelete(void* InValue)
|
|
||||||
{
|
|
||||||
delete reinterpret_cast<T*>(InValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyDeleteFunc = void(*)(void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void AnyDestroy(void* InValue)
|
|
||||||
{
|
|
||||||
if constexpr (!TIsTriviallyDestructible<T>::Value)
|
|
||||||
{
|
|
||||||
typedef T DestructOptionalType;
|
|
||||||
reinterpret_cast<T*>(InValue)->DestructOptionalType::~DestructOptionalType();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyDestroyFunc = void(*)(void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void AnyCopyConstruct(void* Target, const void* Source)
|
|
||||||
{
|
|
||||||
if constexpr (!TIsCopyConstructible<T>::Value) check_no_entry();
|
|
||||||
else new(reinterpret_cast<T*>(Target)) T(*reinterpret_cast<const T*>(Source));
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyCopyConstructFunc = void(*)(void*, const void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void AnyMoveConstruct(void* Target, void* Source)
|
|
||||||
{
|
|
||||||
if constexpr (!TIsMoveConstructible<T>::Value) check_no_entry();
|
|
||||||
else new(reinterpret_cast<T*>(Target)) T(MoveTemp(*reinterpret_cast<T*>(Source)));
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyMoveConstructFunc = void(*)(void*, void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void AnyCopyAssign(void* Target, const void* Source)
|
|
||||||
{
|
|
||||||
if constexpr (TIsCopyAssignable<T>::Value) *reinterpret_cast<T*>(Target) = *reinterpret_cast<const T*>(Source);
|
|
||||||
else if constexpr (TIsCopyConstructible<T>::Value) { AnyDestroy<T>(Target); AnyCopyConstruct<T>(Target, Source); }
|
|
||||||
else check_no_entry();
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyCopyAssignFunc = void(*)(void*, const void*);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void AnyMoveAssign(void* Target, void* Source)
|
|
||||||
{
|
|
||||||
if constexpr (TIsMoveAssignable<T>::Value) *reinterpret_cast<T*>(Target) = MoveTemp(*reinterpret_cast<T*>(Source));
|
|
||||||
else if constexpr (TIsMoveConstructible<T>::Value) { AnyDestroy<T>(Target); AnyMoveConstruct<T>(Target, Source); }
|
|
||||||
else check_no_entry();
|
|
||||||
}
|
|
||||||
|
|
||||||
using FAnyMoveAssignFunc = void(*)(void*, void*);
|
|
||||||
|
|
||||||
//template <typename T>
|
|
||||||
//constexpr bool AnyEqualityOperator(const void* LHS, const void* RHS)
|
|
||||||
//{
|
|
||||||
// if constexpr (!CEqualityComparable<T>) check_no_entry();
|
|
||||||
// else return *reinterpret_cast<const T*>(LHS) == *reinterpret_cast<const T*>(RHS);
|
|
||||||
// return false;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//using FAnyEqualityOperatorFunc = bool(*)(const void*, const void*);
|
|
||||||
//
|
|
||||||
//template <typename T>
|
|
||||||
//void AnySwap(void* A, void* B)
|
|
||||||
//{
|
|
||||||
// if constexpr (TIsSwappable<T>::Value) Swap(*reinterpret_cast<T*>(A), *reinterpret_cast<T*>(B));
|
|
||||||
// else check_no_entry();
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//using FAnySwapFunc = void(*)(void*, void*);
|
|
||||||
//
|
|
||||||
//template <typename T>
|
|
||||||
//size_t AnyTypeHash(const void* InValue)
|
|
||||||
//{
|
|
||||||
// if constexpr (CHashable<T>) return GetTypeHash(*reinterpret_cast<const T*>(InValue));
|
|
||||||
// else return 3516520171;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//using FAnyTypeHashFunc = size_t(*)(const void*);
|
|
||||||
|
|
||||||
struct FAnyRTTI
|
|
||||||
{
|
|
||||||
bool bIsInline;
|
|
||||||
FAnyCopyNewFunc CopyNew;
|
|
||||||
FAnyMoveNewFunc MoveNew;
|
|
||||||
FAnyDeleteFunc Delete;
|
|
||||||
FAnyDestroyFunc Destroy;
|
|
||||||
FAnyCopyConstructFunc CopyConstruct;
|
|
||||||
FAnyMoveConstructFunc MoveConstruct;
|
|
||||||
FAnyCopyAssignFunc CopyAssign;
|
|
||||||
FAnyMoveAssignFunc MoveAssign;
|
|
||||||
// FAnyEqualityOperatorFunc EqualityOperator;
|
|
||||||
// FAnySwapFunc SwapObject;
|
|
||||||
// FAnyTypeHashFunc TypeHash;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, bool bInIsInline>
|
|
||||||
struct TAnyRTTIHelper
|
|
||||||
{
|
|
||||||
static constexpr FAnyRTTI RTTI =
|
|
||||||
{
|
|
||||||
bInIsInline,
|
|
||||||
AnyCopyNew<T>,
|
|
||||||
AnyMoveNew<T>,
|
|
||||||
AnyDelete<T>,
|
|
||||||
AnyDestroy<T>,
|
|
||||||
AnyCopyConstruct<T>,
|
|
||||||
AnyMoveConstruct<T>,
|
|
||||||
AnyCopyAssign<T>,
|
|
||||||
AnyMoveAssign<T>,
|
|
||||||
// AnyEqualityOperator<T>,
|
|
||||||
// AnySwap<T>,
|
|
||||||
// AnyTypeHash<T>,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NAMESPACE_PRIVATE_END
|
NAMESPACE_PRIVATE_END
|
||||||
|
|
||||||
inline constexpr size_t ANY_DEFAULT_INLINE_SIZE = 48;
|
inline constexpr size_t ANY_DEFAULT_INLINE_SIZE = 64 - sizeof(uintptr);
|
||||||
inline constexpr size_t ANY_DEFAULT_INLINE_ALIGNMENT = 16;
|
inline constexpr size_t ANY_DEFAULT_INLINE_ALIGNMENT = 16;
|
||||||
|
|
||||||
template <size_t InlineSize, size_t InlineAlignment = ANY_DEFAULT_INLINE_ALIGNMENT> requires (Memory::IsValidAlignment(InlineAlignment))
|
template <size_t InlineSize, size_t InlineAlignment = ANY_DEFAULT_INLINE_ALIGNMENT> requires (Memory::IsValidAlignment(InlineAlignment))
|
||||||
struct TAny
|
struct alignas(InlineAlignment) TAny
|
||||||
{
|
{
|
||||||
template <typename T>
|
constexpr TAny() : TypeInfo(0) { }
|
||||||
struct TIsInlineStorable : TBoolConstant<sizeof(T) <= InlineSize && alignof(T) <= InlineAlignment> { };
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct TIsTriviallyStorable : TBoolConstant<TIsInlineStorable<T>::Value && TIsTrivial<T>::Value && TIsTriviallyCopyable<T>::Value> { };
|
|
||||||
|
|
||||||
constexpr TAny() : TypeInfo(nullptr), RTTI(nullptr) { }
|
|
||||||
|
|
||||||
constexpr TAny(FInvalid) : TAny() { }
|
constexpr TAny(FInvalid) : TAny() { }
|
||||||
|
|
||||||
FORCEINLINE TAny(const TAny& InValue)
|
FORCEINLINE TAny(const TAny& InValue)
|
||||||
: TypeInfo(InValue.TypeInfo), RTTI(InValue.RTTI)
|
: TypeInfo(InValue.TypeInfo)
|
||||||
{
|
{
|
||||||
if (!IsValid()) return;
|
if (!IsValid()) return;
|
||||||
|
|
||||||
if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
|
switch (GetRepresentation())
|
||||||
else if (IsInline()) RTTI->CopyConstruct(GetStorage(), InValue.GetStorage());
|
{
|
||||||
else DynamicStorage = RTTI->CopyNew(InValue.GetStorage());
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
|
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
GetTypeInfo().CopyConstruct(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
HeapAllocation = Memory::Malloc(GetTypeInfo().GetTypeSize(), GetTypeInfo().GetTypeAlignment());
|
||||||
|
GetTypeInfo().CopyConstruct(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE TAny(TAny&& InValue)
|
FORCEINLINE TAny(TAny&& InValue)
|
||||||
: TypeInfo(InValue.TypeInfo), RTTI(InValue.RTTI)
|
: TypeInfo(InValue.TypeInfo)
|
||||||
{
|
{
|
||||||
if (!IsValid()) return;
|
if (!IsValid()) return;
|
||||||
|
|
||||||
if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
|
switch (GetRepresentation())
|
||||||
else if (IsInline()) RTTI->MoveConstruct(GetStorage(), InValue.GetStorage());
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
DynamicStorage = InValue.DynamicStorage;
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
InValue.TypeInfo = nullptr;
|
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
GetTypeInfo().MoveConstruct(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
HeapAllocation = InValue.HeapAllocation;
|
||||||
|
InValue.TypeInfo = 0;
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,19 +110,38 @@ struct TAny
|
|||||||
}
|
}
|
||||||
else if (GetTypeInfo() == InValue.GetTypeInfo())
|
else if (GetTypeInfo() == InValue.GetTypeInfo())
|
||||||
{
|
{
|
||||||
if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
|
switch (GetRepresentation())
|
||||||
else RTTI->CopyAssign(GetStorage(), InValue.GetStorage());
|
{
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
|
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
GetTypeInfo().CopyAssign(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ResetImpl();
|
ResetImpl();
|
||||||
|
|
||||||
TypeInfo = InValue.TypeInfo;
|
TypeInfo = InValue.TypeInfo;
|
||||||
RTTI = InValue.RTTI;
|
|
||||||
|
|
||||||
if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
|
switch (GetRepresentation())
|
||||||
else if (IsInline()) RTTI->CopyConstruct(GetStorage(), InValue.GetStorage());
|
{
|
||||||
else DynamicStorage = RTTI->CopyNew(InValue.GetStorage());
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
|
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
GetTypeInfo().CopyConstruct(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
HeapAllocation = Memory::Malloc(GetTypeInfo().GetTypeSize(), GetTypeInfo().GetTypeAlignment());
|
||||||
|
GetTypeInfo().CopyConstruct(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
@ -257,13 +157,20 @@ struct TAny
|
|||||||
}
|
}
|
||||||
else if (GetTypeInfo() == InValue.GetTypeInfo())
|
else if (GetTypeInfo() == InValue.GetTypeInfo())
|
||||||
{
|
{
|
||||||
if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
|
switch (GetRepresentation())
|
||||||
else if (IsInline()) RTTI->MoveAssign(GetStorage(), InValue.GetStorage());
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
RTTI->Delete(DynamicStorage);
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
DynamicStorage = InValue.DynamicStorage;
|
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||||
InValue.TypeInfo = nullptr;
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
GetTypeInfo().MoveAssign(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
ResetImpl();
|
||||||
|
HeapAllocation = InValue.HeapAllocation;
|
||||||
|
InValue.TypeInfo = 0;
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -271,11 +178,21 @@ struct TAny
|
|||||||
ResetImpl();
|
ResetImpl();
|
||||||
|
|
||||||
TypeInfo = InValue.TypeInfo;
|
TypeInfo = InValue.TypeInfo;
|
||||||
RTTI = InValue.RTTI;
|
|
||||||
|
|
||||||
if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
|
switch (GetRepresentation())
|
||||||
else if (IsInline()) RTTI->MoveConstruct(GetStorage(), InValue.GetStorage());
|
{
|
||||||
else DynamicStorage = RTTI->MoveNew(InValue.GetStorage());
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
|
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
GetTypeInfo().MoveConstruct(GetAllocation(), InValue.GetAllocation());
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
HeapAllocation = InValue.HeapAllocation;
|
||||||
|
InValue.TypeInfo = 0;
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
@ -290,13 +207,11 @@ struct TAny
|
|||||||
|
|
||||||
if (HoldsAlternative<SelectedType>())
|
if (HoldsAlternative<SelectedType>())
|
||||||
{
|
{
|
||||||
if constexpr (TIsTriviallyStorable<SelectedType>::Value)
|
GetValue<SelectedType>() = Forward<T>(InValue);
|
||||||
Memory::Memcpy(&InlineStorage, &InValue, sizeof(SelectedType));
|
|
||||||
else GetValue<SelectedType>() = Forward<T>(InValue);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Reset();
|
ResetImpl();
|
||||||
EmplaceImpl<SelectedType>(Forward<T>(InValue));
|
EmplaceImpl<SelectedType>(Forward<T>(InValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,26 +230,24 @@ struct TAny
|
|||||||
return GetValue<SelectedType>();
|
return GetValue<SelectedType>();
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const FTypeInfo& GetTypeInfo() const { return TypeInfo != nullptr ? *TypeInfo : Typeid(void); }
|
constexpr const FTypeInfo& GetTypeInfo() const { return IsValid() ? *reinterpret_cast<FTypeInfo*>(TypeInfo & ~RepresentationMask) : Typeid(void); }
|
||||||
|
|
||||||
constexpr bool IsValid() const { return TypeInfo != nullptr; }
|
constexpr bool IsValid() const { return TypeInfo != 0; }
|
||||||
constexpr explicit operator bool() const { return TypeInfo != nullptr; }
|
constexpr explicit operator bool() const { return TypeInfo != 0; }
|
||||||
constexpr bool IsInline() const { return RTTI != nullptr ? RTTI->bIsInline : true; }
|
|
||||||
constexpr bool IsTrivial() const { return RTTI == nullptr; }
|
|
||||||
|
|
||||||
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; }
|
||||||
|
|
||||||
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
||||||
constexpr T& GetValue() & { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast< T*>(GetStorage()); }
|
constexpr T& GetValue() & { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast< T*>(GetAllocation()); }
|
||||||
|
|
||||||
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
||||||
constexpr T&& GetValue() && { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast< T*>(GetStorage())); }
|
constexpr T&& GetValue() && { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast< T*>(GetAllocation())); }
|
||||||
|
|
||||||
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
||||||
constexpr const T& GetValue() const& { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast<const T*>(GetStorage()); }
|
constexpr const T& GetValue() const& { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast<const T*>(GetAllocation()); }
|
||||||
|
|
||||||
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value && TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
||||||
constexpr const T&& GetValue() const&& { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast<const T*>(GetStorage())); }
|
constexpr const T&& GetValue() const&& { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TAny. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast<const T*>(GetAllocation())); }
|
||||||
|
|
||||||
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value&& TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
template <typename T> requires TIsSame<T, typename TDecay<T>::Type>::Value&& TIsObject<typename TDecay<T>::Type>::Value && (!TIsArray<typename TDecay<T>::Type>::Value) && TIsDestructible<typename TDecay<T>::Type>::Value
|
||||||
constexpr T& Get( T& DefaultValue) & { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
constexpr T& Get( T& DefaultValue) & { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
||||||
@ -344,94 +257,119 @@ struct TAny
|
|||||||
|
|
||||||
FORCEINLINE void Reset()
|
FORCEINLINE void Reset()
|
||||||
{
|
{
|
||||||
TypeInfo = nullptr;
|
|
||||||
ResetImpl();
|
ResetImpl();
|
||||||
|
TypeInfo = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FORCEINLINE size_t GetTypeHash() const
|
FORCEINLINE size_t GetTypeHash() const
|
||||||
// {
|
{
|
||||||
// if (!IsValid()) return 20090007;
|
if (!IsValid()) return 20090007;
|
||||||
// return HashCombine(NAMESPACE_REDCRAFT::GetTypeHash(GetTypeInfo()), RTTI->TypeHash(GetStorage()));
|
return HashCombine(GetTypeInfo().GetTypeHash(), GetTypeInfo().HashItem(GetAllocation()));
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// FORCEINLINE void Swap(TAny& InValue)
|
FORCEINLINE void Swap(TAny& InValue)
|
||||||
// {
|
{
|
||||||
// if (!IsValid() && !InValue.IsValid()) return;
|
if (!IsValid() && !InValue.IsValid()) return;
|
||||||
//
|
|
||||||
// if (IsValid() && !InValue.IsValid())
|
if (IsValid() && !InValue.IsValid())
|
||||||
// {
|
{
|
||||||
// InValue = MoveTemp(*this);
|
InValue = MoveTemp(*this);
|
||||||
// Reset();
|
Reset();
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// if (InValue.IsValid() && !IsValid())
|
if (InValue.IsValid() && !IsValid())
|
||||||
// {
|
{
|
||||||
// *this = MoveTemp(InValue);
|
*this = MoveTemp(InValue);
|
||||||
// InValue.Reset();
|
InValue.Reset();
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// if (GetTypeInfo() == InValue.GetTypeInfo())
|
if (GetTypeInfo() == InValue.GetTypeInfo())
|
||||||
// {
|
{
|
||||||
// RTTI->SwapObject(GetStorage(), InValue.GetStorage());
|
GetTypeInfo().SwapItem(GetAllocation(), InValue.GetAllocation());
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// TAny Temp = MoveTemp(*this);
|
TAny Temp = MoveTemp(*this);
|
||||||
// *this = MoveTemp(InValue);
|
*this = MoveTemp(InValue);
|
||||||
// InValue = MoveTemp(Temp);
|
InValue = MoveTemp(Temp);
|
||||||
// }
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
static constexpr uintptr_t RepresentationMask = 3;
|
||||||
|
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
TAlignedStorage<InlineSize, InlineAlignment>::Type InlineStorage;
|
TAlignedStorage<InlineSize, 1>::Type InlineAllocation;
|
||||||
void* DynamicStorage;
|
void* HeapAllocation;
|
||||||
};
|
};
|
||||||
|
|
||||||
const FTypeInfo* TypeInfo;
|
uintptr TypeInfo;
|
||||||
const NAMESPACE_PRIVATE::FAnyRTTI* RTTI;
|
|
||||||
|
|
||||||
constexpr void* GetStorage() { return IsInline() ? &InlineStorage : DynamicStorage; }
|
constexpr NAMESPACE_PRIVATE::EAnyRepresentation GetRepresentation() const { return static_cast<NAMESPACE_PRIVATE::EAnyRepresentation>(TypeInfo & RepresentationMask); }
|
||||||
constexpr const void* GetStorage() const { return IsInline() ? &InlineStorage : DynamicStorage; }
|
|
||||||
|
constexpr void* GetAllocation() { return GetRepresentation() == NAMESPACE_PRIVATE::EAnyRepresentation::Trivial || GetRepresentation() == NAMESPACE_PRIVATE::EAnyRepresentation::Small ? &InlineAllocation : HeapAllocation; }
|
||||||
|
constexpr const void* GetAllocation() const { return GetRepresentation() == NAMESPACE_PRIVATE::EAnyRepresentation::Trivial || GetRepresentation() == NAMESPACE_PRIVATE::EAnyRepresentation::Small ? &InlineAllocation : HeapAllocation; }
|
||||||
|
|
||||||
template <typename SelectedType, typename... Types>
|
template <typename SelectedType, typename... Types>
|
||||||
FORCEINLINE void EmplaceImpl(Types&&... Args)
|
FORCEINLINE void EmplaceImpl(Types&&... Args)
|
||||||
{
|
{
|
||||||
TypeInfo = &Typeid(SelectedType);
|
TypeInfo = reinterpret_cast<uintptr>(&Typeid(SelectedType));
|
||||||
|
|
||||||
if constexpr (TIsTriviallyStorable<SelectedType>::Value)
|
constexpr bool bIsInlineStorable = sizeof(SelectedType) <= InlineSize && alignof(SelectedType) <= InlineAlignment;
|
||||||
|
constexpr bool bIsTriviallyStorable = bIsInlineStorable && TIsTrivial<SelectedType>::Value && TIsTriviallyCopyable<SelectedType>::Value;
|
||||||
|
|
||||||
|
if constexpr (bIsTriviallyStorable)
|
||||||
{
|
{
|
||||||
new(&InlineStorage) SelectedType(Forward<Types>(Args)...);
|
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...);
|
||||||
RTTI = nullptr;
|
TypeInfo |= static_cast<uintptr>(NAMESPACE_PRIVATE::EAnyRepresentation::Trivial);
|
||||||
}
|
}
|
||||||
else if constexpr (TIsInlineStorable<SelectedType>::Value)
|
else if constexpr (bIsInlineStorable)
|
||||||
{
|
{
|
||||||
new(&InlineStorage) SelectedType(Forward<Types>(Args)...);
|
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...);
|
||||||
RTTI = &NAMESPACE_PRIVATE::TAnyRTTIHelper<SelectedType, true>::RTTI;
|
TypeInfo |= static_cast<uintptr>(NAMESPACE_PRIVATE::EAnyRepresentation::Small);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DynamicStorage = new SelectedType(Forward<Types>(Args)...);
|
HeapAllocation = new SelectedType(Forward<Types>(Args)...);
|
||||||
RTTI = &NAMESPACE_PRIVATE::TAnyRTTIHelper<SelectedType, false>::RTTI;
|
TypeInfo |= static_cast<uintptr>(NAMESPACE_PRIVATE::EAnyRepresentation::Big);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE void ResetImpl()
|
FORCEINLINE void ResetImpl()
|
||||||
{
|
{
|
||||||
if (!IsValid() || IsTrivial()) return;
|
if (!IsValid()) return;
|
||||||
else if (IsInline()) RTTI->Destroy(&InlineStorage);
|
|
||||||
else RTTI->Delete(DynamicStorage);
|
switch (GetRepresentation())
|
||||||
|
{
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Trivial:
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Small:
|
||||||
|
GetTypeInfo().Destroy(GetAllocation());
|
||||||
|
break;
|
||||||
|
case NAMESPACE_PRIVATE::EAnyRepresentation::Big:
|
||||||
|
GetTypeInfo().Destroy(GetAllocation());
|
||||||
|
Memory::Free(HeapAllocation);
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// friend FORCEINLINE bool operator==(const TAny& LHS, const TAny& RHS)
|
friend FORCEINLINE bool operator==(const TAny& LHS, const TAny& RHS)
|
||||||
// {
|
{
|
||||||
// if (LHS.GetTypeInfo() != RHS.GetTypeInfo()) return false;
|
if (LHS.GetTypeInfo() != RHS.GetTypeInfo()) return false;
|
||||||
// if (LHS.IsValid() == false) return true;
|
if (LHS.IsValid() == false) return true;
|
||||||
// return LHS.RTTI->EqualityOperator(LHS.GetStorage(), RHS.GetStorage());
|
return LHS.GetTypeInfo().EqualityCompare(LHS.GetAllocation(), RHS.GetAllocation());
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
friend FORCEINLINE partial_ordering operator<=>(const TAny& LHS, const TAny& RHS)
|
||||||
|
{
|
||||||
|
if (LHS.GetTypeInfo() != RHS.GetTypeInfo()) return partial_ordering::unordered;
|
||||||
|
if (LHS.IsValid() == false) return partial_ordering::equivalent;
|
||||||
|
return LHS.GetTypeInfo().SynthThreeWayCompare(LHS.GetAllocation(), RHS.GetAllocation());;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,22 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CoreTypes.h"
|
#include "CoreTypes.h"
|
||||||
#include "Memory/Memory.h"
|
|
||||||
#include "Concepts/Same.h"
|
|
||||||
#include "Templates/Any.h"
|
#include "Templates/Any.h"
|
||||||
#include "Templates/Tuple.h"
|
#include "Templates/Tuple.h"
|
||||||
#include "Templates/Invoke.h"
|
#include "Templates/Invoke.h"
|
||||||
#include "Memory/Alignment.h"
|
#include "Memory/Alignment.h"
|
||||||
#include "Templates/Utility.h"
|
#include "Templates/Utility.h"
|
||||||
#include "Templates/TypeHash.h"
|
#include "Templates/TypeHash.h"
|
||||||
#include "Templates/Container.h"
|
|
||||||
#include "Concepts/Comparable.h"
|
|
||||||
#include "Concepts/Convertible.h"
|
|
||||||
#include "TypeTraits/TypeTraits.h"
|
#include "TypeTraits/TypeTraits.h"
|
||||||
#include "Miscellaneous/TypeInfo.h"
|
#include "Miscellaneous/TypeInfo.h"
|
||||||
#include "Concepts/BooleanTestable.h"
|
#include "Concepts/BooleanTestable.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)
|
||||||
@ -105,7 +103,7 @@ template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::Con
|
|||||||
template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::ConstRValue> { using Type = const T&&; };
|
template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::ConstRValue> { using Type = const T&&; };
|
||||||
|
|
||||||
template <typename R, typename... Types, size_t InlineSize, size_t InlineAlignment, EFunctionSpecifiers Specifiers, EFunctionType FunctionType>
|
template <typename R, typename... Types, size_t InlineSize, size_t InlineAlignment, EFunctionSpecifiers Specifiers, EFunctionType FunctionType>
|
||||||
struct TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, FunctionType>
|
struct alignas(InlineAlignment) TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, FunctionType>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -266,7 +264,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
using StorageType = typename TConditional<FunctionType == EFunctionType::Reference, void*, TAny<InlineSize, InlineAlignment>>::Type;
|
using StorageType = typename TConditional<FunctionType == EFunctionType::Reference, void*, TAny<InlineSize, 1>>::Type;
|
||||||
using StorageRef = typename TConditional<FunctionType == EFunctionType::Reference, void*, typename TFunctionCallSpecifiers<StorageType, Specifiers>::Type&>::Type;
|
using StorageRef = typename TConditional<FunctionType == EFunctionType::Reference, void*, typename TFunctionCallSpecifiers<StorageType, Specifiers>::Type&>::Type;
|
||||||
using CallFunc = ResultType(*)(StorageRef, Types&&...);
|
using CallFunc = ResultType(*)(StorageRef, Types&&...);
|
||||||
|
|
||||||
@ -367,8 +365,8 @@ struct TFunctionSelect<R(Types...) const&&, InlineSize, InlineAlignment, Functio
|
|||||||
|
|
||||||
NAMESPACE_PRIVATE_END
|
NAMESPACE_PRIVATE_END
|
||||||
|
|
||||||
inline constexpr size_t FUNCTION_DEFAULT_INLINE_SIZE = 32;
|
inline constexpr size_t FUNCTION_DEFAULT_INLINE_SIZE = ANY_DEFAULT_INLINE_SIZE - sizeof(uintptr);
|
||||||
inline constexpr size_t FUNCTION_DEFAULT_INLINE_ALIGNMENT = 16;
|
inline constexpr size_t FUNCTION_DEFAULT_INLINE_ALIGNMENT = ANY_DEFAULT_INLINE_ALIGNMENT;
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
using TFunctionRef = typename NAMESPACE_PRIVATE::TFunctionSelect<F, 0, 0, NAMESPACE_PRIVATE::EFunctionType::Reference>::Type;
|
using TFunctionRef = typename NAMESPACE_PRIVATE::TFunctionSelect<F, 0, 0, NAMESPACE_PRIVATE::EFunctionType::Reference>::Type;
|
||||||
|
Loading…
Reference in New Issue
Block a user