ParticlePoolMap.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  17. * A particle pool map.
  18. *
  19. * @tparam T The type of particle.
  20. */
  21. template <typename T>
  22. class ParticlePoolMap{
  23. public:
  24. typedef std::multimap<Ogre::String, T*> PoolMapMap;
  25. // The 'typename' MUST be added, since T is not a fixed type.
  26. typedef typename PoolMapMap::iterator PoolMapIterator;
  27. /**
  28. * Constructor.
  29. */
  30. ParticlePoolMap(){};
  31. /**
  32. * Destructor.
  33. */
  34. virtual ~ParticlePoolMap(){};
  35. /**
  36. * Checks if the pool map is empty.
  37. *
  38. * It doesn't consider locked particles, only the released ones.
  39. *
  40. * @return True if the pool is empty, false if there is at least one
  41. * particle in the pool.
  42. */
  43. bool IsEmpty(){return released_.empty();};
  44. /**
  45. * Retrieves the pool map size.
  46. *
  47. * It doesn't consider locked particles, only the released ones.
  48. *
  49. * @return The number of particles in the pool.
  50. */
  51. size_t GetSize(){return released_.size();};
  52. /**
  53. * Resets the pool iterator.
  54. */
  55. void ResetIterator(){pool_map_iterator_ = released_.begin();};
  56. /**
  57. * Retrieves the first particle.
  58. *
  59. * Before doing so, it resets the pool map iterator. It doesn't
  60. * consider locked particles, only the released ones.
  61. *
  62. * @return A reference to the first particle in the pool map. NULL if
  63. * the pool map is empty.
  64. */
  65. T* GetFirst(){
  66. ResetIterator();
  67. if (End()) return NULL;
  68. T* t = pool_map_iterator_->second;
  69. return t;
  70. };
  71. /**
  72. * Retrieves the next particle in the pool map.
  73. *
  74. * Before doing so, it advances the pool map iterator. It doesn't
  75. * consider locked particles, only the released ones.
  76. *
  77. * @return A reference to the next particle in the pool map. NULL if
  78. * the pool map is empty or the iterator is at the end of it.
  79. */
  80. T* GetNext(){
  81. if (End()) return NULL;
  82. ++ pool_map_iterator_;
  83. if (End()) return NULL;
  84. T* t = pool_map_iterator_->second;
  85. return t;
  86. };
  87. /**
  88. * Checks if the iterator is at the end of the pool map.
  89. *
  90. * If it's at the end, {@see GetNext} will return null when called. It
  91. * doesn't consider locked particles, only the released ones.
  92. *
  93. * @return True if the iterator is at the end of the pool map.
  94. */
  95. bool End(){return pool_map_iterator_ == released_.end();};
  96. /**
  97. * Removes all particles.
  98. *
  99. * Removes both locked and released particles.
  100. */
  101. void Clear(){
  102. locked_.clear();
  103. released_.clear();
  104. };
  105. /**
  106. * Adds a particle to the pool map.
  107. *
  108. * A particle is added in locked state.
  109. *
  110. * @param key[in] PArticle key in the map.
  111. * @param element[in] Particle to add to the pool.
  112. */
  113. void AddElement(const Ogre::String& key, T* element){
  114. locked_.insert(make_pair(key, element));
  115. };
  116. /**
  117. * Releases a locked particle.
  118. *
  119. * @param key[in] The key of the particle to unlock.
  120. * @return A pointer to the previously locked and now released
  121. * particle, or 0 if there were no more locked particles.
  122. */
  123. T* ReleaseElement(const Ogre::String& key){
  124. // Return with 0 if no elements left
  125. if (locked_.empty()) return NULL;
  126. // Return the first element that is encountered
  127. T* t = 0;
  128. PoolMapIterator it;
  129. it = locked_.find(key);
  130. if (it != locked_.end()){
  131. // Get the element and move it to the released elements list
  132. t = it->second;
  133. released_.insert(make_pair(key, t));
  134. locked_.erase(it);
  135. }
  136. return t;
  137. };
  138. /**
  139. * Releases all locked particles.
  140. *
  141. * It also resets the pool map operator.
  142. */
  143. void ReleaseAllElements(){
  144. // Move all elements from locked elements to released elements
  145. PoolMapIterator it;
  146. for (it = locked_.begin(); it != locked_.end(); ++ it)
  147. released_.insert(make_pair(it->first, it->second));
  148. locked_.clear();
  149. ResetIterator();
  150. };
  151. /**
  152. * Locks the released particle pointed by the iterator.
  153. *
  154. * The particle gets added to the end of the locked particle list.
  155. */
  156. void LockLatestElement(){
  157. // Move element pointed by iterator from released elements to
  158. // locked elements
  159. locked_.insert(
  160. make_pair(pool_map_iterator_->first, pool_map_iterator_->second)
  161. );
  162. // Watch the ++ at the end to set mPoolMapIterator to the next
  163. // element
  164. released_.erase(pool_map_iterator_ ++);
  165. };
  166. /**
  167. * Locks all particles.
  168. *
  169. * The particles get added at the end of the locked list in the same
  170. * order they were in the released list. The pool iterator is reseted.
  171. */
  172. void LockAllElements() {
  173. // Move all elements from release elements to locked elements
  174. PoolMapIterator it;
  175. for (it = released_.begin(); it != released_.end(); ++ it)
  176. locked_.insert(make_pair(it->first, it->second));
  177. released_.clear();
  178. ResetIterator();
  179. };
  180. protected:
  181. /**
  182. * List with released particles.
  183. *
  184. * They can be operated upon.
  185. */
  186. PoolMapMap released_;
  187. /**
  188. * List with locked particles.
  189. *
  190. * They can't be operated upon until released.
  191. */
  192. PoolMapMap locked_;
  193. /**
  194. * The pool map iterator.
  195. *
  196. * Determines the current particle to be acted upon. Only works for
  197. * released particles.
  198. */
  199. PoolMapIterator pool_map_iterator_;
  200. };