UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WeakFieldPtr.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 WeakFieldPtr.h: Weak pointer to FField
5=============================================================================*/
6
7#pragma once
8
12#include "UObject/FieldPath.h"
13
14#include <type_traits>
15
16template<class T>
17struct TWeakFieldPtr;
18
19template <
20 typename LhsType,
21 typename RhsType
22 UE_REQUIRES(UE_REQUIRES_EXPR((LhsType*)nullptr == (RhsType*)nullptr))
23>
25
26template <
27 typename LhsType,
28 typename RhsType
29 UE_REQUIRES(UE_REQUIRES_EXPR((LhsType*)nullptr == (const RhsType*)nullptr))
30>
31bool operator==(const TWeakFieldPtr<LhsType>& Lhs, const RhsType* Rhs);
32
33template <
34 typename LhsType,
35 typename RhsType
36 UE_REQUIRES(UE_REQUIRES_EXPR((const LhsType*)nullptr == (RhsType*)nullptr))
37>
38bool operator==(const LhsType* Lhs, const TWeakFieldPtr<RhsType>& Rhs);
39
40#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
41template <
42 typename LhsType,
43 typename RhsType
44 UE_REQUIRES(UE_REQUIRES_EXPR((LhsType*)nullptr != (RhsType*)nullptr))
45>
47
48template <
49 typename LhsType,
50 typename RhsType
51 UE_REQUIRES(UE_REQUIRES_EXPR((LhsType*)nullptr != (const RhsType*)nullptr))
52>
53bool operator!=(const TWeakFieldPtr<LhsType>& Lhs, const RhsType* Rhs);
54
55template <
56 typename LhsType,
57 typename RhsType
58 UE_REQUIRES(UE_REQUIRES_EXPR((const LhsType*)nullptr != (RhsType*)nullptr))
59>
60bool operator!=(const LhsType* Lhs, const TWeakFieldPtr<RhsType>& Rhs);
61#endif
62
63template<class T>
65{
66 template <typename>
67 friend struct TWeakFieldPtr;
68
69 template <
70 typename LhsType,
71 typename RhsType
72 UE_REQUIRES_FRIEND(UE_REQUIRES_EXPR((LhsType*)nullptr == (RhsType*)nullptr))
73 >
74 friend bool operator==(const TWeakFieldPtr<LhsType>& Lhs, const TWeakFieldPtr<RhsType>& Rhs);
75
76 template <
77 typename LhsType,
78 typename RhsType
79 UE_REQUIRES_FRIEND(UE_REQUIRES_EXPR((LhsType*)nullptr == (const RhsType*)nullptr))
80 >
81 friend bool operator==(const TWeakFieldPtr<LhsType>& Lhs, const RhsType* Rhs);
82
83 template <
84 typename LhsType,
85 typename RhsType
86 UE_REQUIRES_FRIEND(UE_REQUIRES_EXPR((const LhsType*)nullptr == (RhsType*)nullptr))
87 >
88 friend bool operator==(const LhsType* Lhs, const TWeakFieldPtr<RhsType>& Rhs);
89
90#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
91 template <
92 typename LhsType,
93 typename RhsType
94 UE_REQUIRES_FRIEND(UE_REQUIRES_EXPR((LhsType*)nullptr != (RhsType*)nullptr))
95 >
96 friend bool operator!=(const TWeakFieldPtr<LhsType>& Lhs, const TWeakFieldPtr<RhsType>& Rhs);
97
98 template <
99 typename LhsType,
100 typename RhsType
101 UE_REQUIRES_FRIEND(UE_REQUIRES_EXPR((LhsType*)nullptr != (const RhsType*)nullptr))
102 >
103 friend bool operator!=(const TWeakFieldPtr<LhsType>& Lhs, const RhsType* Rhs);
104
105 template <
106 typename LhsType,
107 typename RhsType
108 UE_REQUIRES_FRIEND(UE_REQUIRES_EXPR((const LhsType*)nullptr != (RhsType*)nullptr))
109 >
110 friend bool operator!=(const LhsType* Lhs, const TWeakFieldPtr<RhsType>& Rhs);
111#endif
112
113private:
114
115 // These exists only to disambiguate the two constructors below
116 enum EDummy1 { Dummy1 };
117
119 mutable TFieldPath<T> Field;
120
121public:
122 using ElementType = T;
123
124 TWeakFieldPtr() = default;
125 TWeakFieldPtr(const TWeakFieldPtr&) = default;
127 ~TWeakFieldPtr() = default;
128
133 : Owner((UObject*)nullptr)
134 , Field()
135 {
136 }
137
142 template <
143 typename U
144 UE_REQUIRES(std::is_convertible_v<U*, T*>)
145 >
146 inline TWeakFieldPtr(U* InField, EDummy1 = Dummy1)
147 : Owner(InField ? InField->GetOwnerUObject() : (UObject*)nullptr)
148 , Field(InField)
149 {
150 // This static assert is in here rather than in the body of the class because we want
151 // to be able to define TWeakFieldPtr<UUndefinedClass>.
152 static_assert(std::is_convertible_v<T*, const volatile FField*>, "TWeakFieldPtr can only be constructed with FField types");
153 }
154
159 template <
160 typename OtherT
161 UE_REQUIRES(std::is_convertible_v<OtherT*, T*>)
162 >
164 : Owner(Other.Owner)
165 , Field(Other.Field)
166 {
167 // This static assert is in here rather than in the body of the class because we want
168 // to be able to define TWeakFieldPtr<UUndefinedClass>.
169 static_assert(std::is_convertible_v<T*, const volatile FField*>, "TWeakFieldPtr can only be constructed with FField types");
170 }
171
175 inline void Reset()
176 {
177 Owner.Reset();
178 Field.Reset();
179 }
180
185 template <
186 typename U
187 UE_REQUIRES(std::is_convertible_v<U*, T*>)
188 >
189 inline void operator=(U* InField)
190 {
191 Owner = InField ? InField->GetOwnerUObject() : (UObject*)nullptr;
192 Field = (U*)InField;
193 }
194
199 template <
200 typename OtherT
201 UE_REQUIRES(std::is_convertible_v<OtherT*, T*>)
202 >
204 {
205 Owner = Other.Owner;
206 Field = Other.Field;
207 }
208
214 inline T* Get(bool bEvenIfPendingKill) const
215 {
216 if (Owner.Get(bEvenIfPendingKill))
217 {
218 return Field.Get();
219 }
220 else
221 {
222 // Clear potentially stale pointer to the actual field
223 Field.ClearCachedField();
224 }
225 return nullptr;
226 }
227
231 inline T* Get(/*bool bEvenIfPendingKill = false*/) const
232 {
233 if (Owner.Get())
234 {
235 return Field.Get();
236 }
237 else
238 {
239 // Clear potentially stale pointer to the actual field
240 Field.ClearCachedField();
241 }
242 return nullptr;
243 }
244
246 inline T* GetEvenIfUnreachable() const
247 {
248 if (Owner.GetEvenIfUnreachable())
249 {
250 return Field.Get();
251 }
252 else
253 {
254 // Clear potentially stale pointer to the actual field
255 Field.ClearCachedField();
256 }
257 return nullptr;
258 }
259
264 {
265 return *Get();
266 }
267
272 {
273 return Get();
274 }
275
284 {
285 return Owner.IsValid(bEvenIfPendingKill, bThreadsafeTest) && Field.Get();
286 }
287
292 UE_FORCEINLINE_HINT bool IsValid(/*bool bEvenIfPendingKill = false, bool bThreadsafeTest = false*/) const
293 {
294 return Owner.IsValid() && Field.Get();
295 }
296
304 {
305 return Owner.IsStale(bIncludingIfPendingKill, bThreadsafeTest);
306 }
307
309 {
310 return Owner.HasSameIndexAndSerialNumber(Other.Owner);
311 }
312
318
319 inline void Serialize(FArchive& Ar)
320 {
321 Ar << Owner;
322 Ar << Field;
323 }
324};
325
331template <
332 typename LhsType,
333 typename RhsType
335>
337{
338 return Lhs.Field == Rhs.Field;
339}
340
346template <
347 typename LhsType,
348 typename RhsType
349 UE_REQUIRES_DEFINITION(UE_REQUIRES_EXPR((LhsType*)nullptr == (const RhsType*)nullptr))
350>
352{
353 return Lhs.Field == Rhs;
354}
355
361template <
362 typename LhsType,
363 typename RhsType
364 UE_REQUIRES_DEFINITION(UE_REQUIRES_EXPR((const LhsType*)nullptr == (RhsType*)nullptr))
365>
367{
368 return Lhs == Rhs.Field;
369}
370
375template <typename LhsType>
377{
378 return !Lhs.Get();
379}
380
385template <typename RhsType>
387{
388 return !Rhs.Get();
389}
390
391#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
397template <
398 typename LhsType,
399 typename RhsType
401>
403{
404 return !(Lhs == Rhs);
405}
406
412template <
413 typename LhsType,
414 typename RhsType
415 UE_REQUIRES_DEFINITION(UE_REQUIRES_EXPR((LhsType*)nullptr != (const RhsType*)nullptr))
416>
418{
419 return !(Lhs == Rhs);
420}
421
427template <
428 typename LhsType,
429 typename RhsType
430 UE_REQUIRES_DEFINITION(UE_REQUIRES_EXPR((const LhsType*)nullptr != (RhsType*)nullptr))
431>
433{
434 return !(Lhs == Rhs);
435}
436
441template <typename LhsType>
443{
444 return !(Lhs == nullptr);
445}
446
451template <typename RhsType>
453{
454 return !(nullptr == Rhs);
455}
456#endif
457
458// Helper function which deduces the type of the initializer
459template <typename T>
464
465template<class T> struct TIsPODType<TWeakFieldPtr<T> > { enum { Value = true }; };
466template<class T> struct TIsZeroConstructType<TWeakFieldPtr<T> > { enum { Value = true }; };
467template<class T> struct TIsWeakPointerType<TWeakFieldPtr<T> > { enum { Value = true }; };
468
469
473template <typename KeyType, typename ValueType, bool bInAllowDuplicateKeys = false>
474struct TWeakFieldPtrMapKeyFuncs : public TDefaultMapKeyFuncs<KeyType, ValueType, bInAllowDuplicateKeys>
475{
477
479 {
480 return A == B;
481 }
482
484 {
485 return GetTypeHash(Key);
486 }
487};
488
489template<class T>
FPlatformTypes::TYPE_OF_NULLPTR TYPE_OF_NULLPTR
The type of the C++ nullptr keyword.
Definition Platform.h:1157
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_REQUIRES_DEFINITION(...)
Definition Requires.h:88
#define UE_REQUIRES_FRIEND(...)
Definition Requires.h:87
#define UE_REQUIRES(...)
Definition Requires.h:86
#define UE_REQUIRES_EXPR(...)
Definition Requires.h:89
bool operator!=(const TWeakFieldPtr< LhsType > &Lhs, const TWeakFieldPtr< RhsType > &Rhs)
Definition WeakFieldPtr.h:402
bool operator==(const TWeakFieldPtr< LhsType > &Lhs, const TWeakFieldPtr< RhsType > &Rhs)
Definition WeakFieldPtr.h:336
UE_FORCEINLINE_HINT TWeakFieldPtr< T > MakeWeakFieldPtr(T *Ptr)
Definition WeakFieldPtr.h:460
FArchive & operator<<(FArchive &Ar, TWeakFieldPtr< T > &WeakFieldPtr)
Definition WeakFieldPtr.h:490
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
virtual void Serialize(void *V, int64 Length)
Definition Archive.h:1689
Definition Object.h:95
Definition FieldSystemNoiseAlgo.cpp:6
Definition Map.h:77
TTypeTraits< KeyType >::ConstPointerType KeyInitType
Definition Map.h:78
Definition FieldPath.h:283
Definition IsPODType.h:12
@ Value
Definition IsPODType.h:13
Definition UnrealTypeTraits.h:181
@ Value
Definition UnrealTypeTraits.h:182
Definition UnrealTypeTraits.h:172
Definition WeakFieldPtr.h:475
TDefaultMapKeyFuncs< KeyType, ValueType, bInAllowDuplicateKeys >::KeyInitType KeyInitType
Definition WeakFieldPtr.h:476
static UE_FORCEINLINE_HINT uint32 GetKeyHash(KeyInitType Key)
Definition WeakFieldPtr.h:483
static UE_FORCEINLINE_HINT bool Matches(KeyInitType A, KeyInitType B)
Definition WeakFieldPtr.h:478
Definition WeakFieldPtr.h:65
TWeakFieldPtr(const TWeakFieldPtr< OtherT > &Other)
Definition WeakFieldPtr.h:163
UE_FORCEINLINE_HINT T & operator*() const
Definition WeakFieldPtr.h:263
friend bool operator!=(const TWeakFieldPtr< LhsType > &Lhs, const TWeakFieldPtr< RhsType > &Rhs)
Definition WeakFieldPtr.h:402
UE_FORCEINLINE_HINT bool IsValid(bool bEvenIfPendingKill, bool bThreadsafeTest=false) const
Definition WeakFieldPtr.h:283
UE_FORCEINLINE_HINT T * operator->() const
Definition WeakFieldPtr.h:271
T * Get(bool bEvenIfPendingKill) const
Definition WeakFieldPtr.h:214
UE_FORCEINLINE_HINT bool IsStale(bool bIncludingIfPendingKill=false, bool bThreadsafeTest=false) const
Definition WeakFieldPtr.h:303
UE_FORCEINLINE_HINT bool HasSameIndexAndSerialNumber(const TWeakFieldPtr &Other) const
Definition WeakFieldPtr.h:308
UE_FORCEINLINE_HINT bool IsValid() const
Definition WeakFieldPtr.h:292
void Reset()
Definition WeakFieldPtr.h:175
friend bool operator==(const TWeakFieldPtr< LhsType > &Lhs, const TWeakFieldPtr< RhsType > &Rhs)
Definition WeakFieldPtr.h:336
TWeakFieldPtr(U *InField, EDummy1=Dummy1)
Definition WeakFieldPtr.h:146
UE_FORCEINLINE_HINT friend uint32 GetTypeHash(const TWeakFieldPtr &WeakObjectPtr)
Definition WeakFieldPtr.h:314
TWeakFieldPtr & operator=(const TWeakFieldPtr &)=default
TWeakFieldPtr()=default
T * GetEvenIfUnreachable() const
Definition WeakFieldPtr.h:246
friend bool operator==(const TWeakFieldPtr< LhsType > &Lhs, const RhsType *Rhs)
Definition WeakFieldPtr.h:351
TWeakFieldPtr(const TWeakFieldPtr &)=default
friend bool operator==(const LhsType *Lhs, const TWeakFieldPtr< RhsType > &Rhs)
Definition WeakFieldPtr.h:366
void operator=(const TWeakFieldPtr< OtherT > &Other)
Definition WeakFieldPtr.h:203
void Serialize(FArchive &Ar)
Definition WeakFieldPtr.h:319
void operator=(U *InField)
Definition WeakFieldPtr.h:189
~TWeakFieldPtr()=default
T ElementType
Definition WeakFieldPtr.h:122
friend bool operator!=(const LhsType *Lhs, const TWeakFieldPtr< RhsType > &Rhs)
Definition WeakFieldPtr.h:432
TWeakFieldPtr(TYPE_OF_NULLPTR)
Definition WeakFieldPtr.h:132
friend bool operator!=(const TWeakFieldPtr< LhsType > &Lhs, const RhsType *Rhs)
Definition WeakFieldPtr.h:417
T * Get() const
Definition WeakFieldPtr.h:231
Definition WeakObjectPtrTemplates.h:25