Timer: Add utility to ensure safe iteration of timers

This commit is contained in:
Jonathan G Rennison 2024-08-21 21:33:43 +01:00
parent 5eae56fd23
commit a56efeb609
2 changed files with 14 additions and 1 deletions

View File

@ -51,7 +51,7 @@ void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
template<> template<>
void TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta) void TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
{ {
for (auto timer : TimerManager<TimerGameTick>::GetTimers()) { for (auto timer : TimerManager<TimerGameTick>::GetTimerVector()) {
timer->Elapsed(delta); timer->Elapsed(delta);
} }
} }

View File

@ -117,6 +117,19 @@ private:
static btree::btree_set<BaseTimer<TTimerType> *, base_timer_sorter> timers; static btree::btree_set<BaseTimer<TTimerType> *, base_timer_sorter> timers;
return timers; return timers;
} }
/** List of active timers, as a std::vector, to allow for timers to be added/removed during iteration. */
static std::vector<BaseTimer<TTimerType> *> GetTimerVector()
{
std::vector<BaseTimer<TTimerType> *> result;
const auto &timers = TimerManager::GetTimers();
result.reserve(timers.size());
for (BaseTimer<TTimerType> * timer : timers) {
result.push_back(timer);
}
return result;
}
}; };
#endif /* TIMER_MANAGER_H */ #endif /* TIMER_MANAGER_H */