UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SharedPointer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
10
11#define UE_API ULANGCORE_API
12
13namespace uLang
14{
15
16template<class ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType> class TSPtrG;
17template<class ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType> class TSPtrArrayG;
18template<class ObjectType, bool AllowNull, class KeyType, class AllocatorType, typename... AllocatorArgsType> class TSPtrSetG;
19
28{
29public:
30
31 CSharedMix() : _RefCount(0) {}
32 UE_API virtual ~CSharedMix();
33
34 CSharedMix(const CSharedMix & Other) = delete; // Do not copy
35 CSharedMix & operator=(const CSharedMix & Other) = delete; // Do not assign
36
37 uint32_t GetRefCount() const { return _RefCount; }
38
39protected:
40 template<class ObjectType>
45
46 template<class ObjectType>
51
52private:
53
54 template<class ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType> friend class TSPtrG;
55 template<class ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType> friend class TSPtrArrayG;
56 template<class ObjectType, bool AllowNull, class KeyType, class AllocatorType, typename... AllocatorArgsType> friend class TSPtrSetG;
57
58
59
60 void Reference() const { ++_RefCount; }
61 bool Dereference() const;
62
64 mutable uint32_t _RefCount;
65
66};
67
75template<class ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType>
76class TSPtrG
77{
78public:
79
80 // Construction
81
82 ULANG_FORCEINLINE TSPtrG(NullPtrType NullPtr = nullptr) : _Object(nullptr), _Allocator(DefaultInit), _ReleaseFunc(nullptr) { static_assert(AllowNull, "Cannot default construct a shared reference, as it is not allowed to be null."); }
83 ULANG_FORCEINLINE TSPtrG(const TSPtrG & Other) : _Object(Other._Object), _Allocator(Other._Allocator) { EnableRelease(); if (Other._Object) { Other._Object->Reference(); } }
86 ULANG_FORCEINLINE TSPtrG(TSPtrG && Other) : _Object(Other._Object), _Allocator(Other._Allocator) { EnableRelease(); Other._Object = nullptr; } // Note: We null here even when OtherAllowNull==false since 99% likely will be destructed immediately after anyway
88 ULANG_FORCEINLINE TSPtrG(TSPtrG<OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType...> && Other) : _Object(Other._Object), _Allocator(Other._Allocator) { EnableRelease(); Other._Object = nullptr; } // Note: We null here even when OtherAllowNull==false since 99% likely will be destructed immediately after anyway
90
91 template<typename... CtorArgsType>
92 ULANG_FORCEINLINE TSPtrG& SetNew(CtorArgsType&&... CtorArgs) { EnableRelease(); if (_Object) { Release(); } ObjectType * Object = new(_Allocator) ObjectType(uLang::ForwardArg<CtorArgsType>(CtorArgs)...); ULANG_ASSERT(Object); Object->Reference(); _Object = Object; return *this; }
93 template<typename... CtorArgsType>
94 ULANG_FORCEINLINE static TSPtrG New(AllocatorArgsType&&... AllocatorArgs, CtorArgsType&&... CtorArgs) { AllocatorType Allocator(AllocatorArgs...); ObjectType* Object = new(Allocator) ObjectType(uLang::ForwardArg<CtorArgsType>(CtorArgs)...); ULANG_ASSERT(Object); return TSPtrG(Object, uLang::Move(Allocator)); }
95
96 // Assignment
97
98 ULANG_FORCEINLINE TSPtrG & operator=(NullPtrType) { static_assert(AllowNull, "Cannot assign null to shared reference, as it is not allowed to be null."); if (_Object) { Release(); } _Object = nullptr; return *this; }
105
106 // Conversion methods
107
108 ULANG_FORCEINLINE operator ObjectType*() const { return _Object; }
109 ULANG_FORCEINLINE ObjectType & operator*() const { ULANG_ASSERT(_Object); return *_Object; }
110 ULANG_FORCEINLINE ObjectType * operator->() const { ULANG_ASSERT(AllowNull || _Object); return _Object; }
111 ULANG_FORCEINLINE ObjectType * Get() const { ULANG_ASSERT(AllowNull || _Object); return _Object; }
112 ULANG_FORCEINLINE const AllocatorType & GetAllocator() const { return _Allocator; }
113 ULANG_FORCEINLINE void Reset() { if (_Object) { Release(); _Object = nullptr; } }
114
115 ULANG_FORCEINLINE TSPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>& AsRef()&
116 {
117 static_assert(AllowNull, "Unnecessary conversion!");
118 ULANG_ASSERTF(_Object, "Converting null pointer to reference!");
119 return *reinterpret_cast<TSPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...> *>(this);
120 }
121 ULANG_FORCEINLINE TSPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...> AsRef()&&
122 {
123 static_assert(AllowNull, "Unnecessary conversion!");
124 ULANG_ASSERTF(_Object, "Converting null pointer to reference!");
125 return *reinterpret_cast<TSPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...> *>(this);
126 }
127 ULANG_FORCEINLINE const TSPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>& AsRef() const&
128 {
129 static_assert(AllowNull, "Unnecessary conversion!");
130 ULANG_ASSERTF(_Object, "Converting null pointer to reference!");
131 return *reinterpret_cast<const TSPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...> *>(this);
132 }
133
135 ULANG_FORCEINLINE TSPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...>& As() { return *reinterpret_cast<TSPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...> *>(this); }
137 ULANG_FORCEINLINE const TSPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...>& As() const { return *reinterpret_cast<const TSPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...> *>(this); }
138
139 // Comparison operators
140
141 ULANG_FORCEINLINE bool operator==(NullPtrType) const { return _Object == nullptr; }
142 ULANG_FORCEINLINE bool operator!=(NullPtrType) const { return _Object != nullptr; }
143 ULANG_FORCEINLINE bool operator==(const TSPtrG & Other) const { return _Object == Other._Object; }
144 template<class OtherObjectType, bool OtherAllowNull>
146 template<class OtherObjectType, bool OtherAllowNull>
148 ULANG_FORCEINLINE bool operator!=(const TSPtrG & Other) const { return _Object != Other._Object; }
149 template<class OtherObjectType, bool OtherAllowNull>
151 template<class OtherObjectType, bool OtherAllowNull>
153 ULANG_FORCEINLINE bool operator< (const TSPtrG & Other) const { return _Object < Other._Object; }
154 template<class OtherObjectType, bool OtherAllowNull>
156 template<class OtherObjectType, bool OtherAllowNull>
158 ULANG_FORCEINLINE bool operator> (const TSPtrG & Other) const { return _Object > Other._Object; }
159 template<class OtherObjectType, bool OtherAllowNull>
161 template<class OtherObjectType, bool OtherAllowNull>
163
164 // Validation methods
165
166 ULANG_FORCEINLINE operator bool() { return (_Object != nullptr); }
167 ULANG_FORCEINLINE operator bool() const { return (_Object != nullptr); }
168 ULANG_FORCEINLINE bool operator!() const { return (_Object == nullptr); }
169 ULANG_FORCEINLINE bool IsValid() const { return (_Object != nullptr); }
170
171 // Needed to avoid ambiguous function resolution between
172 // uLang and UnrealTemplate.h versions of Swap
174
175 // Composability methods
176 template <typename FuncType, typename... ArgTypes>
177 ULANG_FORCEINLINE TSPtrG Map(FuncType&& Func, ArgTypes&&... Args) &&
178 {
179 TSPtrG Result{*this};
180 Invoke(uLang::ForwardArg<FuncType>(Func), Result, uLang::ForwardArg<ArgTypes>(Args)...);
181 return Result;
182 }
183
184protected:
185
186 template<class OtherObjectType, bool OtherAllowNull, class OtherAllocatorType, typename... OtherAllocatorArgsType> friend class TSPtrG;
187 template<class OtherObjectType, bool OtherAllowNull, class OtherAllocatorType, typename... OtherAllocatorArgsType> friend class TSPtrArrayG;
188 template<class OtherObjectType, bool OtherAllowNull, class OtherKeyType, class OtherAllocatorType, typename... OtherAllocatorArgsType> friend class TSPtrSetG;
189
190
191 // This is needed for CSharedMix to support SharedThis().
192 // `return SharedThis(this)` is an equivalent of `return this` for Shared Pointers.
193 friend class CSharedMix;
194
195 ULANG_FORCEINLINE TSPtrG(ObjectType * Object, const AllocatorType & Allocator)
196 : _Object(Object)
198 , _ReleaseFunc(nullptr)
199 {
200 if (Object)
201 {
202 Object->Reference();
204 }
205 }
206
207 template<class OtherObjectType, bool OtherAllowNull>
209 {
210 if (_Object != Other._Object)
211 {
212 if (Other._Object)
213 {
214 Other._Object->Reference();
216 }
217
218 if (_Object)
219 {
220 Release();
221 }
222
223 _Object = Other._Object;
224 _Allocator = Other._Allocator;
225 }
226
227 return *this;
228 }
229
230 template<class OtherObjectType, bool OtherAllowNull>
232 {
234
235 if (_Object)
236 {
237 Release();
238 }
239
240 _Object = Other._Object;
241 _Allocator = Other._Allocator;
242 Other._Object = nullptr;
243
244 return *this;
245 }
246
249 {
250 _ReleaseFunc = [](ObjectType * Object, const AllocatorType & Allocator)
251 {
252 if (Object->Dereference())
253 {
254 // No references left: Delete the object
255 Object->~ObjectType();
256 Allocator.Deallocate(Object);
257 }
258 };
259 }
260
263 {
264 // Call the function set by EnableRelease() above
266 }
267
269 ObjectType * _Object;
270
273 AllocatorType _Allocator;
274
279 using ReleaseFuncType = void(*)(ObjectType *, const AllocatorType &);
281};
282
284template<class ObjectType>
286
288template<class ObjectType>
290
292template<class ObjectType>
294
296template<class ObjectType>
298
299//=======================================================================================
300// CSharedMix Inline Methods
301//=======================================================================================
302
307ULANG_FORCEINLINE bool CSharedMix::Dereference() const
308{
309 ULANG_ASSERTF(_RefCount > 0, "Tried to dereference an object that has no references!");
310 return (--_RefCount == 0);
311}
312
313}
314
315#undef UE_API
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
#define UE_API
Definition SColorGradingComponentViewer.h:12
#define ULANG_ASSERT(expr)
Definition Common.h:285
#define ULANG_FORCEINLINE
Definition Common.h:188
#define ULANG_ASSERTF(expr, format,...)
Definition Common.h:290
Raw memory allocator that allocates memory from the global heap.
Definition Allocator.h:64
Definition SharedPointer.h:28
static TSPtrG< ObjectType, false, CHeapRawAllocator > SharedThis(ObjectType *This)
Definition SharedPointer.h:41
virtual UE_API ~CSharedMix()
Definition SharedPointer.cpp:8
CSharedMix()
Definition SharedPointer.h:31
uint32_t GetRefCount() const
Definition SharedPointer.h:37
static TSPtrG< const ObjectType, false, CHeapRawAllocator > SharedThis(const ObjectType *This)
Definition SharedPointer.h:47
CSharedMix & operator=(const CSharedMix &Other)=delete
CSharedMix(const CSharedMix &Other)=delete
Definition SharedPointerArray.h:21
Definition SharedPointer.h:77
ULANG_FORCEINLINE bool operator!=(OtherObjectType *Object) const
Definition SharedPointer.h:152
ReleaseFuncType _ReleaseFunc
Definition SharedPointer.h:280
ULANG_FORCEINLINE ObjectType & operator*() const
Definition SharedPointer.h:109
ULANG_FORCEINLINE friend void Swap(TSPtrG &A, TSPtrG &B)
Definition SharedPointer.h:173
ULANG_FORCEINLINE void Reset()
Definition SharedPointer.h:113
ULANG_FORCEINLINE const TSPtrG< OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType... > & As() const
Definition SharedPointer.h:137
ULANG_FORCEINLINE TSPtrG & operator=(const TSPtrG &Other)
Definition SharedPointer.h:99
ULANG_FORCEINLINE TSPtrG & operator=(TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &&Other)
Definition SharedPointer.h:104
ULANG_FORCEINLINE bool operator==(const TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other) const
Definition SharedPointer.h:145
ULANG_FORCEINLINE TSPtrG & SetNew(CtorArgsType &&... CtorArgs)
Definition SharedPointer.h:92
ULANG_FORCEINLINE void Release()
Let go of our object.
Definition SharedPointer.h:262
ULANG_FORCEINLINE TSPtrG & AssignCopy(const TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other)
Definition SharedPointer.h:208
ULANG_FORCEINLINE TSPtrG< ObjectType, false, AllocatorType, AllocatorArgsType... > AsRef() &&
Definition SharedPointer.h:121
ULANG_FORCEINLINE TSPtrG Map(FuncType &&Func, ArgTypes &&... Args) &&
Definition SharedPointer.h:177
ULANG_FORCEINLINE TSPtrG< ObjectType, false, AllocatorType, AllocatorArgsType... > & AsRef() &
Definition SharedPointer.h:115
ULANG_FORCEINLINE TSPtrG & AssignMove(TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &&Other)
Definition SharedPointer.h:231
ULANG_FORCEINLINE TSPtrG(TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &&Other)
Definition SharedPointer.h:88
ULANG_FORCEINLINE ObjectType * Get() const
Definition SharedPointer.h:111
ULANG_FORCEINLINE bool operator==(const TSPtrG &Other) const
Definition SharedPointer.h:143
ULANG_FORCEINLINE bool operator!=(const TSPtrG &Other) const
Definition SharedPointer.h:148
AllocatorType _Allocator
Definition SharedPointer.h:273
static ULANG_FORCEINLINE TSPtrG New(AllocatorArgsType &&... AllocatorArgs, CtorArgsType &&... CtorArgs)
Definition SharedPointer.h:94
ULANG_FORCEINLINE ObjectType * operator->() const
Definition SharedPointer.h:110
ULANG_FORCEINLINE bool IsValid() const
Definition SharedPointer.h:169
ULANG_FORCEINLINE bool operator!=(const TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other) const
Definition SharedPointer.h:150
ULANG_FORCEINLINE TSPtrG(NullPtrType NullPtr=nullptr)
Definition SharedPointer.h:82
ULANG_FORCEINLINE TSPtrG & operator=(TSPtrG &&Other)
Definition SharedPointer.h:102
ULANG_FORCEINLINE TSPtrG< OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType... > & As()
Definition SharedPointer.h:135
ULANG_FORCEINLINE bool operator==(NullPtrType) const
Definition SharedPointer.h:141
ULANG_FORCEINLINE TSPtrG(ObjectType *Object, const AllocatorType &Allocator)
Definition SharedPointer.h:195
ULANG_FORCEINLINE bool operator!() const
Definition SharedPointer.h:168
ULANG_FORCEINLINE const AllocatorType & GetAllocator() const
Definition SharedPointer.h:112
friend class TSPtrG
Definition SharedPointer.h:186
ULANG_FORCEINLINE bool operator!=(NullPtrType) const
Definition SharedPointer.h:142
ULANG_FORCEINLINE ~TSPtrG()
Definition SharedPointer.h:89
ULANG_FORCEINLINE bool operator>(const TSPtrG &Other) const
Definition SharedPointer.h:158
ObjectType * _Object
Pointer to original object.
Definition SharedPointer.h:269
ULANG_FORCEINLINE void EnableRelease()
Set the release function pointer to a valid value.
Definition SharedPointer.h:248
ULANG_FORCEINLINE bool operator<(const TSPtrG &Other) const
Definition SharedPointer.h:153
ULANG_FORCEINLINE bool operator==(OtherObjectType *Object) const
Definition SharedPointer.h:147
void(*)(ObjectType *, const AllocatorType &) ReleaseFuncType
Definition SharedPointer.h:279
ULANG_FORCEINLINE TSPtrG(const TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other)
Definition SharedPointer.h:85
ULANG_FORCEINLINE TSPtrG & operator=(NullPtrType)
Definition SharedPointer.h:98
ULANG_FORCEINLINE TSPtrG(TSPtrG &&Other)
Definition SharedPointer.h:86
ULANG_FORCEINLINE const TSPtrG< ObjectType, false, AllocatorType, AllocatorArgsType... > & AsRef() const &
Definition SharedPointer.h:127
ULANG_FORCEINLINE TSPtrG(const TSPtrG &Other)
Definition SharedPointer.h:83
Definition SharedPointerSet.h:19
Definition VVMEngineEnvironment.h:23
std::nullptr_t NullPtrType
Definition Common.h:325
@ DefaultInit
Definition Common.h:378
ULANG_FORCEINLINE auto Invoke(FuncType &&Func, ArgTypes &&... Args) -> decltype(uLang::ForwardArg< FuncType >(Func)(uLang::ForwardArg< ArgTypes >(Args)...))
Definition Invoke.h:47
ULANG_FORCEINLINE T && ForwardArg(typename TRemoveReference< T >::Type &Obj)
Definition References.h:115
TEnableIf< TUseBitwiseSwap< T >::Value >::Type Swap(T &A, T &B)
Definition Storage.h:138
ULANG_FORCEINLINE TRemoveReference< T >::Type && Move(T &&Obj)
Definition References.h:86
@ false
Definition radaudio_common.h:23