From 579e3e7ccc1b2cf6c090ac4d81959e169572cb19 Mon Sep 17 00:00:00 2001 From: M Stoeckl Date: Sat, 29 Jun 2024 19:12:55 -0400 Subject: [PATCH] Simplify GameObjectIterator Now it is only used on vectors containing only objects with T as a base class. --- src/supertux/game_object_iterator.hpp | 34 ++++++++------------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/supertux/game_object_iterator.hpp b/src/supertux/game_object_iterator.hpp index 5931925da5a..1bef5e52390 100644 --- a/src/supertux/game_object_iterator.hpp +++ b/src/supertux/game_object_iterator.hpp @@ -35,24 +35,28 @@ class GameObjectIterator { if (m_it != m_end) { + // A dynamic_cast is needed to perform sidecasts (a.k.a. crosscasts) + // T may be one of multiple base classes of the object and need not inherit GameObject m_object = dynamic_cast(*m_it); - if (!m_object) - { - skip_to_next(); - } + assert(m_object); } } GameObjectIterator& operator++() { - skip_to_next(); + ++m_it; + if (m_it != m_end) + { + m_object = dynamic_cast(*m_it); + assert(m_object); + } return *this; } GameObjectIterator operator++(int) { GameObjectIterator tmp(*this); - skip_to_next(); + operator++(); return tmp; } @@ -82,24 +86,6 @@ class GameObjectIterator return !(*this == other); } -private: - void skip_to_next() - { - do - { - ++m_it; - if (m_it == m_end) - { - break; - } - else - { - m_object = dynamic_cast(*m_it); - } - } - while (!m_object); - } - private: Iterator m_it; Iterator m_end;