diff --git a/BeefySysLib/util/Array.h b/BeefySysLib/util/Array.h index 9a81ed8a..e485023c 100644 --- a/BeefySysLib/util/Array.h +++ b/BeefySysLib/util/Array.h @@ -744,6 +744,14 @@ public: SetBufferSize(this->mAllocSize + this->mAllocSize / 2 + 1); new (&this->mVals[this->mSize++]) T(val); } + + T& GetSafeRef(int idx) + { + BF_ASSERT(idx >= 0); + while (idx >= this->mSize) + this->Add(T()); + return this->mVals[idx]; + } }; // POD @@ -1055,6 +1063,14 @@ public: this->mVals[this->mSize++] = val; } + T& GetSafeRef(int idx) + { + BF_ASSERT(idx >= 0); + while (idx >= this->mSize) + this->Add(T()); + return this->mVals[idx]; + } + T* GrowUninitialized(int addSize) { if (this->mSize + addSize > this->mAllocSize) @@ -1107,6 +1123,46 @@ public: } }; +template +class OwnedArray : public Array +{ +public: + typedef Array _Base; + + ~OwnedArray() + { + for (auto item : *this) + delete item; + } + + void Clear() + { + for (auto item : *this) + delete item; + _Base::Clear(); + } + + void ClearWithoutDeleting() + { + _Base::Clear(); + } + + T* Alloc() + { + T* item = new T(); + _Base::push_back(item); + return item; + } + + template + T2* Alloc() + { + T2* item = new T2(); + _Base::push_back(item); + return item; + } +}; + NS_BF_END; namespace std