Skip to content

Commit

Permalink
Simplify GameObjectIterator<T>
Browse files Browse the repository at this point in the history
Now it is only used on vectors containing only objects with T
as a base class.
  • Loading branch information
mstoeckl committed Jun 30, 2024
1 parent fc686fa commit 579e3e7
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/supertux/game_object_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T*>(*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<T*>(*m_it);
assert(m_object);
}
return *this;
}

GameObjectIterator operator++(int)
{
GameObjectIterator tmp(*this);
skip_to_next();
operator++();
return tmp;
}

Expand Down Expand Up @@ -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<T*>(*m_it);
}
}
while (!m_object);
}

private:
Iterator m_it;
Iterator m_end;
Expand Down

0 comments on commit 579e3e7

Please sign in to comment.