Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
BasicArray.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// HEADER_UNIT_SKIP - Not used?
6
7#include "Templates/MemoryOps.h"
8
9#if _MSC_VER
10 #pragma warning(push)
11 #pragma warning(disable : 4200)
12#endif
13
14/**
15 * Basic RAII array which can be used without the excessive dependencies needed by TArray,
16 * which needs to be serialisable, have a fixed ABI, have lots of helper algorithms as members,
17 * have auto-shrinking, support allocators etc.
18 */
19template <typename T>
21{
22public:
24 : Data(nullptr)
25 {
26 }
27
28 // Non-copyable for now, but this could be made copyable in future if needed.
29 TBasicArray(const TBasicArray&) = delete;
30 TBasicArray& operator=(const TBasicArray&) = delete;
31
33 : Data(Other.Data)
34 {
35 Other.Data = nullptr;
36 }
37
39 {
41
43 Temp.Data = this->Data;
44 this->Data = TempData;
45
46 return *this;
47 }
48
50 {
51 if (FData* LocalData = this->Data)
52 {
55 }
56 }
57
58 template <typename... ArgTypes>
59 int32 Emplace(ArgTypes&&... Args)
60 {
61 int32 Result = Num();
62
65
66 return Result;
67 }
68
69 template <typename... ArgTypes>
70 void EmplaceAt(int32 Index, ArgTypes&&... Args)
71 {
74 }
75
76 void RemoveAt(int32 Index, int32 NumToRemove = 1)
77 {
78 if (NumToRemove > 0)
79 {
80 FData* LocalData = this->Data;
82
87 }
88 }
89
90 bool IsEmpty() const
91 {
92 return Num() == 0;
93 }
94
95 int32 Num() const
96 {
98 return LocalData ? LocalData->Num : 0;
99 }
100
102 {
104 return LocalData ? &LocalData->Data[0] : nullptr;
105 }
106
107 FORCEINLINE const T* GetData() const
108 {
109 return const_cast<TBasicArray*>(this)->GetData();
110 }
111
112 FORCEINLINE T& operator[](int32 Index)
113 {
114 return GetData()[Index];
115 }
116
117 FORCEINLINE const T& operator[](int32 Index) const
118 {
119 return GetData()[Index];
120 }
121
122private:
123 static constexpr int32 InitialReservationSize = 16;
124
125 static FORCEINLINE int32 ApplyGrowthFactor(int32 CurrentNum)
126 {
127 return CurrentNum + CurrentNum / 2;
128 }
129
130 void* InsertUninitialized(int32 IndexToAdd)
131 {
132 FData* LocalData = this->Data;
133 T* LocationToAdd = nullptr;
134 if (!LocalData)
135 {
136 // IndexToAdd *must* be 0 here - can't assert that though
137
138 LocalData = (FData*)FMemory::Malloc(sizeof(FData) + InitialReservationSize * sizeof(T));
139 LocalData->Num = 1;
141 Data = LocalData;
143 }
144 else
145 {
148
149 if (LocalNum == LocalMax)
150 {
152 LocalData = (FData*)FMemory::Realloc(LocalData, sizeof(FData) + LocalMax * sizeof(T));
154 this->Data = LocalData;
155 }
158 ++LocalData->Num;
159 }
160
161 return LocationToAdd;
162 }
163
164 struct FData
165 {
166 int32 Num;
167 int32 Max;
168 T Data[0];
169 };
170
172
173 friend T* begin( TBasicArray& Arr) { return Arr.GetData(); }
174 friend const T* begin(const TBasicArray& Arr) { return Arr.GetData(); }
175 friend T* end ( TBasicArray& Arr) { return Arr.GetData() + Arr.Num(); }
176 friend const T* end (const TBasicArray& Arr) { return Arr.GetData() + Arr.Num(); }
177};
178
179#if _MSC_VER
180 #pragma warning(pop)
181#endif
#define FORCEINLINE
Definition Platform.h:644
friend const T * end(const TBasicArray &Arr)
Definition BasicArray.h:176
FORCEINLINE const T * GetData() const
Definition BasicArray.h:107
FORCEINLINE const T & operator[](int32 Index) const
Definition BasicArray.h:117
friend T * begin(TBasicArray &Arr)
Definition BasicArray.h:173
static FORCEINLINE int32 ApplyGrowthFactor(int32 CurrentNum)
Definition BasicArray.h:125
TBasicArray(const TBasicArray &)=delete
friend const T * begin(const TBasicArray &Arr)
Definition BasicArray.h:174
void RemoveAt(int32 Index, int32 NumToRemove=1)
Definition BasicArray.h:76
FData * Data
Definition BasicArray.h:171
void * InsertUninitialized(int32 IndexToAdd)
Definition BasicArray.h:130
T * GetData()
Definition BasicArray.h:101
friend T * end(TBasicArray &Arr)
Definition BasicArray.h:175
int32 Num() const
Definition BasicArray.h:95
bool IsEmpty() const
Definition BasicArray.h:90
void EmplaceAt(int32 Index, ArgTypes &&... Args)
Definition BasicArray.h:70
FORCEINLINE T & operator[](int32 Index)
Definition BasicArray.h:112
TBasicArray(TBasicArray &&Other)
Definition BasicArray.h:32
static constexpr int32 InitialReservationSize
Definition BasicArray.h:123
TBasicArray & operator=(const TBasicArray &)=delete
TBasicArray & operator=(TBasicArray &&Other)
Definition BasicArray.h:38
int32 Emplace(ArgTypes &&... Args)
Definition BasicArray.h:59