fix(templates): fix TAny's constraints and simplify naming

This commit is contained in:
_Redstone_c_ 2022-06-21 23:31:46 +08:00
parent f1d4d99ecf
commit 096150a305

View File

@ -9,9 +9,6 @@
#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)
@ -33,13 +30,13 @@ struct alignas(InlineAlignment) TAny
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case EAnyRepresentation::Big: case ERepresentation::Big:
HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment); HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
@ -54,13 +51,13 @@ struct alignas(InlineAlignment) TAny
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case EAnyRepresentation::Big: case ERepresentation::Big:
HeapAllocation = InValue.HeapAllocation; HeapAllocation = InValue.HeapAllocation;
InValue.TypeInfo = 0; InValue.TypeInfo = 0;
break; break;
@ -69,7 +66,7 @@ struct alignas(InlineAlignment) TAny
} }
template <typename T, typename... Types> requires CDestructible<TDecay<T>> template <typename T, typename... Types> requires CDestructible<TDecay<T>>
&& CConstructibleFrom<TDecay<T>, Types...> && CConstructibleFrom<TDecay<T>, Types&&...>
FORCEINLINE explicit TAny(TInPlaceType<T>, Types&&... Args) FORCEINLINE explicit TAny(TInPlaceType<T>, Types&&... Args)
{ {
using SelectedType = TDecay<T>; using SelectedType = TDecay<T>;
@ -98,11 +95,11 @@ struct alignas(InlineAlignment) TAny
{ {
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
case EAnyRepresentation::Big: case ERepresentation::Big:
GetTypeInfoImpl().CopyAssignImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyAssignImpl(GetAllocation(), InValue.GetAllocation());
break; break;
default: check_no_entry(); default: check_no_entry();
@ -116,13 +113,13 @@ struct alignas(InlineAlignment) TAny
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case EAnyRepresentation::Big: case ERepresentation::Big:
HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment); HeapAllocation = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
@ -145,13 +142,13 @@ struct alignas(InlineAlignment) TAny
{ {
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().MoveAssignImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().MoveAssignImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case EAnyRepresentation::Big: case ERepresentation::Big:
ResetImpl(); ResetImpl();
HeapAllocation = InValue.HeapAllocation; HeapAllocation = InValue.HeapAllocation;
InValue.TypeInfo = 0; InValue.TypeInfo = 0;
@ -167,13 +164,13 @@ struct alignas(InlineAlignment) TAny
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
Memory::Memcpy(InlineAllocation, InValue.InlineAllocation); Memory::Memcpy(InlineAllocation, InValue.InlineAllocation);
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation()); GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
break; break;
case EAnyRepresentation::Big: case ERepresentation::Big:
HeapAllocation = InValue.HeapAllocation; HeapAllocation = InValue.HeapAllocation;
InValue.TypeInfo = 0; InValue.TypeInfo = 0;
break; break;
@ -204,7 +201,7 @@ struct alignas(InlineAlignment) TAny
} }
template <typename T, typename... Types> requires CDestructible<TDecay<T>> template <typename T, typename... Types> requires CDestructible<TDecay<T>>
&& CConstructibleFrom<TDecay<T>, T&&> && CConstructibleFrom<TDecay<T>, Types&&...>
FORCEINLINE TDecay<T>& Emplace(Types&&... Args) FORCEINLINE TDecay<T>& Emplace(Types&&... Args)
{ {
ResetImpl(); ResetImpl();
@ -285,11 +282,10 @@ private:
static constexpr uintptr_t RepresentationMask = 3; static constexpr uintptr_t RepresentationMask = 3;
enum class EAnyRepresentation : uint8 enum class ERepresentation : uint8
{ {
Trivial, // Trivial Trivial, // Trivial & Inline
// Inline, // InlineAllocation Small, // InlineAllocation
Small, // Trivial & Inline
Big, // HeapAllocation Big, // HeapAllocation
}; };
@ -351,11 +347,11 @@ private:
uintptr TypeInfo; uintptr TypeInfo;
constexpr EAnyRepresentation GetRepresentation() const { return static_cast<EAnyRepresentation>(TypeInfo & RepresentationMask); } constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(TypeInfo & RepresentationMask); }
constexpr const FTypeInfoImpl& GetTypeInfoImpl() const { return *reinterpret_cast<const FTypeInfoImpl*>(TypeInfo & ~RepresentationMask); } constexpr const FTypeInfoImpl& GetTypeInfoImpl() const { return *reinterpret_cast<const FTypeInfoImpl*>(TypeInfo & ~RepresentationMask); }
constexpr void* GetAllocation() { return GetRepresentation() == EAnyRepresentation::Trivial || GetRepresentation() == EAnyRepresentation::Small ? &InlineAllocation : HeapAllocation; } constexpr void* GetAllocation() { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? &InlineAllocation : HeapAllocation; }
constexpr const void* GetAllocation() const { return GetRepresentation() == EAnyRepresentation::Trivial || GetRepresentation() == EAnyRepresentation::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)
@ -369,17 +365,17 @@ private:
if constexpr (bIsTriviallyStorable) if constexpr (bIsTriviallyStorable)
{ {
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...); new(&InlineAllocation) SelectedType(Forward<Types>(Args)...);
TypeInfo |= static_cast<uintptr>(EAnyRepresentation::Trivial); TypeInfo |= static_cast<uintptr>(ERepresentation::Trivial);
} }
else if constexpr (bIsInlineStorable) else if constexpr (bIsInlineStorable)
{ {
new(&InlineAllocation) SelectedType(Forward<Types>(Args)...); new(&InlineAllocation) SelectedType(Forward<Types>(Args)...);
TypeInfo |= static_cast<uintptr>(EAnyRepresentation::Small); TypeInfo |= static_cast<uintptr>(ERepresentation::Small);
} }
else else
{ {
HeapAllocation = new SelectedType(Forward<Types>(Args)...); HeapAllocation = new SelectedType(Forward<Types>(Args)...);
TypeInfo |= static_cast<uintptr>(EAnyRepresentation::Big); TypeInfo |= static_cast<uintptr>(ERepresentation::Big);
} }
} }
@ -389,12 +385,12 @@ private:
switch (GetRepresentation()) switch (GetRepresentation())
{ {
case EAnyRepresentation::Trivial: case ERepresentation::Trivial:
break; break;
case EAnyRepresentation::Small: case ERepresentation::Small:
GetTypeInfoImpl().DestroyImpl(GetAllocation()); GetTypeInfoImpl().DestroyImpl(GetAllocation());
break; break;
case EAnyRepresentation::Big: case ERepresentation::Big:
GetTypeInfoImpl().DestroyImpl(GetAllocation()); GetTypeInfoImpl().DestroyImpl(GetAllocation());
Memory::Free(HeapAllocation); Memory::Free(HeapAllocation);
break; break;