supertux
game_object_iterator.hpp
1 // SuperTux
2 // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_ITERATOR_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_ITERATOR_HPP
19 
20 #include <vector>
21 
22 #include "game_object_manager.hpp"
23 
24 template<typename T>
26 {
27 public:
28  typedef std::vector<GameObject* >::const_iterator Iterator;
29 
30 public:
31  GameObjectIterator(Iterator it, Iterator end) :
32  m_it(it),
33  m_end(end),
34  m_object()
35  {
36  if (m_it != m_end)
37  {
38  // A dynamic_cast is needed to perform sidecasts (a.k.a. crosscasts)
39  // T may be one of multiple base classes of the object and need not inherit GameObject
40  m_object = dynamic_cast<T*>(*m_it);
41  assert(m_object);
42  }
43  }
44 
45  GameObjectIterator& operator++()
46  {
47  ++m_it;
48  if (m_it != m_end)
49  {
50  m_object = dynamic_cast<T*>(*m_it);
51  assert(m_object);
52  }
53  return *this;
54  }
55 
56  GameObjectIterator operator++(int)
57  {
58  GameObjectIterator tmp(*this);
59  operator++();
60  return tmp;
61  }
62 
63  T* operator->() {
64  return m_object;
65  }
66 
67  const T* operator->() const {
68  return m_object;
69  }
70 
71  T& operator*() const {
72  return *m_object;
73  }
74 
75  T& operator*() {
76  return *m_object;
77  }
78 
79  bool operator==(const GameObjectIterator& other) const
80  {
81  return m_it == other.m_it;
82  }
83 
84  bool operator!=(const GameObjectIterator& other) const
85  {
86  return !(*this == other);
87  }
88 
89 private:
90  Iterator m_it;
91  Iterator m_end;
92  T* m_object;
93 };
94 
95 template<typename T>
97 {
98 public:
99  GameObjectRange(const GameObjectManager& manager) :
100  m_manager(manager)
101  {}
102 
103  GameObjectIterator<T> begin() const {
104  auto& objects = m_manager.get_objects_by_type_index(typeid(T));
105  return GameObjectIterator<T>(objects.begin(), objects.end());
106  }
107 
108  GameObjectIterator<T> end() const {
109  auto& objects = m_manager.get_objects_by_type_index(typeid(T));
110  return GameObjectIterator<T>(objects.end(), objects.end());
111  }
112 
113 private:
114  const GameObjectManager& m_manager;
115 };
116 
117 #endif
118 
119 /* EOF */
Definition: game_object_iterator.hpp:96
This class provides basic controlling functions for a sector.
Definition: game_object_manager.hpp:45
Definition: game_object_iterator.hpp:25