ParticlePool.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #pragma once
  16. #include <list>
  17. /**
  18. * A particle pool
  19. *
  20. * There are actually two pools, one for locked particles and one for released
  21. * ones. Locked particles can't be used until they are released.
  22. *
  23. * @tparam T The type of particle.
  24. */
  25. template <typename T>
  26. class ParticlePool{
  27. public:
  28. typedef std::list<T*> PoolList;
  29. // The 'typename' MUST be added, since T is not a fixed type
  30. typedef typename PoolList::iterator PoolIterator;
  31. /**
  32. * Constructor.
  33. */
  34. ParticlePool(){};
  35. /**
  36. * Destructor.
  37. */
  38. virtual ~ParticlePool(){};
  39. /**
  40. * Checks if the pool is empty.
  41. *
  42. * It doesn't consider locked particles, only the released ones.
  43. *
  44. * @return True if the pool is empty, false if there is at least one
  45. * particle in the pool.
  46. */
  47. bool IsEmpty(){return released_.empty();};
  48. /**
  49. * Retrieves the pool size.
  50. *
  51. * It doesn't consider locked particles, only the released ones.
  52. *
  53. * @return The number of particles in the pool.
  54. */
  55. size_t GetSize(){return released_.size();};
  56. /**
  57. * Resets the pool iterator.
  58. */
  59. void ResetIterator(){pool_iterator_ = released_.begin();};
  60. /**
  61. * Retrieves the first particle.
  62. *
  63. * Before doing so, it resets the pool iterator. It doesn't consider
  64. * locked particles, only the released ones.
  65. *
  66. * @return A reference to the first particle in the pool. NULL if the
  67. * pool is empty.
  68. */
  69. T* GetFirst() {
  70. ResetIterator();
  71. if (End()) return NULL;
  72. T* t = *pool_iterator_;
  73. return t;
  74. };
  75. /**
  76. * Retrieves the next particle in the pool.
  77. *
  78. * Before doing so, it advances the pool iterator. It doesn't consider
  79. * locked particles, only the released ones.
  80. *
  81. * @return A reference to the next particle in the pool. NULL if the
  82. * pool is empty or the iterator is at the end of it.
  83. */
  84. T* GetNext(){
  85. if (End()) return NULL;
  86. ++ pool_iterator_;
  87. if (End()) return NULL;
  88. T* t = *pool_iterator_;
  89. return t;
  90. };
  91. /**
  92. * Checks if the iterator is at the end of the pool.
  93. *
  94. * If it's at the end, {@see GetNext} will return null when called. It
  95. * doesn't consider locked particles, only the released ones.
  96. *
  97. * @return True if the iterator is at the end of the pool.
  98. */
  99. bool End(){
  100. return pool_iterator_ == released_.end();
  101. };
  102. /**
  103. * Removes all particles.
  104. *
  105. * Removes both locked and released particles.
  106. */
  107. void Clear(){
  108. locked_.clear();
  109. released_.clear();
  110. };
  111. /**
  112. * Adds a particle to the pool.
  113. *
  114. * A particle is added in locked state.
  115. *
  116. * @param element[in] Particle to add to the pool.
  117. */
  118. void AddElement(T* element) {
  119. locked_.push_back(element);
  120. };
  121. /**
  122. * Releases a locked particle.
  123. *
  124. * The first locked particle will be the one unlocked.
  125. *
  126. * @return A pointer to the previously locked and now released
  127. * particle, or 0 if there were no more locked particles.
  128. */
  129. T* ReleaseElement() {
  130. // Return with 0 if no elements left
  131. if (locked_.empty()) return 0;
  132. // Move element from locked elements to released elements and return it
  133. T* t = locked_.front();
  134. released_.splice(released_.end(), locked_, locked_.begin());
  135. return t;
  136. };
  137. /**
  138. * Releases all locked particles.
  139. *
  140. * It also resets the pool operator.
  141. */
  142. void ReleaseAllElements(){
  143. // Move all elements from locked elements to released elements
  144. released_.splice(released_.end(), locked_);
  145. ResetIterator();
  146. };
  147. /**
  148. * Locks the released particle pointed by the iterator.
  149. *
  150. * The particle gets added to the end of the locked particle list.
  151. */
  152. void LockLatestElement(){
  153. if (End() == false){
  154. locked_.push_back(*pool_iterator_);
  155. pool_iterator_ = released_.erase(pool_iterator_);
  156. }
  157. };
  158. /**
  159. * Locks all particles.
  160. *
  161. * The particles get added at the end of the locked list in the same
  162. * order they were in the released list. The pool iterator is reseted.
  163. */
  164. void LockAllElements(){
  165. // Move all elements from released elements to locked elements
  166. locked_.splice(locked_.end(), released_);
  167. ResetIterator();
  168. };
  169. /**
  170. * Retrieves the list of released particles.
  171. *
  172. * @return List of released particles.
  173. */
  174. std::list<T*>& GetActiveElementsList(){
  175. return released_;
  176. };
  177. protected:
  178. /**
  179. * List with released particles.
  180. *
  181. * They can be operated upon.
  182. */
  183. PoolList released_;
  184. /**
  185. * List with locked particles.
  186. *
  187. * They can't be operated upon until released.
  188. */
  189. PoolList locked_;
  190. /**
  191. * The pool iterator.
  192. *
  193. * Determines the current particle to be acted upon. Only works for
  194. * released particles.
  195. */
  196. PoolIterator pool_iterator_;
  197. };