game_object_iterator.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_ITERATOR_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_ITERATOR_HPP
  18. #include <vector>
  19. #include "game_object_manager.hpp"
  20. template<typename T>
  21. class GameObjectIterator
  22. {
  23. public:
  24. typedef std::vector<std::unique_ptr<GameObject> >::const_iterator Iterator;
  25. public:
  26. GameObjectIterator(Iterator it, Iterator end) :
  27. m_it(it),
  28. m_end(end),
  29. m_object()
  30. {
  31. if (m_it != m_end)
  32. {
  33. m_object = dynamic_cast<T*>(m_it->get());
  34. if (!m_object)
  35. {
  36. skip_to_next();
  37. }
  38. }
  39. }
  40. GameObjectIterator& operator++()
  41. {
  42. skip_to_next();
  43. return *this;
  44. }
  45. GameObjectIterator operator++(int)
  46. {
  47. GameObjectIterator tmp(*this);
  48. skip_to_next();
  49. return tmp;
  50. }
  51. T* operator->() {
  52. return m_object;
  53. }
  54. const T* operator->() const {
  55. return m_object;
  56. }
  57. T& operator*() const {
  58. return *m_object;
  59. }
  60. T& operator*() {
  61. return *m_object;
  62. }
  63. bool operator==(const GameObjectIterator& other) const
  64. {
  65. return m_it == other.m_it;
  66. }
  67. bool operator!=(const GameObjectIterator& other) const
  68. {
  69. return !(*this == other);
  70. }
  71. private:
  72. void skip_to_next()
  73. {
  74. do
  75. {
  76. ++m_it;
  77. if (m_it == m_end)
  78. {
  79. break;
  80. }
  81. else
  82. {
  83. m_object = dynamic_cast<T*>(m_it->get());
  84. }
  85. }
  86. while (!m_object);
  87. }
  88. private:
  89. Iterator m_it;
  90. Iterator m_end;
  91. T* m_object;
  92. };
  93. template<typename T>
  94. class GameObjectRange
  95. {
  96. public:
  97. GameObjectRange(const GameObjectManager& manager) :
  98. m_manager(manager)
  99. {}
  100. GameObjectIterator<T> begin() const {
  101. return GameObjectIterator<T>(m_manager.get_objects().begin(), m_manager.get_objects().end());
  102. }
  103. GameObjectIterator<T> end() const {
  104. return GameObjectIterator<T>(m_manager.get_objects().end(), m_manager.get_objects().end());
  105. }
  106. private:
  107. const GameObjectManager& m_manager;
  108. };
  109. #endif
  110. /* EOF */