UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TypedElementOwnerStore.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 "Misc/ScopeLock.h"
9
15template <typename ElementOwnerType>
17{
18public:
20 : SynchObject(InSynchObject)
21 {
22 check(SynchObject);
23 SynchObject->Lock();
24 }
25
27 {
28 if (SynchObject)
29 {
30 SynchObject->Unlock();
31 }
32 }
33
36
38 : SynchObject(InOther.SynchObject)
39 , ElementOwner(InOther.ElementOwner)
40 {
41 InOther.SynchObject = nullptr;
42 InOther.ElementOwner = nullptr;
43 }
44
46
47 inline explicit operator bool() const
48 {
49 return IsSet();
50 }
51
52 inline bool IsSet() const
53 {
54 return ElementOwner != nullptr;
55 }
56
58 {
59 check(ElementOwner);
60 return *ElementOwner;
61 }
62
64 {
65 check(ElementOwner);
66 return ElementOwner;
67 }
68
70 {
71 checkf(!ElementOwner, TEXT("Element owner was already set!"));
72 ElementOwner = InElementOwner;
73
74 // If we didn't find an element then we can unlock immediately
75 // Otherwise we hold the lock for as long as the element is being read to avoid a map write from invalidating the reference from reallocation
76 check(SynchObject);
77 if (!ElementOwner)
78 {
79 SynchObject->Unlock();
80 SynchObject = nullptr;
81 }
82 }
83
84private:
85 FTransactionallySafeCriticalSection* SynchObject = nullptr;
86 ElementOwnerType* ElementOwner = nullptr;
87};
88
89template <typename ElementDataType>
91
92template <typename ElementDataType>
94
95template <typename KeyDataType>
97{
98 return TEXT("<GetTypedElementOwnerStoreKeyDebugString not implemented for key type>");
99}
100
102{
103 return InKey->GetPathName();
104}
105
110template <typename ElementDataType, typename KeyDataType = const void*>
112{
113public:
114 static_assert(TNumericLimits<int32>::Max() >= TypedHandleMaxElementId, "TTypedElementOwnerStore internally uses TMap so cannot store TypedHandleMaxElementId! Consider making this container 64-bit aware, or explicitly remove this compile time check.");
115
117
119 {
120 checkf(ElementOwnerMap.Num() == 0, TEXT("Element owners were still registered during destruction! This will leak elements, and you should unregister and destroy all elements prior to destruction!"));
121 }
122
129 {
131 checkf(!ElementOwnerMap.Contains(InKey), TEXT("Element owner has already been registered for this key (%s)! This will leak elements, and you should unregister and destroy the old element owner for this instance before adding a new one!"), *GetTypedElementOwnerStoreKeyDebugString(InKey));
132 checkf(InElementOwner, TEXT("Element owner passed to RegisterElementOwner was null for key (%s)! Element owners must be valid!"), *GetTypedElementOwnerStoreKeyDebugString(InKey));
133 ScopedAccess.Private_SetElementOwner(&ElementOwnerMap.Add(InKey, MoveTemp(InElementOwner)));
134 return ScopedAccess;
135 }
136
142 {
144 {
145 UE::TScopeLock Lock(ElementOwnerMapCS);
146 ElementOwnerMap.RemoveAndCopyValue(InKey, ElementOwner);
147 }
148 return ElementOwner;
149 }
150
155 {
156 UE::TScopeLock Lock(ElementOwnerMapCS);
157 for (auto It = ElementOwnerMap.CreateIterator(); It; ++It)
158 {
159 if (InShouldUnregisterElement(It->Value))
160 {
162 It.RemoveCurrent();
163 }
164 }
165 }
166
184
190 {
192 ScopedAccess.Private_SetElementOwner(ElementOwnerMap.Find(InKey));
193 return ScopedAccess;
194 }
195
206
211 {
212 UE::TScopeLock Lock(ElementOwnerMapCS);
213 return ElementOwnerMap.Contains(InKey);
214 }
215
216private:
218 mutable FTransactionallySafeCriticalSection ElementOwnerMapCS;
219};
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
::FCriticalSection FTransactionallySafeCriticalSection
Definition TransactionallySafeCriticalSection.h:16
constexpr SIZE_T TypedHandleMaxElementId
Definition TypedElementLimits.h:24
FString GetTypedElementOwnerStoreKeyDebugString(const KeyDataType &InKey)
Definition TypedElementOwnerStore.h:96
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
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition ScopeLock.h:21
Definition Object.h:95
Definition NumericLimits.h:41
Definition TypedElementOwnerStore.h:17
TTypedElementOwnerScopedAccessImpl(const TTypedElementOwnerScopedAccessImpl &)=delete
void Private_SetElementOwner(ElementOwnerType *InElementOwner)
Definition TypedElementOwnerStore.h:69
TTypedElementOwnerScopedAccessImpl(TTypedElementOwnerScopedAccessImpl &&InOther)
Definition TypedElementOwnerStore.h:37
TTypedElementOwnerScopedAccessImpl & operator=(const TTypedElementOwnerScopedAccessImpl &)=delete
bool IsSet() const
Definition TypedElementOwnerStore.h:52
ElementOwnerType * operator->() const
Definition TypedElementOwnerStore.h:63
TTypedElementOwnerScopedAccessImpl & operator=(TTypedElementOwnerScopedAccessImpl &&)=delete
TTypedElementOwnerScopedAccessImpl(FTransactionallySafeCriticalSection *InSynchObject)
Definition TypedElementOwnerStore.h:19
ElementOwnerType & operator*() const
Definition TypedElementOwnerStore.h:57
~TTypedElementOwnerScopedAccessImpl()
Definition TypedElementOwnerStore.h:26
Definition TypedElementOwnerStore.h:112
TTypedElementOwnerScopedAccess< ElementDataType > FindElementOwner(const KeyDataType &InKey) const
Definition TypedElementOwnerStore.h:189
TTypedElementOwnerScopedAccess< ElementDataType > FindOrRegisterElementOwner(const KeyDataType &InKey, TFunctionRef< TTypedElementOwner< ElementDataType >()> InCreateElement)
Definition TypedElementOwnerStore.h:171
bool ContainsElementOwner(const KeyDataType &InKey) const
Definition TypedElementOwnerStore.h:210
void UnregisterElementOwners(TFunctionRef< bool(const TTypedElementOwner< ElementDataType > &)> InShouldUnregisterElement, TFunctionRef< void(TTypedElementOwner< ElementDataType > &&)> InOnUnregisterElement)
Definition TypedElementOwnerStore.h:154
TTypedElementOwnerScopedAccess< ElementDataType > RegisterElementOwner(const KeyDataType &InKey, TTypedElementOwner< ElementDataType > &&InElementOwner)
Definition TypedElementOwnerStore.h:128
TTypedElementOwnerScopedMutableAccess< ElementDataType > FindMutableElementOwner(const KeyDataType &InKey)
Definition TypedElementOwnerStore.h:200
TTypedElementOwnerStore()=default
~TTypedElementOwnerStore()
Definition TypedElementOwnerStore.h:118
TTypedElementOwner< ElementDataType > UnregisterElementOwner(const KeyDataType &InKey)
Definition TypedElementOwnerStore.h:141
Definition TypedElementHandle.h:409