UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
FieldIterator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4FieldIterator.h: FField iterators.
5=============================================================================*/
6
7#pragma once
8
9#include "CoreMinimal.h"
10#include "UObject/Field.h"
11#include "UObject/UnrealType.h"
13
17template <class FieldType>
19{
20 check(false);
21}
22
23template <>
25{
26 Owner->GetInnerFields(OutFields);
27}
28
29template <>
33
34//
35// For iterating through all fields in all structs including inner FProperties of top level FProperties.
36//
37template <class T>
39{
40private:
42 TObjectIterator<UStruct> StructIterator;
52 int32 CurrentFieldIndex = -1;
53
54public:
56 : StructIterator(AdditionalExclusionFlags, /*bIncludeDerivedClasses =*/ true, InternalExclusionFlags)
57 , FieldIterator(nullptr)
58 {
59 // Currently 3 would be enough (the current field + its inners which is 2 max for FMapProperty) but we keep one extra as slack
60 // We never free this array memory inside of TAllFieldsIterator except when TAllFieldsIterator gets destroyed for performance reasons so it may only grow.
61 // In the future we may want to support TArrays of TArrays/TMaps (nested containers) and in such case it may grow beyond 4 but that's ok
62 CurrentFields.Reserve(4);
64 }
65
67 UE_FORCEINLINE_HINT explicit operator bool() const
68 {
69 return (bool)FieldIterator || (bool)StructIterator;
70 }
73 {
74 return !(bool)*this;
75 }
76
77 inline bool operator==(const TAllFieldsIterator<T>& Rhs) const
78 {
79 return *FieldIterator == *Rhs.FieldIterator && CurrentFieldIndex == Rhs.CurrentFieldIndex;
80 }
81 inline bool operator!=(const TAllFieldsIterator<T>& Rhs) const
82 {
83 return *FieldIterator != *Rhs.FieldIterator || CurrentFieldIndex != Rhs.CurrentFieldIndex;
84 }
85
91 inline T* operator*()
92 {
93 if (CurrentFieldIndex >= 0)
94 {
95 return CastFieldChecked<T>(CurrentFields[CurrentFieldIndex]);
96 }
97 return nullptr;
98 }
99 inline T* operator->()
100 {
101 if (CurrentFieldIndex >= 0)
102 {
103 return CastFieldChecked<T>(CurrentFields[CurrentFieldIndex]);
104 }
105 return nullptr;
106 }
107protected:
108
110 inline void InitCurrentFields()
111 {
112 CurrentFieldIndex = -1;
113 CurrentFields.Reset();
114 typename T::BaseFieldClass* CurrentField = *FieldIterator;
115 CurrentFields.Add(CurrentField);
117 }
118
120 inline void IterateToNextField()
121 {
122 while (FieldIterator)
123 {
124 for (++CurrentFieldIndex; CurrentFieldIndex < CurrentFields.Num(); ++CurrentFieldIndex)
125 {
126 if (CurrentFields[CurrentFieldIndex]->template IsA<T>())
127 {
128 break;
129 }
130 }
131
132 if (CurrentFieldIndex == CurrentFields.Num())
133 {
134 ++FieldIterator;
135 if (FieldIterator)
136 {
138 }
139 else
140 {
141 CurrentFieldIndex = -1;
142 }
143 }
144 else
145 {
146 break;
147 }
148 }
149 }
150
152 inline void InitFieldIterator()
153 {
154 while (StructIterator)
155 {
156 FieldIterator.~TFieldIterator<typename T::BaseFieldClass>();
158 if (!FieldIterator)
159 {
160 // This struct has no fields, check the next one
161 ++StructIterator;
162 CurrentFieldIndex = -1;
163 }
164 else
165 {
168
169 if (!FieldIterator)
170 {
171 // If the field iterator is invalid after IterateToNextField() call then no fields of the speficied template type were found
172 ++StructIterator;
173 }
174 else
175 {
176 break;
177 }
178 }
179 }
180 }
182 {
183 if (!FieldIterator)
184 {
185 // We finished iterating over all fields of the current struct so move to the next struct
186 ++StructIterator;
188 }
189 }
190};
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
void GetInnerFieldsFromField(FieldType *Owner, TArray< FieldType * > &OutFields)
Definition FieldIterator.h:18
const bool
Definition NetworkReplayStreaming.h:178
EInternalObjectFlags
Definition ObjectMacros.h:631
EObjectFlags
Definition ObjectMacros.h:552
@ RF_ClassDefaultObject
This object is used as the default template for all instances of a class. One object is created for e...
Definition ObjectMacros.h:563
Definition Field.h:556
Definition FieldIterator.h:39
bool operator==(const TAllFieldsIterator< T > &Rhs) const
Definition FieldIterator.h:77
void InitCurrentFields()
Definition FieldIterator.h:110
bool operator!=(const TAllFieldsIterator< T > &Rhs) const
Definition FieldIterator.h:81
void IterateToNextField()
Definition FieldIterator.h:120
T * operator*()
Definition FieldIterator.h:91
UE_FORCEINLINE_HINT bool operator!() const
Definition FieldIterator.h:72
void ConditionallyIterateToNextStruct()
Definition FieldIterator.h:181
void InitFieldIterator()
Definition FieldIterator.h:152
T * operator->()
Definition FieldIterator.h:99
void operator++()
Definition FieldIterator.h:86
TAllFieldsIterator(EObjectFlags AdditionalExclusionFlags=RF_ClassDefaultObject, EInternalObjectFlags InternalExclusionFlags=EInternalObjectFlags::None)
Definition FieldIterator.h:55
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition UnrealType.h:7083
Definition UObjectIterator.h:257
Definition Class.h:181
@ ExcludeSuper
Definition UnrealType.h:7041
@ IncludeInterfaces
Definition UnrealType.h:7054
@ IncludeDeprecated
Definition UnrealType.h:7048