UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TypeContainer.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"
10#include "Templates/Function.h"
11#include "Containers/Map.h"
13#include "HAL/TlsAutoCleanup.h"
14#include "Misc/ScopeLock.h"
15
20{
23
25 Process,
26
28 Thread,
29};
30
31
65template<ESPMode Mode = ESPMode::ThreadSafe>
67{
69 class IInstanceProvider
70 {
71 public:
72
74 virtual ~IInstanceProvider() { }
75
81 virtual TSharedPtr<void, Mode> GetInstance() = 0;
82 };
83
85 template<class T>
86 struct TFunctionInstanceProvider
87 : public IInstanceProvider
88 {
89 TFunctionInstanceProvider(TFunction<TSharedPtr<void, Mode>()>&& InCreateFunc)
90 : CreateFunc(MoveTemp(InCreateFunc))
91 { }
92
93 virtual ~TFunctionInstanceProvider() override { }
94
95 virtual TSharedPtr<void, Mode> GetInstance() override
96 {
97 return CreateFunc();
98 }
99
101 };
102
103
105 template<class T>
106 struct TSharedInstanceProvider
107 : public IInstanceProvider
108 {
109 TSharedInstanceProvider(const TSharedRef<T, Mode>& InInstance)
110 : Instance(InInstance)
111 { }
112
113 virtual ~TSharedInstanceProvider() { }
114
115 virtual TSharedPtr<void, Mode> GetInstance() override
116 {
117 return Instance;
118 }
119
120 TSharedRef<T, Mode> Instance;
121 };
122
123
125 struct FThreadInstanceProvider
126 : public IInstanceProvider
127 {
128 FThreadInstanceProvider(TFunction<TSharedPtr<void, Mode>()>&& InCreateFunc)
129 : CreateFunc(MoveTemp(InCreateFunc))
130 , TlsSlot(FPlatformTLS::AllocTlsSlot())
131 { }
132
133 virtual ~FThreadInstanceProvider() override
134 {
136 }
137
139 uint32 TlsSlot;
140 };
141
142
144 template<class T>
145 struct TThreadInstanceProvider
146 : public FThreadInstanceProvider
147 {
148 typedef TTlsAutoCleanupValue<TSharedPtr<T, Mode>> TlsValueType;
149
150 TThreadInstanceProvider(TFunction<TSharedPtr<void, Mode>()>&& InCreateFunc)
151 : FThreadInstanceProvider(MoveTemp(InCreateFunc))
152 { }
153
154 virtual ~TThreadInstanceProvider() override { }
155
156 virtual TSharedPtr<void, Mode> GetInstance() override
157 {
158 auto TlsValue = (TlsValueType*)FPlatformTLS::GetTlsValue(this->TlsSlot);
159
160 if (TlsValue == nullptr)
161 {
162 TlsValue = new TlsValueType(StaticCastSharedPtr<T>(this->CreateFunc()));
163 TlsValue->Register();
164 FPlatformTLS::SetTlsValue(this->TlsSlot, TlsValue);
165 }
166
167 return TlsValue->Get();
168 }
169 };
170
171public:
172
180 template<class R>
182 {
183 FScopeLock Lock(&CriticalSection);
184 {
185 const TSharedPtr<IInstanceProvider>& Provider = Providers.FindRef(TNameOf<R>::GetName());
186 check(Provider.IsValid());
187
188 return StaticCastSharedPtr<R>(Provider->GetInstance()).ToSharedRef();
189 }
190 }
191
202 template<class R>
204 {
205 return GetInstance<R>().ToSharedRef();
206 }
207
214 template<class R>
215 bool IsRegistered() const
216 {
217 FScopeLock Lock(&CriticalSection);
218 {
219 return Providers.Contains(TNameOf<R>::GetName());
220 }
221 }
222
232 template<class R, class T, typename... P>
234 {
235 static_assert(TPointerIsConvertibleFromTo<T, R>::Value, "T must implement R");
236
238
239 switch(Scope)
240 {
242 Provider = MakeShareable(
244 [this]() -> TSharedPtr<void, Mode> {
245 return MakeShareable(new T(GetInstance<P>()...));
246 }
247 )
248 );
249 break;
250
252 Provider = MakeShareable(
254 [this]() -> TSharedPtr<void, Mode> {
255 return MakeShareable(new T(GetInstance<P>()...));
256 }
257 )
258 );
259 break;
260
261 default:
262 Provider = MakeShareable(
264 MakeShareable(new T(GetInstance<P>()...))
265 )
266 );
267 break;
268 }
269
270 AddProvider(TNameOf<R>::GetName(), Provider);
271 }
272
281 template<class R, class D, typename... P>
282 void RegisterDelegate(D Delegate)
283 {
284 static_assert(std::is_same_v<TSharedRef<R, Mode>, typename D::RetValType>, "Delegate return type must be TSharedPtr<R>");
285
288 [this, Delegate]() -> TSharedPtr<void, Mode> {
289 return Delegate.Execute(GetInstance<P>()...);
290 }
291 )
292 );
293
294 AddProvider(TNameOf<R>::GetName(), Provider);
295 }
296
305 template<class R>
307 {
310 [=, this]() -> TSharedPtr<void, Mode> {
311 return CreateFunc();
312 }
313 )
314 );
315
316 AddProvider(TNameOf<R>::GetName(), Provider);
317 }
318
328 template<class R, typename P0, typename... P>
330 {
333 [=, this]() -> TSharedPtr<void, Mode> {
334 return CreateFunc(GetInstance<P0>(), GetInstance<P>()...);
335 }
336 )
337 );
338
339 AddProvider(TNameOf<R>::GetName(), Provider);
340 }
341
342#if (defined(_MSC_VER) && _MSC_VER >= 1900) || defined(__clang__)
353 template<class R, typename... P>
355 {
358 [this, CreateFunc]() -> TSharedPtr<void, Mode> {
359 return CreateFunc(GetInstance<P>()...);
360 }
361 )
362 );
363
364 AddProvider(TNameOf<R>::GetName(), Provider);
365 }
366#endif
367
376 template<class R, class T>
383
390 template<class R>
392 {
393 FScopeLock Lock(&CriticalSection);
394 {
395 Providers.Remove(TNameOf<R>::GetName());
396 }
397 }
398
399protected:
400
408 {
409 FScopeLock Lock(&CriticalSection);
410 {
411 Providers.Add(Name, Provider);
412 }
413 }
414
415private:
416
418 mutable FCriticalSection CriticalSection;
419
422};
423
424
426class FTypeContainer : public TTypeContainer<ESPMode::ThreadSafe> { };
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable(ObjectType *InObject)
Definition SharedPointer.h:1947
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
ETypeContainerScope
Definition TypeContainer.h:20
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ScopeLock.h:141
Definition TypeContainer.h:426
Definition AndroidPlatformMisc.h:14
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
UE_FORCEINLINE_HINT const bool IsValid() const
Definition SharedPointer.h:1085
Definition SharedPointer.h:153
Definition TlsAutoCleanup.h:26
Definition TypeContainer.h:67
void RegisterFactory(const TFunction< TSharedRef< R, Mode >()> &CreateFunc)
Definition TypeContainer.h:306
TSharedRef< R, Mode > GetInstanceRef() const
Definition TypeContainer.h:203
void RegisterDelegate(D Delegate)
Definition TypeContainer.h:282
TSharedRef< R, Mode > GetInstance() const
Definition TypeContainer.h:181
void AddProvider(const TCHAR *Name, const TSharedPtr< IInstanceProvider > &Provider)
Definition TypeContainer.h:407
bool IsRegistered() const
Definition TypeContainer.h:215
void RegisterFactory(const TFunction< TSharedRef< R, Mode >(TSharedRef< P0, Mode >, TSharedRef< P, Mode >...)> &CreateFunc)
Definition TypeContainer.h:329
void RegisterClass(ETypeContainerScope Scope=ETypeContainerScope::Process)
Definition TypeContainer.h:233
void Unregister()
Definition TypeContainer.h:391
void RegisterInstance(const TSharedRef< T, Mode > &Instance)
Definition TypeContainer.h:377
static uint32 AllocTlsSlot(void)
Definition AndroidPlatformTLS.h:30
static UE_FORCEINLINE_HINT void FreeTlsSlot(uint32 SlotIndex)
Definition AndroidPlatformTLS.h:67
static UE_FORCEINLINE_HINT void * GetTlsValue(uint32 SlotIndex)
Definition AndroidPlatformTLS.h:57
static UE_FORCEINLINE_HINT void SetTlsValue(uint32 SlotIndex, void *Value)
Definition AndroidPlatformTLS.h:47
Definition UnrealTypeTraits.h:191
Definition PointerIsConvertibleFromTo.h:60