diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-11 13:02:11 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-11 13:02:11 -0400 |
| commit | 40ab6886e72c660d424fec9140feb8685db7d363 (patch) | |
| tree | d8254bf0733489a588e311b34f540320b7f5b23c /src/dynamic-array.hpp | |
| parent | 5bee70037463b3e921efd42ec9bbf4f31a5cf299 (diff) | |
DynArray empty push returns reference to new object
Diffstat (limited to 'src/dynamic-array.hpp')
| -rw-r--r-- | src/dynamic-array.hpp | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index 645f9c8..95ba967 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -31,7 +31,7 @@ struct DynArray: DynArrayBase { T *GetPtr(); const int64_t Count(); bool Has(const T &val); - void Push(); + T& Push(); void Push(const T &val); T Pop(); void Remove(std::size_t index); @@ -127,7 +127,7 @@ template <typename T> inline bool DynArray<T>::Has(const T &val) { return false; } -template <typename T> inline void DynArray<T>::Push() { +template <typename T> inline T &DynArray<T>::Push() { if (this->elementCount + 1 > this->reservedCount) { auto safeReserveCount = this->reservedCount < 2 ? 2 : this->reservedCount; DynArrayReserve(this, int64_t(safeReserveCount * 1.5)); @@ -135,6 +135,7 @@ template <typename T> inline void DynArray<T>::Push() { auto itemPtr = this->ptr + (sizeof(T) * this->elementCount); const auto &targetItem = new(itemPtr) T{}; this->elementCount += 1; + return *reinterpret_cast<T *>(itemPtr); } template <typename T> inline void DynArray<T>::Push(const T &val) { |
