ObjectIter.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // ObjectIter.h
  24. // Class definitions for various object iterators
  25. // Author: Steven Johnson, September 2001
  26. #pragma once
  27. #ifndef _OBJECT_ITER_H_
  28. #define _OBJECT_ITER_H_
  29. #include "Common/GameType.h"
  30. #include "Common/GameMemory.h"
  31. // forward declaration
  32. class Object;
  33. //-------------------------------------------------------------------------------------------
  34. /** */
  35. enum IterOrderType
  36. {
  37. ITER_FASTEST, ///< iterate in arbitrary order
  38. ITER_SORTED_NEAR_TO_FAR, ///< iterate in nearest-to-farthest order (may be slower)
  39. ITER_SORTED_FAR_TO_NEAR, ///< iterate in farthest-to-nearest order (may be slower)
  40. ITER_SORTED_CHEAP_TO_EXPENSIVE, ///< iterate in cheapest-to-most-expensive order (slower)
  41. ITER_SORTED_EXPENSIVE_TO_CHEAP ///< iterate in most-expensive-to-cheapest order (slower)
  42. };
  43. //-------------------------------------------------------------------------------------------
  44. /**
  45. ObjectIterator is an ABC that's used to iterate
  46. over an arbitrary list of Objects. (duh!)
  47. As of this writing, only PartitionManager actually
  48. provides an instance of this, but it seems fairly likely
  49. that other folks might want to in the future, so
  50. here it is.
  51. typical usage:
  52. iter = ThePartitionManager->iterateInRange(...);
  53. for (Object *otherObject = iter->first(); otherObject; otherObject = iter->next())
  54. {
  55. // do something with other
  56. }
  57. iter->deleteInstance(); // you own it, so you must delete it
  58. note that the iterator is required to deal intelligently with deleted objects;
  59. in particular, next() will check if an obj has been killed and simply skip it.
  60. */
  61. class ObjectIterator : public MemoryPoolObject
  62. {
  63. MEMORY_POOL_GLUE_ABC(ObjectIterator)
  64. public:
  65. virtual Object *first() = 0; ///< reset iterator and return first item (or null if iter is empty)
  66. virtual Object *next() = 0; ///< advance and return next item (or null if no more to iterate)
  67. };
  68. inline ObjectIterator::~ObjectIterator() { }
  69. //-------------------------------------------------------------------------------------------
  70. /**
  71. A basic implementation of ObjectIterator, with (hidden) extensions
  72. to allow for sorting by a numeric field.
  73. */
  74. class SimpleObjectIterator : public ObjectIterator
  75. {
  76. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(SimpleObjectIterator, "SimpleObjectIteratorPool" )
  77. private:
  78. class Clump : public MemoryPoolObject
  79. {
  80. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(Clump, "SimpleObjectIteratorClumpPool" )
  81. public:
  82. Clump *m_nextClump;
  83. Object *m_obj;
  84. Real m_numeric; // typically, dist-squared
  85. Clump();
  86. //~Clump();
  87. };
  88. typedef Real (*ClumpCompareProc)(Clump *a, Clump *b);
  89. static ClumpCompareProc theClumpCompareProcs[];
  90. static Real sortNearToFar(Clump *a, Clump *b);
  91. static Real sortFarToNear(Clump *a, Clump *b);
  92. static Real sortCheapToExpensive(Clump *a, Clump *b);
  93. static Real sortExpensiveToCheap(Clump *a, Clump *b);
  94. Clump *m_firstClump;
  95. Clump *m_curClump;
  96. Int m_clumpCount;
  97. void reset();
  98. public:
  99. SimpleObjectIterator();
  100. //~SimpleObjectIterator(); // provided by MPO
  101. Object *first() { return firstWithNumeric(NULL); }
  102. Object *next() { return nextWithNumeric(NULL); }
  103. Object *firstWithNumeric(Real *num = NULL) { reset(); return nextWithNumeric(num); }
  104. Object *nextWithNumeric(Real *num = NULL);
  105. // methods that are not inherited from ObjectIterator:
  106. /**
  107. throw away all contents of the iterator.
  108. */
  109. void makeEmpty();
  110. /**
  111. insert an object at the head of the iterator. the given numeric value
  112. (typically, dist-squared) is used only for subsequent sort() calls;
  113. if you aren't going to sort, pass 0.0f.
  114. */
  115. void insert(Object *obj, Real numeric = 0.0f);
  116. /**
  117. sort the iterator based on the numeric values for objects and the
  118. order given. Note that some orders (ITER_FASTEST) do nothing!
  119. */
  120. void sort(IterOrderType order);
  121. /**
  122. return the total number of objects in the iterator.
  123. */
  124. Int getCount() { return m_clumpCount; }
  125. };
  126. #endif // _OBJECT_ITER_H_