UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
InstancedStruct.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "StructUtils.h"
8
9#include "InstancedStruct.generated.h"
10
11#define UE_API COREUOBJECT_API
12
13struct FConstStructView;
14template<typename BaseStructT> struct TConstStructView;
16template<typename BaseStructT> struct TConstSharedStruct;
18
30USTRUCT(BlueprintType, meta = (DisableSplitPin, HasNativeMake = "/Script/Engine.BlueprintInstancedStructLibrary.MakeInstancedStruct"))
32{
34
35public:
36
38
40
46
48 {
49 InitializeAs(InOther.GetScriptStruct(), InOther.GetMemory());
50 }
51
53 : FInstancedStruct(InOther.GetScriptStruct(), InOther.GetMutableMemory())
54 {
55 InOther.SetStructData(nullptr,nullptr);
56 }
57
59 {
60 Reset();
61 }
62
64
66 {
67 if (this != &InOther)
68 {
69 InitializeAs(InOther.GetScriptStruct(), InOther.GetMemory());
70 }
71 return *this;
72 }
73
75 {
76 if (this != &InOther)
77 {
78 Reset();
79
80 SetStructData(InOther.GetScriptStruct(), InOther.GetMutableMemory());
81 InOther.SetStructData(nullptr,nullptr);
82 }
83 return *this;
84 }
85
87 UE_API void InitializeAs(const UScriptStruct* InScriptStruct, const uint8* InStructMemory = nullptr);
88
90 template<typename T, typename... TArgs>
91 T& InitializeAs(TArgs&&... InArgs)
92 {
93 UE::StructUtils::CheckStructType<T>();
94
96 uint8* Memory = nullptr;
97
98 const UScriptStruct* CurrentScriptStruct = GetScriptStruct();
100 {
101 // Struct type already matches; return the struct memory to a destroyed state so we can placement new over it
102 Memory = GetMutableMemory();
103 ((T*)Memory)->~T();
104 }
105 else
106 {
107 // Struct type mismatch; reset and reinitialize
108 Reset();
109
110 const int32 MinAlignment = Struct->GetMinAlignment();
111 const int32 RequiredSize = Struct->GetStructureSize();
112 Memory = (uint8*)FMemory::Malloc(FMath::Max(1, RequiredSize), MinAlignment);
113 SetStructData(Struct, Memory);
114 }
115
116 check(Memory);
117 new (Memory) T(Forward<TArgs>(InArgs)...);
118
119 return *((T*)Memory);
120 }
121
123 template<typename T>
125 {
126 UE::StructUtils::CheckStructType<T>();
127
128 FInstancedStruct InstancedStruct;
129 InstancedStruct.InitializeAs(TBaseStructure<T>::Get(), nullptr);
130 return InstancedStruct;
131 }
132
134 template<typename T>
135 static FInstancedStruct Make(const T& Struct)
136 {
137 UE::StructUtils::CheckStructType<T>();
138
139 FInstancedStruct InstancedStruct;
140 InstancedStruct.InitializeAs(TBaseStructure<T>::Get(), reinterpret_cast<const uint8*>(&Struct));
141 return InstancedStruct;
142 }
143
145 template<typename T, typename... TArgs>
146 static FInstancedStruct Make(TArgs&&... InArgs)
147 {
148 UE::StructUtils::CheckStructType<T>();
149
150 FInstancedStruct InstancedStruct;
151 InstancedStruct.InitializeAs<T>(Forward<TArgs>(InArgs)...);
152 return InstancedStruct;
153 }
154
156 UE_API bool Serialize(FArchive& Ar, UStruct* DefaultsStruct = nullptr, const void* Defaults = nullptr);
157 UE_API bool Identical(const FInstancedStruct* Other, uint32 PortFlags) const;
158 UE_API void AddStructReferencedObjects(FReferenceCollector& Collector);
159 UE_API bool ExportTextItem(FString& ValueStr, FInstancedStruct const& DefaultValue, UObject* Parent, int32 PortFlags, UObject* ExportRootScope) const;
160 UE_API bool ImportTextItem(const TCHAR*& Buffer, int32 PortFlags, UObject* Parent, FOutputDevice* ErrorText, FArchive* InSerializingArchive = nullptr);
161 UE_API bool SerializeFromMismatchedTag(const FPropertyTag& Tag, FStructuredArchive::FSlot Slot);
163 UE_API bool NetSerialize(FArchive& Ar, UPackageMap* Map, bool& bOutSuccess);
164 UE_API bool FindInnerPropertyInstance(FName PropertyName, const FProperty*& OutProp, const void*& OutData) const;
167
170 {
171 return ScriptStruct;
172 }
173
175 const uint8* GetMemory() const
176 {
177 return StructMemory;
178 }
179
181 UE_API void Reset();
182
184 template<typename T>
185 const T& Get() const
186 {
187 return UE::StructUtils::GetStructRef<T>(ScriptStruct, StructMemory);
188 }
189
191 template<typename T>
192 const T* GetPtr() const
193 {
194 return UE::StructUtils::GetStructPtr<T>(ScriptStruct, StructMemory);
195 }
196
199 {
200 return StructMemory;
201 }
202
204 template<typename T>
206 {
207 uint8* Memory = GetMutableMemory();
208 const UScriptStruct* Struct = GetScriptStruct();
209 check(Memory != nullptr);
210 check(Struct != nullptr);
211 check(Struct->IsChildOf(TBaseStructure<T>::Get()));
212 return *((T*)Memory);
213 }
214
216 template<typename T>
218 {
219 uint8* Memory = GetMutableMemory();
220 const UScriptStruct* Struct = GetScriptStruct();
221 if (Memory != nullptr && Struct && Struct->IsChildOf(TBaseStructure<T>::Get()))
222 {
223 return ((T*)Memory);
224 }
225 return nullptr;
226 }
227
229 bool IsValid() const
230 {
231 return GetMemory() != nullptr && GetScriptStruct() != nullptr;
232 }
233
236 {
237 return Identical(&Other, PPF_None);
238 }
239
241 {
242 return !Identical(&Other, PPF_None);
243 }
244
245#if WITH_EDITOR
248#endif // WITH_EDITOR
249
252
253protected:
254
256 : ScriptStruct(InScriptStruct)
257 , StructMemory(InStructMemory)
258 {}
260 {
261 StructMemory = nullptr;
262 ScriptStruct = nullptr;
263 }
265 {
266 ScriptStruct = InScriptStruct;
267 StructMemory = InStructMemory;
268 }
269
270 TObjectPtr<const UScriptStruct> ScriptStruct = nullptr;
271 uint8* StructMemory = nullptr;
272};
273
274template<>
292
305template<typename BaseStructT>
307{
308public:
309 TInstancedStruct() = default;
310
315 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
317 {
318 InitializeAsScriptStruct(InOther.GetScriptStruct(), InOther.GetMemory());
319 }
320
321 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
323 {
324 InitializeAsScriptStruct(InScriptStruct);
325 }
326
327 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
329 : InstancedStruct(InOther.InstancedStruct)
330 {
331 }
332
333 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
335 : InstancedStruct(MoveTemp(InOther.InstancedStruct))
336 {
337 }
338
339 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
341 {
342 if (TConstStructView<T>(*this) != InOther)
343 {
344 InstancedStruct.InitializeAs(InOther.GetScriptStruct(), InOther.GetMemory());
345 }
346 return *this;
347 }
348
349 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
351 {
352 InstancedStruct = InOther.InstancedStruct;
353 return *this;
354 }
355
356 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
358 {
359 InstancedStruct = MoveTemp(InOther.InstancedStruct);
360 return *this;
361 }
362
365 {
366 checkf(InScriptStruct->IsChildOf(TBaseStructure<BaseStructT>::Get()), TEXT("ScriptStruct must be a child of BaseStruct!"));
367 InstancedStruct.InitializeAs(InScriptStruct, InStructMemory);
368 }
369
371 template<typename T = BaseStructT, typename... TArgs, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
372 void InitializeAs(TArgs&&... InArgs)
373 {
374 InstancedStruct.InitializeAs<T>(Forward<TArgs>(InArgs)...);
375 }
376
378 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
380 {
381 TInstancedStruct This;
382 This.InstancedStruct.InitializeAs(TBaseStructure<T>::Get(), nullptr);
383 return This;
384 }
385
387 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
388 static TInstancedStruct Make(const T& Struct)
389 {
390 TInstancedStruct This;
391 This.InstancedStruct.InitializeAs(TBaseStructure<T>::Get(), reinterpret_cast<const uint8*>(&Struct));
392 return This;
393 }
394
396 template<typename T = BaseStructT, typename... TArgs, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
397 static TInstancedStruct Make(TArgs&&... InArgs)
398 {
399 TInstancedStruct This;
400 This.InstancedStruct.template InitializeAs<T>(Forward<TArgs>(InArgs)...);
401 return This;
402 }
403
406 {
407 return InstancedStruct.GetScriptStruct();
408 }
409
411 const uint8* GetMemory() const
412 {
413 return InstancedStruct.GetMemory();
414 }
415
417 void Reset()
418 {
419 InstancedStruct.Reset();
420 }
421
423 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
424 const T& Get() const
425 {
426 return InstancedStruct.Get<T>();
427 }
428
430 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
431 const T* GetPtr() const
432 {
433 return UE::StructUtils::GetStructPtr<T, BaseStructT>(InstancedStruct.GetScriptStruct(), InstancedStruct.GetMemory());
434 }
435
438 {
439 return InstancedStruct.GetMutableMemory();
440 }
441
443 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
445 {
446 return InstancedStruct.GetMutable<T>();
447 }
448
450 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
452 {
453 return UE::StructUtils::GetStructPtr<T, BaseStructT>(InstancedStruct.GetScriptStruct(), InstancedStruct.GetMutableMemory());
454 }
455
457 bool IsValid() const
458 {
459 return InstancedStruct.IsValid();
460 }
461
463 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
465 {
466 return InstancedStruct == Other.InstancedStruct;
467 }
468
469 template<typename T = BaseStructT, typename = std::enable_if_t<std::is_base_of_v<BaseStructT, std::decay_t<T>>>>
471 {
472 return InstancedStruct != Other.InstancedStruct;
473 }
474
476 {
477 InstancedStruct.AddStructReferencedObjects(Collector);
478 }
479
481 {
482 return InstancedStruct.Serialize(Ar);
483 }
484
485 bool NetSerialize(FArchive& Ar, UPackageMap* Map, bool& bOutSuccess)
486 {
487 return InstancedStruct.NetSerialize(Ar, Map, bOutSuccess);
488 }
489
490private:
499 FInstancedStruct InstancedStruct;
500
501 template <typename U> friend struct TInstancedStruct;
502};
503
504#undef UE_API
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define USTRUCT(...)
Definition ObjectMacros.h:746
@ PPF_None
Definition PropertyPortFlags.h:15
EPropertyVisitorControlFlow
Definition PropertyVisitor.h:15
#define UE_API
Definition InstancedStruct.h:11
decltype(auto) Visit(Func &&Callable, Variants &&... Args)
Definition TVariant.h:271
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
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
Definition NameTypes.h:617
Definition OutputDevice.h:133
Definition UnrealType.h:174
Definition UObjectGlobals.h:2492
Definition StructuredArchiveSlots.h:52
Definition Array.h:670
Definition AssetRegistryState.h:50
Definition Object.h:95
Definition CoreNet.h:191
Definition Class.h:1720
virtual COREUOBJECT_API void * ResolveVisitedPathInfo(void *Data, const FPropertyVisitorInfo &Info) const override
Definition Class.cpp:3871
virtual COREUOBJECT_API bool FindInnerPropertyInstance(FName PropertyName, const void *Data, const FProperty *&OutProp, const void *&OutData) const
Definition Class.cpp:3839
Definition Class.h:480
int16 MinAlignment
Definition Class.h:508
virtual COREUOBJECT_API void GetPreloadDependencies(TArray< UObject * > &OutDeps) override
Definition Class.cpp:750
Definition UserDefinedStruct.h:61
Definition SharedStruct.h:538
Definition StructView.h:217
Definition InstancedStruct.h:32
T & GetMutable()
Definition InstancedStruct.h:205
T & InitializeAs(TArgs &&... InArgs)
Definition InstancedStruct.h:91
T * GetMutablePtr()
Definition InstancedStruct.h:217
uint8 * GetMutableMemory()
Definition InstancedStruct.h:198
static FInstancedStruct Make()
Definition InstancedStruct.h:124
static UE_API FNetSerializeInstancedStruct NetSerializeScriptStructDelegate
Definition InstancedStruct.h:251
static FInstancedStruct Make(TArgs &&... InArgs)
Definition InstancedStruct.h:146
FInstancedStruct & operator=(const FInstancedStruct &InOther)
Definition InstancedStruct.h:65
bool operator!=(const FInstancedStruct &Other) const
Definition InstancedStruct.h:240
void ResetStructData()
Definition InstancedStruct.h:259
void SetStructData(const UScriptStruct *InScriptStruct, uint8 *InStructMemory)
Definition InstancedStruct.h:264
UE_API void InitializeAs(const UScriptStruct *InScriptStruct, const uint8 *InStructMemory=nullptr)
Definition InstancedStruct.cpp:69
static FInstancedStruct Make(const T &Struct)
Definition InstancedStruct.h:135
const T & Get() const
Definition InstancedStruct.h:185
FInstancedStruct(FInstancedStruct &&InOther)
Definition InstancedStruct.h:52
~FInstancedStruct()
Definition InstancedStruct.h:58
FInstancedStruct & operator=(FInstancedStruct &&InOther)
Definition InstancedStruct.h:74
DECLARE_DELEGATE_RetVal_ThreeParams(bool, FNetSerializeInstancedStruct, FInstancedStruct &, FArchive &, UPackageMap *)
const uint8 * GetMemory() const
Definition InstancedStruct.h:175
bool operator==(const FInstancedStruct &Other) const
Definition InstancedStruct.h:235
FInstancedStruct(const FInstancedStruct &InOther)
Definition InstancedStruct.h:47
FInstancedStruct(const UScriptStruct *InScriptStruct, uint8 *InStructMemory)
Definition InstancedStruct.h:255
bool IsValid() const
Definition InstancedStruct.h:229
const UScriptStruct * GetScriptStruct() const
Definition InstancedStruct.h:169
const T * GetPtr() const
Definition InstancedStruct.h:192
Definition PropertyTag.h:38
Definition PropertyVisitor.h:268
Definition PropertyVisitor.h:32
static UScriptStruct * Get()
Definition Class.h:5275
Definition Class.h:5288
Definition SharedStruct.h:740
Definition StructView.h:328
Definition InstancedStruct.h:307
const uint8 * GetMemory() const
Definition InstancedStruct.h:411
TInstancedStruct & operator=(const TConstStructView< T > InOther)
Definition InstancedStruct.h:340
static TInstancedStruct Make(const T &Struct)
Definition InstancedStruct.h:388
TInstancedStruct & operator=(TInstancedStruct< T > &&InOther)
Definition InstancedStruct.h:357
T * GetMutablePtr()
Definition InstancedStruct.h:451
TInstancedStruct(const TConstStructView< T > InOther)
Definition InstancedStruct.h:316
void InitializeAsScriptStruct(const UScriptStruct *InScriptStruct, const uint8 *InStructMemory=nullptr)
Definition InstancedStruct.h:364
TInstancedStruct(TInstancedStruct< T > &&InOther)
Definition InstancedStruct.h:334
static TInstancedStruct Make()
Definition InstancedStruct.h:379
TInstancedStruct(const UScriptStruct *InScriptStruct)
Definition InstancedStruct.h:322
bool IsValid() const
Definition InstancedStruct.h:457
T & GetMutable()
Definition InstancedStruct.h:444
void AddReferencedObjects(FReferenceCollector &Collector)
Definition InstancedStruct.h:475
TInstancedStruct()=default
TInstancedStruct(const TInstancedStruct< T > &InOther)
Definition InstancedStruct.h:328
bool NetSerialize(FArchive &Ar, UPackageMap *Map, bool &bOutSuccess)
Definition InstancedStruct.h:485
uint8 * GetMutableMemory()
Definition InstancedStruct.h:437
void InitializeAs(TArgs &&... InArgs)
Definition InstancedStruct.h:372
static TInstancedStruct Make(TArgs &&... InArgs)
Definition InstancedStruct.h:397
const T & Get() const
Definition InstancedStruct.h:424
bool Serialize(FArchive &Ar)
Definition InstancedStruct.h:480
const UScriptStruct * GetScriptStruct() const
Definition InstancedStruct.h:405
bool operator!=(const TInstancedStruct< T > &Other) const
Definition InstancedStruct.h:470
TInstancedStruct & operator=(const TInstancedStruct< T > &InOther)
Definition InstancedStruct.h:350
bool operator==(const TInstancedStruct< T > &Other) const
Definition InstancedStruct.h:464
void Reset()
Definition InstancedStruct.h:417
const T * GetPtr() const
Definition InstancedStruct.h:431
Definition ObjectPtr.h:488
Definition StructOpsTypeTraits.h:11
@ WithStructuredSerializeFromMismatchedTag
Definition StructOpsTypeTraits.h:29
@ WithVisitor
Definition StructOpsTypeTraits.h:37
@ WithGetPreloadDependencies
Definition StructOpsTypeTraits.h:32
@ WithAddStructReferencedObjects
Definition StructOpsTypeTraits.h:22
@ WithNetSerializer
Definition StructOpsTypeTraits.h:26
@ WithFindInnerPropertyInstance
Definition StructOpsTypeTraits.h:34
@ WithClearOnFinishDestroy
Definition StructOpsTypeTraits.h:36
@ WithExportTextItem
Definition StructOpsTypeTraits.h:20
@ WithSerializer
Definition StructOpsTypeTraits.h:23
@ WithImportTextItem
Definition StructOpsTypeTraits.h:21
@ WithIdentical
Definition StructOpsTypeTraits.h:19
Definition StructOpsTypeTraits.h:46