UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UncheckedArray.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "CoreMinimal.h"
5#include "Containers/Array.h"
7
8// If CHAOS_CHECK_UNCHECKED_ARRAY is 1, we still enable range checks on "unchecked" arrays.
9// This is for debug builds or sanity checks in other builds.
10#ifndef CHAOS_CHECK_UNCHECKED_ARRAY
11#if UE_BUILD_DEBUG
12#define CHAOS_CHECK_UNCHECKED_ARRAY 1
13#else
14#define CHAOS_CHECK_UNCHECKED_ARRAY 0
15#endif
16#endif
17
18// @macro CHAOS_CARRAY_SENTINEL: Set to 1 to add some sentinels into TCArray to trap buffer overruns
19#ifndef CHAOS_CARRAY_SENTINEL
20#define CHAOS_CARRAY_SENTINEL 0
21#endif
22
23// @macro IF_CHAOS_CARRAY_SENTINEL(X): Include some code block X only when sentinels are enabled
24// This is intended for one-line statements with no commas in them and is just shorthand for
25// #if CHAOS_CARRAY_SENTINEL
26// X;
27// #endif
28#if CHAOS_CARRAY_SENTINEL
29#define IF_CHAOS_CARRAY_SENTINEL(X) X
30#else
31#define IF_CHAOS_CARRAY_SENTINEL(X)
32#endif
33
34namespace Chaos
35{
45 template<typename T, int32 N>
46 class TCArray
47 {
48 public:
49 static const int32 MaxElements = N;
50 using ElementType = T;
51
53 {
54 return TCArray<T, N>(N);
55 }
56
58 {
59 return TCArray<T, N>(0);
60 }
61
62 inline TCArray() : NumElements(0)
63 {
64 }
65
66 inline int32 Num() const
67 {
68 CheckSentinels();
69
70 return NumElements;
71 }
72
73 inline int32 Max() const
74 {
75 CheckSentinels();
76
77 return MaxElements;
78 }
79
80 inline bool IsEmpty() const
81 {
82 CheckSentinels();
83
84 return NumElements == 0;
85 }
86
87 inline bool IsFull() const
88 {
89 CheckSentinels();
90
91 return NumElements == MaxElements;
92 }
93
95 {
96 check(Index < NumElements);
97 CheckSentinels();
98
99 return Elements[Index];
100 }
101
102 inline const ElementType& operator[](const int32 Index) const
103 {
104 check(Index < NumElements);
105 CheckSentinels();
106
107 return Elements[Index];
108 }
109
114 inline void SetNum(const int32 InNum)
115 {
117 CheckSentinels();
118
119 NumElements = InNum;
120 }
121
126 inline void Reset()
127 {
128 CheckSentinels();
129
130 NumElements = 0;
131 }
132
137 inline void Empty()
138 {
139 CheckSentinels();
140
141 NumElements = 0;
142 }
143
147 UE_DEPRECATED(5.5, "Renamed to AddUinitialized() to match TArray API")
149 {
150 return AddUninitialized();
151 }
152
157 {
158 check(NumElements < MaxElements);
159 CheckSentinels();
160
161 return NumElements++;
162 }
163
167 inline int32 Add(const ElementType& V)
168 {
169 check(NumElements < MaxElements);
170
171 Elements[NumElements] = V;
172
173 CheckSentinels();
174 return NumElements++;
175 }
176
181 {
182 check(NumElements < MaxElements);
183
184 Elements[NumElements] = MoveTemp(V);
185
186 CheckSentinels();
187 return NumElements++;
188 }
189
194 {
195 const int32 Index = Add(MoveTemp(V));
196
197 CheckSentinels();
198
199 return Index;
200 }
201
206 inline void RemoveAt(const int32 Index)
207 {
208 check(Index < NumElements);
209 CheckSentinels();
210
211 for (int32 MoveIndex = Index; MoveIndex < NumElements - 1; ++MoveIndex)
212 {
213 Elements[MoveIndex] = MoveTemp(Elements[MoveIndex + 1]);
214 }
215 --NumElements;
216
217 CheckSentinels();
218 }
219
224 inline void RemoveAtSwap(const int32 Index)
225 {
226 check(Index < NumElements);
227 CheckSentinels();
228
229 if (Index < NumElements - 1)
230 {
231 Elements[Index] = MoveTemp(Elements[NumElements - 1]);
232 }
233 --NumElements;
234
235 CheckSentinels();
236 }
237
239 {
240 CheckSentinels();
241
242 return &Elements[0];
243 }
244
245 inline const ElementType* GetData() const
246 {
247 CheckSentinels();
248
249 return &Elements[0];
250 }
251
253 {
254 CheckSentinels();
255
256 return &Elements[0];
257 }
258
259 inline const ElementType* begin() const
260 {
261 CheckSentinels();
262
263 return &Elements[0];
264 }
265
266 inline ElementType* end()
267 {
268 CheckSentinels();
269
270 return &Elements[NumElements];
271 }
272
273 inline const ElementType* end() const
274 {
275 CheckSentinels();
276
277 return &Elements[NumElements];
278 }
279
280 private:
281 explicit TCArray(int32 InNumElements)
282 : NumElements(InNumElements)
283 {
284 check(Num() <= Max());
285 }
286
287 static constexpr int32 SentinelValue = 0xA1B2C3D4;
288
289 void CheckSentinels() const
290 {
291#if CHAOS_CARRAY_SENTINEL
292 if (!ensureAlways(Sentinel0 == SentinelValue) || !ensureAlways(Sentinel1 == SentinelValue) || !ensureAlways(Sentinel2 == SentinelValue))
293 {
294 UE_LOG(LogChaos, Fatal, TEXT("TCArray Sentinel(s) Corrupted: 0x%08x, 0x%08x, 0x%08x [Expected 0x%08x]"), Sentinel0, Sentinel1, Sentinel2, SentinelValue);
295 }
296#endif
297 }
298
300
301 // NOTE: Element count before array for better cache behaviour
302 int32 NumElements;
303
305
306 ElementType Elements[MaxElements];
307
309 };
310
311
320 template<int32 NumInlineElements>
321 class TUncheckedFixedAllocator : public TFixedAllocator<NumInlineElements>
322 {
323 public:
324#if !CHAOS_CHECK_UNCHECKED_ARRAY
325 enum { RequireRangeCheck = false };
326#endif
327 };
328
330 {
331 public:
332#if !CHAOS_CHECK_UNCHECKED_ARRAY
333 enum { RequireRangeCheck = false };
334#endif
335 };
336
337 template<typename T, int32 N>
339
340 template<typename T, int32 N>
342
343 template<typename T>
345}
346
347template<int32 NumInlineElements>
348struct TAllocatorTraits<Chaos::TUncheckedFixedAllocator<NumInlineElements>> : TAllocatorTraits<TFixedAllocator<NumInlineElements>>
349{
350};
351
352template<>
353struct TAllocatorTraits<Chaos::FUncheckedHeapAllocator> : TAllocatorTraits<FHeapAllocator>
354{
355};
356
357template <typename T, int N>
358struct TIsContiguousContainer<Chaos::TCArray<T, N>>
359{
360 enum { Value = true };
361};
362
#define ensureAlways( InExpression)
Definition AssertionMacros.h:466
#define check(expr)
Definition AssertionMacros.h:314
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#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
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
#define IF_CHAOS_CARRAY_SENTINEL(X)
Definition UncheckedArray.h:31
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition UncheckedArray.h:330
@ RequireRangeCheck
Definition UncheckedArray.h:333
A c-style array of objects with non-shipping bounds checking.
Definition UncheckedArray.h:47
void Empty()
Set the number of elements to 0.
Definition UncheckedArray.h:137
int32 Max() const
Definition UncheckedArray.h:73
const ElementType * GetData() const
Definition UncheckedArray.h:245
int32 Num() const
Definition UncheckedArray.h:66
void Reset()
Set the number of elements to 0.
Definition UncheckedArray.h:126
void SetNum(const int32 InNum)
Set the number of elements in the array.
Definition UncheckedArray.h:114
T ElementType
Definition UncheckedArray.h:50
int32 Add(const ElementType &V)
Copy the element to the end of the array.
Definition UncheckedArray.h:167
TCArray()
Definition UncheckedArray.h:62
int32 AddUninitialized()
Increase the size of the array without re-initializing the new element.
Definition UncheckedArray.h:156
static TCArray< T, N > MakeEmpty()
Definition UncheckedArray.h:57
int32 Add()
Increase the size of the array without re-initializing the new element.
Definition UncheckedArray.h:148
ElementType * GetData()
Definition UncheckedArray.h:238
int32 Emplace(ElementType &&V)
Move the element to the end of the array.
Definition UncheckedArray.h:193
void RemoveAtSwap(const int32 Index)
Remove the element at the specified index Moves the last element into the gap.
Definition UncheckedArray.h:224
int32 Add(ElementType &&V)
Move the element to the end of the array.
Definition UncheckedArray.h:180
void RemoveAt(const int32 Index)
Remove the element at the specified index Moves all higher elements down to fill the gap.
Definition UncheckedArray.h:206
ElementType * begin()
Definition UncheckedArray.h:252
const ElementType & operator[](const int32 Index) const
Definition UncheckedArray.h:102
ElementType & operator[](const int32 Index)
Definition UncheckedArray.h:94
ElementType * end()
Definition UncheckedArray.h:266
const ElementType * end() const
Definition UncheckedArray.h:273
static TCArray< T, N > MakeFull()
Definition UncheckedArray.h:52
bool IsEmpty() const
Definition UncheckedArray.h:80
bool IsFull() const
Definition UncheckedArray.h:87
static const int32 MaxElements
Definition UncheckedArray.h:49
const ElementType * begin() const
Definition UncheckedArray.h:259
A fixed allocator without array bounds checking except in Debug builds.
Definition UncheckedArray.h:322
@ RequireRangeCheck
Definition UncheckedArray.h:325
Definition Array.h:670
Definition ContainerAllocationPolicies.h:1276
Definition ContainerAllocationPolicies.h:618
Definition SkeletalMeshComponent.h:307
U16 Index
Definition radfft.cpp:71
Definition ContainerAllocationPolicies.h:256
Definition IsContiguousContainer.h:16
static constexpr bool Value
Definition IsContiguousContainer.h:20