refactor(templates): optimize visit functions for TOptional, TVariant and TAny, add necessary constraints

This commit is contained in:
_Redstone_c_ 2022-03-23 17:49:30 +08:00
parent 2f55c84c32
commit 0c6bf06a56
3 changed files with 64 additions and 56 deletions

View File

@ -335,13 +335,23 @@ struct TAny
constexpr void* GetData() { return IsInline() ? &InlineValue : DynamicValue; } constexpr void* GetData() { return IsInline() ? &InlineValue : DynamicValue; }
constexpr const void* GetData() const { return IsInline() ? &InlineValue : DynamicValue; } constexpr const void* GetData() const { return IsInline() ? &InlineValue : DynamicValue; }
template <typename 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*>(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> 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 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()); }
template <typename 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*>(GetData()); }
template <typename 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*>(GetData())); }
template <typename T> constexpr T Get(T&& DefaultValue) && { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; } 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> constexpr T Get(T&& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; } 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())); }
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()); }
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
constexpr T& Get( T& DefaultValue) & { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
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& Get(const T& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
void Reset() void Reset()
{ {

View File

@ -9,12 +9,12 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
template <typename OptionalType> requires (TIsObject<OptionalType>::Value && !TIsArray<OptionalType>::Value&& TIsDestructible<OptionalType>::Value) template <typename OptionalType> requires TIsObject<OptionalType>::Value && (!TIsArray<OptionalType>::Value) && TIsDestructible<OptionalType>::Value
struct TOptional struct TOptional
{ {
public: public:
using Type = OptionalType; using ValueType = OptionalType;
constexpr TOptional() : bIsValid(false) { } constexpr TOptional() : bIsValid(false) { }
@ -61,7 +61,7 @@ public:
constexpr ~TOptional() constexpr ~TOptional()
{ {
Reset(); if constexpr (!TIsTriviallyDestructible<OptionalType>::Value) Reset();
} }
constexpr TOptional& operator=(const TOptional& InValue) constexpr TOptional& operator=(const TOptional& InValue)
@ -169,10 +169,13 @@ 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 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 *(OptionalType*)&Value; } constexpr void* GetData() { 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 MoveTemp(*(OptionalType*)&Value); } constexpr const void* GetData() const { return &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 *(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 MoveTemp(*(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 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 MoveTemp(*reinterpret_cast<const OptionalType*>(&Value)); }
constexpr const OptionalType* operator->() const { return &GetValue(); } constexpr const OptionalType* operator->() const { return &GetValue(); }
constexpr OptionalType* operator->() { return &GetValue(); } constexpr OptionalType* operator->() { return &GetValue(); }
@ -182,11 +185,8 @@ public:
constexpr const OptionalType& operator*() const& { return GetValue(); } constexpr const OptionalType& operator*() const& { return GetValue(); }
constexpr const OptionalType&& operator*() const&& { return GetValue(); } constexpr const OptionalType&& operator*() const&& { return GetValue(); }
template <typename T = OptionalType> constexpr OptionalType& Get( OptionalType& DefaultValue) & { return IsValid() ? GetValue() : DefaultValue; }
constexpr OptionalType Get(T&& DefaultValue) && { return IsValid() ? GetValue() : DefaultValue; } constexpr const OptionalType& Get(const OptionalType& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; }
template <typename T = OptionalType>
constexpr OptionalType Get(T&& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; }
constexpr void Reset() constexpr void Reset()
{ {

View File

@ -72,8 +72,7 @@ struct TVariantSelectedType<T>
template <typename T> template <typename T>
constexpr void VariantDestroy(void* InValue) constexpr void VariantDestroy(void* InValue)
{ {
if constexpr (!TIsDestructible<T>::Value) check_no_entry(); if constexpr (!TIsTriviallyDestructible<T>::Value)
else if constexpr (!TIsTriviallyDestructible<T>::Value)
{ {
typedef T DestructOptionalType; typedef T DestructOptionalType;
reinterpret_cast<T*>(InValue)->DestructOptionalType::~DestructOptionalType(); reinterpret_cast<T*>(InValue)->DestructOptionalType::~DestructOptionalType();
@ -153,8 +152,7 @@ struct TVariantHelper
template <typename R, typename F, typename T> template <typename R, typename F, typename T>
constexpr R VariantVisitLValue(F&& Func, void* Arg) constexpr R VariantVisitLValue(F&& Func, void* Arg)
{ {
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry(); if constexpr(TIsVoid<R>::Value) Invoke(Forward<F>(Func), *reinterpret_cast<T*>(Arg));
else if constexpr(TIsVoid<R>::Value) Invoke(Forward<F>(Func), *reinterpret_cast<T*>(Arg));
else return InvokeResult<R>(Forward<F>(Func), *reinterpret_cast<T*>(Arg)); else return InvokeResult<R>(Forward<F>(Func), *reinterpret_cast<T*>(Arg));
} }
@ -164,8 +162,7 @@ using FVariantVisitLValueFunc = R(*)(F&&, void*);
template <typename R, typename F, typename T> template <typename R, typename F, typename T>
constexpr R VariantVisitRValue(F&& Func, void* Arg) constexpr R VariantVisitRValue(F&& Func, void* Arg)
{ {
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry(); if constexpr (TIsVoid<R>::Value) Invoke(Forward<F>(Func), MoveTemp(*reinterpret_cast<T*>(Arg)));
else if constexpr (TIsVoid<R>::Value) Invoke(Forward<F>(Func), MoveTemp(*reinterpret_cast<T*>(Arg)));
else return InvokeResult<R>(Forward<F>(Func), MoveTemp(*reinterpret_cast<T*>(Arg))); else return InvokeResult<R>(Forward<F>(Func), MoveTemp(*reinterpret_cast<T*>(Arg)));
} }
@ -175,8 +172,7 @@ using FVariantVisitRValueFunc = R(*)(F&&, void*);
template <typename R, typename F, typename T> template <typename R, typename F, typename T>
constexpr R VariantVisitConstLValue(F&& Func, const void* Arg) constexpr R VariantVisitConstLValue(F&& Func, const void* Arg)
{ {
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry(); if constexpr (TIsVoid<R>::Value) Invoke(Forward<F>(Func), *reinterpret_cast<const T*>(Arg));
else if constexpr (TIsVoid<R>::Value) Invoke(Forward<F>(Func), *reinterpret_cast<const T*>(Arg));
else return InvokeResult<R>(Forward<F>(Func), *reinterpret_cast<const T*>(Arg)); else return InvokeResult<R>(Forward<F>(Func), *reinterpret_cast<const T*>(Arg));
} }
@ -186,8 +182,7 @@ using FVariantVisitConstLValueFunc = R(*)(F&&, const void*);
template <typename R, typename F, typename T> template <typename R, typename F, typename T>
constexpr R VariantVisitConstRValue(F&& Func, const void* Arg) constexpr R VariantVisitConstRValue(F&& Func, const void* Arg)
{ {
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry(); if constexpr (TIsVoid<R>::Value) Invoke(Forward<F>(Func), MoveTemp(*reinterpret_cast<const T*>(Arg)));
else if constexpr (TIsVoid<R>::Value) Invoke(Forward<F>(Func), MoveTemp(*reinterpret_cast<const T*>(Arg)));
else return InvokeResult<R>(Forward<F>(Func), MoveTemp(*reinterpret_cast<const T*>(Arg))); else return InvokeResult<R>(Forward<F>(Func), MoveTemp(*reinterpret_cast<const T*>(Arg)));
} }
@ -208,7 +203,7 @@ NAMESPACE_PRIVATE_END
template <typename... Types> requires (true && ... && (TIsObject<Types>::Value && !TIsArray<Types>::Value && TIsDestructible<Types>::Value)) template <typename... Types> requires (true && ... && (TIsObject<Types>::Value && !TIsArray<Types>::Value && TIsDestructible<Types>::Value))
struct TVariant struct TVariant
{ {
struct FAlternativeSize : TConstant<size_t, sizeof...(Types)> { }; static constexpr size_t AlternativeSize = sizeof...(Types);
template <size_t I> struct TAlternativeType : NAMESPACE_PRIVATE::TVariantAlternativeType<I, Types...> { }; template <size_t I> struct TAlternativeType : NAMESPACE_PRIVATE::TVariantAlternativeType<I, Types...> { };
template <typename T> struct TAlternativeIndex : NAMESPACE_PRIVATE::TVariantAlternativeIndex<T, Types...> { }; template <typename T> struct TAlternativeIndex : NAMESPACE_PRIVATE::TVariantAlternativeIndex<T, Types...> { };
@ -229,7 +224,7 @@ struct TVariant
if (GetIndex() != INDEX_NONE) FHelper::MoveConstructFuncs[InValue.GetIndex()](&Value, &InValue.Value); if (GetIndex() != INDEX_NONE) FHelper::MoveConstructFuncs[InValue.GetIndex()](&Value, &InValue.Value);
} }
template <size_t I, typename... ArgTypes> requires (I < FAlternativeSize::Value) template <size_t I, typename... ArgTypes> requires (I < AlternativeSize)
&& TIsConstructible<typename TAlternativeType<I>::Type, ArgTypes...>::Value && TIsConstructible<typename TAlternativeType<I>::Type, ArgTypes...>::Value
constexpr explicit TVariant(TInPlaceIndex<I>, ArgTypes&&... Args) constexpr explicit TVariant(TInPlaceIndex<I>, ArgTypes&&... Args)
: TypeIndex(I) : TypeIndex(I)
@ -252,7 +247,7 @@ struct TVariant
constexpr ~TVariant() constexpr ~TVariant()
{ {
Reset(); if constexpr (!(true && ... && TIsTriviallyDestructible<Types>::Value)) Reset();
} }
constexpr TVariant& operator=(const TVariant& InValue) constexpr TVariant& operator=(const TVariant& InValue)
@ -313,7 +308,7 @@ struct TVariant
return *this; return *this;
} }
template <size_t I, typename... ArgTypes> requires (I < FAlternativeSize::Value) template <size_t I, typename... ArgTypes> requires (I < AlternativeSize)
&& TIsConstructible<typename TAlternativeType<I>::Type, ArgTypes...>::Value && TIsConstructible<typename TAlternativeType<I>::Type, ArgTypes...>::Value
constexpr typename TAlternativeType<I>::Type& Emplace(ArgTypes&&... Args) constexpr typename TAlternativeType<I>::Type& Emplace(ArgTypes&&... Args)
{ {
@ -343,23 +338,23 @@ struct TVariant
constexpr void* GetData() { return &Value; } constexpr void* GetData() { return &Value; }
constexpr const void* GetData() const { return &Value; } constexpr const void* GetData() const { return &Value; }
template <size_t I> 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> 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> 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); }
template <size_t I> 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 MoveTemp(*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 MoveTemp(*reinterpret_cast<const TAlternativeType<I>::Type*>(&Value)); }
template <typename T> constexpr T& GetValue() & { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast< T*>(&Value); } template <typename T> requires (TAlternativeIndex<T>::Value != INDEX_NONE) constexpr T& GetValue() & { checkf(HoldsAlternative<T>(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast< T*>(&Value); }
template <typename T> constexpr T&& GetValue() && { checkf(HoldsAlternative<T>(), 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< T*>(&Value)); } template <typename T> requires (TAlternativeIndex<T>::Value != INDEX_NONE) constexpr T&& GetValue() && { checkf(HoldsAlternative<T>(), 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< T*>(&Value)); }
template <typename T> constexpr const T& GetValue() const& { checkf(HoldsAlternative<T>(), 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 T*>(&Value); } template <typename T> requires (TAlternativeIndex<T>::Value != INDEX_NONE) constexpr const T& GetValue() const& { checkf(HoldsAlternative<T>(), 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 T*>(&Value); }
template <typename T> constexpr const T&& GetValue() const&& { checkf(HoldsAlternative<T>(), 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<const T*>(&Value)); } template <typename T> requires (TAlternativeIndex<T>::Value != INDEX_NONE) constexpr const T&& GetValue() const&& { checkf(HoldsAlternative<T>(), 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<const T*>(&Value)); }
template <size_t I> constexpr typename TAlternativeType<I>::Type Get(typename TAlternativeType<I>::Type&& DefaultValue) && { return HoldsAlternative<I>() ? GetValue<I>() : DefaultValue; } template <size_t I> requires (I < AlternativeSize) constexpr typename TAlternativeType<I>::Type& Get(typename TAlternativeType<I>::Type& DefaultValue)& { return HoldsAlternative<I>() ? GetValue<I>() : DefaultValue; }
template <size_t I> constexpr typename TAlternativeType<I>::Type Get(typename TAlternativeType<I>::Type&& DefaultValue) const& { return HoldsAlternative<I>() ? GetValue<I>() : DefaultValue; } template <size_t I> requires (I < AlternativeSize) constexpr const typename TAlternativeType<I>::Type& Get(const typename TAlternativeType<I>::Type& DefaultValue) const& { return HoldsAlternative<I>() ? GetValue<I>() : DefaultValue; }
template <typename T> constexpr T Get(T&& DefaultValue) && { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; } template <typename T> requires (TAlternativeIndex<T>::Value != INDEX_NONE) constexpr T& Get(T& DefaultValue)& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
template <typename T> constexpr T Get(T&& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; } template <typename T> requires (TAlternativeIndex<T>::Value != INDEX_NONE) constexpr const T& Get(const T& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
template <typename F> template <typename F> requires (true && ... && TIsInvocable<F, Types>::Value)
constexpr auto Visit(F&& Func) & constexpr auto Visit(F&& Func) &
{ {
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type; using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
@ -367,7 +362,7 @@ struct TVariant
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitLValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitLValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename F> template <typename F> requires (true && ... && TIsInvocable<F, Types>::Value)
constexpr auto Visit(F&& Func) && constexpr auto Visit(F&& Func) &&
{ {
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type; using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
@ -375,7 +370,7 @@ struct TVariant
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitRValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitRValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename F> template <typename F> requires (true && ... && TIsInvocable<F, Types>::Value)
constexpr auto Visit(F&& Func) const& constexpr auto Visit(F&& Func) const&
{ {
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type; using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
@ -383,7 +378,7 @@ struct TVariant
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitConstLValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitConstLValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename F> template <typename F> requires (true && ... && TIsInvocable<F, Types>::Value)
constexpr auto Visit(F&& Func) const&& constexpr auto Visit(F&& Func) const&&
{ {
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type; using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
@ -391,28 +386,28 @@ struct TVariant
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitConstRValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitConstRValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename R, typename F> template <typename R, typename F> requires (true && ... && TIsInvocableResult<R, F, Types>::Value)
constexpr R Visit(F&& Func) & constexpr R Visit(F&& Func) &
{ {
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid()."); checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
return R(NAMESPACE_PRIVATE::TVariantVisitHelper<R, F, Types...>::VisitLValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return R(NAMESPACE_PRIVATE::TVariantVisitHelper<R, F, Types...>::VisitLValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename R, typename F> template <typename R, typename F> requires (true && ... && TIsInvocableResult<R, F, Types>::Value)
constexpr R Visit(F&& Func) && constexpr R Visit(F&& Func) &&
{ {
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid()."); checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
return R(NAMESPACE_PRIVATE::TVariantVisitHelper<R, F, Types...>::VisitRValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return R(NAMESPACE_PRIVATE::TVariantVisitHelper<R, F, Types...>::VisitRValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename R, typename F> template <typename R, typename F> requires (true && ... && TIsInvocableResult<R, F, Types>::Value)
constexpr R Visit(F&& Func) const& constexpr R Visit(F&& Func) const&
{ {
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid()."); checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
return R(NAMESPACE_PRIVATE::TVariantVisitHelper<R, F, Types...>::VisitConstLValueFuncs[GetIndex()](Forward<F>(Func), &Value)); return R(NAMESPACE_PRIVATE::TVariantVisitHelper<R, F, Types...>::VisitConstLValueFuncs[GetIndex()](Forward<F>(Func), &Value));
} }
template <typename R, typename F> template <typename R, typename F> requires (true && ... && TIsInvocableResult<R, F, Types>::Value)
constexpr R Visit(F&& Func) const&& constexpr R Visit(F&& Func) const&&
{ {
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid()."); checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
@ -423,7 +418,10 @@ struct TVariant
{ {
if (GetIndex() == INDEX_NONE) return; if (GetIndex() == INDEX_NONE) return;
if constexpr (!(true && ... && TIsTriviallyDestructible<Types>::Value))
{
FHelper::DestroyFuncs[GetIndex()](&Value); FHelper::DestroyFuncs[GetIndex()](&Value);
}
TypeIndex = INDEX_NONE; TypeIndex = INDEX_NONE;
} }