UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TypedElementCounter.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "Containers/Map.h"
9#include "CoreMinimal.h"
11#include "HAL/Platform.h"
13#include "Templates/Function.h"
14#include "Templates/Tuple.h"
15#include "Templates/UniquePtr.h"
17#include "UObject/Interface.h"
18#include "UObject/NameTypes.h"
23
24#include <type_traits>
25
26#include "TypedElementCounter.generated.h"
27
28class UObject;
30
31template <typename KeyType>
33{
34 if constexpr (std::is_convertible_v<KeyType, const UObject*>)
35 {
36 return std::remove_pointer_t<KeyType>::StaticClass()->GetFName();
37 }
38 else
39 {
40 static const FName KeyTypeName = TNameOf<KeyType>::GetName();
41 return KeyTypeName;
42 }
43}
44
50{
51public:
53
56
59
62
67
72
77
82 template <typename KeyType>
83 void IncrementCounter(const FName InCategory, const KeyType& InKey)
84 {
85 TUniquePtr<ICounterCategory>& CounterCategory = CounterCategories.FindOrAdd(InCategory);
86 if (!CounterCategory)
87 {
89 }
90 CounterCategory->IncrementCounter(InKey);
91 }
92
97 template <typename KeyType>
98 void DecrementCounter(const FName InCategory, const KeyType& InKey)
99 {
100 if (TUniquePtr<ICounterCategory>* CounterCategory = CounterCategories.Find(InCategory))
101 {
102 (*CounterCategory)->DecrementCounter(InKey);
103 }
104 }
105
109 template <typename KeyType>
111 {
112 if (const TUniquePtr<ICounterCategory>* CounterCategory = CounterCategories.Find(InCategory))
113 {
114 return (*CounterCategory)->GetCounterValue(InKey);
115 }
116 return 0;
117 }
118
122 template <typename KeyType>
124 {
125 if (const TUniquePtr<ICounterCategory>* CounterCategory = CounterCategories.Find(InCategory))
126 {
127 (*CounterCategory)->ForEachCounterValue<KeyType>(InCallback);
128 }
129 }
130
134 template <typename KeyType>
135 void ClearCounter(const FName InCategory, const KeyType& InKey)
136 {
137 if (TUniquePtr<ICounterCategory>* CounterCategory = CounterCategories.Find(InCategory))
138 {
139 (*CounterCategory)->ClearCounter(InKey);
140 }
141 }
142
147
152
157
158private:
159 class ICounterCategory
160 {
161 public:
162 virtual ~ICounterCategory() = default;
163
164 template <typename KeyType>
165 FCounterValue GetCounterValue(const KeyType& InKey) const
166 {
167 return GetCounterValueImpl(&InKey, GetTypedElementCounterKeyName<KeyType>());
168 }
169
170 template <typename KeyType>
171 void ForEachCounterValue(TFunctionRef<bool(const KeyType&, FCounterValue)> InCallback) const
172 {
173 ForEachCounterValueImpl(&InCallback, GetTypedElementCounterKeyName<KeyType>());
174 }
175
176 template <typename KeyType>
177 void IncrementCounter(const KeyType& InKey)
178 {
179 IncrementCounterImpl(&InKey, GetTypedElementCounterKeyName<KeyType>());
180 }
181
182 template <typename KeyType>
183 void DecrementCounter(const KeyType& InKey)
184 {
185 DecrementCounterImpl(&InKey, GetTypedElementCounterKeyName<KeyType>());
186 }
187
188 template <typename KeyType>
189 void ClearCounter(const KeyType& InKey)
190 {
191 ClearCounterImpl(&InKey, GetTypedElementCounterKeyName<KeyType>());
192 }
193
194 void ClearCounters()
195 {
196 ClearCountersImpl();
197 }
198
199 protected:
200 virtual FCounterValue GetCounterValueImpl(const void* InKey, const FName InKeyTypeName) const = 0;
201 virtual void ForEachCounterValueImpl(const void* InCallback, const FName InKeyTypeName) const = 0;
202 virtual void IncrementCounterImpl(const void* InKey, const FName InKeyTypeName) = 0;
203 virtual void DecrementCounterImpl(const void* InKey, const FName InKeyTypeName) = 0;
204 virtual void ClearCounterImpl(const void* InKey, const FName InKeyTypeName) = 0;
205 virtual void ClearCountersImpl() = 0;
206 };
207
208 template <typename KeyType>
209 class TCounterCategory : public ICounterCategory
210 {
211 public:
212 TCounterCategory()
213 : KeyTypeName(GetTypedElementCounterKeyName<KeyType>())
214 {
215 }
216
217 private:
218 virtual FCounterValue GetCounterValueImpl(const void* InKey, const FName InKeyTypeName) const override
219 {
220 checkf(KeyTypeName == InKeyTypeName, TEXT("Invalid counter access! Counter is keyed against '%s' but '%s' was given."), *KeyTypeName.ToString(), *InKeyTypeName.ToString());
221
222 const KeyType* KeyPtr = static_cast<const KeyType*>(InKey);
223 return Counters.FindRef(*KeyPtr);
224 }
225
226 virtual void ForEachCounterValueImpl(const void* InCallback, const FName InKeyTypeName) const override
227 {
228 checkf(KeyTypeName == InKeyTypeName, TEXT("Invalid counter access! Counter is keyed against '%s' but '%s' was given."), *KeyTypeName.ToString(), *InKeyTypeName.ToString());
229
230 const TFunctionRef<bool(const KeyType&, FCounterValue)>* CallbackPtr = static_cast<const TFunctionRef<bool(const KeyType&, FCounterValue)>*>(InCallback);
232 {
234 {
235 break;
236 }
237 }
238 }
239
240 virtual void IncrementCounterImpl(const void* InKey, const FName InKeyTypeName) override
241 {
242 checkf(KeyTypeName == InKeyTypeName, TEXT("Invalid counter access! Counter is keyed against '%s' but '%s' was given."), *KeyTypeName.ToString(), *InKeyTypeName.ToString());
243
244 const KeyType* KeyPtr = static_cast<const KeyType*>(InKey);
245 FCounterValue& CounterValue = Counters.FindOrAdd(*KeyPtr);
246 ++CounterValue;
247 }
248
249 virtual void DecrementCounterImpl(const void* InKey, const FName InKeyTypeName) override
250 {
251 checkf(KeyTypeName == InKeyTypeName, TEXT("Invalid counter access! Counter is keyed against '%s' but '%s' was given."), *KeyTypeName.ToString(), *InKeyTypeName.ToString());
252
253 const KeyType* KeyPtr = static_cast<const KeyType*>(InKey);
254 if (FCounterValue* CounterValue = Counters.Find(*KeyPtr))
255 {
256 checkf(*CounterValue > 0, TEXT("Counter value unflow!"));
257 if (--(*CounterValue) == 0)
258 {
259 Counters.Remove(*KeyPtr);
260 }
261 }
262 }
263
264 virtual void ClearCounterImpl(const void* InKey, const FName InKeyTypeName) override
265 {
266 checkf(KeyTypeName == InKeyTypeName, TEXT("Invalid counter access! Counter is keyed against '%s' but '%s' was given."), *KeyTypeName.ToString(), *InKeyTypeName.ToString());
267
268 const KeyType* KeyPtr = static_cast<const KeyType*>(InKey);
269 Counters.Remove(*KeyPtr);
270 }
271
272 virtual void ClearCountersImpl() override
273 {
274 Counters.Empty();
275 }
276
277 private:
278 FName KeyTypeName;
280 };
281
286
291};
292
293UINTERFACE(MinimalAPI, meta = (CannotImplementInterfaceInBlueprint))
298
314
315template <>
316struct TTypedElement<ITypedElementCounterInterface> : public TTypedElementBase<ITypedElementCounterInterface>
317{
318 void IncrementCountersForElement(FTypedElementCounter& InOutCounter) const { InterfacePtr->IncrementCountersForElement(*this, InOutCounter); }
319 void DecrementCountersForElement(FTypedElementCounter& InOutCounter) const { InterfacePtr->DecrementCountersForElement(*this, InOutCounter); }
320};
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
TSizedDefaultAllocator< 32 > FDefaultAllocator
Definition ContainerAllocationPolicies.h:831
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UINTERFACE(...)
Definition ObjectMacros.h:780
FName GetTypedElementCounterKeyName()
Definition TypedElementCounter.h:32
Definition NameTypes.h:617
Definition TypedElementCounter.h:50
int32 FCounterValue
Definition TypedElementCounter.h:52
FCounterValue GetCounterValue(const FName InCategory, const KeyType &InKey) const
Definition TypedElementCounter.h:110
void ForEachCounterValue(const FName InCategory, TFunctionRef< bool(const KeyType &, FCounterValue)> InCallback) const
Definition TypedElementCounter.h:123
void ClearCounter(const FName InCategory, const KeyType &InKey)
Definition TypedElementCounter.h:135
FTypedElementCounter()=default
static TYPEDELEMENTFRAMEWORK_API FName GetElementTypeCategoryName()
Definition TypedElementCounter.cpp:62
FTypedElementCounter & operator=(FTypedElementCounter &&)=default
void DecrementCounter(const FName InCategory, const KeyType &InKey)
Definition TypedElementCounter.h:98
FTypedElementCounter(FTypedElementCounter &&)=default
FTypedElementCounter & operator=(const FTypedElementCounter &)=delete
FTypedElementCounter(const FTypedElementCounter &)=delete
void IncrementCounter(const FName InCategory, const KeyType &InKey)
Definition TypedElementCounter.h:83
TYPEDELEMENTFRAMEWORK_API void ClearCounters()
Definition TypedElementCounter.cpp:57
Definition TypedElementCounter.h:300
virtual void IncrementCountersForElement(const FTypedElementHandle &InElementHandle, FTypedElementCounter &InOutCounter)
Definition TypedElementCounter.h:307
virtual void DecrementCountersForElement(const FTypedElementHandle &InElementHandle, FTypedElementCounter &InOutCounter)
Definition TypedElementCounter.h:312
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition SortedMap.h:20
Definition UniquePtr.h:107
Definition Interface.h:19
Definition Object.h:95
Definition TypedElementCounter.h:295
Definition TypedElementRegistry.h:55
Definition NameTypes.h:1639
Definition TypedElementHandle.h:18
static TCHAR const * GetName()
Definition UnrealTypeTraits.h:192
Definition Tuple.h:652
Definition TypedElementHandle.h:271
BaseInterfaceType * InterfacePtr
Definition TypedElementHandle.h:386
void IncrementCountersForElement(FTypedElementCounter &InOutCounter) const
Definition TypedElementCounter.h:318
void DecrementCountersForElement(FTypedElementCounter &InOutCounter) const
Definition TypedElementCounter.h:319
Definition TypedElementHandle.h:396
Definition WeakObjectPtrTemplates.h:25