2020-09-24 23:44:35 -05:00
|
|
|
#ifndef STORM_ARRAY_TS_FIXED_ARRAY_HPP
|
|
|
|
|
#define STORM_ARRAY_TS_FIXED_ARRAY_HPP
|
|
|
|
|
|
2020-11-01 17:45:45 -06:00
|
|
|
#include "storm/Memory.hpp"
|
2020-11-14 17:12:00 -06:00
|
|
|
#include "storm/array/TSBaseArray.hpp"
|
2020-09-24 23:44:35 -05:00
|
|
|
#include <cstdint>
|
|
|
|
|
|
2020-11-14 17:12:00 -06:00
|
|
|
template <class T>
|
2020-09-24 23:44:35 -05:00
|
|
|
class TSFixedArray : public TSBaseArray<T> {
|
|
|
|
|
public:
|
2023-02-21 22:10:14 -06:00
|
|
|
TSFixedArray();
|
2021-01-03 10:36:37 -06:00
|
|
|
~TSFixedArray();
|
2023-02-04 23:48:20 -06:00
|
|
|
TSFixedArray<T>& operator=(const TSFixedArray<T>& source);
|
2023-02-21 22:10:14 -06:00
|
|
|
void Clear();
|
2023-02-04 23:48:20 -06:00
|
|
|
void ReallocAndClearData(uint32_t count);
|
2020-09-24 23:44:35 -05:00
|
|
|
void ReallocData(uint32_t count);
|
2023-02-04 23:48:20 -06:00
|
|
|
void Set(uint32_t count, const T* data);
|
2020-09-24 23:44:35 -05:00
|
|
|
void SetCount(uint32_t count);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-21 22:10:14 -06:00
|
|
|
template <class T>
|
|
|
|
|
TSFixedArray<T>::TSFixedArray() {
|
|
|
|
|
this->Constructor();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 10:36:37 -06:00
|
|
|
template <class T>
|
|
|
|
|
TSFixedArray<T>::~TSFixedArray() {
|
2023-01-03 16:58:17 -06:00
|
|
|
for (uint32_t i = 0; i < this->Count(); i++) {
|
2021-01-03 10:36:37 -06:00
|
|
|
auto element = &this->operator[](i);
|
|
|
|
|
element->~T();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->Ptr()) {
|
|
|
|
|
SMemFree(this->Ptr(), this->MemFileName(), this->MemLineNo(), 0x0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 23:48:20 -06:00
|
|
|
template <class T>
|
|
|
|
|
TSFixedArray<T>& TSFixedArray<T>::operator=(const TSFixedArray<T>& source) {
|
|
|
|
|
if (this != &source) {
|
|
|
|
|
this->Set(source.Count(), source.Ptr());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 22:10:14 -06:00
|
|
|
template <class T>
|
|
|
|
|
void TSFixedArray<T>::Clear() {
|
|
|
|
|
this->~TSFixedArray<T>();
|
|
|
|
|
this->Constructor();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 23:48:20 -06:00
|
|
|
template <class T>
|
|
|
|
|
void TSFixedArray<T>::ReallocAndClearData(uint32_t count) {
|
|
|
|
|
this->m_alloc = count;
|
|
|
|
|
|
|
|
|
|
if (this->m_data || count) {
|
|
|
|
|
void* m = SMemReAlloc(this->m_data, sizeof(T) * count, this->MemFileName(), this->MemLineNo(), 0x0);
|
|
|
|
|
this->m_data = static_cast<T*>(m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 17:12:00 -06:00
|
|
|
template <class T>
|
2020-09-24 23:44:35 -05:00
|
|
|
void TSFixedArray<T>::ReallocData(uint32_t count) {
|
|
|
|
|
T* oldData = this->m_data;
|
|
|
|
|
|
|
|
|
|
if (count < this->m_count) {
|
2023-01-03 16:58:17 -06:00
|
|
|
for (uint32_t i = count; i < this->m_count; i++) {
|
2023-02-05 21:36:49 -06:00
|
|
|
(&this->m_data[i])->~T();
|
2020-09-24 23:44:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->m_alloc = count;
|
|
|
|
|
|
|
|
|
|
void* v6 = SMemReAlloc(oldData, sizeof(T) * count, nullptr, 0, 0x10);
|
|
|
|
|
this->m_data = (T*)v6;
|
|
|
|
|
|
|
|
|
|
if (!v6) {
|
|
|
|
|
this->m_data = (T*)SMemAlloc(sizeof(T) * count, nullptr, 0, 0x0);
|
|
|
|
|
|
|
|
|
|
if (oldData) {
|
|
|
|
|
uint32_t smallestCount = count >= this->m_count ? this->m_count : count;
|
|
|
|
|
|
2023-01-03 16:58:17 -06:00
|
|
|
for (uint32_t i = 0; i < smallestCount; i++) {
|
2023-02-05 21:36:49 -06:00
|
|
|
new (&this->m_data[i]) T(oldData[i]);
|
|
|
|
|
(&oldData[i])->~T();
|
2020-09-24 23:44:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SMemFree(oldData, nullptr, 0, 0x0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 23:48:20 -06:00
|
|
|
template <class T>
|
|
|
|
|
void TSFixedArray<T>::Set(uint32_t count, const T* data) {
|
|
|
|
|
this->ReallocAndClearData(count);
|
|
|
|
|
|
2023-02-05 14:54:26 -06:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2023-02-04 23:48:20 -06:00
|
|
|
new (&this->m_data[i]) T(data[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->m_count = count;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 17:12:00 -06:00
|
|
|
template <class T>
|
2020-09-24 23:44:35 -05:00
|
|
|
void TSFixedArray<T>::SetCount(uint32_t count) {
|
|
|
|
|
if (count != this->m_count) {
|
|
|
|
|
if (count) {
|
|
|
|
|
this->ReallocData(count);
|
|
|
|
|
|
2023-01-03 16:58:17 -06:00
|
|
|
for (uint32_t i = this->m_count; i < count; i++) {
|
2020-09-24 23:44:35 -05:00
|
|
|
new (&this->m_data[i]) T();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->m_count = count;
|
|
|
|
|
} else {
|
|
|
|
|
this->Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|