UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StateStreamStore.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "Pow2ChunkedArray.h"
7
9// TStateStreamStore is a threadsafe, reference stable storage. User can hold on to pointers of
10// elements while other elements are added/removed.
11// Store is backed by a Pow2ChunkedArray and use a "free list" to be able to reuse removed elements
12
13template<typename T>
15{
16public:
19
20 inline uint32 Add(const T& Value);
21 inline void* AddUninitialized(uint32& OutIndex);
22 inline void Remove(uint32 Index);
23
24 template <typename... ArgsType>
25 inline uint32 Emplace(ArgsType&&... Args);
26
28
29 inline uint32 GetUsedCount() const;
30
31private:
32 FRWLock Lock;
34 uint32 FirstFreeIndex = ~0u;
35};
36
38// Implementation
39
40template<typename T>
42{
43 static_assert(sizeof(T) >= sizeof(uint32));
44}
45
46template<typename T>
48{
49 if (!std::is_trivially_destructible_v<T>)
50 {
51 while (FirstFreeIndex != ~0u)
52 {
53 T& Element = Array[FirstFreeIndex];
54 FirstFreeIndex = *reinterpret_cast<uint32*>(&Element);
55 new (&Element) T();
56 }
57 }
58}
59
60template<typename T>
62{
64 new (AddUninitialized(Index)) T(Value);
65 return Index;
66}
67
68template<typename T>
70{
72 if (FirstFreeIndex == ~0u)
73 {
75 return Array.AddUninitialized(OutIndex);
76 }
77
78 uint32 Index = FirstFreeIndex;
79 void* Element = &Array[Index];
80 FirstFreeIndex = *reinterpret_cast<uint32*>(Element);
82
83 OutIndex = Index;
84 return &Array[Index];
85}
86
87template<typename T>
89{
90 T& Element = Array[Index];
91 Element.~T();
93 *reinterpret_cast<uint32*>(&Element) = FirstFreeIndex;
94 FirstFreeIndex = Index;
96}
97
98template<typename T>
99template <typename... ArgsType>
101{
102 uint32 OutIndex;
103 new (AddUninitialized(OutIndex)) T(Forward<ArgsType>(Args)...);
104 return OutIndex;
105}
106
107template<typename T>
112
113template<typename T>
115{
116 uint32 UsedCount = Array.Num();
117 uint32 Index = FirstFreeIndex;
118 while (Index != ~0u)
119 {
120 --UsedCount;
121 Index = *(uint32*)&Array[Index];
122 }
123 return UsedCount;
124}
125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Pow2ChunkedArray.h:18
Definition StateStreamStore.h:15
T & operator[](uint32 Index)
Definition StateStreamStore.h:108
uint32 Emplace(ArgsType &&... Args)
Definition StateStreamStore.h:100
~TStateStreamStore()
Definition StateStreamStore.h:47
uint32 GetUsedCount() const
Definition StateStreamStore.h:114
void Remove(uint32 Index)
Definition StateStreamStore.h:88
void * AddUninitialized(uint32 &OutIndex)
Definition StateStreamStore.h:69
TStateStreamStore()
Definition StateStreamStore.h:41
uint32 Add(const T &Value)
Definition StateStreamStore.h:61
Definition CriticalSection.h:14
UE_REWRITE void WriteLock()
Definition CriticalSection.h:21
UE_REWRITE void WriteUnlock()
Definition CriticalSection.h:26
U16 Index
Definition radfft.cpp:71