From 40ab6886e72c660d424fec9140feb8685db7d363 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Wed, 11 Oct 2023 13:02:11 -0400 Subject: DynArray empty push returns reference to new object --- src/dynamic-array.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/dynamic-array.hpp') 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 inline bool DynArray::Has(const T &val) { return false; } -template inline void DynArray::Push() { +template inline T &DynArray::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 inline void DynArray::Push() { auto itemPtr = this->ptr + (sizeof(T) * this->elementCount); const auto &targetItem = new(itemPtr) T{}; this->elementCount += 1; + return *reinterpret_cast(itemPtr); } template inline void DynArray::Push(const T &val) { -- cgit v1.2.3