6#include "Templates/EnableIf.h"
7#include "Templates/IsConstructible.h"
8#include "Templates/Decay.h"
11
12
17
18
29
30
31
32template <
typename T,
typename... Ts>
35 static_assert(!
UE::Core::
Private::TTypePackContainsDuplicates<T, Ts...>::Value,
"All the types used in TVariant should be unique");
39 static_assert(
sizeof...(Ts) <= 255,
"TVariant cannot hold more than 256 types");
45 static_assert(TIsConstructible<T>::Value,
"To default-initialize a TVariant, the first type in the parameter pack must be default constructible. Use FEmptyVariantState as the first type if none of the other types can be listed first.");
46 new(&UE::Core::Private::CastToStorage(*
this).Storage) T();
51 template <
typename U,
typename... TArgs>
54 constexpr SIZE_T Index = UE::Core::Private::TParameterPackTypeIndex<U, T, Ts...>::Value;
55 static_assert(Index != (SIZE_T)-1,
"The TVariant is not declared to hold the type being constructed");
57 new(&UE::Core::Private::CastToStorage(*
this).Storage) U(Forward<TArgs>(Args)...);
58 TypeIndex = (uint8)Index;
63 : TypeIndex(Other.TypeIndex)
65 UE::Core::Private::TCopyConstructorLookup<T, Ts...>::Construct(TypeIndex, &UE::Core::Private::CastToStorage(*
this).Storage, &UE::Core::Private::CastToStorage(Other).Storage);
70 : TypeIndex(Other.TypeIndex)
72 UE::Core::Private::TMoveConstructorLookup<T, Ts...>::Construct(TypeIndex, &UE::Core::Private::CastToStorage(*
this).Storage, &UE::Core::Private::CastToStorage(Other).Storage);
80 TVariant Temp = Other;
91 TVariant Temp = MoveTemp(Other);
100 UE::Core::Private::TDestructorLookup<T, Ts...>::Destruct(TypeIndex, &UE::Core::Private::CastToStorage(*
this).Storage);
104 template <
typename U>
107 static_assert(UE::Core::Private::TParameterPackTypeIndex<U, T, Ts...>::Value != (SIZE_T)-1,
"The TVariant is not declared to hold the type passed to IsType<>");
108 return UE::Core::Private::TIsType<U, T, Ts...>::IsSame(TypeIndex);
112 template <
typename U>
115 constexpr SIZE_T Index = UE::Core::Private::TParameterPackTypeIndex<U, T, Ts...>::Value;
116 static_assert(Index != (SIZE_T)-1,
"The TVariant is not declared to hold the type passed to Get<>");
118 check(Index == TypeIndex);
121 return *
reinterpret_cast<U*>(
reinterpret_cast<
void*>(&UE::Core::Private::CastToStorage(*
this).Storage));
125 template <
typename U>
129 return const_cast<TVariant*>(
this)->
template Get<U>();
133 template <
typename U>
136 constexpr SIZE_T Index = UE::Core::Private::TParameterPackTypeIndex<U, T, Ts...>::Value;
137 static_assert(Index != (SIZE_T)-1,
"The TVariant is not declared to hold the type passed to TryGet<>");
140 return Index == (SIZE_T)TypeIndex ?
reinterpret_cast<U*>(
reinterpret_cast<
void*>(&UE::Core::Private::CastToStorage(*
this).Storage)) :
nullptr;
144 template <
typename U>
148 return const_cast<TVariant*>(
this)->
template TryGet<U>();
152 template <
typename U>
155 Emplace<U>(MoveTemp(Value));
159 template <
typename U>
166 template <
typename U,
typename... TArgs>
169 constexpr SIZE_T Index = UE::Core::Private::TParameterPackTypeIndex<U, T, Ts...>::Value;
170 static_assert(Index != (SIZE_T)-1,
"The TVariant is not declared to hold the type passed to Emplace<>");
172 UE::Core::Private::TDestructorLookup<T, Ts...>::Destruct(TypeIndex, &UE::Core::Private::CastToStorage(*
this).Storage);
173 new(&UE::Core::Private::CastToStorage(*
this).Storage) U(Forward<TArgs>(Args)...);
174 TypeIndex = (uint8)Index;
178 template <
typename U>
182 static_assert(Index != (SIZE_T)-1,
"The TVariant is not declared to hold the type passed to IndexOfType<>");
189 return (SIZE_T)TypeIndex;
201 static constexpr bool Value =
false;
204template <
typename... Ts>
215template <
typename>
struct TVariantSize;
217template <
typename... Ts>
218struct TVariantSize<TVariant<Ts...>>
220 static constexpr SIZE_T
Value =
sizeof...(Ts);
223template <
typename T>
struct TVariantSize<T&> :
public TVariantSize<T> {};
224template <
typename T>
struct TVariantSize<T&&> :
public TVariantSize<T> {};
225template <
typename T>
struct TVariantSize<
const T> :
public TVariantSize<T> {};
230 typename... Variants,
233decltype(
auto)
Visit(Func&& Callable, Variants&&... Args)
236 constexpr SIZE_T NumPermutations = (1 * ... * (TVariantSize<Variants>::Value));
238 constexpr SIZE_T VariantSizes[] = { TVariantSize<Variants>::Value... };
242 return UE::Core::Private::VisitImpl(
243 UE::Core::Private::EncodeIndices(Args...),
244 Forward<Func>(Callable),
245 TMakeIntegerSequence<SIZE_T, NumPermutations>{},
246 TMakeIntegerSequence<SIZE_T,
sizeof...(Variants)>{},
247 Forward<Variants>(Args)...
252
253
254
255
256
257
258template <
typename... Ts>
265 check(Index <
sizeof...(Ts));
267 UE::Core::Private::TVariantLoadFromArchiveLookup<Ts...>::Load((SIZE_T)Index, Ar, Variant);
271 uint8 Index = (uint8)Variant.GetIndex();
273 Visit([&Ar](
auto& StoredValue)
decltype(auto) Visit(Func &&Callable, Variants &&... Args)
TVariant(TInPlaceType< U > &&, TArgs &&... Args)
static constexpr SIZE_T IndexOfType()
TVariant(const TVariant &Other)
TVariant(TVariant &&Other)
void Set(typename TIdentity< U >::Type &&Value)
void Set(const typename TIdentity< U >::Type &Value)
TVariant & operator=(const TVariant &Other)
TVariant & operator=(TVariant &&Other)
void Emplace(TArgs &&... Args)
constexpr SIZE_T Multiply(const SIZE_T *Args, SIZE_T Num)
static constexpr bool Value
static constexpr bool Value
static constexpr SIZE_T Value