#pragma once #include "CoreTypes.h" #include "Templates/Invoke.h" #include "Templates/Utility.h" #include "Templates/TypeHash.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" #include "Miscellaneous/TypeInfo.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_PRIVATE_BEGIN template struct TVariantAlternativeIndex; template struct TVariantAlternativeIndex : TConstant::Value ? 0 : (TVariantAlternativeIndex::Value == INDEX_NONE ? INDEX_NONE : TVariantAlternativeIndex::Value + 1)> { }; template struct TVariantAlternativeIndex : TConstant { }; template struct TVariantAlternativeType; template struct TVariantAlternativeType { static_assert(I < sizeof...(Types) + 1, "Variant type index is invalid"); using Type = TVariantAlternativeType::Type; }; template struct TVariantAlternativeType<0, T, Types...> { using Type = T; }; template <> struct TVariantAlternativeType<0> { }; template struct TVariantSelectedType; template struct TVariantSelectedType { using TypeAlternativeA = typename TConditional::Value, U, void>::Type; using TypeAlternativeB = typename TVariantSelectedType::Type; using Type = typename TConditional::Type, void>::Value, TypeAlternativeB, typename TConditional::Type, void>::Value, TypeAlternativeA, typename TConditional::Type, typename TRemoveCVRef::Type>::Value, TypeAlternativeB, TypeAlternativeA>::Type>::Type>::Type; // 0 - Type not found // 1 - Same type found // 2 - Multiple types found // 3 - The type found static constexpr uint8 Flag = TIsSame::Type, void>::Value ? 0 : TIsSame::Type, typename TRemoveCVRef::Type>::Value ? 2 : TIsSame::Type, typename TRemoveCVRef< T>::Type>::Value ? 1 : !TIsSame::Type, void>::Value && !TIsSame::Value ? 2 : 3; static constexpr bool Value = Flag & 1; }; template struct TVariantSelectedType { static constexpr uint8 Flag = 0; using Type = void; }; template constexpr R VariantVisitLValue(F&& Func, void* Arg) { if constexpr(TIsVoid::Value) Invoke(Forward(Func), *reinterpret_cast(Arg)); else return InvokeResult(Forward(Func), *reinterpret_cast(Arg)); } template using FVariantVisitLValueFunc = R(*)(F&&, void*); template constexpr R VariantVisitRValue(F&& Func, void* Arg) { if constexpr (TIsVoid::Value) Invoke(Forward(Func), MoveTemp(*reinterpret_cast(Arg))); else return InvokeResult(Forward(Func), MoveTemp(*reinterpret_cast(Arg))); } template using FVariantVisitRValueFunc = R(*)(F&&, void*); template constexpr R VariantVisitConstLValue(F&& Func, const void* Arg) { if constexpr (TIsVoid::Value) Invoke(Forward(Func), *reinterpret_cast(Arg)); else return InvokeResult(Forward(Func), *reinterpret_cast(Arg)); } template using FVariantVisitConstLValueFunc = R(*)(F&&, const void*); template constexpr R VariantVisitConstRValue(F&& Func, const void* Arg) { if constexpr (TIsVoid::Value) Invoke(Forward(Func), MoveTemp(*reinterpret_cast(Arg))); else return InvokeResult(Forward(Func), MoveTemp(*reinterpret_cast(Arg))); } template using FVariantVisitConstRValueFunc = R(*)(F&&, const void*); template struct TVariantVisitHelper { static constexpr FVariantVisitLValueFunc VisitLValueFuncs[] = { VariantVisitLValue... }; static constexpr FVariantVisitRValueFunc VisitRValueFuncs[] = { VariantVisitRValue... }; static constexpr FVariantVisitConstLValueFunc VisitConstLValueFuncs[] = { VariantVisitConstLValue... }; static constexpr FVariantVisitConstRValueFunc VisitConstRValueFuncs[] = { VariantVisitConstRValue... }; }; NAMESPACE_PRIVATE_END template requires (true && ... && (TIsObject::Value && !TIsArray::Value && TIsDestructible::Value)) && (sizeof...(Types) < 0xFF) struct TVariant { static constexpr size_t AlternativeSize = sizeof...(Types); template struct TAlternativeType : NAMESPACE_PRIVATE::TVariantAlternativeType { }; template struct TAlternativeIndex : NAMESPACE_PRIVATE::TVariantAlternativeIndex { }; constexpr TVariant() : TypeIndex(0xFF) { }; constexpr TVariant(FInvalid) : TVariant() { }; constexpr TVariant(const TVariant& InValue) requires (true && ... && TIsCopyConstructible::Value) : TypeIndex(static_cast(InValue.GetIndex())) { if (IsValid()) TypeInfos[InValue.GetIndex()]->CopyConstruct(&Value, &InValue.Value); } constexpr TVariant(TVariant&& InValue) requires (true && ... && TIsMoveConstructible::Value) : TypeIndex(static_cast(InValue.GetIndex())) { if (IsValid()) TypeInfos[InValue.GetIndex()]->MoveConstruct(&Value, &InValue.Value); } template requires (I < AlternativeSize) && TIsConstructible::Type, ArgTypes...>::Value constexpr explicit TVariant(TInPlaceIndex, ArgTypes&&... Args) : TypeIndex(I) { using SelectedType = typename TAlternativeType::Type; new(&Value) SelectedType(Forward(Args)...); } template requires (TAlternativeIndex::Value != INDEX_NONE) && TIsConstructible::Value>::Type, ArgTypes...>::Value constexpr explicit TVariant(TInPlaceType, ArgTypes&&... Args) : TVariant(InPlaceIndex::Value>, Forward(Args)...) { } template requires NAMESPACE_PRIVATE::TVariantSelectedType::Type, Types...>::Value && (!TIsTInPlaceType::Type>::Value) && (!TIsTInPlaceIndex::Type>::Value) && (!TIsSame::Type, TVariant>::Value) constexpr TVariant(T&& InValue) : TVariant(InPlaceType::Type, Types...>::Type>, Forward(InValue)) { } constexpr ~TVariant() { if constexpr (!(true && ... && TIsTriviallyDestructible::Value)) Reset(); } constexpr TVariant& operator=(const TVariant& InValue) requires (true && ... && (TIsCopyConstructible::Value && TIsCopyAssignable::Value)) { if (&InValue == this) return *this; if (!InValue.IsValid()) { Reset(); return *this; } if (GetIndex() == InValue.GetIndex()) TypeInfos[InValue.GetIndex()]->CopyAssign(&Value, &InValue.Value); else { Reset(); TypeInfos[InValue.GetIndex()]->CopyConstruct(&Value, &InValue.Value); TypeIndex = static_cast(InValue.GetIndex()); } return *this; } constexpr TVariant& operator=(TVariant&& InValue) requires (true && ... && (TIsMoveConstructible::Value && TIsMoveAssignable::Value)) { if (&InValue == this) return *this; if (!InValue.IsValid()) { Reset(); return *this; } if (GetIndex() == InValue.GetIndex()) TypeInfos[InValue.GetIndex()]->MoveAssign(&Value, &InValue.Value); else { Reset(); TypeInfos[InValue.GetIndex()]->MoveConstruct(&Value, &InValue.Value); TypeIndex = static_cast(InValue.GetIndex()); } return *this; } template requires NAMESPACE_PRIVATE::TVariantSelectedType::Type, Types...>::Value constexpr TVariant& operator=(T&& InValue) { using SelectedType = typename NAMESPACE_PRIVATE::TVariantSelectedType::Type, Types...>::Type; if (GetIndex() == TAlternativeIndex::Value) GetValue() = Forward(InValue); else { Reset(); new(&Value) SelectedType(Forward(InValue)); TypeIndex = TAlternativeIndex::Value; } return *this; } template requires (I < AlternativeSize) && TIsConstructible::Type, ArgTypes...>::Value constexpr typename TAlternativeType::Type& Emplace(ArgTypes&&... Args) { Reset(); using SelectedType = typename TAlternativeType::Type; SelectedType* Result = new(&Value) SelectedType(Forward(Args)...); TypeIndex = I; return *Result; } template requires (TAlternativeIndex::Value != INDEX_NONE) && TIsConstructible::Value>::Type, ArgTypes...>::Value constexpr T& Emplace(ArgTypes&&... Args) { return Emplace::Value>(Forward(Args)...); } constexpr const FTypeInfo& GetTypeInfo() const { return IsValid() ? *TypeInfos[GetIndex()] : Typeid(void); } constexpr size_t GetIndex() const { return TypeIndex != 0xFF ? TypeIndex : INDEX_NONE; } constexpr bool IsValid() const { return TypeIndex != 0xFF; } constexpr explicit operator bool() const { return TypeIndex != 0xFF; } template constexpr bool HoldsAlternative() const { return IsValid() ? GetIndex() == I : false; } template constexpr bool HoldsAlternative() const { return IsValid() ? GetIndex() == TAlternativeIndex::Value : false; } template requires (I < AlternativeSize) constexpr typename TAlternativeType::Type& GetValue() & { checkf(HoldsAlternative(), 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::Type*>(&Value); } template requires (I < AlternativeSize) constexpr typename TAlternativeType::Type&& GetValue() && { checkf(HoldsAlternative(), 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::Type*>(&Value)); } template requires (I < AlternativeSize) constexpr const typename TAlternativeType::Type& GetValue() const& { checkf(HoldsAlternative(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast::Type*>(&Value); } template requires (I < AlternativeSize) constexpr const typename TAlternativeType::Type&& GetValue() const&& { checkf(HoldsAlternative(), 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::Type*>(&Value)); } template requires (TAlternativeIndex::Value != INDEX_NONE) constexpr T& GetValue() & { checkf(HoldsAlternative(), 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 requires (TAlternativeIndex::Value != INDEX_NONE) constexpr T&& GetValue() && { checkf(HoldsAlternative(), 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 requires (TAlternativeIndex::Value != INDEX_NONE) constexpr const T& GetValue() const& { checkf(HoldsAlternative(), TEXT("It is an error to call GetValue() on an wrong TVariant. Please either check HoldsAlternative() or use Get(DefaultValue) instead.")); return *reinterpret_cast(&Value); } template requires (TAlternativeIndex::Value != INDEX_NONE) constexpr const T&& GetValue() const&& { checkf(HoldsAlternative(), 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(&Value)); } template requires (I < AlternativeSize) constexpr typename TAlternativeType::Type& Get( typename TAlternativeType::Type& DefaultValue) & { return HoldsAlternative() ? GetValue() : DefaultValue; } template requires (I < AlternativeSize) constexpr const typename TAlternativeType::Type& Get(const typename TAlternativeType::Type& DefaultValue) const& { return HoldsAlternative() ? GetValue() : DefaultValue; } template requires (TAlternativeIndex::Value != INDEX_NONE) constexpr T& Get(T& DefaultValue)& { return HoldsAlternative() ? GetValue() : DefaultValue; } template requires (TAlternativeIndex::Value != INDEX_NONE) constexpr const T& Get(const T& DefaultValue) const& { return HoldsAlternative() ? GetValue() : DefaultValue; } template requires (true && ... && TIsInvocable::Value) constexpr auto Visit(F&& Func) & { using ReturnType = typename TCommonType::Type...>::Type; checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitLValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocable::Value) constexpr auto Visit(F&& Func) && { using ReturnType = typename TCommonType::Type...>::Type; checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitRValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocable::Value) constexpr auto Visit(F&& Func) const& { using ReturnType = typename TCommonType::Type...>::Type; checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitConstLValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocable::Value) constexpr auto Visit(F&& Func) const&& { using ReturnType = typename TCommonType::Type...>::Type; checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitConstRValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocableResult::Value) constexpr R Visit(F&& Func) & { checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return R(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitLValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocableResult::Value) constexpr R Visit(F&& Func) && { checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return R(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitRValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocableResult::Value) constexpr R Visit(F&& Func) const& { checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return R(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitConstLValueFuncs[GetIndex()](Forward(Func), &Value)); } template requires (true && ... && TIsInvocableResult::Value) constexpr R Visit(F&& Func) const&& { checkf(IsValid(), TEXT("It is an error to call Visit() on an wrong TVariant. Please either check IsValid().")); return R(NAMESPACE_PRIVATE::TVariantVisitHelper::VisitConstRValueFuncs[GetIndex()](Forward(Func), &Value)); } constexpr void Reset() { if (GetIndex() == INDEX_NONE) return; if constexpr (!(true && ... && TIsTriviallyDestructible::Value)) { TypeInfos[GetIndex()]->Destroy(&Value); } TypeIndex = static_cast(INDEX_NONE); } constexpr size_t GetTypeHash() const requires (true && ... && CHashable) { if (!IsValid()) return 114514; return HashCombine(NAMESPACE_REDCRAFT::GetTypeHash(GetIndex()), TypeInfos[GetIndex()]->HashItem(&Value)); } constexpr void Swap(TVariant& InValue) requires (true && ... && (TIsMoveConstructible::Value && TIsSwappable::Value)) { if (!IsValid() && !InValue.IsValid()) return; if (IsValid() && !InValue.IsValid()) { InValue = MoveTemp(*this); Reset(); return; } if (InValue.IsValid() && !IsValid()) { *this = MoveTemp(InValue); InValue.Reset(); return; } if (GetIndex() == InValue.GetIndex()) { TypeInfos[GetIndex()]->SwapItem(&Value, &InValue.Value); return; } TVariant Temp = MoveTemp(*this); *this = MoveTemp(InValue); InValue = MoveTemp(Temp); } private: static constexpr const FTypeInfo* TypeInfos[] = { &Typeid(Types)... }; TAlignedUnion<1, Types...>::Type Value; uint8 TypeIndex; friend constexpr bool operator==(const TVariant& LHS, const TVariant& RHS) requires (true && ... && CEqualityComparable) { if (LHS.GetIndex() != RHS.GetIndex()) return false; if (LHS.IsValid() == false) return true; return TypeInfos[LHS.GetIndex()]->EqualityCompare(&LHS.Value, &RHS.Value); } friend constexpr partial_ordering operator<=>(const TVariant& LHS, const TVariant& RHS) requires (true && ... && CSynthThreeWayComparable) { if (LHS.GetIndex() != RHS.GetIndex()) return partial_ordering::unordered; if (LHS.IsValid() == false) return partial_ordering::equivalent; return TypeInfos[LHS.GetIndex()]->SynthThreeWayCompare(&LHS.Value, &RHS.Value); } }; template requires (!TIsSame>::Value) && CEqualityComparable constexpr bool operator==(const TVariant& LHS, const T& RHS) { return LHS.template HoldsAlternative() ? LHS.template GetValue() == RHS : false; } template constexpr bool operator==(const TVariant& LHS, FInvalid) { return !LHS.IsValid(); } template struct TIsTVariant : FFalse { }; template struct TIsTVariant> : FTrue { }; template requires TIsTVariant::Type>::Value struct TVariantAlternativeSize : TConstant { }; template requires TIsTVariant::Type>::Value struct TVariantAlternativeType { using Type = typename TCopyCV::Type, typename TRemoveCVRef::Type::template TAlternativeType::Type>::Type; }; template requires TIsTVariant::Type>::Value struct TVariantAlternativeIndex : VariantType::template TAlternativeIndex { }; NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END