UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PersistentObjectPtr.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 PersistentObjectPtr.h: Template that is a base class for Lazy and Asset pointers
5=============================================================================*/
6
7#pragma once
8
9#include "UObject/Object.h"
11
12class UObject;
13
17template<class TObjectID>
19{
21
27
29 inline void Reset()
30 {
31 WeakPtr.Reset();
32 ObjectID.Reset();
33 }
34
37 {
38 WeakPtr.Reset();
39 }
40
43 : WeakPtr()
44 , ObjectID(InObjectID)
45 {
46 }
47
49 inline void operator=(const TObjectID& InObjectID)
50 {
51 WeakPtr.Reset();
52 ObjectID = InObjectID;
53 }
54
57 {
58 if (Object)
59 {
60 ObjectID = TObjectID::GetOrCreateIDForObject(Object);
61 if (CanCacheObjectPointer(Object))
62 {
63 WeakPtr = Object;
64 }
65 else
66 {
67 WeakPtr.Reset();
68 }
69 }
70 else
71 {
72 Reset();
73 }
74 }
76 {
77 *this = FObjectPtr(const_cast<UObject*>(Object));
78 }
79 template <typename T>
81 {
82 // This needs to be a template instead of TObjectPtr<const UObject> because C++ does derived-to-base
83 // pointer conversions ('standard conversion sequences') in more cases than TSmartPtr<Derived>-to-TSmartPtr<Base>
84 // conversions ('user-defined conversions'), meaning it doesn't auto-convert in many real use cases.
85 //
86 // https://en.cppreference.com/w/cpp/language/implicit_conversion
87
88 *this = FObjectPtr(Object);
89 }
90
92 inline void operator=(const FWeakObjectPtr& Other)
93 {
94 // If object exists need to make sure it gets registered properly in above function, if it doesn't exist just empty it
95 const UObject* Object = Other.Get();
96 *this = Object;
97 }
98
105 {
106 return ObjectID;
107 }
108
111 {
112 return ObjectID;
113 }
114
120 inline UObject* Get() const
121 {
122 UObject* Object = WeakPtr.Get();
123
124 // Do a full resolve if the cached object is null but we have a valid object ID that might resolve
125 // This used to check TObjectID::GetCurrentTag() before resolving but that was unreliable and did not improve performance in actual use
126 if (!Object && ObjectID.IsValid())
127 {
128 Object = ObjectID.ResolveObject();
129 if (CanCacheObjectPointer(FObjectPtr(Object)))
130 {
131 WeakPtr = Object;
132 }
133
134 // Make sure it isn't garbage to match the default behavior of WeakPtr.Get() without looking it up again
135 return ::GetValid(Object);
136 }
137 return Object;
138 }
139
146 inline UObject* Get(bool bEvenIfPendingKill) const
147 {
149
150 // Do a full resolve if the cached object is null but we have a valid object ID that might resolve
151 // This used to check TObjectID::GetCurrentTag() before resolving but that was unreliable and did not improve performance in actual use
152 if (!Object && ObjectID.IsValid())
153 {
154 Object = ObjectID.ResolveObject();
156
157 if (CanCacheObjectPointer(FObjectPtr(Object)))
158 {
159 WeakPtr = Object;
160 }
161 // Get the object again using the correct flag
163 }
164 return Object;
165 }
166
169 {
170 return *Get();
171 }
172
175 {
176 return Get();
177 }
178
181 {
182 return ObjectID == Rhs.ObjectID;
183 }
184
186 {
187 return !IsValid();
188 }
189
192 {
193 return ObjectID != Rhs.ObjectID;
194 }
195
197 {
198 return IsValid();
199 }
200
201#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
203 {
204 return !Rhs.IsValid();
205 }
206
208 {
209 return Rhs.IsValid();
210 }
211#endif
212
219 {
220 return Get() == nullptr && ObjectID.IsValid();
221 }
222
229 {
230 return !!Get();
231 }
232
239 {
240 return WeakPtr.IsStale();
241 }
248 {
249 return !ObjectID.IsValid();
250 }
251
254 {
255 return GetTypeHash(Ptr.ObjectID);
256 }
257
258private:
259
260 // Returns whether the object pointer can be stored in WeakPtr for later retrieval
261 // For example, objects that are in the process of being async loaded may not be cached
262 inline bool CanCacheObjectPointer(FObjectPtr Ptr) const
263 {
265 {
266 return false;
267 }
268 return true;
269 }
270
272 mutable FWeakObjectPtr WeakPtr;
274 TObjectID ObjectID;
275};
276
277template <class TObjectID> struct TIsPODType<TPersistentObjectPtr<TObjectID> > { enum { Value = TIsPODType<TObjectID>::Value }; };
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 EInternalObjectFlags_AsyncLoading
Definition ObjectMacros.h:681
bool(* IsInAsyncLoadingThread)()
Definition ThreadingBase.cpp:357
uint32_t uint32
Definition binka_ue_file_header.h:6
UE_FORCEINLINE_HINT bool HasAnyInternalFlags(EInternalObjectFlags FlagsToCheck) const
Definition UObjectBaseUtility.h:289
Definition Object.h:95
Definition ObjectPtr.h:55
Definition WeakObjectPtr.h:49
COREUOBJECT_API bool IsStale(bool bIncludingGarbage=true, bool bThreadsafeTest=false) const
Definition WeakObjectPtr.cpp:65
COREUOBJECT_API class UObject * Get(bool bEvenIfGarbage) const
Definition WeakObjectPtr.cpp:122
void Reset()
Definition WeakObjectPtr.h:129
Definition IsPODType.h:12
@ Value
Definition IsPODType.h:13
Definition UnrealTypeTraits.h:181
@ Value
Definition UnrealTypeTraits.h:182
Definition ObjectPtr.h:488
Definition PersistentObjectPtr.h:19
void operator=(const FWeakObjectPtr &Other)
Definition PersistentObjectPtr.h:92
UE_FORCEINLINE_HINT bool IsPending() const
Definition PersistentObjectPtr.h:218
TObjectID ElementType
Definition PersistentObjectPtr.h:20
UE_FORCEINLINE_HINT bool operator==(const TPersistentObjectPtr &Rhs) const
Definition PersistentObjectPtr.h:180
UE_FORCEINLINE_HINT friend bool operator==(TYPE_OF_NULLPTR, const TPersistentObjectPtr &Rhs)
Definition PersistentObjectPtr.h:202
UE_FORCEINLINE_HINT friend uint32 GetTypeHash(const TPersistentObjectPtr &Ptr)
Definition PersistentObjectPtr.h:253
UE_FORCEINLINE_HINT void operator=(TObjectPtr< T > Object)
Definition PersistentObjectPtr.h:80
UE_FORCEINLINE_HINT bool IsStale() const
Definition PersistentObjectPtr.h:238
UObject * Get() const
Definition PersistentObjectPtr.h:120
void operator=(FObjectPtr Object)
Definition PersistentObjectPtr.h:56
UE_FORCEINLINE_HINT friend bool operator!=(TYPE_OF_NULLPTR, const TPersistentObjectPtr &Rhs)
Definition PersistentObjectPtr.h:207
UE_FORCEINLINE_HINT UObject * operator->() const
Definition PersistentObjectPtr.h:174
TPersistentObjectPtr(const TObjectID &InObjectID)
Definition PersistentObjectPtr.h:42
UE_FORCEINLINE_HINT bool IsValid() const
Definition PersistentObjectPtr.h:228
UE_FORCEINLINE_HINT TObjectID & GetUniqueID()
Definition PersistentObjectPtr.h:110
UE_FORCEINLINE_HINT bool operator==(TYPE_OF_NULLPTR) const
Definition PersistentObjectPtr.h:185
UE_FORCEINLINE_HINT const TObjectID & GetUniqueID() const
Definition PersistentObjectPtr.h:104
UE_FORCEINLINE_HINT UObject & operator*() const
Definition PersistentObjectPtr.h:168
UE_FORCEINLINE_HINT TPersistentObjectPtr()
Definition PersistentObjectPtr.h:23
UObject * Get(bool bEvenIfPendingKill) const
Definition PersistentObjectPtr.h:146
UE_FORCEINLINE_HINT bool IsNull() const
Definition PersistentObjectPtr.h:247
UE_FORCEINLINE_HINT bool operator!=(TYPE_OF_NULLPTR) const
Definition PersistentObjectPtr.h:196
void operator=(const TObjectID &InObjectID)
Definition PersistentObjectPtr.h:49
void Reset()
Definition PersistentObjectPtr.h:29
UE_FORCEINLINE_HINT void operator=(const UObject *Object)
Definition PersistentObjectPtr.h:75
UE_FORCEINLINE_HINT void ResetWeakPtr()
Definition PersistentObjectPtr.h:36
UE_FORCEINLINE_HINT bool operator!=(const TPersistentObjectPtr &Rhs) const
Definition PersistentObjectPtr.h:191