UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StrongObjectPtrTemplates.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
9
10class UObject;
11
20
24template <typename ObjectType, typename ReferencerNameProvider>
26{
27public:
28 using ElementType = ObjectType;
29
31 : Object(InOther.Object)
32 {
33 InOther.Object = nullptr;
34 }
35
37 {
38 if (this != &InOther)
39 {
40 Reset();
41 Object = InOther.Object;
42 InOther.Object = nullptr;
43 }
44 return *this;
45 }
46
51
53 {
54 static_assert(TPointerIsConvertibleFromTo<ObjectType, const volatile UObject>::Value, "TStrongObjectPtr can only be constructed with UObject types");
55 }
56
57 [[nodiscard]] inline explicit TStrongObjectPtr(ObjectType* InObject)
58 {
59 static_assert(TPointerIsConvertibleFromTo<ObjectType, const volatile UObject>::Value, "TStrongObjectPtr can only be constructed with UObject types");
60 Reset(InObject);
61 }
62
67
68 template <
69 typename OtherObjectType,
71 UE_REQUIRES(std::is_convertible_v<OtherObjectType*, ObjectType*>)
72 >
77
79 {
80 Reset(InOther.Get());
81 return *this;
82 }
83
84 template <
85 typename OtherObjectType,
87 UE_REQUIRES(std::is_convertible_v<OtherObjectType*, ObjectType*>)
88 >
94
95 [[nodiscard]] inline ObjectType& operator*() const
96 {
97 check(IsValid());
98 return *(ObjectType*)Get();
99 }
100
101 [[nodiscard]] inline ObjectType* operator->() const
102 {
103 check(IsValid());
104 return (ObjectType*)Get();
105 }
106
108 {
109 return Object != nullptr;
110 }
111
112 [[nodiscard]] UE_FORCEINLINE_HINT explicit operator bool() const
113 {
114 return IsValid();
115 }
116
117 [[nodiscard]] UE_FORCEINLINE_HINT ObjectType* Get() const
118 {
119 return (ObjectType*)Object;
120 }
121
122 inline void Reset()
123 {
124 if (Object)
125 {
126 // UObject type is forward declared, ReleaseRef() is not known.
127 // So move the implementation to the cpp file instead.
129 Object = nullptr;
130 }
131 }
132
133private:
134 template<class T, class TWeakObjectPtrBase> friend struct TWeakObjectPtr;
135
136 // Attach an object without incrementing its ref-count.
137 inline void Attach(ObjectType* InNewObject)
138 {
139 Reset();
141 }
142
143 // Detach the current owned object without decrementing its ref-count.
144 inline ObjectType* Detach()
145 {
146 ObjectType* DetachedObject = Get();
147 Object = nullptr;
148 return DetachedObject;
149 }
150
151public:
152 inline void Reset(ObjectType* InNewObject)
153 {
154 if (InNewObject)
155 {
156 if (Object == InNewObject)
157 {
158 return;
159 }
160
161 if (Object)
162 {
163 // UObject type is forward declared, ReleaseRef() is not known.
164 // So move the implementation to the cpp file instead.
166 }
167 InNewObject->AddRef();
169 }
170 else
171 {
172 Reset();
173 }
174 }
175
180
181private:
182 // Store as UObject to allow forward declarations without having to fully resolve ObjectType before construction.
183 // This is required because the destructor calls Reset, which need to be fully resolved at declaration.
184 const UObject* Object{ nullptr };
185
187 {
188 return InLHS.Get() == InRHS.Get();
189 }
190
192 {
193 return !InLHS.IsValid();
194 }
195
197 {
198 return !InRHS.IsValid();
199 }
200
201 template <
202 typename RHSObjectType,
204 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<ObjectType*>() == std::declval<RHSObjectType*>()))
205 >
210
211 template <
212 typename LHSObjectType,
214 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<LHSObjectType*>() == std::declval<ObjectType*>()))
215 >
220
221#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
223 {
224 return InLHS.Get() != InRHS.Get();
225 }
226
228 {
229 return InLHS.IsValid();
230 }
231
233 {
234 return InRHS.IsValid();
235 }
236
237 template <
238 typename RHSObjectType,
240 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<ObjectType*>() == std::declval<RHSObjectType*>()))
241 >
246
247 template <
248 typename LHSObjectType,
250 UE_REQUIRES(UE_REQUIRES_EXPR(std::declval<LHSObjectType*>() == std::declval<ObjectType*>()))
251 >
256#endif
257};
#define check(expr)
Definition AssertionMacros.h:314
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
const bool
Definition NetworkReplayStreaming.h:178
#define UE_REQUIRES(...)
Definition Requires.h:86
#define UE_REQUIRES_EXPR(...)
Definition Requires.h:89
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition StrongObjectPtrTemplates.h:26
TStrongObjectPtr(ObjectType *InObject)
Definition StrongObjectPtrTemplates.h:57
void Reset()
Definition StrongObjectPtrTemplates.h:122
TStrongObjectPtr & operator=(const TStrongObjectPtr &InOther)
Definition StrongObjectPtrTemplates.h:78
TStrongObjectPtr(TStrongObjectPtr &&InOther)
Definition StrongObjectPtrTemplates.h:30
UE_FORCEINLINE_HINT TStrongObjectPtr(TYPE_OF_NULLPTR=nullptr)
Definition StrongObjectPtrTemplates.h:52
ObjectType * operator->() const
Definition StrongObjectPtrTemplates.h:101
UE_FORCEINLINE_HINT bool IsValid() const
Definition StrongObjectPtrTemplates.h:107
friend UE_FORCEINLINE_HINT bool operator==(const TStrongObjectPtr< LHSObjectType, LHSReferencerNameProvider > &InLHS, const TStrongObjectPtr &InRHS)
Definition StrongObjectPtrTemplates.h:216
friend UE_FORCEINLINE_HINT bool operator==(const TStrongObjectPtr &InLHS, const TStrongObjectPtr< RHSObjectType, RHSReferencerNameProvider > &InRHS)
Definition StrongObjectPtrTemplates.h:206
friend UE_FORCEINLINE_HINT bool operator==(const TStrongObjectPtr &InLHS, const TStrongObjectPtr &InRHS)
Definition StrongObjectPtrTemplates.h:186
UE_FORCEINLINE_HINT TStrongObjectPtr(const TStrongObjectPtr &InOther)
Definition StrongObjectPtrTemplates.h:63
friend UE_FORCEINLINE_HINT bool operator!=(const TStrongObjectPtr &InLHS, const TStrongObjectPtr &InRHS)
Definition StrongObjectPtrTemplates.h:222
friend UE_FORCEINLINE_HINT bool operator==(const TStrongObjectPtr &InLHS, TYPE_OF_NULLPTR)
Definition StrongObjectPtrTemplates.h:191
friend UE_FORCEINLINE_HINT bool operator==(TYPE_OF_NULLPTR, const TStrongObjectPtr &InRHS)
Definition StrongObjectPtrTemplates.h:196
UE_FORCEINLINE_HINT friend uint32 GetTypeHash(const TStrongObjectPtr &InStrongObjectPtr)
Definition StrongObjectPtrTemplates.h:176
UE_FORCEINLINE_HINT TStrongObjectPtr(const TStrongObjectPtr< OtherObjectType, OtherReferencerNameProvider > &InOther)
Definition StrongObjectPtrTemplates.h:73
ObjectType ElementType
Definition StrongObjectPtrTemplates.h:28
void Reset(ObjectType *InNewObject)
Definition StrongObjectPtrTemplates.h:152
TStrongObjectPtr & operator=(const TStrongObjectPtr< OtherObjectType, OtherReferencerNameProvider > &InOther)
Definition StrongObjectPtrTemplates.h:89
UE_FORCEINLINE_HINT ObjectType * Get() const
Definition StrongObjectPtrTemplates.h:117
ObjectType & operator*() const
Definition StrongObjectPtrTemplates.h:95
UE_FORCEINLINE_HINT ~TStrongObjectPtr()
Definition StrongObjectPtrTemplates.h:47
friend UE_FORCEINLINE_HINT bool operator!=(TYPE_OF_NULLPTR, const TStrongObjectPtr &InRHS)
Definition StrongObjectPtrTemplates.h:232
TStrongObjectPtr & operator=(TStrongObjectPtr &&InOther)
Definition StrongObjectPtrTemplates.h:36
friend UE_FORCEINLINE_HINT bool operator!=(const TStrongObjectPtr &InLHS, const TStrongObjectPtr< RHSObjectType, RHSReferencerNameProvider > &InRHS)
Definition StrongObjectPtrTemplates.h:242
friend UE_FORCEINLINE_HINT bool operator!=(const TStrongObjectPtr &InLHS, TYPE_OF_NULLPTR)
Definition StrongObjectPtrTemplates.h:227
friend UE_FORCEINLINE_HINT bool operator!=(const TStrongObjectPtr< LHSObjectType, LHSReferencerNameProvider > &InLHS, const TStrongObjectPtr &InRHS)
Definition StrongObjectPtrTemplates.h:252
Definition Object.h:95
Definition StrongObjectPtrTemplates.h:13
COREUOBJECT_API void ReleaseUObject(const UObject *)
Definition StrongObjectPtr.cpp:10
Definition PointerIsConvertibleFromTo.h:60
Definition WeakObjectPtrTemplates.h:25