UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WeakObjectPtrTemplates.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
8#include "Containers/Map.h"
9#include "Misc/UEOps.h"
12
13#include <type_traits>
14
15struct FFieldPath;
16struct FWeakObjectPtr;
17struct FUObjectItem;
18struct FRemoteObjectId;
19
23template<class T, class TWeakObjectPtrBase>
25{
26 friend struct FFieldPath;
27
28 template <class, class>
29 friend struct TWeakObjectPtr;
30
31 friend struct FFieldPath;
32
33 // Although templated, these parameters are not intended to be anything other than the default,
34 // and are only templates for module organization reasons.
35 static_assert(std::is_same_v<TWeakObjectPtrBase*, FWeakObjectPtr*>, "TWeakObjectPtrBase should not be overridden");
36
37public:
38 using ElementType = T;
39
40 [[nodiscard]] TWeakObjectPtr() = default;
43 ~TWeakObjectPtr() = default;
44
49 WeakPtr((UObject*)nullptr)
50 {
51 }
52
57 template <
58 typename U
59 UE_REQUIRES(std::is_convertible_v<U, T*>)
60 >
62 WeakPtr((const UObject*)Object)
63 {
64 // This static assert is in here rather than in the body of the class because we want
65 // to be able to define TWeakObjectPtr<UUndefinedClass>.
66 static_assert(std::is_convertible_v<T*, const volatile UObject*>, "TWeakObjectPtr can only be constructed with UObject types");
67 }
68
73 template <
74 typename OtherT
75 UE_REQUIRES(std::is_convertible_v<OtherT*, T*>)
76 >
78 WeakPtr(Other.WeakPtr) // we do a C-style cast to private base here to avoid clang 3.6.0 compilation problems with friend declarations
79 {
80 }
81
82#if UE_WITH_REMOTE_OBJECT_HANDLE
83 explicit TWeakObjectPtr(const FRemoteObjectId& RemoteId)
84 : WeakPtr(RemoteId)
85 {
86 }
87#endif
88
93 {
94 WeakPtr.Reset();
95 }
96
101 template <
102 typename U
104 >
106 {
107 T* TempObject = Object;
108 WeakPtr = TempObject;
109 return *this;
110 }
111
116 template <
117 typename OtherT
118 UE_REQUIRES(std::is_convertible_v<OtherT*, T*>)
119 >
121 {
122 WeakPtr = Other.WeakPtr;
123
124 return *this;
125 }
126
133 {
134 return (T*)WeakPtr.Get(bEvenIfPendingKill);
135 }
136
140 [[nodiscard]] FORCEINLINE T* Get(/*bool bEvenIfPendingKill = false*/) const
141 {
142 return (T*)WeakPtr.Get();
143 }
144
151 {
153 StrongPtr.Attach((T*)WeakPtr.Pin(bEvenIfPendingKill).Detach());
154 return StrongPtr;
155 }
156
160 [[nodiscard]] FORCEINLINE TStrongObjectPtr<T> Pin(/*bool bEvenIfPendingKill = false*/) const
161 {
163 StrongPtr.Attach((T*)WeakPtr.Pin().Detach());
164 return StrongPtr;
165 }
166
174 {
176 StrongPtr.Attach((T*)WeakPtr.TryPin(bOutPinValid, bEvenIfPendingKill).Detach());
177 return StrongPtr;
178 }
179
183 [[nodiscard]] FORCEINLINE TStrongObjectPtr<T> TryPin(bool& bOutPinValid /*, bool bEvenIfPendingKill = false */ ) const
184 {
185 return TryPin(bOutPinValid, false);
186 }
187
190 {
191 return (T*)WeakPtr.GetEvenIfUnreachable();
192 }
193
198 {
199 return *Get();
200 }
201
206 {
207 return Get();
208 }
209
210 // This is explicitly not added to avoid resolving weak pointers too often - use Get() once in a function.
211 explicit operator bool() const = delete;
212
216 explicit operator TWeakObjectPtrBase() const
217 {
218 return WeakPtr;
219 }
220
233 {
234 return WeakPtr.IsValid(bEvenIfPendingKill, bThreadsafeTest);
235 }
236
244 [[nodiscard]] FORCEINLINE bool IsValid(/*bool bEvenIfPendingKill = false, bool bThreadsafeTest = false*/) const
245 {
246 return WeakPtr.IsValid();
247 }
248
255 [[nodiscard]] FORCEINLINE bool IsStale(bool bIncludingIfPendingKill = true, bool bThreadsafeTest = false) const
256 {
257 return WeakPtr.IsStale(bIncludingIfPendingKill, bThreadsafeTest);
258 }
259
265 {
266 return WeakPtr.IsExplicitlyNull();
267 }
268
274 {
275 return WeakPtr.HasSameIndexAndSerialNumber(Other.WeakPtr);
276 }
277
282 template <
283 typename OtherT
284 UE_REQUIRES(UE_REQUIRES_EXPR((T*)nullptr == (OtherT*)nullptr))
285 >
287 {
288 return WeakPtr.HasSameIndexAndSerialNumber(Other.WeakPtr);
289 }
290
291#if UE_WITH_REMOTE_OBJECT_HANDLE
292 FORCEINLINE bool HasSameObject(const UObject* Other) const
293 {
294 return WeakPtr.HasSameObject(Other);
295 }
296
297 FORCEINLINE auto GetRemoteId() const
298 {
299 return WeakPtr.GetRemoteId();
300 }
301#endif // UE_WITH_REMOTE_OBJECT_HANDLE
302
304 {
305 return WeakPtr.IsRemote();
306 }
307
312 {
313 Ar << WeakPtr;
314 }
315
318 {
319 return WeakPtr.GetTypeHash();
320 }
321
327 template <typename RhsT, typename = decltype((T*)nullptr == (RhsT*)nullptr)>
329 {
330 return WeakPtr == Rhs.WeakPtr;
331 }
332
333 template <typename RhsT, typename = decltype((T*)nullptr == (RhsT*)nullptr)>
334 [[nodiscard]] FORCENOINLINE bool UEOpEquals(const RhsT* Rhs) const
335 {
336 // NOTE: this constructs a TWeakObjectPtrBase, which has some amount of overhead, so this may not be an efficient operation
337 return WeakPtr == TWeakObjectPtrBase(Rhs);
338 }
339
341 {
342 return !IsValid();
343 }
344
345#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
350 template <typename RhsT, typename = decltype((T*)nullptr != (RhsT*)nullptr)>
352 {
353 return !(*this == Rhs);
354 }
355
356 template <typename RhsT, typename = decltype((T*)nullptr != (RhsT*)nullptr)>
357 [[nodiscard]] FORCEINLINE bool operator!=(const RhsT* Rhs) const
358 {
359 return !(*this == Rhs);
360 }
361
363 {
364 return !(*this == nullptr);
365 }
366#endif
367
368private:
369 FORCEINLINE FUObjectItem* Internal_GetObjectItem() const
370 {
371 return WeakPtr.Internal_GetObjectItem();
372 }
373
374 TWeakObjectPtrBase WeakPtr;
375};
376
377template <typename T>
379
380template <typename T>
382
383// Helper function which deduces the type of the initializer
384template <typename T>
389
390
394template <typename ElementType, bool bInAllowDuplicateKeys = false>
395struct TWeakObjectPtrSetKeyFuncs : DefaultKeyFuncs<ElementType, bInAllowDuplicateKeys>
396{
398
400 {
401 return A.HasSameIndexAndSerialNumber(B);
402 }
403
405 {
406 return GetTypeHash(Key);
407 }
408};
409
413template <typename KeyType, typename ValueType, bool bInAllowDuplicateKeys = false>
414struct TWeakObjectPtrMapKeyFuncs : public TDefaultMapKeyFuncs<KeyType, ValueType, bInAllowDuplicateKeys>
415{
417
419 {
420 return A.HasSameIndexAndSerialNumber(B);
421 }
422
424 {
425 return GetTypeHash(Key);
426 }
427};
428
429template <typename T>
430struct TCallTraits<TWeakObjectPtr<T>> : public TCallTraitsBase<TWeakObjectPtr<T>>
431{
433};
434
436template<typename DestArrayType, typename SourceArrayType>
438{
439 const int32 Count = Src.Num();
440 Dest.Empty(Count);
441 for (int32 Index = 0; Index < Count; Index++)
442 {
443 if (auto Value = Src[Index].Get())
444 {
445 Dest.Add(Value);
446 }
447 }
448}
449
451template<typename DestArrayType, typename SourceArrayType>
453{
454 const int32 Count = Src.Num();
455 Dest.Empty(Count);
456 for (int32 Index = 0; Index < Count; Index++)
457 {
458 if (auto* Value = Src[Index])
459 {
460 Dest.Add(Value);
461 }
462 }
463}
464
466template <typename T>
471
472
476template<class T, class TWeakObjectPtrBase>
482
483#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_4
484#include "Templates/AndOrNot.h"
485#include "Templates/IsPointer.h"
487#endif
#define FORCENOINLINE
Definition AndroidPlatform.h:142
#define FORCEINLINE
Definition AndroidPlatform.h:140
FPlatformTypes::TYPE_OF_NULLPTR TYPE_OF_NULLPTR
The type of the C++ nullptr keyword.
Definition Platform.h:1157
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
const bool
Definition NetworkReplayStreaming.h:178
#define UE_REQUIRES(...)
Definition Requires.h:86
#define UE_REQUIRES_EXPR(...)
Definition Requires.h:89
FORCEINLINE uint32 GetTypeHash(const TWeakObjectPtr< T > &WeakObjectPtr)
Definition WeakObjectPtrTemplates.h:467
void CopyToWeakArray(DestArrayType &Dest, const SourceArrayType &Src)
Definition WeakObjectPtrTemplates.h:452
void CopyFromWeakArray(DestArrayType &Dest, const SourceArrayType &Src)
Definition WeakObjectPtrTemplates.h:437
FORCEINLINE TWeakObjectPtr< T > MakeWeakObjectPtr(T *Ptr)
Definition WeakObjectPtrTemplates.h:385
FArchive & operator<<(FArchive &Ar, TWeakObjectPtr< T, TWeakObjectPtrBase > &WeakObjectPtr)
Definition WeakObjectPtrTemplates.h:477
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 StrongObjectPtrTemplates.h:26
Definition Object.h:95
U16 Index
Definition radfft.cpp:71
Definition SetUtilities.h:36
TTypeTraits< ElementType >::ConstPointerType KeyInitType
Definition SetUtilities.h:37
Definition FieldPath.h:36
Definition RemoteObjectTypes.h:212
Definition UObjectArray.h:50
Definition WeakObjectPtr.h:49
Definition UnrealTypeTraits.h:267
Definition UnrealTypeTraits.h:283
Definition Map.h:77
TTypeTraits< KeyType >::ConstPointerType KeyInitType
Definition Map.h:78
Definition WeakObjectPtrTemplates.h:415
static FORCEINLINE uint32 GetKeyHash(KeyInitType Key)
Definition WeakObjectPtrTemplates.h:423
TDefaultMapKeyFuncs< KeyType, ValueType, bInAllowDuplicateKeys >::KeyInitType KeyInitType
Definition WeakObjectPtrTemplates.h:416
static FORCEINLINE bool Matches(KeyInitType A, KeyInitType B)
Definition WeakObjectPtrTemplates.h:418
Definition WeakObjectPtrTemplates.h:396
static FORCEINLINE uint32 GetKeyHash(KeyInitType Key)
Definition WeakObjectPtrTemplates.h:404
static FORCEINLINE bool Matches(KeyInitType A, KeyInitType B)
Definition WeakObjectPtrTemplates.h:399
DefaultKeyFuncs< ElementType, bInAllowDuplicateKeys >::KeyInitType KeyInitType
Definition WeakObjectPtrTemplates.h:397
Definition WeakObjectPtrTemplates.h:25
FORCEINLINE TWeakObjectPtr & operator=(U *Object)
Definition WeakObjectPtrTemplates.h:105
FORCEINLINE T * operator->() const
Definition WeakObjectPtrTemplates.h:205
FORCEINLINE TStrongObjectPtr< T > TryPin(bool &bOutPinValid, bool bEvenIfPendingKill) const
Definition WeakObjectPtrTemplates.h:173
FORCENOINLINE bool UEOpEquals(const RhsT *Rhs) const
Definition WeakObjectPtrTemplates.h:334
TWeakObjectPtr()=default
FORCEINLINE TWeakObjectPtr(const TWeakObjectPtr< OtherT, TWeakObjectPtrBase > &Other)
Definition WeakObjectPtrTemplates.h:77
FORCEINLINE void Reset()
Definition WeakObjectPtrTemplates.h:92
FORCEINLINE bool IsExplicitlyNull() const
Definition WeakObjectPtrTemplates.h:264
FORCEINLINE TWeakObjectPtr(U Object)
Definition WeakObjectPtrTemplates.h:61
FORCEINLINE bool IsStale(bool bIncludingIfPendingKill=true, bool bThreadsafeTest=false) const
Definition WeakObjectPtrTemplates.h:255
TWeakObjectPtr & operator=(const TWeakObjectPtr &)=default
FORCEINLINE bool operator!=(const RhsT *Rhs) const
Definition WeakObjectPtrTemplates.h:357
FORCEINLINE TStrongObjectPtr< T > Pin(bool bEvenIfPendingKill) const
Definition WeakObjectPtrTemplates.h:150
FORCEINLINE uint32 GetWeakPtrTypeHash() const
Definition WeakObjectPtrTemplates.h:317
FORCEINLINE T & operator*() const
Definition WeakObjectPtrTemplates.h:197
TWeakObjectPtr(const TWeakObjectPtr &)=default
FORCEINLINE TWeakObjectPtr(TYPE_OF_NULLPTR)
Definition WeakObjectPtrTemplates.h:48
friend struct TWeakObjectPtr
Definition WeakObjectPtrTemplates.h:29
FORCEINLINE bool operator!=(const TWeakObjectPtr< RhsT, TWeakObjectPtrBase > &Rhs) const
Definition WeakObjectPtrTemplates.h:351
FORCEINLINE TWeakObjectPtr & operator=(const TWeakObjectPtr< OtherT, TWeakObjectPtrBase > &Other)
Definition WeakObjectPtrTemplates.h:120
FORCEINLINE T * GetEvenIfUnreachable() const
Definition WeakObjectPtrTemplates.h:189
T ElementType
Definition WeakObjectPtrTemplates.h:38
FORCEINLINE bool IsValid() const
Definition WeakObjectPtrTemplates.h:244
FORCEINLINE T * Get() const
Definition WeakObjectPtrTemplates.h:140
FORCEINLINE TStrongObjectPtr< T > TryPin(bool &bOutPinValid) const
Definition WeakObjectPtrTemplates.h:183
FORCEINLINE T * Get(bool bEvenIfPendingKill) const
Definition WeakObjectPtrTemplates.h:132
FORCEINLINE bool IsValid(bool bEvenIfPendingKill, bool bThreadsafeTest=false) const
Definition WeakObjectPtrTemplates.h:232
FORCEINLINE bool HasSameIndexAndSerialNumber(const TWeakObjectPtr &Other) const
Definition WeakObjectPtrTemplates.h:273
FORCENOINLINE bool UEOpEquals(const TWeakObjectPtr< RhsT, TWeakObjectPtrBase > &Rhs) const
Definition WeakObjectPtrTemplates.h:328
FORCEINLINE bool IsRemote() const
Definition WeakObjectPtrTemplates.h:303
FORCEINLINE bool HasSameIndexAndSerialNumber(const TWeakObjectPtr< OtherT, TWeakObjectPtrBase > &Other) const
Definition WeakObjectPtrTemplates.h:286
~TWeakObjectPtr()=default
FORCEINLINE TStrongObjectPtr< T > Pin() const
Definition WeakObjectPtrTemplates.h:160
FORCEINLINE void Serialize(FArchive &Ar)
Definition WeakObjectPtrTemplates.h:311
FORCEINLINE bool operator!=(TYPE_OF_NULLPTR) const
Definition WeakObjectPtrTemplates.h:362
FORCENOINLINE bool UEOpEquals(TYPE_OF_NULLPTR) const
Definition WeakObjectPtrTemplates.h:340