UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StructUtils.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "UObject/Class.h"
7
8namespace UE::StructUtils
9{
10 template <typename T>
19
20 template <typename T>
29
31 template<typename T>
32 T& GetStructRef(const UScriptStruct* ScriptStruct, void* StructMemory)
33 {
34 check(StructMemory != nullptr);
35 check(ScriptStruct != nullptr);
36 checkf(ScriptStruct == TBaseStructure<T>::Get() || ScriptStruct->IsChildOf(TBaseStructure<T>::Get()),
37 TEXT("Incompatible struct types: Cannot cast '%s' to '%s'"),
38 *ScriptStruct->GetPathName(),
39 *TBaseStructure<T>::Get()->GetPathName());
40 return *((T*)StructMemory);
41 }
42
44 template<typename T>
45 T* GetStructPtr(const UScriptStruct* ScriptStruct, void* StructMemory)
46 {
47 if (StructMemory != nullptr
48 && ScriptStruct
49 && (ScriptStruct == TBaseStructure<T>::Get()
50 || ScriptStruct->IsChildOf(TBaseStructure<T>::Get())))
51 {
52 return ((T*)StructMemory);
53 }
54 return nullptr;
55 }
56
58 template<typename T, typename BaseStructT>
59 T* GetStructPtr(const UScriptStruct* ScriptStruct, void* StructMemory)
60 {
61 if constexpr (std::is_same_v<BaseStructT, std::decay_t<T>>)
62 {
63#if WITH_EDITOR || !UE_BUILD_SHIPPING
64 if (StructMemory && ScriptStruct)
65 {
66 // An extra guard to verify that ScriptStruct is actually safe to cast to T
67 if (ensureAlwaysMsgf(ScriptStruct == TBaseStructure<T>::Get() || ScriptStruct->IsChildOf(TBaseStructure<T>::Get()),
68 TEXT("Incompatible struct types: Cannot cast '%s' to '%s'. Falling back to return nullptr, but this may crash in shipping!"), *ScriptStruct->GetPathName(), *TBaseStructure<T>::Get()->GetPathName()))
69 {
70 return ((T*)StructMemory);
71 }
72 }
73 return nullptr;
74#else
75 return ((T*)StructMemory);
76#endif
77 }
78 else
79 {
80 return GetStructPtr<T>(ScriptStruct, StructMemory);
81 }
82 }
83
85 template<typename T>
86 const T& GetStructRef(const UScriptStruct* ScriptStruct, const void* StructMemory)
87 {
88 return GetStructRef<T>(ScriptStruct, const_cast<void*>(StructMemory));
89 }
90
92 template<typename T>
93 const T* GetStructPtr(const UScriptStruct* ScriptStruct, const void* StructMemory)
94 {
95 return GetStructPtr<T>(ScriptStruct, const_cast<void*>(StructMemory));
96 }
97
99 template<typename T, typename BaseStructT>
100 const T* GetStructPtr(const UScriptStruct* ScriptStruct, const void* StructMemory)
101 {
102 return GetStructPtr<T, BaseStructT>(ScriptStruct, const_cast<void*>(StructMemory));
103 }
104
110 {
111 // Clamp minimum index at the start of the range, adjusting the length down if necessary
115
116 // Clamp maximum index at the end of the range
117 InOutIndex = (InOutIndex > ArrayNum) ? ArrayNum : InOutIndex;
118
119 // Clamp count between 0 and the distance to the end of the range
120 InOutCount = FMath::Clamp(InOutCount, 0, (ArrayNum - InOutIndex));
121 }
122}
#define ensureAlwaysMsgf(InExpression, InFormat,...)
Definition AssertionMacros.h:467
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
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
COREUOBJECT_API FString GetPathName(const UObject *StopOuter=NULL) const
Definition UObjectBaseUtility.cpp:38
Definition Class.h:1720
bool IsChildOf() const
Definition Class.h:788
Definition PropertyBag.cpp:61
void CheckWrapperType()
Definition StructUtils.h:21
void CheckStructType()
Definition StructUtils.h:11
void CalcMidIndexAndCount(int32 ArrayNum, int32 &InOutIndex, int32 &InOutCount)
Definition StructUtils.h:109
T * GetStructPtr(const UScriptStruct *ScriptStruct, void *StructMemory)
Definition StructUtils.h:45
T & GetStructRef(const UScriptStruct *ScriptStruct, void *StructMemory)
Definition StructUtils.h:32
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition Class.h:5288
Definition UnrealTypeTraits.h:40