CommonLibSSE (powerof3)
GArrayDataBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace RE
4 {
5  template <class T, class Allocator, class SizePolicy>
7  {
8  public:
9  using ValueType = T;
10  using AllocatorType = Allocator;
11  using SizePolicyType = SizePolicy;
13 
15  data(nullptr),
16  size(0),
17  policy()
18  {}
19 
20  GArrayDataBase(const SizePolicy& a_policy) :
21  data(nullptr),
22  size(0),
23  policy(a_policy)
24  {}
25 
27  {
28  Allocator::DestructArray(data, size);
29  Allocator::Free(data);
30  }
31 
33  {
34  return policy.GetCapacity();
35  }
36 
38  {
39  Allocator::DestructArray(data, size);
40  Allocator::Free(data);
41  data = nullptr;
42  size = 0;
43  policy.SetCapacity(0);
44  }
45 
46  void Reserve(const void* a_heapAddr, UPInt a_newCapacity)
47  {
48  if (policy.NeverShrinking() && a_newCapacity < GetCapacity())
49  return;
50 
51  if (a_newCapacity < policy.GetMinCapacity())
52  a_newCapacity = policy.GetMinCapacity();
53 
54  if (a_newCapacity == 0) {
55  if (data) {
56  Allocator::Free(data);
57  data = nullptr;
58  }
59  policy.SetCapacity(0);
60  } else {
61  UPInt gran = policy.GetGranularity();
62  UPInt newCapacity = (a_newCapacity + gran - 1) / gran * gran;
63  if (data) {
64  if (Allocator::IsMovable()) {
65  data = (T*)Allocator::Realloc(data, sizeof(T) * newCapacity);
66  } else {
67  T* newData = (T*)Allocator::Alloc(a_heapAddr, sizeof(T) * newCapacity);
68  for (UPInt i = 0; i < size; ++i) {
69  Allocator::Construct(&newData[i], data[i]);
70  Allocator::Destruct(&data[i]);
71  }
72  Allocator::Free(data);
73  data = newData;
74  }
75  } else {
76  data = (T*)Allocator::Alloc(a_heapAddr, sizeof(T) * newCapacity);
77  }
78  policy.SetCapacity(newCapacity);
79  }
80  }
81 
82  void ResizeNoConstruct(const void* a_heapAddr, UPInt a_newSize)
83  {
84  UPInt oldSize = size;
85 
86  if (a_newSize < oldSize) {
87  Allocator::DestructArray(data + a_newSize, oldSize - a_newSize);
88  if (a_newSize < (policy.GetCapacity() >> 1)) {
89  Reserve(a_heapAddr, a_newSize);
90  }
91  } else if (a_newSize >= policy.GetCapacity()) {
92  Reserve(a_heapAddr, a_newSize + (a_newSize >> 2));
93  }
94  size = a_newSize;
95  }
96 
97  // members
98  T* data; // 00
99  UPInt size; // 08
100  SizePolicy policy; // 10
101  };
102 }
Definition: AbsorbEffect.h:6
std::size_t UPInt
Definition: SFTypes.h:5
Definition: GArrayDataBase.h:7
T ValueType
Definition: GArrayDataBase.h:9
UPInt GetCapacity() const
Definition: GArrayDataBase.h:32
SizePolicy SizePolicyType
Definition: GArrayDataBase.h:11
GArrayDataBase()
Definition: GArrayDataBase.h:14
void ClearAndRelease()
Definition: GArrayDataBase.h:37
void ResizeNoConstruct(const void *a_heapAddr, UPInt a_newSize)
Definition: GArrayDataBase.h:82
void Reserve(const void *a_heapAddr, UPInt a_newCapacity)
Definition: GArrayDataBase.h:46
T * data
Definition: GArrayDataBase.h:98
GArrayDataBase(const SizePolicy &a_policy)
Definition: GArrayDataBase.h:20
Allocator AllocatorType
Definition: GArrayDataBase.h:10
~GArrayDataBase()
Definition: GArrayDataBase.h:26
UPInt size
Definition: GArrayDataBase.h:99
SizePolicy policy
Definition: GArrayDataBase.h:100