refactor(templates): refactor FAny to a non-template class
This commit is contained in:
parent
6b42dbdc05
commit
a8e1852b34
@ -448,7 +448,7 @@ void TestAny()
|
|||||||
FTracker& operator=(const FTracker& InValue) { always_check_no_entry(); return *this; }
|
FTracker& operator=(const FTracker& InValue) { always_check_no_entry(); return *this; }
|
||||||
FTracker& operator=(FTracker&& InValue) { return *this; }
|
FTracker& operator=(FTracker&& InValue) { return *this; }
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
{
|
||||||
FAny TempA;
|
FAny TempA;
|
||||||
FAny TempB(Invalid);
|
FAny TempB(Invalid);
|
||||||
@ -492,7 +492,7 @@ void TestAny()
|
|||||||
always_check(!TempD.IsValid());
|
always_check(!TempD.IsValid());
|
||||||
always_check(0 == TempA);
|
always_check(0 == TempA);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
FAny TempA;
|
FAny TempA;
|
||||||
FAny TempB(Invalid);
|
FAny TempB(Invalid);
|
||||||
@ -536,7 +536,7 @@ void TestAny()
|
|||||||
always_check(!TempD.IsValid());
|
always_check(!TempD.IsValid());
|
||||||
always_check(FIntegral(0) == TempA);
|
always_check(FIntegral(0) == TempA);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
FAny TempA;
|
FAny TempA;
|
||||||
FAny TempB(Invalid);
|
FAny TempB(Invalid);
|
||||||
@ -580,7 +580,7 @@ void TestAny()
|
|||||||
always_check(!TempD.IsValid());
|
always_check(!TempD.IsValid());
|
||||||
always_check(FFloating(0.0) == TempA);
|
always_check(FFloating(0.0) == TempA);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
FAny TempA;
|
FAny TempA;
|
||||||
FAny TempB(InPlaceType<int32>, 0);
|
FAny TempB(InPlaceType<int32>, 0);
|
||||||
@ -624,24 +624,6 @@ void TestAny()
|
|||||||
TempZ = FTracker();
|
TempZ = FTracker();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
always_check(GetTypeHash(FAny(114)) == GetTypeHash(FAny(114)));
|
|
||||||
always_check(GetTypeHash(FAny(114)) != GetTypeHash(FAny(514)));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
FAny TempA = Invalid;
|
|
||||||
FAny TempB = static_cast<int16>(16);
|
|
||||||
FAny TempC = static_cast<int32>(16);
|
|
||||||
FAny TempD = static_cast<int32>(32);
|
|
||||||
|
|
||||||
always_check(TempA == TempA);
|
|
||||||
always_check(TempA != TempB);
|
|
||||||
always_check(TempB != TempC);
|
|
||||||
always_check(TempB != TempC);
|
|
||||||
always_check(TempD >= TempC);
|
|
||||||
always_check(TempA <=> TempB == partial_ordering::unordered);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestTuple()
|
void TestTuple()
|
||||||
|
@ -13,122 +13,67 @@ NAMESPACE_REDCRAFT_BEGIN
|
|||||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||||
NAMESPACE_MODULE_BEGIN(Utility)
|
NAMESPACE_MODULE_BEGIN(Utility)
|
||||||
|
|
||||||
// TAny's CustomStorage concept, see FAnyDefaultStorage
|
// NOTE: In the STL, the assignment operation of the std::any type uses the copy-and-swap idiom
|
||||||
template <typename T>
|
// instead of directly calling the assignment operation of the contained value.
|
||||||
concept CAnyCustomStorage = CDefaultConstructible<T>
|
// The purpose of this is as follows:
|
||||||
&& !CCopyConstructible<T> && !CMoveConstructible<T>
|
// 1) the copy assignment might not exist.
|
||||||
&& !CCopyAssignable<T> && !CMoveAssignable<T>
|
// 2) the typical case is that the objects are different.
|
||||||
&& CDestructible<T>
|
// 3) it is less exception-safe
|
||||||
&& CSameAs<decltype(T::InlineSize), const size_t>
|
// But we don't follow the the copy-and-swap idiom, because we assume that no function throws an exception.
|
||||||
&& CSameAs<decltype(T::InlineAlignment), const size_t>
|
|
||||||
&& requires(const T& A)
|
|
||||||
{
|
|
||||||
{ A.InlineAllocation() } -> CSameAs<const void*>;
|
|
||||||
{ A.HeapAllocation() } -> CSameAs<void*>;
|
|
||||||
{ A.TypeInfo() } -> CSameAs<uintptr>;
|
|
||||||
}
|
|
||||||
&& requires(T& A)
|
|
||||||
{
|
|
||||||
{ A.InlineAllocation() } -> CSameAs<void*>;
|
|
||||||
{ A.HeapAllocation() } -> CSameAs<void*&>;
|
|
||||||
{ A.TypeInfo() } -> CSameAs<uintptr&>;
|
|
||||||
}
|
|
||||||
&& requires(T& A, const T& B, T&& C)
|
|
||||||
{
|
|
||||||
A.CopyCustom(B);
|
|
||||||
A.MoveCustom(MoveTemp(C));
|
|
||||||
};
|
|
||||||
|
|
||||||
// TAny's default storage structure
|
class alignas(16) FAny
|
||||||
struct alignas(16) FAnyDefaultStorage : FSingleton
|
|
||||||
{
|
|
||||||
// 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 CAnyCustomStorage 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 CAnyCustomStorage Interface
|
|
||||||
|
|
||||||
union
|
|
||||||
{
|
|
||||||
uint8 InlineAllocationImpl[InlineSize];
|
|
||||||
void* HeapAllocationImpl;
|
|
||||||
};
|
|
||||||
|
|
||||||
uintptr TypeInfoImpl;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
static_assert(CAnyCustomStorage<FAnyDefaultStorage>);
|
|
||||||
|
|
||||||
// You can add custom storage area through CustomStorage, such as TFunction
|
|
||||||
// It is not recommended to use this, FAny is recommended
|
|
||||||
template <CAnyCustomStorage CustomStorage>
|
|
||||||
class TAny
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline static constexpr size_t InlineSize = CustomStorage::InlineSize;
|
FORCEINLINE constexpr FAny() { Invalidate(); }
|
||||||
inline static constexpr size_t InlineAlignment = CustomStorage::InlineAlignment;
|
|
||||||
|
|
||||||
constexpr TAny() { Storage.TypeInfo() = 0; }
|
FORCEINLINE constexpr FAny(FInvalid) : FAny() { }
|
||||||
|
|
||||||
constexpr TAny(FInvalid) : TAny() { }
|
FORCEINLINE FAny(const FAny& InValue)
|
||||||
|
: TypeInfo(InValue.TypeInfo)
|
||||||
FORCEINLINE TAny(const TAny& InValue)
|
|
||||||
{
|
{
|
||||||
Storage.CopyCustom(InValue.Storage);
|
|
||||||
|
|
||||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
|
||||||
|
|
||||||
if (!IsValid()) return;
|
if (!IsValid()) return;
|
||||||
|
|
||||||
switch (GetRepresentation())
|
switch (GetRepresentation())
|
||||||
{
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
|
break;
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
Memory::Memcpy(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
SmallStorage.RTTI = InValue.SmallStorage.RTTI;
|
||||||
|
SmallStorage.RTTI->CopyConstruct(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
Storage.HeapAllocation() = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
|
BigStorage.RTTI = InValue.BigStorage.RTTI;
|
||||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
BigStorage.External = Memory::Malloc(BigStorage.RTTI->TypeSize, BigStorage.RTTI->TypeAlignment);
|
||||||
|
BigStorage.RTTI->CopyConstruct(BigStorage.External, InValue.BigStorage.External);
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE TAny(TAny&& InValue)
|
FORCEINLINE FAny(FAny&& 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::Empty:
|
||||||
|
break;
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
Memory::Memmove(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
|
SmallStorage.RTTI = InValue.SmallStorage.RTTI;
|
||||||
|
SmallStorage.RTTI->MoveConstruct(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
|
BigStorage.RTTI = InValue.BigStorage.RTTI;
|
||||||
InValue.Storage.TypeInfo() = 0;
|
BigStorage.External = InValue.BigStorage.External;
|
||||||
|
InValue.Invalidate();
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
@ -136,28 +81,25 @@ public:
|
|||||||
|
|
||||||
template <typename T, typename... Ts> requires (CDestructible<TDecay<T>>
|
template <typename T, typename... Ts> requires (CDestructible<TDecay<T>>
|
||||||
&& CConstructibleFrom<TDecay<T>, Ts&&...>)
|
&& CConstructibleFrom<TDecay<T>, Ts&&...>)
|
||||||
FORCEINLINE explicit TAny(TInPlaceType<T>, Ts&&... Args)
|
FORCEINLINE explicit FAny(TInPlaceType<T>, Ts&&... Args)
|
||||||
{
|
{
|
||||||
using SelectedType = TDecay<T>;
|
EmplaceImpl<T>(Forward<Ts>(Args)...);
|
||||||
EmplaceImpl<SelectedType>(Forward<Ts>(Args)...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> requires (!CBaseOf<TAny, TDecay<T>> && !CTInPlaceType<TDecay<T>>
|
template <typename T> requires (!CBaseOf<FAny, TDecay<T>> && !CTInPlaceType<TDecay<T>>
|
||||||
&& CDestructible<TDecay<T>> && CConstructibleFrom<TDecay<T>, T&&>)
|
&& CDestructible<TDecay<T>> && CConstructibleFrom<TDecay<T>, T&&>)
|
||||||
FORCEINLINE TAny(T&& InValue) : TAny(InPlaceType<TDecay<T>>, Forward<T>(InValue))
|
FORCEINLINE FAny(T&& InValue) : FAny(InPlaceType<TDecay<T>>, Forward<T>(InValue))
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
FORCEINLINE ~TAny()
|
FORCEINLINE ~FAny()
|
||||||
{
|
{
|
||||||
ResetImpl();
|
Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE TAny& operator=(const TAny& InValue)
|
FORCEINLINE FAny& operator=(const FAny& InValue)
|
||||||
{
|
{
|
||||||
if (&InValue == this) return *this;
|
if (&InValue == this) return *this;
|
||||||
|
|
||||||
Storage.CopyCustom(InValue.Storage);
|
|
||||||
|
|
||||||
if (!InValue.IsValid())
|
if (!InValue.IsValid())
|
||||||
{
|
{
|
||||||
Reset();
|
Reset();
|
||||||
@ -166,33 +108,43 @@ public:
|
|||||||
{
|
{
|
||||||
switch (GetRepresentation())
|
switch (GetRepresentation())
|
||||||
{
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
|
break;
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
Memory::Memcpy(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
|
SmallStorage.RTTI = InValue.SmallStorage.RTTI;
|
||||||
|
SmallStorage.RTTI->CopyAssign(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
GetTypeInfoImpl().CopyAssignImpl(GetAllocation(), InValue.GetAllocation());
|
BigStorage.RTTI = InValue.BigStorage.RTTI;
|
||||||
|
BigStorage.RTTI->CopyAssign(BigStorage.External, InValue.BigStorage.External);
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ResetImpl();
|
Destroy();
|
||||||
|
|
||||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
TypeInfo = InValue.TypeInfo;
|
||||||
|
|
||||||
switch (GetRepresentation())
|
switch (GetRepresentation())
|
||||||
{
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
|
break;
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
Memory::Memcpy(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
SmallStorage.RTTI = InValue.SmallStorage.RTTI;
|
||||||
|
SmallStorage.RTTI->CopyConstruct(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
Storage.HeapAllocation() = Memory::Malloc(GetTypeInfoImpl().TypeSize, GetTypeInfoImpl().TypeAlignment);
|
BigStorage.RTTI = InValue.BigStorage.RTTI;
|
||||||
GetTypeInfoImpl().CopyConstructImpl(GetAllocation(), InValue.GetAllocation());
|
BigStorage.External = Memory::Malloc(BigStorage.RTTI->TypeSize, BigStorage.RTTI->TypeAlignment);
|
||||||
|
BigStorage.RTTI->CopyConstruct(BigStorage.External, InValue.BigStorage.External);
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
@ -201,12 +153,10 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE TAny& operator=(TAny&& InValue)
|
FORCEINLINE FAny& operator=(FAny&& InValue)
|
||||||
{
|
{
|
||||||
if (&InValue == this) return *this;
|
if (&InValue == this) return *this;
|
||||||
|
|
||||||
Storage.MoveCustom(MoveTemp(InValue.Storage));
|
|
||||||
|
|
||||||
if (!InValue.IsValid())
|
if (!InValue.IsValid())
|
||||||
{
|
{
|
||||||
Reset();
|
Reset();
|
||||||
@ -215,37 +165,45 @@ public:
|
|||||||
{
|
{
|
||||||
switch (GetRepresentation())
|
switch (GetRepresentation())
|
||||||
{
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
|
break;
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
Memory::Memmove(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
GetTypeInfoImpl().MoveAssignImpl(GetAllocation(), InValue.GetAllocation());
|
SmallStorage.RTTI = InValue.SmallStorage.RTTI;
|
||||||
|
SmallStorage.RTTI->MoveAssign(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
ResetImpl();
|
Destroy();
|
||||||
Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
|
BigStorage.RTTI = InValue.BigStorage.RTTI;
|
||||||
InValue.Storage.TypeInfo() = 0;
|
BigStorage.External = InValue.BigStorage.External;
|
||||||
|
InValue.Invalidate();
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ResetImpl();
|
Destroy();
|
||||||
|
|
||||||
Storage.TypeInfo() = InValue.Storage.TypeInfo();
|
TypeInfo = InValue.TypeInfo;
|
||||||
|
|
||||||
switch (GetRepresentation())
|
switch (GetRepresentation())
|
||||||
{
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
|
break;
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
Memory::Memcpy(Storage.InlineAllocation(), InValue.Storage.InlineAllocation(), Storage.InlineSize);
|
Memory::Memmove(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
GetTypeInfoImpl().MoveConstructImpl(GetAllocation(), InValue.GetAllocation());
|
SmallStorage.RTTI = InValue.SmallStorage.RTTI;
|
||||||
|
SmallStorage.RTTI->MoveConstruct(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
Storage.HeapAllocation() = InValue.Storage.HeapAllocation();
|
BigStorage.RTTI = InValue.BigStorage.RTTI;
|
||||||
InValue.Storage.TypeInfo() = 0;
|
BigStorage.External = InValue.BigStorage.External;
|
||||||
|
InValue.Invalidate();
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
@ -254,20 +212,20 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> requires (!CBaseOf<TAny, TDecay<T>> && !CTInPlaceType<TDecay<T>>
|
template <typename T> requires (!CBaseOf<FAny, TDecay<T>> && !CTInPlaceType<TDecay<T>>
|
||||||
&& CDestructible<TDecay<T>> && CConstructibleFrom<TDecay<T>, T&&>)
|
&& CDestructible<TDecay<T>>&& CConstructibleFrom<TDecay<T>, T&&>)
|
||||||
FORCEINLINE TAny& operator=(T&& InValue)
|
FORCEINLINE FAny& operator=(T&& InValue)
|
||||||
{
|
{
|
||||||
using SelectedType = TDecay<T>;
|
using DecayedType = TDecay<T>;
|
||||||
|
|
||||||
if (HoldsAlternative<SelectedType>())
|
if (HoldsAlternative<DecayedType>())
|
||||||
{
|
{
|
||||||
GetValue<SelectedType>() = Forward<T>(InValue);
|
GetValue<DecayedType>() = Forward<T>(InValue);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ResetImpl();
|
Destroy();
|
||||||
EmplaceImpl<SelectedType>(Forward<T>(InValue));
|
EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
@ -277,58 +235,46 @@ public:
|
|||||||
&& CConstructibleFrom<TDecay<T>, Ts&&...>)
|
&& CConstructibleFrom<TDecay<T>, Ts&&...>)
|
||||||
FORCEINLINE TDecay<T>& Emplace(Ts&&... Args)
|
FORCEINLINE TDecay<T>& Emplace(Ts&&... Args)
|
||||||
{
|
{
|
||||||
ResetImpl();
|
Destroy();
|
||||||
|
EmplaceImpl<T>(Forward<Ts>(Args)...);
|
||||||
using SelectedType = TDecay<T>;
|
return GetValue<TDecay<T>>();
|
||||||
EmplaceImpl<SelectedType>(Forward<Ts>(Args)...);
|
|
||||||
return GetValue<SelectedType>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const type_info& GetTypeInfo() const { return IsValid() ? *GetTypeInfoImpl().NativeTypeInfo : typeid(void); }
|
FORCEINLINE constexpr const type_info& GetTypeInfo() const { return IsValid() ? GetTypeInfoImpl() : typeid(void); }
|
||||||
|
|
||||||
constexpr bool IsValid() const { return Storage.TypeInfo() != 0; }
|
FORCEINLINE constexpr bool IsValid() const { return TypeInfo != 0; }
|
||||||
constexpr explicit operator bool() const { return Storage.TypeInfo() != 0; }
|
FORCEINLINE constexpr explicit operator bool() const { return TypeInfo != 0; }
|
||||||
|
|
||||||
template <typename T> constexpr bool HoldsAlternative() const { return IsValid() ? GetTypeInfo() == typeid(T) : false; }
|
template <typename T> FORCEINLINE constexpr bool HoldsAlternative() const { return IsValid() ? GetTypeInfo() == typeid(T) : false; }
|
||||||
|
|
||||||
template <typename T> requires (CDestructible<TDecay<T>>)
|
template <typename T> requires (CSameAs<T, TDecay<T>>&& CDestructible<TDecay<T>>)
|
||||||
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()); }
|
FORCEINLINE 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()); }
|
||||||
|
|
||||||
template <typename T> requires (CDestructible<TDecay<T>>)
|
template <typename T> requires (CSameAs<T, TDecay<T>>&& CDestructible<TDecay<T>>)
|
||||||
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())); }
|
FORCEINLINE 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())); }
|
||||||
|
|
||||||
template <typename T> requires (CDestructible<TDecay<T>>)
|
template <typename T> requires (CSameAs<T, TDecay<T>>&& CDestructible<TDecay<T>>)
|
||||||
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()); }
|
FORCEINLINE 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()); }
|
||||||
|
|
||||||
template <typename T> requires (CDestructible<TDecay<T>>)
|
template <typename T> requires (CSameAs<T, TDecay<T>>&& CDestructible<TDecay<T>>)
|
||||||
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())); }
|
FORCEINLINE 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())); }
|
||||||
|
|
||||||
|
template <typename T> requires (CSameAs<T, TDecay<T>> && CDestructible<TDecay<T>>)
|
||||||
|
FORCEINLINE constexpr T& Get( T& DefaultValue) & { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
||||||
|
|
||||||
template <typename T> requires (CSameAs<T, TDecay<T>> && CDestructible<TDecay<T>>)
|
template <typename T> requires (CSameAs<T, TDecay<T>> && CDestructible<TDecay<T>>)
|
||||||
constexpr T& Get( T& DefaultValue) & { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
FORCEINLINE constexpr const T& Get(const T& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
||||||
|
|
||||||
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()
|
FORCEINLINE void Reset()
|
||||||
{
|
{
|
||||||
ResetImpl();
|
Destroy();
|
||||||
Storage.TypeInfo() = 0;
|
Invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE size_t GetTypeHash() const
|
FORCEINLINE void Swap(FAny& InValue)
|
||||||
{
|
|
||||||
using NAMESPACE_REDCRAFT::GetTypeHash;
|
|
||||||
if (!IsValid()) return 20090007;
|
|
||||||
return HashCombine(GetTypeHash(GetTypeInfo()), GetTypeInfoImpl().HashImpl(GetAllocation()));
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
@ -342,173 +288,256 @@ public:
|
|||||||
InValue.Reset();
|
InValue.Reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GetTypeInfo() == InValue.GetTypeInfo())
|
if (GetTypeInfo() == InValue.GetTypeInfo())
|
||||||
{
|
{
|
||||||
GetTypeInfoImpl().SwapImpl(GetAllocation(), InValue.GetAllocation());
|
switch (GetRepresentation())
|
||||||
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
|
break;
|
||||||
|
case ERepresentation::Trivial:
|
||||||
|
uint8 Buffer[sizeof(TrivialStorage.Internal)];
|
||||||
|
Memory::Memmove(Buffer, TrivialStorage.Internal);
|
||||||
|
Memory::Memmove(TrivialStorage.Internal, InValue.TrivialStorage.Internal);
|
||||||
|
Memory::Memmove(InValue.TrivialStorage.Internal, Buffer);
|
||||||
|
break;
|
||||||
|
case ERepresentation::Small:
|
||||||
|
SmallStorage.RTTI->SwapObject(&SmallStorage.Internal, &InValue.SmallStorage.Internal);
|
||||||
|
break;
|
||||||
|
case ERepresentation::Big:
|
||||||
|
NAMESPACE_REDCRAFT::Swap(BigStorage.External, InValue.BigStorage.External);
|
||||||
|
break;
|
||||||
|
default: check_no_entry();
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
TAny Temp = MoveTemp(*this);
|
FAny Temp = MoveTemp(*this);
|
||||||
*this = MoveTemp(InValue);
|
*this = MoveTemp(InValue);
|
||||||
InValue = MoveTemp(Temp);
|
InValue = MoveTemp(Temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
CustomStorage Storage;
|
struct FRTTI
|
||||||
|
|
||||||
static constexpr uintptr_t RepresentationMask = 3;
|
|
||||||
|
|
||||||
enum class ERepresentation : uint8
|
|
||||||
{
|
{
|
||||||
Trivial, // Trivial & Inline
|
|
||||||
Small, // InlineAllocation
|
|
||||||
Big, // HeapAllocation
|
|
||||||
};
|
|
||||||
|
|
||||||
struct FTypeInfoImpl
|
|
||||||
{
|
|
||||||
const type_info* NativeTypeInfo;
|
|
||||||
|
|
||||||
const size_t TypeSize;
|
const size_t TypeSize;
|
||||||
const size_t TypeAlignment;
|
const size_t TypeAlignment;
|
||||||
|
|
||||||
using FCopyConstructImpl = void(*)(void*, const void*);
|
using FCopyConstruct = void(*)(void*, const void*);
|
||||||
using FMoveConstructImpl = void(*)(void*, void*);
|
using FMoveConstruct = void(*)(void*, void*);
|
||||||
using FCopyAssignImpl = void(*)(void*, const void*);
|
using FCopyAssign = void(*)(void*, const void*);
|
||||||
using FMoveAssignImpl = void(*)(void*, void*);
|
using FMoveAssign = void(*)(void*, void*);
|
||||||
using FDestroyImpl = void(*)(void* );
|
using FDestruct = void(*)(void* );
|
||||||
|
using FSwapObject = void(*)(void*, void*);
|
||||||
using FEqualityCompareImpl = bool (*)(const void*, const void*);
|
|
||||||
using FSynthThreeWayCompareImpl = partial_ordering (*)(const void*, const void*);
|
|
||||||
using FHashImpl = size_t (*)(const void* );
|
|
||||||
using FSwapImpl = void (*)( void*, void*);
|
|
||||||
|
|
||||||
const FCopyConstructImpl CopyConstructImpl;
|
|
||||||
const FMoveConstructImpl MoveConstructImpl;
|
|
||||||
const FCopyAssignImpl CopyAssignImpl;
|
|
||||||
const FMoveAssignImpl MoveAssignImpl;
|
|
||||||
const FDestroyImpl DestroyImpl;
|
|
||||||
|
|
||||||
const FEqualityCompareImpl EqualityCompareImpl;
|
const FCopyConstruct CopyConstruct;
|
||||||
const FSynthThreeWayCompareImpl SynthThreeWayCompareImpl;
|
const FMoveConstruct MoveConstruct;
|
||||||
const FHashImpl HashImpl;
|
const FCopyAssign CopyAssign;
|
||||||
const FSwapImpl SwapImpl;
|
const FMoveAssign MoveAssign;
|
||||||
|
const FDestruct Destruct;
|
||||||
|
const FSwapObject SwapObject;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr FTypeInfoImpl(TInPlaceType<T>)
|
FORCEINLINE constexpr FRTTI(TInPlaceType<T>)
|
||||||
|
: TypeSize( sizeof(T)), TypeAlignment(alignof(T))
|
||||||
: NativeTypeInfo (&typeid(T))
|
, CopyConstruct(
|
||||||
, TypeSize ( sizeof(T))
|
[](void* A, const void* B)
|
||||||
, TypeAlignment (alignof(T))
|
{
|
||||||
|
new (A) T(*reinterpret_cast<const T*>(B));
|
||||||
, 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(T).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(T).name()); })
|
)
|
||||||
, CopyAssignImpl ([](void* A, const void* B) { if constexpr (requires(T* A, const T* B) { Memory::CopyAssign (A, B); }) Memory::CopyAssign (reinterpret_cast<T*>(A), reinterpret_cast<const T*>(B)); else checkf(false, TEXT("The type '%s' is not copy assignable."), typeid(T).name()); })
|
, MoveConstruct(
|
||||||
, MoveAssignImpl ([](void* A, void* B) { if constexpr (requires(T* A, T* B) { Memory::MoveAssign (A, B); }) Memory::MoveAssign (reinterpret_cast<T*>(A), reinterpret_cast< T*>(B)); else checkf(false, TEXT("The type '%s' is not move assignable."), typeid(T).name()); })
|
[](void* A, void* B)
|
||||||
, DestroyImpl ([](void* A ) { if constexpr (requires(T* A ) { Memory::Destruct (A ); }) Memory::Destruct (reinterpret_cast<T*>(A) ); else checkf(false, TEXT("The type '%s' is not destructible."), typeid(T).name()); })
|
{
|
||||||
|
new (A) T(MoveTemp(*reinterpret_cast<T*>(B)));
|
||||||
, EqualityCompareImpl ([](const void* A, const void* B) -> bool { if constexpr (CEqualityComparable<T> ) return (*reinterpret_cast<const T*>(A) == *reinterpret_cast<const T*>(B)); else checkf(false, TEXT("The type '%s' is not equality comparable."), typeid(T).name()); return false; })
|
}
|
||||||
, SynthThreeWayCompareImpl ([](const void* A, const void* B) -> partial_ordering { if constexpr (CSynthThreeWayComparable<T>) return NAMESPACE_REDCRAFT::SynthThreeWayCompare (*reinterpret_cast<const T*>(A), *reinterpret_cast<const T*>(B)); else checkf(false, TEXT("The type '%s' is not synth three-way comparable."), typeid(T).name()); return partial_ordering::unordered; })
|
)
|
||||||
, HashImpl ([](const void* A ) -> size_t { if constexpr (CHashable<T> ) return NAMESPACE_REDCRAFT::GetTypeHash (*reinterpret_cast<const T*>(A) ); else checkf(false, TEXT("The type '%s' is not hashable."), typeid(T).name()); return 1080551797; })
|
, CopyAssign(
|
||||||
, SwapImpl ([]( void* A, void* B) -> void { if constexpr (CSwappable<T> ) NAMESPACE_REDCRAFT::Swap (*reinterpret_cast< T*>(A), *reinterpret_cast< T*>(B)); else checkf(false, TEXT("The type '%s' is not swappable."), typeid(T).name()); })
|
[](void* A, const void* B)
|
||||||
|
{
|
||||||
|
if constexpr (CCopyAssignable<T>)
|
||||||
|
{
|
||||||
|
*reinterpret_cast<T*>(A) = *reinterpret_cast<const T*>(B);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reinterpret_cast<T*>(A)->~T();
|
||||||
|
new (A) T(*reinterpret_cast<const T*>(B));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
, MoveAssign(
|
||||||
|
[](void* A, void* B)
|
||||||
|
{
|
||||||
|
if constexpr (CMoveAssignable<T>)
|
||||||
|
{
|
||||||
|
*reinterpret_cast<T*>(A) = MoveTemp(*reinterpret_cast<T*>(B));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reinterpret_cast<T*>(A)->~T();
|
||||||
|
new (A) T(MoveTemp(*reinterpret_cast<T*>(B)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
, Destruct(
|
||||||
|
[](void* A)
|
||||||
|
{
|
||||||
|
reinterpret_cast<T*>(A)->~T();
|
||||||
|
}
|
||||||
|
)
|
||||||
|
, SwapObject{
|
||||||
|
[](void* A, void* B)
|
||||||
|
{
|
||||||
|
NAMESPACE_REDCRAFT::Swap(*reinterpret_cast<T*>(A), *reinterpret_cast<T*>(B));
|
||||||
|
}
|
||||||
|
}
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(Storage.TypeInfo() & RepresentationMask); }
|
struct FTrivialStorage
|
||||||
constexpr const FTypeInfoImpl& GetTypeInfoImpl() const { return *reinterpret_cast<const FTypeInfoImpl*>(Storage.TypeInfo() & ~RepresentationMask); }
|
{
|
||||||
|
uint8 Internal[64 - sizeof(uintptr)];
|
||||||
|
};
|
||||||
|
|
||||||
constexpr void* GetAllocation() { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? Storage.InlineAllocation() : Storage.HeapAllocation(); }
|
struct FSmallStorage
|
||||||
constexpr const void* GetAllocation() const { return GetRepresentation() == ERepresentation::Trivial || GetRepresentation() == ERepresentation::Small ? Storage.InlineAllocation() : Storage.HeapAllocation(); }
|
{
|
||||||
|
uint8 Internal[sizeof(FTrivialStorage) - sizeof(const FRTTI*)];
|
||||||
|
const FRTTI* RTTI;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename SelectedType, typename... Ts>
|
struct FBigStorage
|
||||||
|
{
|
||||||
|
uint8 Padding[sizeof(FTrivialStorage) - sizeof(void*) - sizeof(const FRTTI*)];
|
||||||
|
void* External;
|
||||||
|
const FRTTI* RTTI;
|
||||||
|
};
|
||||||
|
|
||||||
|
static_assert(sizeof(FTrivialStorage) == sizeof(FSmallStorage));
|
||||||
|
static_assert(sizeof(FTrivialStorage) == sizeof( FBigStorage));
|
||||||
|
|
||||||
|
static_assert(alignof(type_info) >= 4);
|
||||||
|
|
||||||
|
static constexpr uintptr_t RepresentationMask = 3;
|
||||||
|
|
||||||
|
enum class ERepresentation : uintptr
|
||||||
|
{
|
||||||
|
Empty = 0, // EmptyType
|
||||||
|
Trivial = 1, // TrivialStorage
|
||||||
|
Small = 2, // SmallStorage
|
||||||
|
Big = 3, // BigStorage
|
||||||
|
};
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
FTrivialStorage TrivialStorage;
|
||||||
|
FSmallStorage SmallStorage;
|
||||||
|
FBigStorage BigStorage;
|
||||||
|
};
|
||||||
|
|
||||||
|
uintptr TypeInfo;
|
||||||
|
|
||||||
|
FORCEINLINE ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(TypeInfo & RepresentationMask); }
|
||||||
|
FORCEINLINE const type_info& GetTypeInfoImpl() const { return *reinterpret_cast<const type_info*>(TypeInfo & ~RepresentationMask); }
|
||||||
|
|
||||||
|
FORCEINLINE void* GetStorage()
|
||||||
|
{
|
||||||
|
switch (GetRepresentation())
|
||||||
|
{
|
||||||
|
case ERepresentation::Empty: return nullptr;
|
||||||
|
case ERepresentation::Trivial: return &TrivialStorage.Internal;
|
||||||
|
case ERepresentation::Small: return &SmallStorage.Internal;
|
||||||
|
case ERepresentation::Big: return BigStorage.External;
|
||||||
|
default: check_no_entry(); return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FORCEINLINE const void* GetStorage() const
|
||||||
|
{
|
||||||
|
switch (GetRepresentation())
|
||||||
|
{
|
||||||
|
case ERepresentation::Empty: return nullptr;
|
||||||
|
case ERepresentation::Trivial: return &TrivialStorage.Internal;
|
||||||
|
case ERepresentation::Small: return &SmallStorage.Internal;
|
||||||
|
case ERepresentation::Big: return BigStorage.External;
|
||||||
|
default: check_no_entry(); return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename... Ts>
|
||||||
FORCEINLINE void EmplaceImpl(Ts&&... Args)
|
FORCEINLINE void EmplaceImpl(Ts&&... Args)
|
||||||
{
|
{
|
||||||
static constexpr const FTypeInfoImpl SelectedTypeInfo(InPlaceType<SelectedType>);
|
using DecayedType = TDecay<T>;
|
||||||
Storage.TypeInfo() = reinterpret_cast<uintptr>(&SelectedTypeInfo);
|
|
||||||
|
|
||||||
constexpr bool bIsInlineStorable = sizeof(SelectedType) <= Storage.InlineSize && alignof(SelectedType) <= Storage.InlineAlignment;
|
TypeInfo = reinterpret_cast<uintptr>(&typeid(DecayedType));
|
||||||
constexpr bool bIsTriviallyStorable = bIsInlineStorable && CTrivial<SelectedType> && CTriviallyCopyable<SelectedType>;
|
|
||||||
|
if constexpr (CEmpty<DecayedType> && CTrivial<DecayedType>) return; // ERepresentation::Empty
|
||||||
|
|
||||||
|
constexpr bool bIsTriviallyStorable = sizeof(DecayedType) <= sizeof(TrivialStorage.Internal) && alignof(DecayedType) <= alignof(FAny) && CTriviallyCopyable<DecayedType>;
|
||||||
|
constexpr bool bIsSmallStorable = sizeof(DecayedType) <= sizeof( SmallStorage.Internal) && alignof(DecayedType) <= alignof(FAny);
|
||||||
|
|
||||||
|
static constexpr const FRTTI SelectedRTTI(InPlaceType<DecayedType>);
|
||||||
|
|
||||||
if constexpr (bIsTriviallyStorable)
|
if constexpr (bIsTriviallyStorable)
|
||||||
{
|
{
|
||||||
new(Storage.InlineAllocation()) SelectedType(Forward<Ts>(Args)...);
|
new (&TrivialStorage.Internal) DecayedType(Forward<Ts>(Args)...);
|
||||||
Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Trivial);
|
TypeInfo |= static_cast<uintptr>(ERepresentation::Trivial);
|
||||||
}
|
}
|
||||||
else if constexpr (bIsInlineStorable)
|
else if constexpr (bIsSmallStorable)
|
||||||
{
|
{
|
||||||
new(Storage.InlineAllocation()) SelectedType(Forward<Ts>(Args)...);
|
new (&SmallStorage.Internal) DecayedType(Forward<Ts>(Args)...);
|
||||||
Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Small);
|
SmallStorage.RTTI = &SelectedRTTI;
|
||||||
|
TypeInfo |= static_cast<uintptr>(ERepresentation::Small);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Storage.HeapAllocation() = new SelectedType(Forward<Ts>(Args)...);
|
BigStorage.External = Memory::Malloc(sizeof(DecayedType), alignof(DecayedType));
|
||||||
Storage.TypeInfo() |= static_cast<uintptr>(ERepresentation::Big);
|
new (BigStorage.External) DecayedType(Forward<Ts>(Args)...);
|
||||||
|
BigStorage.RTTI = &SelectedRTTI;
|
||||||
|
TypeInfo |= static_cast<uintptr>(ERepresentation::Big);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCEINLINE void ResetImpl()
|
FORCEINLINE void Destroy()
|
||||||
{
|
{
|
||||||
if (!IsValid()) return;
|
if (!IsValid()) return;
|
||||||
|
|
||||||
switch (GetRepresentation())
|
switch (GetRepresentation())
|
||||||
{
|
{
|
||||||
|
case ERepresentation::Empty:
|
||||||
case ERepresentation::Trivial:
|
case ERepresentation::Trivial:
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Small:
|
case ERepresentation::Small:
|
||||||
GetTypeInfoImpl().DestroyImpl(GetAllocation());
|
SmallStorage.RTTI->Destruct(&SmallStorage.Internal);
|
||||||
break;
|
break;
|
||||||
case ERepresentation::Big:
|
case ERepresentation::Big:
|
||||||
GetTypeInfoImpl().DestroyImpl(GetAllocation());
|
BigStorage.RTTI->Destruct(BigStorage.External);
|
||||||
Memory::Free(Storage.HeapAllocation());
|
Memory::Free(BigStorage.External);
|
||||||
break;
|
break;
|
||||||
default: check_no_entry();
|
default: check_no_entry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
friend FORCEINLINE bool operator==(const TAny& LHS, const TAny& RHS)
|
FORCEINLINE constexpr void Invalidate() { TypeInfo = 0; }
|
||||||
|
|
||||||
|
template <typename T> requires (!CBaseOf<FAny, TRemoveCVRef<T>>)
|
||||||
|
friend FORCEINLINE constexpr bool operator==(const FAny& LHS, const T& RHS)
|
||||||
{
|
{
|
||||||
if (LHS.GetTypeInfo() != RHS.GetTypeInfo()) return false;
|
return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() == RHS : false;
|
||||||
if (LHS.IsValid() == false) return true;
|
|
||||||
return LHS.GetTypeInfoImpl().EqualityCompareImpl(LHS.GetAllocation(), RHS.GetAllocation());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
friend FORCEINLINE partial_ordering operator<=>(const TAny& LHS, const TAny& RHS)
|
friend FORCEINLINE constexpr bool operator==(const FAny& LHS, FInvalid)
|
||||||
{
|
{
|
||||||
if (LHS.GetTypeInfo() != RHS.GetTypeInfo()) return partial_ordering::unordered;
|
return !LHS.IsValid();
|
||||||
if (LHS.IsValid() == false) return partial_ordering::equivalent;
|
|
||||||
return LHS.GetTypeInfoImpl().SynthThreeWayCompareImpl(LHS.GetAllocation(), RHS.GetAllocation());;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class FAny : STRONG_INHERIT(TAny<FAnyDefaultStorage>);
|
|
||||||
|
|
||||||
static_assert(sizeof(FAny) == 64, "The byte size of FAny is unexpected");
|
static_assert(sizeof(FAny) == 64, "The byte size of FAny is unexpected");
|
||||||
|
|
||||||
template <typename T, CAnyCustomStorage StorageType> requires (!CBaseOf<FAny, TRemoveCVRef<T>>)
|
static_assert(alignof(FAny) == 16, "The byte alignment of FAny is unexpected");
|
||||||
constexpr bool operator==(const TAny<StorageType>& LHS, const T& RHS)
|
|
||||||
{
|
|
||||||
return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() == RHS : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <CAnyCustomStorage StorageType>
|
|
||||||
constexpr bool operator==(const TAny<StorageType>& LHS, FInvalid)
|
|
||||||
{
|
|
||||||
return !LHS.IsValid();
|
|
||||||
}
|
|
||||||
|
|
||||||
NAMESPACE_PRIVATE_BEGIN
|
|
||||||
|
|
||||||
template <typename T> struct TIsTAny : FFalse { };
|
|
||||||
template <CAnyCustomStorage StorageType> struct TIsTAny<TAny<StorageType>> : FTrue { };
|
|
||||||
|
|
||||||
NAMESPACE_PRIVATE_END
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept CTAny = NAMESPACE_PRIVATE::TIsTAny<TRemoveCV<T>>::Value;
|
|
||||||
|
|
||||||
NAMESPACE_MODULE_END(Utility)
|
NAMESPACE_MODULE_END(Utility)
|
||||||
NAMESPACE_MODULE_END(Redcraft)
|
NAMESPACE_MODULE_END(Redcraft)
|
||||||
|
@ -321,15 +321,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
union
|
|
||||||
{
|
|
||||||
uint8 InternalStorage[64 - sizeof(uintptr) - sizeof(uintptr)];
|
|
||||||
void* ExternalStorage;
|
|
||||||
};
|
|
||||||
|
|
||||||
uintptr RTTI;
|
|
||||||
uintptr Callable;
|
|
||||||
|
|
||||||
struct FMovableRTTI
|
struct FMovableRTTI
|
||||||
{
|
{
|
||||||
const size_t TypeSize;
|
const size_t TypeSize;
|
||||||
@ -390,6 +381,15 @@ private:
|
|||||||
Small = 2, // InternalStorage
|
Small = 2, // InternalStorage
|
||||||
Big = 3, // ExternalStorage
|
Big = 3, // ExternalStorage
|
||||||
};
|
};
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
uint8 InternalStorage[64 - sizeof(uintptr) - sizeof(uintptr)];
|
||||||
|
void* ExternalStorage;
|
||||||
|
};
|
||||||
|
|
||||||
|
uintptr RTTI;
|
||||||
|
uintptr Callable;
|
||||||
|
|
||||||
FORCEINLINE constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(RTTI & RepresentationMask); }
|
FORCEINLINE constexpr ERepresentation GetRepresentation() const { return static_cast<ERepresentation>(RTTI & RepresentationMask); }
|
||||||
FORCEINLINE constexpr const FRTTI& GetRTTI() const { return *reinterpret_cast<const FRTTI*>(RTTI & ~RepresentationMask); }
|
FORCEINLINE constexpr const FRTTI& GetRTTI() const { return *reinterpret_cast<const FRTTI*>(RTTI & ~RepresentationMask); }
|
||||||
|
Loading…
Reference in New Issue
Block a user