refactor(templates): remove the GetData function from the non-container class

This commit is contained in:
_Redstone_c_ 2022-04-24 22:42:40 +08:00
parent b3a97cfe21
commit c2eca8df1e
5 changed files with 85 additions and 129 deletions

View File

@ -179,9 +179,9 @@ struct TAny
{ {
if (!IsValid()) return; if (!IsValid()) return;
if (IsTrivial()) Memory::Memcpy(InlineValue, InValue.InlineValue); if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
else if (IsInline()) RTTI->CopyConstruct(GetData(), InValue.GetData()); else if (IsInline()) RTTI->CopyConstruct(GetStorage(), InValue.GetStorage());
else DynamicValue = RTTI->CopyNew(InValue.GetData()); else DynamicStorage = RTTI->CopyNew(InValue.GetStorage());
} }
FORCEINLINE TAny(TAny&& InValue) FORCEINLINE TAny(TAny&& InValue)
@ -189,11 +189,11 @@ struct TAny
{ {
if (!IsValid()) return; if (!IsValid()) return;
if (IsTrivial()) Memory::Memcpy(InlineValue, InValue.InlineValue); if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
else if (IsInline()) RTTI->MoveConstruct(GetData(), InValue.GetData()); else if (IsInline()) RTTI->MoveConstruct(GetStorage(), InValue.GetStorage());
else else
{ {
DynamicValue = InValue.DynamicValue; DynamicStorage = InValue.DynamicStorage;
InValue.TypeInfo = nullptr; InValue.TypeInfo = nullptr;
} }
} }
@ -228,8 +228,8 @@ struct TAny
} }
else if (GetTypeInfo() == InValue.GetTypeInfo()) else if (GetTypeInfo() == InValue.GetTypeInfo())
{ {
if (IsTrivial()) Memory::Memcpy(InlineValue, InValue.InlineValue); if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
else RTTI->CopyAssign(GetData(), InValue.GetData()); else RTTI->CopyAssign(GetStorage(), InValue.GetStorage());
} }
else else
{ {
@ -238,9 +238,9 @@ struct TAny
TypeInfo = InValue.TypeInfo; TypeInfo = InValue.TypeInfo;
RTTI = InValue.RTTI; RTTI = InValue.RTTI;
if (IsTrivial()) Memory::Memcpy(InlineValue, InValue.InlineValue); if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
else if (IsInline()) RTTI->CopyConstruct(GetData(), InValue.GetData()); else if (IsInline()) RTTI->CopyConstruct(GetStorage(), InValue.GetStorage());
else DynamicValue = RTTI->CopyNew(InValue.GetData()); else DynamicStorage = RTTI->CopyNew(InValue.GetStorage());
} }
return *this; return *this;
@ -256,12 +256,12 @@ struct TAny
} }
else if (GetTypeInfo() == InValue.GetTypeInfo()) else if (GetTypeInfo() == InValue.GetTypeInfo())
{ {
if (IsTrivial()) Memory::Memcpy(InlineValue, InValue.InlineValue); if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
else if (IsInline()) RTTI->MoveAssign(GetData(), InValue.GetData()); else if (IsInline()) RTTI->MoveAssign(GetStorage(), InValue.GetStorage());
else else
{ {
RTTI->Delete(DynamicValue); RTTI->Delete(DynamicStorage);
DynamicValue = InValue.DynamicValue; DynamicStorage = InValue.DynamicStorage;
InValue.TypeInfo = nullptr; InValue.TypeInfo = nullptr;
} }
} }
@ -272,9 +272,9 @@ struct TAny
TypeInfo = InValue.TypeInfo; TypeInfo = InValue.TypeInfo;
RTTI = InValue.RTTI; RTTI = InValue.RTTI;
if (IsTrivial()) Memory::Memcpy(InlineValue, InValue.InlineValue); if (IsTrivial()) Memory::Memcpy(InlineStorage, InValue.InlineStorage);
else if (IsInline()) RTTI->MoveConstruct(GetData(), InValue.GetData()); else if (IsInline()) RTTI->MoveConstruct(GetStorage(), InValue.GetStorage());
else DynamicValue = RTTI->MoveNew(InValue.GetData()); else DynamicStorage = RTTI->MoveNew(InValue.GetStorage());
} }
return *this; return *this;
@ -290,7 +290,7 @@ struct TAny
if (HoldsAlternative<SelectedType>()) if (HoldsAlternative<SelectedType>())
{ {
if constexpr (TIsTriviallyStorable<SelectedType>::Value) if constexpr (TIsTriviallyStorable<SelectedType>::Value)
Memory::Memcpy(&InlineValue, &InValue, sizeof(SelectedType)); Memory::Memcpy(&InlineStorage, &InValue, sizeof(SelectedType));
else GetValue<SelectedType>() = Forward<T>(InValue); else GetValue<SelectedType>() = Forward<T>(InValue);
} }
else else
@ -323,20 +323,17 @@ struct TAny
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; }
constexpr void* GetData() { return IsInline() ? &InlineValue : DynamicValue; } 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 void* GetData() const { return IsInline() ? &InlineValue : DynamicValue; } 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 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*>(GetData()); } 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 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*>(GetData())); } 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 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*>(GetData()); } 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 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*>(GetData())); }
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; }
@ -350,13 +347,13 @@ struct TAny
ResetImpl(); ResetImpl();
} }
// constexpr 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(GetData())); // return HashCombine(NAMESPACE_REDCRAFT::GetTypeHash(GetTypeInfo()), RTTI->TypeHash(GetStorage()));
// } // }
// //
// constexpr void Swap(TAny& InValue) // FORCEINLINE void Swap(TAny& InValue)
// { // {
// if (!IsValid() && !InValue.IsValid()) return; // if (!IsValid() && !InValue.IsValid()) return;
// //
@ -376,7 +373,7 @@ struct TAny
// //
// if (GetTypeInfo() == InValue.GetTypeInfo()) // if (GetTypeInfo() == InValue.GetTypeInfo())
// { // {
// RTTI->SwapObject(GetData(), InValue.GetData()); // RTTI->SwapObject(GetStorage(), InValue.GetStorage());
// return; // return;
// } // }
// //
@ -389,13 +386,16 @@ private:
union union
{ {
TAlignedStorage<InlineSize, InlineAlignment>::Type InlineValue; TAlignedStorage<InlineSize, InlineAlignment>::Type InlineStorage;
void* DynamicValue; void* DynamicStorage;
}; };
const FTypeInfo* TypeInfo; const FTypeInfo* TypeInfo;
const NAMESPACE_PRIVATE::FAnyRTTI* RTTI; const NAMESPACE_PRIVATE::FAnyRTTI* RTTI;
constexpr void* GetStorage() { return IsInline() ? &InlineStorage : DynamicStorage; }
constexpr const void* GetStorage() const { return IsInline() ? &InlineStorage : DynamicStorage; }
template <typename SelectedType, typename... Types> template <typename SelectedType, typename... Types>
FORCEINLINE void EmplaceImpl(Types&&... Args) FORCEINLINE void EmplaceImpl(Types&&... Args)
{ {
@ -403,17 +403,17 @@ private:
if constexpr (TIsTriviallyStorable<SelectedType>::Value) if constexpr (TIsTriviallyStorable<SelectedType>::Value)
{ {
new(&InlineValue) SelectedType(Forward<Types>(Args)...); new(&InlineStorage) SelectedType(Forward<Types>(Args)...);
RTTI = nullptr; RTTI = nullptr;
} }
else if constexpr (TIsInlineStorable<SelectedType>::Value) else if constexpr (TIsInlineStorable<SelectedType>::Value)
{ {
new(&InlineValue) SelectedType(Forward<Types>(Args)...); new(&InlineStorage) SelectedType(Forward<Types>(Args)...);
RTTI = &NAMESPACE_PRIVATE::TAnyRTTIHelper<SelectedType, true>::RTTI; RTTI = &NAMESPACE_PRIVATE::TAnyRTTIHelper<SelectedType, true>::RTTI;
} }
else else
{ {
DynamicValue = new SelectedType(Forward<Types>(Args)...); DynamicStorage = new SelectedType(Forward<Types>(Args)...);
RTTI = &NAMESPACE_PRIVATE::TAnyRTTIHelper<SelectedType, false>::RTTI; RTTI = &NAMESPACE_PRIVATE::TAnyRTTIHelper<SelectedType, false>::RTTI;
} }
} }
@ -421,15 +421,15 @@ private:
FORCEINLINE void ResetImpl() FORCEINLINE void ResetImpl()
{ {
if (!IsValid() || IsTrivial()) return; if (!IsValid() || IsTrivial()) return;
else if (IsInline()) RTTI->Destroy(&InlineValue); else if (IsInline()) RTTI->Destroy(&InlineStorage);
else RTTI->Delete(DynamicValue); else RTTI->Delete(DynamicStorage);
} }
// 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.GetData(), RHS.GetData()); // return LHS.RTTI->EqualityOperator(LHS.GetStorage(), RHS.GetStorage());
// } // }
}; };

