UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WeakInterfacePtr.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "UObject/Object.h"
8#include "Templates/Casts.h"
9#include "ScriptInterface.h"
10
11#include <type_traits>
12
16template<class T>
18{
19 using ElementType = T;
21
28
33 ObjectInstance(nullptr)
34 {
35 }
36
41 template<
42 typename U
43 UE_REQUIRES(std::is_convertible_v<U, TCopyQualifiersFromTo_T<U, UObject>*>)
44 >
46 {
48 if (InterfaceInstance != nullptr)
49 {
50 ObjectInstance = Object;
51 }
52 }
53
58 [[nodiscard]] TWeakInterfacePtr(T* Interface)
59 {
60 ObjectInstance = Cast<UObject>(Interface);
61 if (ObjectInstance != nullptr)
62 {
63 InterfaceInstance = Interface;
64 }
65 }
66
73 {
74 ObjectInstance = ScriptInterface.GetObject();
75 InterfaceInstance = ScriptInterface.GetInterface();
76 }
77
81 inline void Reset()
82 {
83 InterfaceInstance = nullptr;
84 ObjectInstance.Reset();
85 }
86
91 {
92 return InterfaceInstance != nullptr && ObjectInstance.IsValid(bEvenIfPendingKill, bThreadsafeTest);
93 }
94
99 {
100 return InterfaceInstance != nullptr && ObjectInstance.IsValid();
101 }
102
106 [[nodiscard]] UE_FORCEINLINE_HINT bool IsStale(bool bEvenIfPendingKill = false, bool bThreadsafeTest = false) const
107 {
108 return InterfaceInstance != nullptr && ObjectInstance.IsStale(bEvenIfPendingKill, bThreadsafeTest);
109 }
110
115 {
116 return IsValid() ? InterfaceInstance : nullptr;
117 }
118
123 {
124 return ObjectInstance.Get();
125 }
126
130 [[nodiscard]] inline T& operator*() const
131 {
132 check(IsValid());
133 return *InterfaceInstance;
134 }
135
139 [[nodiscard]] inline T* operator->() const
140 {
141 check(IsValid());
142 return InterfaceInstance;
143 }
144
149 {
151 return *this;
152 }
153
158 {
159 ObjectInstance = Other.GetObject();
160 InterfaceInstance = (T*)Other.GetInterface();
161 return *this;
162 }
163
165 {
166 return InterfaceInstance == Other.InterfaceInstance;
167 }
168
170 {
171 return InterfaceInstance != Other.InterfaceInstance;
172 }
173
175 {
176 return !IsValid();
177 }
178
180 {
181 return !(*this == nullptr);
182 }
183
185 {
186 UObjectType* Object = ObjectInstance.Get();
187 if (Object)
188 {
190 }
191
192 return TScriptInterface<T>();
193 }
194
196 {
197 return ObjectInstance;
198 }
199
200private:
201 TWeakObjectPtr<UObjectType> ObjectInstance;
202 T* InterfaceInstance = nullptr;
203};
#define FORCENOINLINE
Definition AndroidPlatform.h:142
#define check(expr)
Definition AssertionMacros.h:314
typename TCopyQualifiersFromTo< From, To >::Type TCopyQualifiersFromTo_T
Definition CopyQualifiersFromTo.h:17
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 Requires.h:86
UE_REWRITE constexpr T ImplicitConv(typename TIdentity< T >::Type Obj)
Definition UnrealTemplate.h:743
UEBinkAudioDecodeInterface * Interface
Definition binka_ue_decode_test.cpp:24
Definition ScriptInterface.h:139
Definition WeakInterfacePtr.h:18
TWeakInterfacePtr(U &&Object)
Definition WeakInterfacePtr.h:45
UE_FORCEINLINE_HINT TWeakObjectPtr< UObjectType > GetWeakObjectPtr() const
Definition WeakInterfacePtr.h:195
UE_FORCEINLINE_HINT bool operator!=(TYPE_OF_NULLPTR) const
Definition WeakInterfacePtr.h:179
UE_FORCEINLINE_HINT TWeakInterfacePtr()=default
UE_FORCEINLINE_HINT TWeakInterfacePtr(TYPE_OF_NULLPTR)
Definition WeakInterfacePtr.h:32
UE_FORCEINLINE_HINT T * Get() const
Definition WeakInterfacePtr.h:114
UE_FORCEINLINE_HINT ~TWeakInterfacePtr()=default
TWeakInterfacePtr(const TScriptInterface< T > &ScriptInterface)
Definition WeakInterfacePtr.h:72
FORCENOINLINE bool operator==(TYPE_OF_NULLPTR) const
Definition WeakInterfacePtr.h:174
UE_FORCEINLINE_HINT TWeakInterfacePtr(TWeakInterfacePtr &&Other)=default
UE_FORCEINLINE_HINT bool IsValid(bool bEvenIfPendingKill, bool bThreadsafeTest=false) const
Definition WeakInterfacePtr.h:90
UE_FORCEINLINE_HINT TWeakInterfacePtr & operator=(const TWeakInterfacePtr &Other)=default
UE_FORCEINLINE_HINT UObjectType * GetObject() const
Definition WeakInterfacePtr.h:122
UE_FORCEINLINE_HINT bool operator!=(const TWeakInterfacePtr< T > &Other) const
Definition WeakInterfacePtr.h:169
void Reset()
Definition WeakInterfacePtr.h:81
T & operator*() const
Definition WeakInterfacePtr.h:130
TWeakInterfacePtr< T > & operator=(const TScriptInterface< T > &Other)
Definition WeakInterfacePtr.h:157
UE_FORCEINLINE_HINT bool IsStale(bool bEvenIfPendingKill=false, bool bThreadsafeTest=false) const
Definition WeakInterfacePtr.h:106
TCopyQualifiersFromTo_T< T, UObject > UObjectType
Definition WeakInterfacePtr.h:20
TWeakInterfacePtr(T *Interface)
Definition WeakInterfacePtr.h:58
T * operator->() const
Definition WeakInterfacePtr.h:139
UE_FORCEINLINE_HINT TWeakInterfacePtr(const TWeakInterfacePtr &Other)=default
TScriptInterface< T > ToScriptInterface() const
Definition WeakInterfacePtr.h:184
UE_FORCEINLINE_HINT bool operator==(const TWeakInterfacePtr< T > &Other) const
Definition WeakInterfacePtr.h:164
T ElementType
Definition WeakInterfacePtr.h:19
TWeakInterfacePtr< T > & operator=(T *Other)
Definition WeakInterfacePtr.h:148
UE_FORCEINLINE_HINT TWeakInterfacePtr & operator=(TWeakInterfacePtr &&Other)=default
UE_FORCEINLINE_HINT bool IsValid() const
Definition WeakInterfacePtr.h:98
Definition WeakObjectPtrTemplates.h:25
FORCEINLINE void Reset()
Definition WeakObjectPtrTemplates.h:92
FORCEINLINE bool IsStale(bool bIncludingIfPendingKill=true, bool bThreadsafeTest=false) const
Definition WeakObjectPtrTemplates.h:255
FORCEINLINE T * Get(bool bEvenIfPendingKill) const
Definition WeakObjectPtrTemplates.h:132
FORCEINLINE bool IsValid(bool bEvenIfPendingKill, bool bThreadsafeTest=false) const
Definition WeakObjectPtrTemplates.h:232