feat(templates): add TVariant and the corresponding testing
This commit is contained in:
parent
8188d29a58
commit
d8ae39b980
@ -2,6 +2,10 @@
|
||||
#include "Miscellaneous/AssertionMacros.h"
|
||||
#include "Templates/Templates.h"
|
||||
|
||||
#pragma warning(disable : 4930)
|
||||
#pragma warning(disable : 4101)
|
||||
#pragma warning(disable : 4244)
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
@ -12,6 +16,7 @@ void TestTemplates()
|
||||
TestReferenceWrapper();
|
||||
TestCompare();
|
||||
TestOptional();
|
||||
TestVariant();
|
||||
TestMiscellaneous();
|
||||
}
|
||||
|
||||
@ -270,6 +275,172 @@ void TestOptional()
|
||||
TempZ = FTracker();
|
||||
}
|
||||
|
||||
void TestVariant()
|
||||
{
|
||||
TVariant<int32> TempA;
|
||||
TVariant<int32> TempB(Invalid);
|
||||
TVariant<int32> TempC(InPlaceType<int32>, 0);
|
||||
TVariant<int32> TempD(0);
|
||||
TVariant<int32> TempE(0l);
|
||||
TVariant<int32> TempF(0.0);
|
||||
TVariant<int32> TempG(TempA);
|
||||
TVariant<int32> TempH(TempD);
|
||||
TVariant<int32> TempI(TVariant<int32>(0));
|
||||
TVariant<int32> TempJ(TVariant<int32>(Invalid));
|
||||
|
||||
TVariant<int32> TempK, TempL, TempM, TempN;
|
||||
TempK = TempA;
|
||||
TempL = TempD;
|
||||
TempM = TVariant<int32>(0);
|
||||
TempN = TVariant<int32>(Invalid);
|
||||
|
||||
TempL = 303;
|
||||
TempM = 404;
|
||||
|
||||
TVariant<int32> TempO;
|
||||
TempO.Emplace<int32>(202);
|
||||
TempO.Emplace<0>(404);
|
||||
|
||||
always_check(TempO);
|
||||
always_check(TempO.IsValid());
|
||||
|
||||
always_check(TempO == 404);
|
||||
always_check(TempO.GetValue<int32>() == 404);
|
||||
always_check(TempO.Get<0>(500) == 404);
|
||||
|
||||
TempO.Reset();
|
||||
always_check(TempO == TempO);
|
||||
always_check(!(TempO != TempO));
|
||||
always_check(TempO.Get<int32>(500) == 500);
|
||||
|
||||
int32 TempP = 200;
|
||||
TempO = TempP;
|
||||
TempO = 300;
|
||||
|
||||
always_check(TempO != TempA);
|
||||
always_check(TempO != TempD);
|
||||
always_check(TempO == TempO);
|
||||
always_check(TempO == 300);
|
||||
always_check(300 == TempO);
|
||||
|
||||
Swap(TempD, TempA);
|
||||
|
||||
int16 TempQ = 1024;
|
||||
TVariant<int16, int32> TempR = TempQ;
|
||||
|
||||
TVariant<int16, int32> TempS(InPlaceType<int32>, TempQ);
|
||||
TVariant<int16, int32> TempT(TempQ);
|
||||
TVariant<int16, int32> TempU(TempR);
|
||||
TVariant<int16, int32> TempV(TVariant<int16, int32>(2048));
|
||||
|
||||
TVariant<int16, int32> TempW, TempX, TempY;
|
||||
TempW = TempQ;
|
||||
TempX = TempR;
|
||||
TempY = TVariant<int16, int32>(2048);
|
||||
|
||||
Swap(TempW, TempX);
|
||||
Swap(TempW, TempX);
|
||||
|
||||
struct FTracker
|
||||
{
|
||||
FTracker() { }
|
||||
FTracker(const FTracker& InValue) { always_check_no_entry(); }
|
||||
FTracker(FTracker&& InValue) { }
|
||||
FTracker& operator=(const FTracker& InValue) { always_check_no_entry(); return *this; }
|
||||
FTracker& operator=(FTracker&& InValue) { return *this; }
|
||||
};
|
||||
|
||||
TVariant<FTracker> TempZ(Invalid);
|
||||
TempZ = TVariant<FTracker>();
|
||||
TempZ = FTracker();
|
||||
|
||||
always_check((TIsSame<int32, TVariantAlternativeType<0, TVariant<int32, float>>::Type>::Value));
|
||||
always_check((TIsSame<float, TVariantAlternativeType<1, TVariant<int32, float>>::Type>::Value));
|
||||
always_check((TIsSame<const int32, TVariantAlternativeType<0, const TVariant<int32, float>>::Type>::Value));
|
||||
|
||||
always_check((TVariantAlternativeIndex<int32, TVariant<int32, float>>::Value == 0));
|
||||
always_check((TVariantAlternativeIndex<float, TVariant<int32, float>>::Value == 1));
|
||||
|
||||
bool bIsConst;
|
||||
bool bIsLValue;
|
||||
bool bIsRValue;
|
||||
|
||||
auto TestQualifiers = [&bIsConst, &bIsLValue, &bIsRValue](auto&& Arg) -> int32
|
||||
{
|
||||
using T = decltype(Arg);
|
||||
always_check(Arg == 10);
|
||||
always_check(TIsConst<typename TRemoveReference<T>::Type>::Value == bIsConst);
|
||||
always_check(TIsLValueReference<T>::Value == bIsLValue);
|
||||
always_check(TIsRValueReference<T>::Value == bIsRValue);
|
||||
return 0;
|
||||
};
|
||||
|
||||
bIsConst = false;
|
||||
bIsLValue = true;
|
||||
bIsRValue = false;
|
||||
|
||||
TVariant<int32> TempLA = 10;
|
||||
auto ReturnLA = TempLA.Visit(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnLA)>::Value));
|
||||
|
||||
bIsConst = true;
|
||||
bIsLValue = true;
|
||||
bIsRValue = false;
|
||||
|
||||
const TVariant<int32> TempLB = TempLA;
|
||||
auto ReturnLB = TempLB.Visit(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnLB)>::Value));
|
||||
|
||||
bIsConst = false;
|
||||
bIsLValue = false;
|
||||
bIsRValue = true;
|
||||
|
||||
TVariant<int32> TempRA = 10;
|
||||
auto ReturnRA = MoveTemp(TempRA).Visit(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnRA)>::Value));
|
||||
|
||||
bIsConst = true;
|
||||
bIsLValue = false;
|
||||
bIsRValue = true;
|
||||
|
||||
const TVariant<int32> TempRB = TempLA;
|
||||
auto ReturnRB = MoveTemp(TempRB).Visit(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnRB)>::Value));
|
||||
|
||||
bIsConst = false;
|
||||
bIsLValue = true;
|
||||
bIsRValue = false;
|
||||
|
||||
TVariant<int32> TempLC = 10;
|
||||
auto ReturnLC = TempLC.Visit<int32>(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnLC)>::Value));
|
||||
|
||||
bIsConst = true;
|
||||
bIsLValue = true;
|
||||
bIsRValue = false;
|
||||
|
||||
const TVariant<int32> TempLD = TempLC;
|
||||
auto ReturnLD = TempLD.Visit<int32>(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnLD)>::Value));
|
||||
|
||||
bIsConst = false;
|
||||
bIsLValue = false;
|
||||
bIsRValue = true;
|
||||
|
||||
TVariant<int32> TempRC = 10;
|
||||
auto ReturnRC = MoveTemp(TempRC).Visit<int32>(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnRC)>::Value));
|
||||
|
||||
bIsConst = true;
|
||||
bIsLValue = false;
|
||||
bIsRValue = true;
|
||||
|
||||
const TVariant<int32> TempRD = TempLC;
|
||||
auto ReturnRD = MoveTemp(TempRD).Visit<int32>(TestQualifiers);
|
||||
always_check((TIsSame<int32, decltype(ReturnRD)>::Value));
|
||||
|
||||
}
|
||||
|
||||
NAMESPACE_UNNAMED_BEGIN
|
||||
|
||||
template <typename T>
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "TypeTraits/HelperClasses.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
@ -24,6 +25,12 @@ template <typename T> inline constexpr TInPlaceType<T> InPlaceType{};
|
||||
template <size_t I> struct TInPlaceIndex { explicit TInPlaceIndex() = default; };
|
||||
template <size_t I> inline constexpr TInPlaceIndex<I> InPlaceIndex{};
|
||||
|
||||
template <typename T> struct TIsInPlaceTypeSpecialization : FFalse { };
|
||||
template <typename T> struct TIsInPlaceTypeSpecialization<TInPlaceType<T>> : FTrue { };
|
||||
|
||||
template <typename T> struct TIsInPlaceIndexSpecialization : FFalse { };
|
||||
template <size_t I> struct TIsInPlaceIndexSpecialization<TInPlaceIndex<I>> : FTrue { };
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
||||
|
@ -9,3 +9,4 @@
|
||||
#include "Templates/ReferenceWrapper.h"
|
||||
#include "Templates/Compare.h"
|
||||
#include "Templates/Optional.h"
|
||||
#include "Templates/Variant.h"
|
||||
|
@ -26,10 +26,6 @@ template <typename T>
|
||||
constexpr typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj)
|
||||
{
|
||||
typedef typename TRemoveReference<T>::Type CastType;
|
||||
|
||||
static_assert(TIsLValueReference<T>::Value, "MoveTemp called on an rvalue.");
|
||||
static_assert(!TIsConst<CastType>::Value, "MoveTemp called on a const object.");
|
||||
|
||||
return (CastType&&)Obj;
|
||||
}
|
||||
|
||||
|
526
Redcraft.Utility/Source/Public/Templates/Variant.h
Normal file
526
Redcraft.Utility/Source/Public/Templates/Variant.h
Normal file
@ -0,0 +1,526 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Templates/Invoke.h"
|
||||
#include "Templates/Utility.h"
|
||||
#include "Templates/Placeholders.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
|
||||
template <typename T, typename... Types>
|
||||
struct TVariantAlternativeIndex;
|
||||
|
||||
template <typename T, typename U, typename... Types>
|
||||
struct TVariantAlternativeIndex<T, U, Types...>
|
||||
: TConstant<size_t, TIsSame<T, U>::Value ? 0 : (TVariantAlternativeIndex<T, Types...>::Value == static_cast<size_t>(INDEX_NONE)
|
||||
? static_cast<size_t>(INDEX_NONE) : TVariantAlternativeIndex<T, Types...>::Value + 1)>
|
||||
{ };
|
||||
|
||||
template <typename T>
|
||||
struct TVariantAlternativeIndex<T> : TConstant<size_t, static_cast<size_t>(INDEX_NONE)> { };
|
||||
|
||||
template <size_t I, typename... Types>
|
||||
struct TVariantAlternativeType;
|
||||
|
||||
template <size_t I, typename T, typename... Types>
|
||||
struct TVariantAlternativeType<I, T, Types...>
|
||||
{
|
||||
static_assert(I < sizeof...(Types) + 1, "Variant type index is invalid");
|
||||
using Type = TVariantAlternativeType<I - 1, Types...>::Type;
|
||||
};
|
||||
|
||||
template <typename T, typename... Types>
|
||||
struct TVariantAlternativeType<0, T, Types...> { using Type = T; };
|
||||
|
||||
template <typename T, typename... Types>
|
||||
struct TVariantSelectedType;
|
||||
|
||||
template <typename T, typename U, typename... Types>
|
||||
struct TVariantSelectedType<T, U, Types...>
|
||||
{
|
||||
using TypeAlternativeA = typename TConditional<TIsConstructible<U, T&&>::Value, U, void>::Type;
|
||||
using TypeAlternativeB = typename TVariantSelectedType<T, Types...>::Type;
|
||||
|
||||
using Type = typename TConditional<TIsSame<TypeAlternativeA, void>::Value, TypeAlternativeB,
|
||||
typename TConditional<TIsSame<TypeAlternativeB, void>::Value, TypeAlternativeA,
|
||||
typename TConditional<TIsSame<TypeAlternativeB, T>::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, T>::Value ? 1 :
|
||||
!TIsSame<TypeAlternativeA, void>::Value && !TIsSame<TypeAlternativeB, void>::Value ? 2 : 3;
|
||||
|
||||
static constexpr bool Value = Flag & 1;
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct TVariantSelectedType<T>
|
||||
{
|
||||
static constexpr uint8 Flag = 0;
|
||||
using Type = void;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr void VariantDestroy(void* InValue)
|
||||
{
|
||||
if constexpr (!TIsDestructible<T>::Value) check_no_entry();
|
||||
else if constexpr (!TIsTriviallyDestructible<T>::Value)
|
||||
{
|
||||
typedef T DestructOptionalType;
|
||||
reinterpret_cast<T*>(InValue)->DestructOptionalType::~DestructOptionalType();
|
||||
}
|
||||
}
|
||||
|
||||
using FVariantDestroyFunc = void(*)(void*);
|
||||
|
||||
template <typename T>
|
||||
constexpr void VariantCopyConstruct(void* Target, const void* Source)
|
||||
{
|
||||
if constexpr (!TIsCopyConstructible<T>::Value || TIsConst<T>::Value) check_no_entry();
|
||||
new(reinterpret_cast<T*>(Target)) T(*reinterpret_cast<const T*>(Source));
|
||||
}
|
||||
|
||||
using FVariantCopyConstructFunc = void(*)(void*, const void*);
|
||||
|
||||
template <typename T>
|
||||
constexpr void VariantMoveConstruct(void* Target, void* Source)
|
||||
{
|
||||
if constexpr (!TIsMoveConstructible<T>::Value || TIsConst<T>::Value) check_no_entry();
|
||||
new(reinterpret_cast<T*>(Target)) T(MoveTemp(*reinterpret_cast<T*>(Source)));
|
||||
}
|
||||
|
||||
using FVariantMoveConstructFunc = void(*)(void*, void*);
|
||||
|
||||
template <typename T>
|
||||
constexpr void VariantCopyAssign(void* Target, const void* Source)
|
||||
{
|
||||
if constexpr (!TIsCopyAssignable<T>::Value || TIsConst<T>::Value) check_no_entry();
|
||||
*reinterpret_cast<T*>(Target) = *reinterpret_cast<const T*>(Source);
|
||||
}
|
||||
|
||||
using FVariantCopyAssignFunc = void(*)(void*, const void*);
|
||||
|
||||
template <typename T>
|
||||
constexpr void VariantMoveAssign(void* Target, void* Source)
|
||||
{
|
||||
if constexpr (!TIsMoveAssignable<T>::Value || TIsConst<T>::Value) check_no_entry();
|
||||
*reinterpret_cast<T*>(Target) = MoveTemp(*reinterpret_cast<T*>(Source));
|
||||
}
|
||||
|
||||
using FVariantMoveAssignFunc = void(*)(void*, void*);
|
||||
|
||||
template <typename T>
|
||||
constexpr bool VariantEqualityOperator(const void* LHS, const void* RHS)
|
||||
{
|
||||
if constexpr (!CEqualityComparable<T>) check_no_entry();
|
||||
else return *reinterpret_cast<const T*>(LHS) == *reinterpret_cast<const T*>(RHS);
|
||||
return false;
|
||||
}
|
||||
|
||||
using FVariantEqualityOperatorFunc = bool(*)(const void*, const void*);
|
||||
|
||||
template <typename T>
|
||||
constexpr bool VariantInequalityOperator(const void* LHS, const void* RHS)
|
||||
{
|
||||
if constexpr (!CEqualityComparable<T>) check_no_entry();
|
||||
else return *reinterpret_cast<const T*>(LHS) != *reinterpret_cast<const T*>(RHS);
|
||||
return false;
|
||||
}
|
||||
|
||||
using FVariantInequalityOperatorFunc = bool(*)(const void*, const void*);
|
||||
|
||||
template <typename... Types>
|
||||
struct TVariantHelper
|
||||
{
|
||||
static constexpr FVariantDestroyFunc DestroyFuncs[] = { VariantDestroy<Types>... };
|
||||
static constexpr FVariantCopyConstructFunc CopyConstructFuncs[] = { VariantCopyConstruct<Types>... };
|
||||
static constexpr FVariantMoveConstructFunc MoveConstructFuncs[] = { VariantMoveConstruct<Types>... };
|
||||
static constexpr FVariantCopyAssignFunc CopyAssignFuncs[] = { VariantCopyAssign<Types>... };
|
||||
static constexpr FVariantMoveAssignFunc MoveAssignFuncs[] = { VariantMoveAssign<Types>... };
|
||||
static constexpr FVariantEqualityOperatorFunc EqualityOperatorFuncs[] = { VariantEqualityOperator<Types>... };
|
||||
static constexpr FVariantInequalityOperatorFunc InequalityOperatorFuncs[] = { VariantInequalityOperator<Types>... };
|
||||
};
|
||||
|
||||
template <typename... Types> constexpr FVariantDestroyFunc TVariantHelper<Types...>::DestroyFuncs[];
|
||||
template <typename... Types> constexpr FVariantCopyConstructFunc TVariantHelper<Types...>::CopyConstructFuncs[];
|
||||
template <typename... Types> constexpr FVariantMoveConstructFunc TVariantHelper<Types...>::MoveConstructFuncs[];
|
||||
template <typename... Types> constexpr FVariantCopyAssignFunc TVariantHelper<Types...>::CopyAssignFuncs[];
|
||||
template <typename... Types> constexpr FVariantMoveAssignFunc TVariantHelper<Types...>::MoveAssignFuncs[];
|
||||
template <typename... Types> constexpr FVariantEqualityOperatorFunc TVariantHelper<Types...>::EqualityOperatorFuncs[];
|
||||
template <typename... Types> constexpr FVariantInequalityOperatorFunc TVariantHelper<Types...>::InequalityOperatorFuncs[];
|
||||
|
||||
template <typename R, typename F, typename T>
|
||||
constexpr R VariantVisitLValue(F&& Func, void* Arg)
|
||||
{
|
||||
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry();
|
||||
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));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
using FVariantVisitLValueFunc = R(*)(F&&, void*);
|
||||
|
||||
template <typename R, typename F, typename T>
|
||||
constexpr R VariantVisitRValue(F&& Func, void* Arg)
|
||||
{
|
||||
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry();
|
||||
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)));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
using FVariantVisitRValueFunc = R(*)(F&&, void*);
|
||||
|
||||
template <typename R, typename F, typename T>
|
||||
constexpr R VariantVisitConstLValue(F&& Func, const void* Arg)
|
||||
{
|
||||
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry();
|
||||
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));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
using FVariantVisitConstLValueFunc = R(*)(F&&, const void*);
|
||||
|
||||
template <typename R, typename F, typename T>
|
||||
constexpr R VariantVisitConstRValue(F&& Func, const void* Arg)
|
||||
{
|
||||
if constexpr (!TIsInvocableResult<R, F, T>::Value) check_no_entry();
|
||||
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)));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
using FVariantVisitConstRValueFunc = R(*)(F&&, const void*);
|
||||
|
||||
template <typename R, typename F, typename... Types>
|
||||
struct TVariantVisitHelper
|
||||
{
|
||||
static constexpr FVariantVisitLValueFunc<R, F> VisitLValueFuncs[] = { VariantVisitLValue<R, F, Types>... };
|
||||
static constexpr FVariantVisitRValueFunc<R, F> VisitRValueFuncs[] = { VariantVisitRValue<R, F, Types>... };
|
||||
static constexpr FVariantVisitConstLValueFunc<R, F> VisitConstLValueFuncs[] = { VariantVisitConstLValue<R, F, Types>... };
|
||||
static constexpr FVariantVisitConstRValueFunc<R, F> VisitConstRValueFuncs[] = { VariantVisitConstRValue<R, F, Types>... };
|
||||
};
|
||||
|
||||
template <typename R, typename F, typename... Types> constexpr FVariantVisitLValueFunc<R, F> TVariantVisitHelper<R, F, Types...>::VisitLValueFuncs[];
|
||||
template <typename R, typename F, typename... Types> constexpr FVariantVisitRValueFunc<R, F> TVariantVisitHelper<R, F, Types...>::VisitRValueFuncs[];
|
||||
template <typename R, typename F, typename... Types> constexpr FVariantVisitConstLValueFunc<R, F> TVariantVisitHelper<R, F, Types...>::VisitConstLValueFuncs[];
|
||||
template <typename R, typename F, typename... Types> constexpr FVariantVisitConstRValueFunc<R, F> TVariantVisitHelper<R, F, Types...>::VisitConstRValueFuncs[];
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
template <typename... Types>
|
||||
struct TVariant
|
||||
{
|
||||
struct FAlternativeSize : TConstant<size_t, sizeof...(Types)> { };
|
||||
|
||||
template <size_t I> struct TAlternativeType : NAMESPACE_PRIVATE::TVariantAlternativeType<I, Types...> { };
|
||||
template <typename T> struct TAlternativeIndex : NAMESPACE_PRIVATE::TVariantAlternativeIndex<T, Types...> { };
|
||||
|
||||
constexpr TVariant() : TypeIndex(static_cast<size_t>(INDEX_NONE)) { };
|
||||
|
||||
constexpr TVariant(FInvalid) : TVariant() { };
|
||||
|
||||
constexpr TVariant(const TVariant& InValue)
|
||||
: TypeIndex(InValue.GetIndex())
|
||||
{
|
||||
if (GetIndex() != static_cast<size_t>(INDEX_NONE)) FHelper::CopyConstructFuncs[InValue.GetIndex()](&Value, &InValue.Value);
|
||||
}
|
||||
|
||||
constexpr TVariant(TVariant&& InValue)
|
||||
: TypeIndex(InValue.GetIndex())
|
||||
{
|
||||
if (GetIndex() != static_cast<size_t>(INDEX_NONE)) FHelper::MoveConstructFuncs[InValue.GetIndex()](&Value, &InValue.Value);
|
||||
}
|
||||
|
||||
template<size_t I, typename... ArgTypes> requires (I < FAlternativeSize::Value)
|
||||
&& TIsConstructible<typename TAlternativeType<I>::Type, ArgTypes...>::Value
|
||||
constexpr explicit TVariant(TInPlaceIndex<I>, ArgTypes&&... Args)
|
||||
: TypeIndex(I)
|
||||
{
|
||||
using SelectedType = typename TAlternativeType<I>::Type;
|
||||
new(&Value) SelectedType(Forward<ArgTypes>(Args)...);
|
||||
}
|
||||
|
||||
template<typename T, typename... ArgTypes> requires (TAlternativeIndex<T>::Value != static_cast<size_t>(INDEX_NONE))
|
||||
&& TIsConstructible<typename TAlternativeType<TAlternativeIndex<T>::Value>::Type, ArgTypes...>::Value
|
||||
constexpr explicit TVariant(TInPlaceType<T>, ArgTypes&&... Args)
|
||||
: TVariant(InPlaceIndex<TAlternativeIndex<T>::Value>, Forward<ArgTypes>(Args)...)
|
||||
{ }
|
||||
|
||||
template <typename T> requires NAMESPACE_PRIVATE::TVariantSelectedType<typename TRemoveReference<T>::Type, Types...>::Value
|
||||
&& (!TIsInPlaceTypeSpecialization<typename TRemoveCVRef<T>::Type>::Value) && (!TIsInPlaceIndexSpecialization<typename TRemoveCVRef<T>::Type>::Value)
|
||||
&& (!TIsSame<typename TRemoveCVRef<T>::Type, TVariant>::Value)
|
||||
constexpr TVariant(T&& InValue) : TVariant(InPlaceType<typename NAMESPACE_PRIVATE::TVariantSelectedType<typename TRemoveReference<T>::Type, Types...>::Type>, Forward<T>(InValue))
|
||||
{ }
|
||||
|
||||
constexpr ~TVariant()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
constexpr TVariant& operator=(const TVariant& InValue)
|
||||
{
|
||||
if (&InValue == this) return *this;
|
||||
|
||||
if (!InValue.IsValid())
|
||||
{
|
||||
Reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
if (GetIndex() == InValue.GetIndex()) FHelper::CopyAssignFuncs[InValue.GetIndex()](&Value, &InValue.Value);
|
||||
else
|
||||
{
|
||||
Reset();
|
||||
FHelper::CopyConstructFuncs[InValue.GetIndex()](&Value, &InValue.Value);
|
||||
TypeIndex = InValue.GetIndex();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr TVariant& operator=(TVariant&& InValue)
|
||||
{
|
||||
if (&InValue == this) return *this;
|
||||
|
||||
if (!InValue.IsValid())
|
||||
{
|
||||
Reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
if (GetIndex() == InValue.GetIndex()) FHelper::MoveAssignFuncs[InValue.GetIndex()](&Value, &InValue.Value);
|
||||
else
|
||||
{
|
||||
Reset();
|
||||
FHelper::MoveConstructFuncs[InValue.GetIndex()](&Value, &InValue.Value);
|
||||
TypeIndex = InValue.GetIndex();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T> requires NAMESPACE_PRIVATE::TVariantSelectedType<typename TRemoveReference<T>::Type, Types...>::Value
|
||||
constexpr TVariant& operator=(T&& InValue)
|
||||
{
|
||||
using SelectedType = typename NAMESPACE_PRIVATE::TVariantSelectedType<typename TRemoveReference<T>::Type, Types...>::Type;
|
||||
|
||||
if (GetIndex() == TAlternativeIndex<SelectedType>::Value) GetValue<SelectedType>() = Forward<T>(InValue);
|
||||
else
|
||||
{
|
||||
Reset();
|
||||
new(&Value) SelectedType(Forward<T>(InValue));
|
||||
TypeIndex = TAlternativeIndex<SelectedType>::Value;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <size_t I, typename... ArgTypes> requires (I < FAlternativeSize::Value)
|
||||
&& TIsConstructible<typename TAlternativeType<I>::Type, ArgTypes...>::Value
|
||||
constexpr typename TAlternativeType<I>::Type& Emplace(ArgTypes&&... Args)
|
||||
{
|
||||
Reset();
|
||||
|
||||
using SelectedType = typename TAlternativeType<I>::Type;
|
||||
SelectedType* Result = new(&Value) SelectedType(Forward<ArgTypes>(Args)...);
|
||||
TypeIndex = I;
|
||||
|
||||
return *Result;
|
||||
}
|
||||
|
||||
template <typename T, typename... ArgTypes> requires (TAlternativeIndex<T>::Value != static_cast<size_t>(INDEX_NONE))
|
||||
&& TIsConstructible<typename TAlternativeType<TAlternativeIndex<T>::Value>::Type, ArgTypes...>::Value
|
||||
constexpr T& Emplace(ArgTypes&&... Args)
|
||||
{
|
||||
return Emplace<TAlternativeIndex<T>::Value>(Forward<ArgTypes>(Args)...);
|
||||
}
|
||||
|
||||
constexpr size_t GetIndex() const { return TypeIndex; }
|
||||
constexpr bool IsValid() const { return GetIndex() != static_cast<size_t>(INDEX_NONE); }
|
||||
constexpr explicit operator bool() const { return GetIndex() != static_cast<size_t>(INDEX_NONE); }
|
||||
|
||||
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; }
|
||||
|
||||
constexpr void* GetData() { 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> 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> 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> 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> 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> constexpr typename TAlternativeType<I>::Type Get(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> constexpr T Get(T&& DefaultValue) const& { return HoldsAlternative<T>() ? GetValue<T>() : DefaultValue; }
|
||||
|
||||
template <typename F>
|
||||
constexpr auto Visit(F&& Func) &
|
||||
{
|
||||
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
|
||||
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
|
||||
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitLValueFuncs[GetIndex()](Forward<F>(Func), &Value));
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
constexpr auto Visit(F&& Func) &&
|
||||
{
|
||||
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
|
||||
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
|
||||
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitRValueFuncs[GetIndex()](Forward<F>(Func), &Value));
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
constexpr auto Visit(F&& Func) const&
|
||||
{
|
||||
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
|
||||
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
|
||||
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitConstLValueFuncs[GetIndex()](Forward<F>(Func), &Value));
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
constexpr auto Visit(F&& Func) const&&
|
||||
{
|
||||
using ReturnType = typename TCommonType<typename TInvokeResult<F, Types>::Type...>::Type;
|
||||
checkf(IsValid(), "It is an error to call Visit() on an wrong TVariant. Please either check IsValid().");
|
||||
return ReturnType(NAMESPACE_PRIVATE::TVariantVisitHelper<ReturnType, F, Types...>::VisitConstRValueFuncs[GetIndex()](Forward<F>(Func), &Value));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
constexpr R Visit(F&& Func) &
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
constexpr R Visit(F&& Func) &&
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
constexpr R Visit(F&& Func) const&
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
template <typename R, typename F>
|
||||
constexpr R Visit(F&& Func) const&&
|
||||
{
|
||||
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...>::VisitConstRValueFuncs[GetIndex()](Forward<F>(Func), &Value));
|
||||
}
|
||||
|
||||
constexpr void Reset()
|
||||
{
|
||||
if (GetIndex() == static_cast<size_t>(INDEX_NONE)) return;
|
||||
|
||||
FHelper::DestroyFuncs[GetIndex()](&Value);
|
||||
|
||||
TypeIndex = static_cast<size_t>(INDEX_NONE);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
using FHelper = NAMESPACE_PRIVATE::TVariantHelper<Types...>;
|
||||
|
||||
TAlignedUnion<1, Types...>::Type Value;
|
||||
size_t TypeIndex;
|
||||
|
||||
friend constexpr bool operator==(const TVariant& LHS, const TVariant& RHS)
|
||||
{
|
||||
if (LHS.GetIndex() != RHS.GetIndex()) return false;
|
||||
if (LHS.IsValid() == false) return true;
|
||||
return FHelper::EqualityOperatorFuncs[LHS.GetIndex()](&LHS.Value, &RHS.Value);
|
||||
}
|
||||
|
||||
friend constexpr bool operator!=(const TVariant& LHS, const TVariant& RHS)
|
||||
{
|
||||
if (LHS.GetIndex() != RHS.GetIndex()) return true;
|
||||
if (LHS.IsValid() == false) return false;
|
||||
return FHelper::InequalityOperatorFuncs[LHS.GetIndex()](&LHS.Value, &RHS.Value);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename T, typename... Types>
|
||||
constexpr bool operator==(const TVariant<Types...>& LHS, const T& RHS)
|
||||
{
|
||||
return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() == RHS : false;
|
||||
}
|
||||
|
||||
template <typename T, typename... Types>
|
||||
constexpr bool operator!=(const TVariant<Types...>& LHS, const T& RHS)
|
||||
{
|
||||
return LHS.template HoldsAlternative<T>() ? LHS.template GetValue<T>() != RHS : true;
|
||||
}
|
||||
|
||||
template <typename T, typename... Types>
|
||||
constexpr bool operator==(const T& LHS, const TVariant<Types...>& RHS)
|
||||
{
|
||||
return RHS.template HoldsAlternative<T>() ? LHS == RHS.template GetValue<T>() : false;
|
||||
}
|
||||
|
||||
template <typename T, typename... Types>
|
||||
constexpr bool operator!=(const T& LHS, const TVariant<Types...>& RHS)
|
||||
{
|
||||
return RHS.template HoldsAlternative<T>() ? LHS != RHS.template GetValue<T>() : true;
|
||||
}
|
||||
|
||||
template <typename... Types> requires (true && ... && (TIsMoveConstructible<Types>::Value && TIsSwappable<Types>::Value))
|
||||
constexpr void Swap(TVariant<Types...>& A, TVariant<Types...>& B)
|
||||
{
|
||||
if (!A && !B) return;
|
||||
|
||||
if (A && !B)
|
||||
{
|
||||
B = MoveTemp(A);
|
||||
A.Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (B && !A)
|
||||
{
|
||||
A = MoveTemp(B);
|
||||
B.Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
TVariant<Types...> Temp = MoveTemp(A);
|
||||
A = MoveTemp(B);
|
||||
B = MoveTemp(Temp);
|
||||
}
|
||||
|
||||
template <typename T > struct TIsVariantSpecialization : FFalse { };
|
||||
template <typename... Types> struct TIsVariantSpecialization<TVariant<Types...>> : FTrue { };
|
||||
|
||||
template <size_t I, typename VariantType> requires TIsVariantSpecialization<typename TRemoveCVRef<VariantType>::Type>::Value
|
||||
struct TVariantAlternativeType { using Type = typename TCopyCV<typename TRemoveReference<VariantType>::Type, typename TRemoveCVRef<VariantType>::Type::template TAlternativeType<I>::Type>::Type; };
|
||||
|
||||
template <typename T, typename VariantType> requires TIsVariantSpecialization<typename TRemoveCVRef<VariantType>::Type>::Value
|
||||
struct TVariantAlternativeIndex : VariantType::template TAlternativeIndex<T> { };
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
@ -11,6 +11,7 @@ void REDCRAFTUTILITY_API TestInvoke();
|
||||
void REDCRAFTUTILITY_API TestReferenceWrapper();
|
||||
void REDCRAFTUTILITY_API TestCompare();
|
||||
void REDCRAFTUTILITY_API TestOptional();
|
||||
void REDCRAFTUTILITY_API TestVariant();
|
||||
void REDCRAFTUTILITY_API TestMiscellaneous();
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
|
Loading…
Reference in New Issue
Block a user