From 451f7e76d7847ebeb4dbc7d12f2573b634970aad Mon Sep 17 00:00:00 2001 From: EmosewaMC <39972741+EmosewaMC@users.noreply.github.com> Date: Wed, 14 Jun 2023 23:16:31 -0700 Subject: [PATCH] switch to unique_ptrs for callback timers --- dGame/Entity.cpp | 42 +++++++++------------------ dGame/Entity.h | 6 ++-- dGame/dEntity/EntityCallbackTimer.cpp | 2 +- dGame/dEntity/EntityCallbackTimer.h | 2 +- dGame/dEntity/EntityTimer.cpp | 2 +- dGame/dEntity/EntityTimer.h | 2 +- 6 files changed, 21 insertions(+), 35 deletions(-) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 819431d6..7112868e 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -787,23 +787,21 @@ CppScripts::Script* Entity::GetScript() const { } void Entity::Update(const float deltaTime) { - auto namedTimerItr = std::remove_if(m_Timers.begin(), m_Timers.end(), [this, &deltaTime](EntityTimer* timer) { + auto namedTimerItr = std::remove_if(m_Timers.begin(), m_Timers.end(), [this, &deltaTime](const std::unique_ptr& timer) { timer->Update(deltaTime); if (timer->GetTime() <= 0) { GetScript()->OnTimerDone(this, timer->GetName()); TriggerEvent(eTriggerEventType::TIMER_DONE, this); - delete timer; return true; } return false; }); m_Timers.erase(namedTimerItr, m_Timers.end()); - auto callbackTimerItr = std::remove_if(m_CallbackTimers.begin(), m_CallbackTimers.end(), [this, &deltaTime](EntityCallbackTimer* timer) { + auto callbackTimerItr = std::remove_if(m_CallbackTimers.begin(), m_CallbackTimers.end(), [this, &deltaTime](const std::unique_ptr& timer) { timer->Update(deltaTime); if (timer->GetTime() <= 0) { timer->ExecuteCallback(); - delete timer; return true; } return false; @@ -811,8 +809,12 @@ void Entity::Update(const float deltaTime) { m_CallbackTimers.erase(callbackTimerItr, m_CallbackTimers.end()); // Add pending timers to the list of timers so they start next tick. - if (m_PendingTimers.size() > 0) { - m_Timers.insert(m_Timers.end(), m_PendingTimers.begin(), m_PendingTimers.end()); + if (!m_PendingTimers.empty()) { + // unique_ptrs are not CopyConstructible. Must move an rvalue. + this->m_Timers.reserve(m_Timers.size() + m_PendingTimers.size()); + for (auto& timer : m_PendingTimers) { + this->m_Timers.push_back(std::move(timer)); + } m_PendingTimers.clear(); } @@ -1163,24 +1165,21 @@ void Entity::RemoveParent() { } void Entity::AddTimer(const std::string& name, float time) { - m_PendingTimers.push_back(new EntityTimer(name, time)); + m_PendingTimers.emplace_back(std::make_unique(name, time)); } void Entity::AddCallbackTimer(const float time, const std::function& callback) { - m_CallbackTimers.push_back(new EntityCallbackTimer(time, callback)); + m_CallbackTimers.emplace_back(std::make_unique(time, callback)); } bool Entity::HasTimer(const std::string& name) { - auto possibleTimer = std::find_if(m_Timers.begin(), m_Timers.end(), [name](EntityTimer* timer) { + auto possibleTimer = std::find_if(m_Timers.begin(), m_Timers.end(), [name](const std::unique_ptr& timer) { return timer->GetName() == name; }); return possibleTimer != m_Timers.end(); } void Entity::CancelCallbackTimers() { - std::for_each(m_CallbackTimers.begin(), m_CallbackTimers.end(), [](EntityCallbackTimer* timer) { - delete timer; - }); m_CallbackTimers.clear(); } @@ -1191,27 +1190,14 @@ void Entity::ScheduleKillAfterUpdate(Entity* murderer) { } void Entity::CancelTimer(const std::string& name) { - auto toErase = std::remove_if(m_Timers.begin(), m_Timers.end(), [&name](EntityTimer* timer) { - if (timer->GetName() == name) { - delete timer; - return true; - } - return false; - }); + auto toErase = std::remove_if(m_Timers.begin(), m_Timers.end(), [&name](const std::unique_ptr& timer) { + return timer->GetName() == name; + }); m_Timers.erase(m_Timers.begin(), toErase); } // ### LEFT OFF HERE ### void Entity::CancelAllTimers() { - for (auto* timer : m_Timers) { - delete timer; - } - m_Timers.clear(); - - for (auto* callBackTimer : m_CallbackTimers) { - delete callBackTimer; - } - m_CallbackTimers.clear(); } diff --git a/dGame/Entity.h b/dGame/Entity.h index 00cf79e4..a1a8d449 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -338,9 +338,9 @@ protected: std::vector> m_PhantomCollisionCallbacks; std::unordered_map m_Components; - std::vector m_Timers; - std::vector m_PendingTimers; - std::vector m_CallbackTimers; + std::vector> m_Timers; + std::vector> m_PendingTimers; + std::vector> m_CallbackTimers; bool m_ShouldDestroyAfterUpdate; diff --git a/dGame/dEntity/EntityCallbackTimer.cpp b/dGame/dEntity/EntityCallbackTimer.cpp index ff9d8c7a..16a3ff3f 100644 --- a/dGame/dEntity/EntityCallbackTimer.cpp +++ b/dGame/dEntity/EntityCallbackTimer.cpp @@ -1,6 +1,6 @@ #include "EntityCallbackTimer.h" -EntityCallbackTimer::EntityCallbackTimer(float time, std::function callback) { +EntityCallbackTimer::EntityCallbackTimer(const float& time, const std::function& callback) { m_Time = time; m_Callback = callback; } diff --git a/dGame/dEntity/EntityCallbackTimer.h b/dGame/dEntity/EntityCallbackTimer.h index 168c16fd..2b7429f4 100644 --- a/dGame/dEntity/EntityCallbackTimer.h +++ b/dGame/dEntity/EntityCallbackTimer.h @@ -5,7 +5,7 @@ class EntityCallbackTimer { public: - EntityCallbackTimer(float time, std::function callback); + EntityCallbackTimer(const float& time, const std::function& callback); ~EntityCallbackTimer(); void ExecuteCallback(); diff --git a/dGame/dEntity/EntityTimer.cpp b/dGame/dEntity/EntityTimer.cpp index 0363fc5b..d273f109 100644 --- a/dGame/dEntity/EntityTimer.cpp +++ b/dGame/dEntity/EntityTimer.cpp @@ -1,6 +1,6 @@ #include "EntityTimer.h" -EntityTimer::EntityTimer(std::string name, float time) { +EntityTimer::EntityTimer(const std::string& name, const float& time) { m_Name = name; m_Time = time; } diff --git a/dGame/dEntity/EntityTimer.h b/dGame/dEntity/EntityTimer.h index 9de0345d..a5071a82 100644 --- a/dGame/dEntity/EntityTimer.h +++ b/dGame/dEntity/EntityTimer.h @@ -4,7 +4,7 @@ class EntityTimer { public: - EntityTimer(std::string name, float time); + EntityTimer(const std::string& name, const float& time); ~EntityTimer(); std::string GetName();