View File

@ -54,7 +54,7 @@ template <typename F, size_t I, size_t J, EFunctionSpecifiers S> struct TIsTFunc
template <typename T> struct TIsTUniqueFunction : FFalse { }; template <typename T> struct TIsTUniqueFunction : FFalse { };
template <typename F, size_t I, size_t J, EFunctionSpecifiers S> struct TIsTUniqueFunction<TFunctionImpl<F, I, J, S, EFunctionType::Unique>> : FTrue { }; template <typename F, size_t I, size_t J, EFunctionSpecifiers S> struct TIsTUniqueFunction<TFunctionImpl<F, I, J, S, EFunctionType::Unique>> : FTrue { };
struct FFunctionIsBoundImpl struct FFunctionIsBound
{ {
template <typename T> template <typename T>
static constexpr bool F(const T& Func) static constexpr bool F(const T& Func)
@ -95,62 +95,13 @@ struct TIsInvocableResultWithSpecifiers<EFunctionSpecifiers::ConstLValue, R, F,
template <typename R, typename F, typename... Types> template <typename R, typename F, typename... Types>
struct TIsInvocableResultWithSpecifiers<EFunctionSpecifiers::ConstRValue, R, F, Types...> : TIsInvocableResult<R, const F, Types...> { }; struct TIsInvocableResultWithSpecifiers<EFunctionSpecifiers::ConstRValue, R, F, Types...> : TIsInvocableResult<R, const F, Types...> { };
template <EFunctionSpecifiers Specifiers, typename T, typename F> template <typename T, EFunctionSpecifiers Specifiers> struct TFunctionCallSpecifiers;
struct TFunctionCallImpl; template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::None> { using Type = T& ; };
template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::LValue> { using Type = T& ; };
template <typename T, typename R, typename... Types> template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::RValue> { using Type = T&&; };
struct TFunctionCallImpl<EFunctionSpecifiers::None, T, R(Types...)> template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::Const> { using Type = const T& ; };
{ template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::ConstLValue> { using Type = const T& ; };
static inline R F(void* Func, Types&&... Args) template <typename T> struct TFunctionCallSpecifiers<T, EFunctionSpecifiers::ConstRValue> { using Type = const T&&; };
{
return InvokeResult<R>(*reinterpret_cast<T*>(Func), Forward<Types>(Args)...);
}
};
template <typename T, typename R, typename... Types>
struct TFunctionCallImpl<EFunctionSpecifiers::LValue, T, R(Types...)>
{
static inline R F(void* Func, Types&&... Args)
{
return InvokeResult<R>(*reinterpret_cast<T*>(Func), Forward<Types>(Args)...);
}
};
template <typename T, typename R, typename... Types>
struct TFunctionCallImpl<EFunctionSpecifiers::RValue, T, R(Types...)>
{
static inline R F(void* Func, Types&&... Args)
{
return InvokeResult<R>(MoveTemp(*reinterpret_cast<T*>(Func)), Forward<Types>(Args)...);
}
};
template <typename T, typename R, typename... Types>
struct TFunctionCallImpl<EFunctionSpecifiers::Const, T, R(Types...)>
{
static inline R F(void* Func, Types&&... Args)
{
return InvokeResult<R>(AsConst(*reinterpret_cast<T*>(Func)), Forward<Types>(Args)...);
}
};
template <typename T, typename R, typename... Types>
struct TFunctionCallImpl<EFunctionSpecifiers::ConstLValue, T, R(Types...)>
{
static inline R F(void* Func, Types&&... Args)
{
return InvokeResult<R>(AsConst(*reinterpret_cast<T*>(Func)), Forward<Types>(Args)...);
}
};
template <typename T, typename R, typename... Types>
struct TFunctionCallImpl<EFunctionSpecifiers::ConstRValue, T, R(Types...)>
{
static inline R F(void* Func, Types&&... Args)
{
return InvokeResult<R>(MoveTemp(AsConst(*reinterpret_cast<T*>(Func))), Forward<Types>(Args)...);
}
};
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 TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, FunctionType>
@ -160,14 +111,14 @@ public:
using ResultType = R; using ResultType = R;
using ArgumentType = TTuple<Types...>; using ArgumentType = TTuple<Types...>;
constexpr TFunctionImpl(nullptr_t = nullptr) requires (FunctionType != EFunctionType::Reference) : Call(nullptr) { } constexpr TFunctionImpl(nullptr_t = nullptr) requires (FunctionType != EFunctionType::Reference) : Callable(nullptr) { }
TFunctionImpl(const TFunctionImpl& InValue) requires (FunctionType != EFunctionType::Unique) TFunctionImpl(const TFunctionImpl& InValue) requires (FunctionType != EFunctionType::Unique)
: Call(InValue.Call), Storage(InValue.Storage) : Callable(InValue.Callable), Storage(InValue.Storage)
{ } { }
TFunctionImpl(TFunctionImpl&& InValue) TFunctionImpl(TFunctionImpl&& InValue)
: Call(InValue.Call), Storage(MoveTemp(InValue.Storage)) : Callable(InValue.Callable), Storage(MoveTemp(InValue.Storage))
{ if constexpr (FunctionType != EFunctionType::Reference) InValue.Reset(); } { if constexpr (FunctionType != EFunctionType::Reference) InValue.Reset(); }
template <typename T> requires (!TIsTFunctionImpl<typename TDecay<T>::Type>::Value) && (!TIsTInPlaceType<typename TDecay<T>::Type>::Value) template <typename T> requires (!TIsTFunctionImpl<typename TDecay<T>::Type>::Value) && (!TIsTInPlaceType<typename TDecay<T>::Type>::Value)
@ -182,10 +133,10 @@ public:
if constexpr (FunctionType == EFunctionType::Reference) if constexpr (FunctionType == EFunctionType::Reference)
{ {
checkf(FFunctionIsBoundImpl::F(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef")); checkf(FFunctionIsBound::F(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef"));
} }
if (!FFunctionIsBoundImpl::F(InValue)) Call = nullptr; if (!FFunctionIsBound::F(InValue)) Callable = nullptr;
else EmplaceImpl<DecayedFunctorType>(Forward<T>(InValue)); else EmplaceImpl<DecayedFunctorType>(Forward<T>(InValue));
} }
@ -203,16 +154,16 @@ public:
requires (FunctionType == EFunctionType::Reference) && (OtherFunctionType != EFunctionType::Reference) requires (FunctionType == EFunctionType::Reference) && (OtherFunctionType != EFunctionType::Reference)
FORCEINLINE TFunctionImpl(const TFunctionImpl<R(Types...), OtherInlineSize, OtherInlineAlignment, Specifiers, OtherFunctionType>& InValue) FORCEINLINE TFunctionImpl(const TFunctionImpl<R(Types...), OtherInlineSize, OtherInlineAlignment, Specifiers, OtherFunctionType>& InValue)
{ {
checkf(FFunctionIsBoundImpl::F(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef")); checkf(FFunctionIsBound::F(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef"));
EmplaceImpl<TFunctionImpl<R(Types...), OtherInlineSize, OtherInlineAlignment, Specifiers, OtherFunctionType>>(InValue); EmplaceImpl<TFunctionImpl<R(Types...), OtherInlineSize, OtherInlineAlignment, Specifiers, OtherFunctionType>>(InValue);
} }
FORCEINLINE TFunctionImpl(const TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, EFunctionType::Object>& InValue) requires (FunctionType == EFunctionType::Unique) FORCEINLINE TFunctionImpl(const TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, EFunctionType::Object>& InValue) requires (FunctionType == EFunctionType::Unique)
: Call((*reinterpret_cast<const TFunctionImpl*>(&InValue)).Call), Storage((*reinterpret_cast<const TFunctionImpl*>(&InValue)).Storage) : Callable((*reinterpret_cast<const TFunctionImpl*>(&InValue)).Callable), Storage((*reinterpret_cast<const TFunctionImpl*>(&InValue)).Storage)
{ } { }
FORCEINLINE TFunctionImpl(TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, EFunctionType::Object>&& InValue) requires (FunctionType == EFunctionType::Unique) FORCEINLINE TFunctionImpl(TFunctionImpl<R(Types...), InlineSize, InlineAlignment, Specifiers, EFunctionType::Object>&& InValue) requires (FunctionType == EFunctionType::Unique)
: Call((*reinterpret_cast<TFunctionImpl*>(&InValue)).Call), Storage(MoveTemp((*reinterpret_cast<TFunctionImpl*>(&InValue)).Storage)) : Callable((*reinterpret_cast<TFunctionImpl*>(&InValue)).Callable), Storage(MoveTemp((*reinterpret_cast<TFunctionImpl*>(&InValue)).Storage))
{ InValue.Reset(); } { InValue.Reset(); }
~TFunctionImpl() = default; ~TFunctionImpl() = default;
@ -253,7 +204,7 @@ public:
{ {
using DecayedFunctorType = typename TDecay<T>::Type; using DecayedFunctorType = typename TDecay<T>::Type;
if (!FFunctionIsBoundImpl::F(InValue)) Reset(); if (!FFunctionIsBound::F(InValue)) Reset();
else EmplaceImpl<DecayedFunctorType>(Forward<T>(InValue)); else EmplaceImpl<DecayedFunctorType>(Forward<T>(InValue));
return *this; return *this;
@ -278,11 +229,8 @@ public:
FORCEINLINE ResultType operator()(Types... Args) const& requires (Specifiers == EFunctionSpecifiers::ConstLValue) { return CallImpl(Forward<Types>(Args)...); } FORCEINLINE ResultType operator()(Types... Args) const& requires (Specifiers == EFunctionSpecifiers::ConstLValue) { return CallImpl(Forward<Types>(Args)...); }
FORCEINLINE ResultType operator()(Types... Args) const&& requires (Specifiers == EFunctionSpecifiers::ConstRValue) { return CallImpl(Forward<Types>(Args)...); } FORCEINLINE ResultType operator()(Types... Args) const&& requires (Specifiers == EFunctionSpecifiers::ConstRValue) { return CallImpl(Forward<Types>(Args)...); }
constexpr bool IsValid() const { return Call != nullptr; } constexpr bool IsValid() const { return Callable != nullptr; }
constexpr explicit operator bool() const { return Call != nullptr; } constexpr explicit operator bool() const { return Callable != nullptr; }
FORCEINLINE void* GetData() { if constexpr (FunctionType != EFunctionType::Reference) return Storage.GetData(); else return Storage; }
FORCEINLINE const void* GetData() const { if constexpr (FunctionType != EFunctionType::Reference) return Storage.GetData(); else return Storage; }
FORCEINLINE const FTypeInfo& TargetType() const requires (FunctionType != EFunctionType::Reference) { return IsValid() ? Storage.GetTypeInfo() : Typeid(void); }; FORCEINLINE const FTypeInfo& TargetType() const requires (FunctionType != EFunctionType::Reference) { return IsValid() ? Storage.GetTypeInfo() : Typeid(void); };
@ -291,7 +239,7 @@ public:
template <typename T> FORCEINLINE const T& Target() const& requires (FunctionType != EFunctionType::Reference) && 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 { return static_cast<const StorageType& >(Storage).template GetValue<T>(); } template <typename T> FORCEINLINE const T& Target() const& requires (FunctionType != EFunctionType::Reference) && 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 { return static_cast<const StorageType& >(Storage).template GetValue<T>(); }
template <typename T> FORCEINLINE const T&& Target() const&& requires (FunctionType != EFunctionType::Reference) && 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 { return static_cast<const StorageType&&>(Storage).template GetValue<T>(); } template <typename T> FORCEINLINE const T&& Target() const&& requires (FunctionType != EFunctionType::Reference) && 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 { return static_cast<const StorageType&&>(Storage).template GetValue<T>(); }
constexpr void Reset() requires (FunctionType != EFunctionType::Reference) { Call = nullptr; } constexpr void Reset() requires (FunctionType != EFunctionType::Reference) { Callable = nullptr; }
constexpr void Swap(TFunctionImpl& InValue) requires (FunctionType != EFunctionType::Reference) constexpr void Swap(TFunctionImpl& InValue) requires (FunctionType != EFunctionType::Reference)
{ {
@ -311,37 +259,54 @@ public:
return; return;
} }
NAMESPACE_REDCRAFT::Swap(Call, InValue.Call); NAMESPACE_REDCRAFT::Swap(Callable, InValue.Callable);
NAMESPACE_REDCRAFT::Swap(Storage, InValue.Storage); NAMESPACE_REDCRAFT::Swap(Storage, InValue.Storage);
} }
private: private:
using CallFunc = ResultType(*)(void*, Types&&...);
using StorageType = typename TConditional<FunctionType == EFunctionType::Reference, void*, TAny<InlineSize, InlineAlignment>>::Type; using StorageType = typename TConditional<FunctionType == EFunctionType::Reference, void*, TAny<InlineSize, InlineAlignment>>::Type;
using StorageRef = typename TConditional<FunctionType == EFunctionType::Reference, void*, typename TFunctionCallSpecifiers<StorageType, Specifiers>::Type&>::Type;
using CallFunc = ResultType(*)(StorageRef, Types&&...);
CallFunc Call;
StorageType Storage; StorageType Storage;
CallFunc Callable;
template <typename SelectedType, typename... ArgTypes> template <typename SelectedType, typename... ArgTypes>
FORCEINLINE void EmplaceImpl(ArgTypes&&... Args) FORCEINLINE void EmplaceImpl(ArgTypes&&... Args)
{ {
if constexpr (FunctionType == EFunctionType::Reference) Storage = ((void*)&Args, ...); if constexpr (FunctionType == EFunctionType::Reference) Storage = ((void*)&Args, ...);
else Storage.template Emplace<SelectedType>(Forward<ArgTypes>(Args)...); else Storage.template Emplace<SelectedType>(Forward<ArgTypes>(Args)...);
Call = &TFunctionCallImpl<Specifiers, SelectedType, ResultType(Types...)>::F;
Callable = [](StorageRef Storage, Types&&... Args) -> ResultType
{
const auto GetFunc = [&Storage]() -> decltype(auto)
{
if constexpr (FunctionType == EFunctionType::Reference) return *reinterpret_cast<SelectedType*>(Storage);
else return Storage.template GetValue<SelectedType>();
};
return InvokeResult<R>(Forward<typename TFunctionCallSpecifiers<SelectedType, Specifiers>::Type>(GetFunc()), Forward<Types>(Args)...);
};
}
FORCEINLINE ResultType CallImpl(Types&&... Args)
{
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
return Callable(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 Call(const_cast<TFunctionImpl&>(*this).GetData(), Forward<Types>(Args)...); return Callable(Storage, Forward<Types>(Args)...);
} }
FORCEINLINE void AssignImpl(const TFunctionImpl& InValue) FORCEINLINE void AssignImpl(const TFunctionImpl& InValue)
{ {
if (InValue.IsValid()) if (InValue.IsValid())
{ {
Call = InValue.Call; Callable = InValue.Callable;
Storage = InValue.Storage; Storage = InValue.Storage;
} }
else Reset(); else Reset();
@ -351,7 +316,7 @@ private:
{ {
if (InValue.IsValid()) if (InValue.IsValid())
{ {
Call = InValue.Call; Callable = InValue.Callable;
Storage = MoveTemp(InValue.Storage); Storage = MoveTemp(InValue.Storage);
InValue.Reset(); InValue.Reset();
} }

View File

@ -189,9 +189,6 @@ public:
constexpr bool IsValid() const { return bIsValid; } constexpr bool IsValid() const { return bIsValid; }
constexpr explicit operator bool() const { return bIsValid; } constexpr explicit operator bool() const { return bIsValid; }
constexpr void* GetData() { return &Value; }
constexpr const void* GetData() const { return &Value; }
constexpr OptionalType& GetValue() & { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return *reinterpret_cast< OptionalType*>(&Value); } constexpr OptionalType& GetValue() & { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return *reinterpret_cast< OptionalType*>(&Value); }
constexpr OptionalType&& GetValue() && { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast< OptionalType*>(&Value)); } constexpr OptionalType&& GetValue() && { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast< OptionalType*>(&Value)); }
constexpr const OptionalType& GetValue() const& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return *reinterpret_cast<const OptionalType*>(&Value); } constexpr const OptionalType& GetValue() const& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return *reinterpret_cast<const OptionalType*>(&Value); }

View File

@ -308,9 +308,6 @@ public:
TTuple& operator=(const TTuple&) = default; TTuple& operator=(const TTuple&) = default;
TTuple& operator=(TTuple&&) = default; TTuple& operator=(TTuple&&) = default;
constexpr void* GetData() { return this; }
constexpr const void* GetData() const { return this; }
template <size_t I> requires (I < ElementSize) constexpr TElementType<I>::Type& GetValue() & { return static_cast< NAMESPACE_PRIVATE::TTupleElement<typename TElementType<I>::Type, I>& >(*this).GetValue(); } template <size_t I> requires (I < ElementSize) constexpr TElementType<I>::Type& GetValue() & { return static_cast< NAMESPACE_PRIVATE::TTupleElement<typename TElementType<I>::Type, I>& >(*this).GetValue(); }
template <size_t I> requires (I < ElementSize) constexpr const TElementType<I>::Type& GetValue() const & { return static_cast<const NAMESPACE_PRIVATE::TTupleElement<typename TElementType<I>::Type, I>& >(*this).GetValue(); } template <size_t I> requires (I < ElementSize) constexpr const TElementType<I>::Type& GetValue() const & { return static_cast<const NAMESPACE_PRIVATE::TTupleElement<typename TElementType<I>::Type, I>& >(*this).GetValue(); }
template <size_t I> requires (I < ElementSize) constexpr volatile TElementType<I>::Type& GetValue() volatile& { return static_cast< volatile NAMESPACE_PRIVATE::TTupleElement<typename TElementType<I>::Type, I>& >(*this).GetValue(); } template <size_t I> requires (I < ElementSize) constexpr volatile TElementType<I>::Type& GetValue() volatile& { return static_cast< volatile NAMESPACE_PRIVATE::TTupleElement<typename TElementType<I>::Type, I>& >(*this).GetValue(); }

View File

@ -339,9 +339,6 @@ struct TVariant
template <size_t I> constexpr bool HoldsAlternative() const { return IsValid() ? GetIndex() == I : false; } template <size_t I> constexpr bool HoldsAlternative() const { return IsValid() ? GetIndex() == I : false; }
template <typename T> constexpr bool HoldsAlternative() const { return IsValid() ? GetIndex() == TAlternativeIndex<T>::Value : false; } template <typename T> constexpr bool HoldsAlternative() const { return IsValid() ? GetIndex() == TAlternativeIndex<T>::Value : false; }
constexpr void* GetData() { return &Value; }
constexpr const void* GetData() const { return &Value; }
template <size_t I> requires (I < AlternativeSize) constexpr typename TAlternativeType<I>::Type& GetValue() & { checkf(HoldsAlternative<I>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast< TAlternativeType<I>::Type*>(&Value); } template <size_t I> requires (I < AlternativeSize) constexpr typename TAlternativeType<I>::Type& GetValue() & { checkf(HoldsAlternative<I>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast< TAlternativeType<I>::Type*>(&Value); }
template <size_t I> requires (I < AlternativeSize) constexpr typename TAlternativeType<I>::Type&& GetValue() && { checkf(HoldsAlternative<I>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast< TAlternativeType<I>::Type*>(&Value)); } template <size_t I> requires (I < AlternativeSize) constexpr typename TAlternativeType<I>::Type&& GetValue() && { checkf(HoldsAlternative<I>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return MoveTemp(*reinterpret_cast< TAlternativeType<I>::Type*>(&Value)); }
template <size_t I> requires (I < AlternativeSize) constexpr const typename TAlternativeType<I>::Type& GetValue() const& { checkf(HoldsAlternative<I>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast<const TAlternativeType<I>::Type*>(&Value); } template <size_t I> requires (I < AlternativeSize) constexpr const typename TAlternativeType<I>::Type& GetValue() const& { checkf(HoldsAlternative<I>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast<const TAlternativeType<I>::Type*>(&Value); }
@ -456,7 +453,7 @@ struct TVariant
if (GetIndex() == InValue.GetIndex()) if (GetIndex() == InValue.GetIndex())
{ {
FHelper::SwapFuncs[GetIndex()](GetData(), InValue.GetData()); FHelper::SwapFuncs[GetIndex()](&Value, &InValue.Value);
return; return;
} }