UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TVariantMeta.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
10#include "Concepts/Insertable.h"
11
13
14template <typename T, typename... Ts>
15class TVariant;
16
18template <typename T>
19constexpr bool TIsVariant_V = false;
20
21template <typename... Ts>
22constexpr bool TIsVariant_V<TVariant<Ts...>> = true;
23
24template <typename T> constexpr bool TIsVariant_V<const T> = TIsVariant_V<T>;
25template <typename T> constexpr bool TIsVariant_V< volatile T> = TIsVariant_V<T>;
26template <typename T> constexpr bool TIsVariant_V<const volatile T> = TIsVariant_V<T>;
27
29template <typename>
30constexpr SIZE_T TVariantSize_V = 0;
31
32template <typename... Ts>
33constexpr SIZE_T TVariantSize_V<TVariant<Ts...>> = sizeof...(Ts);
34
35template <typename T> constexpr SIZE_T TVariantSize_V<const T> = TVariantSize_V<T>;
36template <typename T> constexpr SIZE_T TVariantSize_V< volatile T> = TVariantSize_V<T>;
38
39template <typename T>
40struct UE_DEPRECATED(5.4, "TIsVariant<T> has been deprecated, please use TIsVariant_V<std::remove_reference_t<T>> instead") TIsVariant
41{
42 static constexpr inline bool Value = TIsVariant_V<T>;
43};
44
46template <typename> struct TVariantSize;
47
48template <typename T>
49struct UE_DEPRECATED(5.4, "TVariantSize<T> has been deprecated, please use TVariantSize_V<std::remove_reference_t<T>> instead") TVariantSize
50{
52};
53
54namespace UE
55{
56namespace Core
57{
58namespace Private
59{
61 template <typename T>
63 {
64 using Type = FArchive;
65 };
66
68 template <typename...>
70
72 template <typename T>
74 {
75 static constexpr inline bool Value = false;
76 };
77
82 template <typename T, typename... Ts>
83 struct TTypePackContainsDuplicates<T, T, Ts...>
84 {
85 static constexpr inline bool Value = true;
86 };
87
89 template <typename T, typename U, typename... Rest>
90 struct TTypePackContainsDuplicates<T, U, Rest...>
91 {
92 static constexpr inline bool Value = TTypePackContainsDuplicates<T, Rest...>::Value || TTypePackContainsDuplicates<U, Rest...>::Value;
93 };
94
96 template <typename... Ts>
98 {
99 static constexpr inline bool Value = (std::is_reference_v<Ts> || ...);
100 };
101
103 template <typename... Ts>
105 {
106 static constexpr SIZE_T MaxOf(const SIZE_T Sizes[])
107 {
108 SIZE_T MaxSize = 0;
109 for (SIZE_T Itr = 0; Itr < sizeof...(Ts); ++Itr)
110 {
111 if (Sizes[Itr] > MaxSize)
112 {
113 MaxSize = Sizes[Itr];
114 }
115 }
116 return MaxSize;
117 }
118 static constexpr SIZE_T MaxSizeof()
119 {
120 constexpr SIZE_T Sizes[] = { sizeof(Ts)... };
121 return MaxOf(Sizes);
122 }
123 static constexpr SIZE_T MaxAlignof()
124 {
125 constexpr SIZE_T Sizes[] = { alignof(Ts)... };
126 return MaxOf(Sizes);
127 }
128
129 static constexpr inline SIZE_T SizeofValue = MaxSizeof();
130 static constexpr inline SIZE_T AlignofValue = MaxAlignof();
131 static_assert(SizeofValue > 0, "MaxSizeof must be greater than 0");
132 static_assert(AlignofValue > 0, "MaxAlignof must be greater than 0");
133
135 template <SIZE_T N>
137 {
138 using ReturnType = typename TNthTypeFromParameterPack<N, Ts...>::Type;
139 return *reinterpret_cast<ReturnType*>(&Storage);
140 }
141
143 template <SIZE_T N>
145 {
146 using ReturnType = typename TNthTypeFromParameterPack<N, Ts...>::Type;
147 return (ReturnType&&)GetValueAsIndexedType<N>();
148 }
149
151 template <SIZE_T N>
152 const auto& GetValueAsIndexedType() const&
153 {
154 // Temporarily remove the const qualifier so we can implement GetValueAsIndexedType in one location.
155 return const_cast<TVariantStorage*>(this)->template GetValueAsIndexedType<N>();
156 }
157
159
160#if !PLATFORM_COMPILER_SUPPORTS_CONSTRAINED_DESTRUCTORS
163#endif
164 };
165
167 template <SIZE_T N, typename LookupType, typename... Ts>
169 {
170 static constexpr inline SIZE_T Value = (SIZE_T)-1;
171 };
172
174 template <SIZE_T N, typename T, typename... Ts>
175 struct TParameterPackTypeIndexHelper<N, T, T, Ts...>
176 {
177 static constexpr inline SIZE_T Value = N;
178 };
179
181 template <SIZE_T N, typename LookupType, typename T, typename... Ts>
183 {
184 static constexpr inline SIZE_T Value = TParameterPackTypeIndexHelper<N + 1, LookupType, Ts...>::Value;
185 };
186
188 template <typename LookupType, typename... Ts>
190 {
191 static constexpr inline SIZE_T Value = TParameterPackTypeIndexHelper<0, LookupType, Ts...>::Value;
192 };
193
195 template <typename T>
197 {
198 static constexpr void Destruct(void* Storage)
199 {
200 DestructItem(static_cast<T*>(Storage));
201 }
202 };
203
205 template <typename... Ts>
207 {
209 static void Destruct(SIZE_T TypeIndex, void* Value)
210 {
211 static constexpr void(*Destructors[])(void*) = { &TDestructorCaller<Ts>::Destruct... };
212 check(TypeIndex < UE_ARRAY_COUNT(Destructors));
213 Destructors[TypeIndex](Value);
214 }
215 };
216
218 template <typename T>
220 {
222 static void Construct(void* Storage, const void* Value)
223 {
224 ::new(Storage) T(*static_cast<const T*>(Value));
225 }
226 };
227
229 template <typename... Ts>
231 {
233 static void Construct(SIZE_T TypeIndex, void* Storage, const void* Value)
234 {
235 static constexpr void(*CopyConstructors[])(void*, const void*) = { &TCopyConstructorCaller<Ts>::Construct... };
237 CopyConstructors[TypeIndex](Storage, Value);
238 }
239 };
240
241
243 template <typename T>
245 {
247 static void Construct(void* Storage, void* Value)
248 {
249 ::new(Storage) T(MoveTemp(*static_cast<T*>(Value)));
250 }
251 };
252
254 template <typename... Ts>
256 {
258 static void Construct(SIZE_T TypeIndex, void* Target, void* Source)
259 {
260 static constexpr void(*MoveConstructors[])(void*, void*) = { &TMoveConstructorCaller<Ts>::Construct... };
262 MoveConstructors[TypeIndex](Target, Source);
263 }
264 };
265
267 template <typename T, typename VariantType>
269 {
271 static void Load(FArchive& Ar, VariantType& OutVariant)
272 {
273 OutVariant.template Emplace<T>();
274 Ar << OutVariant.template Get<T>();
275 }
276 };
277
279 template <typename... Ts>
281 {
282 using VariantType = TVariant<Ts...>;
283 static_assert((std::is_default_constructible_v<Ts> && ...), "Each type in TVariant template parameter pack must be default constructible in order to use FArchive serialization");
284 static_assert((TModels_V<CInsertable<FArchive&>, Ts> && ...), "Each type in TVariant template parameter pack must be able to use operator<< with an FArchive");
285
287 static void Load(SIZE_T TypeIndex, FArchive& Ar, VariantType& OutVariant)
288 {
290 check(TypeIndex < UE_ARRAY_COUNT(Loaders));
291 Loaders[TypeIndex](Ar, OutVariant);
292 }
293 };
294
296 template <typename LookupType, typename... Ts>
297 struct TIsType
298 {
300 static bool IsSame(SIZE_T TypeIndex)
301 {
302 static constexpr bool bIsSameType[] = { std::is_same_v<Ts, LookupType>... };
303 check(TypeIndex < UE_ARRAY_COUNT(bIsSameType));
304 return bIsSameType[TypeIndex];
305 }
306 };
307
309 template <typename T>
310 inline SIZE_T EncodeIndices(const T& Variant)
311 {
312 return Variant.GetIndex();
313 }
314
315 template <typename Variant0, typename... Variants>
316 inline SIZE_T EncodeIndices(const Variant0& First, const Variants&... Rest)
317 {
318 return First.GetIndex() + TVariantSize_V<Variant0> * EncodeIndices(Rest...);
319 }
320
323 {
324 while (VariantIndex)
325 {
327 --VariantIndex;
328 ++VariantSizes;
329 }
330 return EncodedIndex % *VariantSizes;
331 }
332
334 template <typename... Ts>
339
340 template <typename... Ts>
345
346 template <typename... Ts>
348 {
349 return *(const TVariantStorage<Ts...>*)(&Variant);
350 }
351
353 template <SIZE_T EncodedIndex, SIZE_T... VariantIndices, typename Func, typename... Variants>
354 inline decltype(auto) VisitApplyEncoded(Func&& Callable, Variants&&... Args)
355 {
357 return Callable(CastToStorage(Forward<Variants>(Args)).template GetValueAsIndexedType<DecodeIndex(EncodedIndex, VariantIndices, VariantSizes)>()...);
358 }
359
368 template <typename InvokeFn, SIZE_T... VariantIndices>
369 struct TWrapper
370 {
371 template <SIZE_T EncodedIndex>
373 };
374
376 template <typename Func, SIZE_T... EncodedIndices, SIZE_T... VariantIndices, typename... Variants>
378 {
379 using ReturnType = decltype(VisitApplyEncoded<0, VariantIndices...>(Forward<Func>(Callable), Forward<Variants>(Args)...));
380 using InvokeFn = ReturnType(*)(Func&&, Variants&&...);
382 static constexpr InvokeFn Invokers[] = { WrapperType::template FuncPtr<EncodedIndices>... };
383 return Invokers[EncodedIndex](Forward<Func>(Callable), Forward<Variants>(Args)...);
384 }
385
386#if !PLATFORM_COMPILER_SUPPORTS_CONSTRAINED_DESTRUCTORS
387 template <typename T, typename... Ts>
396#endif
397
398} // namespace Private
399} // namespace Core
400} // namespace UE
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define check(expr)
Definition AssertionMacros.h:314
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FORCEINLINE constexpr void DestructItem(ElementType *Element)
Definition MemoryOps.h:56
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
constexpr SIZE_T TVariantSize_V
Definition TVariantMeta.h:30
constexpr SIZE_T TVariantSize_V< const T >
Definition TVariantMeta.h:35
constexpr bool TIsVariant_V< volatile T >
Definition TVariantMeta.h:25
constexpr bool TIsVariant_V< const volatile T >
Definition TVariantMeta.h:26
constexpr bool TIsVariant_V< const T >
Definition TVariantMeta.h:24
constexpr SIZE_T TVariantSize_V< const volatile T >
Definition TVariantMeta.h:37
constexpr bool TIsVariant_V
Definition TVariantMeta.h:19
constexpr SIZE_T TVariantSize_V< volatile T >
Definition TVariantMeta.h:36
#define UE_ARRAY_COUNT(array)
Definition UnrealTemplate.h:212
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Core.Build.cs:8
Definition Archive.h:1208
Definition TVariant.h:48
Definition OverriddenPropertySet.cpp:45
SIZE_T EncodeIndices(const T &Variant)
Definition TVariantMeta.h:310
decltype(auto) VisitApplyEncoded(Func &&Callable, Variants &&... Args)
Definition TVariantMeta.h:354
decltype(auto) VisitImpl(SIZE_T EncodedIndex, Func &&Callable, TIntegerSequence< SIZE_T, EncodedIndices... > &&, TIntegerSequence< SIZE_T, VariantIndices... > &&VariantIndicesSeq, Variants &&... Args)
Definition TVariantMeta.h:377
constexpr SIZE_T DecodeIndex(SIZE_T EncodedIndex, SIZE_T VariantIndex, const SIZE_T *VariantSizes)
Definition TVariantMeta.h:322
UE_FORCEINLINE_HINT TVariantStorage< Ts... > & CastToStorage(TVariant< Ts... > &Variant)
Definition TVariantMeta.h:335
Definition AdvancedWidgetsModule.cpp:13
Definition TypeCompatibleBytes.h:17
Definition IntegerSequence.h:9
Definition UnrealTypeTraits.h:64
Definition TVariantMeta.h:46
Definition TVariantMeta.h:63
Definition TVariantMeta.h:98
static constexpr bool Value
Definition TVariantMeta.h:99
Definition TVariantMeta.h:220
static void Construct(void *Storage, const void *Value)
Definition TVariantMeta.h:222
Definition TVariantMeta.h:231
static void Construct(SIZE_T TypeIndex, void *Storage, const void *Value)
Definition TVariantMeta.h:233
~TDestructibleVariantStorage()
Definition TVariantMeta.h:391
Definition TVariantMeta.h:197
static constexpr void Destruct(void *Storage)
Definition TVariantMeta.h:198
Definition TVariantMeta.h:207
static void Destruct(SIZE_T TypeIndex, void *Value)
Definition TVariantMeta.h:209
Definition TVariantMeta.h:298
static bool IsSame(SIZE_T TypeIndex)
Definition TVariantMeta.h:300
Definition TVariantMeta.h:245
static void Construct(void *Storage, void *Value)
Definition TVariantMeta.h:247
Definition TVariantMeta.h:256
static void Construct(SIZE_T TypeIndex, void *Target, void *Source)
Definition TVariantMeta.h:258
static constexpr SIZE_T Value
Definition TVariantMeta.h:170
Definition TVariantMeta.h:190
static constexpr SIZE_T Value
Definition TVariantMeta.h:191
static void Load(FArchive &Ar, VariantType &OutVariant)
Definition TVariantMeta.h:271
static void Load(SIZE_T TypeIndex, FArchive &Ar, VariantType &OutVariant)
Definition TVariantMeta.h:287
Definition TVariantMeta.h:105
static constexpr SIZE_T AlignofValue
Definition TVariantMeta.h:130
static constexpr SIZE_T MaxAlignof()
Definition TVariantMeta.h:123
static constexpr SIZE_T MaxOf(const SIZE_T Sizes[])
Definition TVariantMeta.h:106
static constexpr SIZE_T SizeofValue
Definition TVariantMeta.h:129
const auto & GetValueAsIndexedType() const &
Definition TVariantMeta.h:152
static constexpr SIZE_T MaxSizeof()
Definition TVariantMeta.h:118
uint8 TypeIndex
Definition TVariantMeta.h:162
auto && GetValueAsIndexedType() &&
Definition TVariantMeta.h:144
TAlignedBytes< SizeofValue, AlignofValue > Storage
Definition TVariantMeta.h:158
auto & GetValueAsIndexedType() &
Definition TVariantMeta.h:136
Definition TVariantMeta.h:370
static constexpr InvokeFn FuncPtr
Definition TVariantMeta.h:372