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 +++-- src/event.cpp | 3 +-- 2 files changed, 4 insertions(+), 4 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 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) { diff --git a/src/event.cpp b/src/event.cpp index b6e18b3..0b1d48d 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -26,8 +26,7 @@ void Event_RegisterCallback(const char *name, EventHandler handler) { EventBucket *bkt = nullptr; EventBucketFind(name, safeName, bkt); if (bkt == nullptr) { - eventBuckets.Push(); - bkt = eventBuckets.GetPtr() + (sizeof(EventBucket) * (eventBuckets.Count() - 1)); + bkt = &eventBuckets.Push(); memcpy(bkt->name, safeName, 16); } bkt->callbacks.Push(handler); -- cgit v1.2.3