5#include "Templates/MemoryOps.h"
6#include "Templates/TypeCompatibleBytes.h"
7#include "Templates/UnrealTemplate.h"
8#include "Templates/UnrealTypeTraits.h"
9#include "Delegates/IntegerSequence.h"
10#include "Templates/AndOrNot.h"
11#include "Concepts/Insertable.h"
13#include "Misc/AssertionMacros.h"
15template <
typename T,
typename... Ts>
38 template <
typename...>
39 struct TTypePackContainsDuplicates;
43 struct TTypePackContainsDuplicates<T>
45 static constexpr bool Value =
false;
49
50
51
52 template <
typename T,
typename... Ts>
53 struct TTypePackContainsDuplicates<T, T, Ts...>
55 static constexpr bool Value =
true;
59 template <
typename T,
typename U,
typename... Rest>
60 struct TTypePackContainsDuplicates<T, U, Rest...>
62 static constexpr bool Value = TTypePackContainsDuplicates<T, Rest...>::Value || TTypePackContainsDuplicates<U, Rest...>::Value;
66 template <
typename... Ts>
73 template <
typename... Ts>
76 static constexpr SIZE_T
MaxOf(
const SIZE_T Sizes[])
79 for (SIZE_T Itr = 0; Itr <
sizeof...(Ts); ++Itr)
81 if (Sizes[Itr] > MaxSize)
90 constexpr SIZE_T Sizes[] = {
sizeof(Ts)... };
95 constexpr SIZE_T Sizes[] = {
alignof(Ts)... };
101 static_assert(
SizeofValue > 0,
"MaxSizeof must be greater than 0");
102 static_assert(
AlignofValue > 0,
"MaxAlignof must be greater than 0");
108 using ReturnType =
typename TNthTypeFromParameterPack<N, Ts...>::Type;
109 return *
reinterpret_cast<ReturnType*>(&
Storage);
116 using ReturnType =
typename TNthTypeFromParameterPack<N, Ts...>::Type;
117 return (ReturnType&&)GetValueAsIndexedType<N>();
125 return const_cast<
TVariantStorage*>(
this)->
template GetValueAsIndexedType<N>();
132 template <SIZE_T N,
typename LookupType,
typename... Ts>
135 static constexpr SIZE_T
Value = (SIZE_T)-1;
139 template <SIZE_T N,
typename T,
typename... Ts>
146 template <SIZE_T N,
typename LookupType,
typename T,
typename... Ts>
153 template <
typename LookupType,
typename... Ts>
160 template <
typename T>
165 DestructItem(
static_cast<T*>(Storage));
170 template <
typename... Ts>
174 static void Destruct(SIZE_T TypeIndex,
void* Value)
183 template <
typename T>
194 template <
typename... Ts>
198 static void Construct(SIZE_T TypeIndex,
void* Storage,
const void* Value)
208 template <
typename T>
219 template <
typename... Ts>
223 static void Construct(SIZE_T TypeIndex,
void* Target,
void* Source)
232 template <
typename T,
typename VariantType>
244 template <
typename... Ts>
247 using VariantType = TVariant<Ts...>;
248 static_assert((
std::
is_default_constructible<
Ts>::
value && ...),
"Each type in TVariant template parameter pack must be default constructible in order to use FArchive serialization");
249 static_assert((
TModels<
CInsertable<
FArchive&>, Ts>::Value && ...),
"Each type in TVariant template parameter pack must be able to use operator<< with an FArchive");
252 static void Load(SIZE_T TypeIndex,
FArchive& Ar, VariantType& OutVariant)
261 template <
typename LookupType,
typename... Ts>
274 template <
typename... Ts>
278 template <
typename T>
284 template <
typename Variant0,
typename... Variants>
291 constexpr SIZE_T
DecodeIndex(SIZE_T EncodedIndex, SIZE_T VariantIndex,
const SIZE_T* VariantSizes)
295 EncodedIndex /= *VariantSizes;
299 return EncodedIndex % *VariantSizes;
304 constexpr SIZE_T
Multiply(
const SIZE_T* Args, SIZE_T Num)
317 template <
typename... Ts>
323 template <
typename... Ts>
329 template <
typename... Ts>
336 template <SIZE_T EncodedIndex, SIZE_T... VariantIndices,
typename Func,
typename... Variants>
339 constexpr SIZE_T VariantSizes[] = { TVariantSize<Variants>::Value... };
340 return Callable(CastToStorage(Forward<Variants>(Args)).
template GetValueAsIndexedType<DecodeIndex(EncodedIndex, VariantIndices, VariantSizes)>()...);
344
345
346
347
348
349
350
351 template <
typename InvokeFn, SIZE_T... VariantIndices>
359 template <
typename Func, SIZE_T... EncodedIndices, SIZE_T... VariantIndices,
typename... Variants>
362 using ReturnType =
decltype(VisitApplyEncoded<0, VariantIndices...>(Forward<Func>(Callable), Forward<Variants>(Args)...));
363 using InvokeFn = ReturnType(*)(Func&&, Variants&&...);
364 using WrapperType =
TWrapper<InvokeFn, VariantIndices...>;
365 static constexpr InvokeFn Invokers[] = { WrapperType::
template FuncPtr<EncodedIndices>... };
366 return Invokers[EncodedIndex](Forward<Func>(Callable), Forward<Variants>(Args)...);
#define UE_ARRAY_COUNT(array)
SIZE_T EncodeIndices(const Variant0 &First, const Variants &... Rest)
SIZE_T EncodeIndices(const T &Variant)
constexpr SIZE_T Multiply(const SIZE_T *Args, SIZE_T Num)
decltype(auto) VisitApplyEncoded(Func &&Callable, Variants &&... Args)
FORCEINLINE TVariantStorage< Ts... > & CastToStorage(TVariant< Ts... > &Variant)
decltype(auto) VisitImpl(SIZE_T EncodedIndex, Func &&Callable, TIntegerSequence< SIZE_T, EncodedIndices... > &&, TIntegerSequence< SIZE_T, VariantIndices... > &&VariantIndicesSeq, Variants &&... Args)
constexpr SIZE_T DecodeIndex(SIZE_T EncodedIndex, SIZE_T VariantIndex, const SIZE_T *VariantSizes)
FORCEINLINE TVariantStorage< Ts... > && CastToStorage(TVariant< Ts... > &&Variant)
FORCEINLINE const TVariantStorage< Ts... > & CastToStorage(const TVariant< Ts... > &Variant)
static constexpr bool Value
static void Construct(void *Storage, const void *Value)
static void Construct(SIZE_T TypeIndex, void *Storage, const void *Value)
static constexpr void Destruct(void *Storage)
static void Destruct(SIZE_T TypeIndex, void *Value)
static bool IsSame(SIZE_T TypeIndex)
static void Construct(void *Storage, void *Value)
static void Construct(SIZE_T TypeIndex, void *Target, void *Source)
static constexpr SIZE_T Value
static constexpr SIZE_T Value
static constexpr SIZE_T Value
static constexpr SIZE_T Value
static constexpr bool Value
static constexpr bool Value
static constexpr bool Value
static void Load(FArchive &Ar, VariantType &OutVariant)
static void Load(SIZE_T TypeIndex, FArchive &Ar, VariantType &OutVariant)
static constexpr SIZE_T AlignofValue
static constexpr SIZE_T MaxAlignof()
static constexpr SIZE_T MaxOf(const SIZE_T Sizes[])
static constexpr SIZE_T SizeofValue
const auto & GetValueAsIndexedType() const &
static constexpr SIZE_T MaxSizeof()
auto && GetValueAsIndexedType() &&
TAlignedBytes< SizeofValue, AlignofValue > Storage
auto & GetValueAsIndexedType() &
static constexpr InvokeFn FuncPtr