ParticlePool.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef PARTICLE_POOL_H
  2. #define PARTICLE_POOL_H
  3. #include <list>
  4. template <typename T>
  5. class ParticlePool
  6. {
  7. public:
  8. typedef std::list<T*> PoolList;
  9. typedef typename PoolList::iterator PoolIterator; // The 'typename' MUST be added, since T is not a fixed type
  10. ParticlePool() {};
  11. virtual ~ParticlePool() {};
  12. bool
  13. IsEmpty()
  14. {
  15. return m_Released.empty();
  16. };
  17. size_t
  18. GetSize()
  19. {
  20. return m_Released.size();
  21. };
  22. void
  23. ResetIterator()
  24. {
  25. m_PoolIterator = m_Released.begin();
  26. };
  27. T*
  28. GetFirst()
  29. {
  30. ResetIterator();
  31. if (End())
  32. {
  33. return NULL;
  34. }
  35. T* t = *m_PoolIterator;
  36. return t;
  37. };
  38. T*
  39. GetNext()
  40. {
  41. if (End())
  42. {
  43. return NULL;
  44. }
  45. ++m_PoolIterator;
  46. if (End())
  47. {
  48. return NULL;
  49. }
  50. T* t = *m_PoolIterator;
  51. return t;
  52. };
  53. bool
  54. End()
  55. {
  56. return m_PoolIterator == m_Released.end();
  57. };
  58. void
  59. Clear()
  60. {
  61. m_Locked.clear();
  62. m_Released.clear();
  63. };
  64. void
  65. AddElement(T* element)
  66. {
  67. m_Locked.push_back(element);
  68. };
  69. T*
  70. ReleaseElement()
  71. {
  72. // Return with 0 if no elements left
  73. if (m_Locked.empty())
  74. {
  75. return 0;
  76. }
  77. // Move element from locked elements to released elements and return it
  78. T* t = m_Locked.front();
  79. m_Released.splice(m_Released.end(), m_Locked, m_Locked.begin());
  80. return t;
  81. };
  82. void
  83. ReleaseAllElements()
  84. {
  85. // Move all elements from locked elements to released elements
  86. m_Released.splice(m_Released.end(), m_Locked);
  87. ResetIterator();
  88. };
  89. void
  90. LockLatestElement()
  91. {
  92. if (End() == false)
  93. {
  94. m_Locked.push_back(*m_PoolIterator);
  95. m_PoolIterator = m_Released.erase(m_PoolIterator);
  96. }
  97. };
  98. void LockAllElements()
  99. {
  100. // Move all elements from released elements to locked elements
  101. m_Locked.splice(m_Locked.end(), m_Released);
  102. ResetIterator();
  103. };
  104. std::list<T*>&
  105. GetActiveElementsList()
  106. {
  107. return m_Released;
  108. };
  109. protected:
  110. PoolList m_Released; // List with precreated 'released' elements
  111. PoolList m_Locked; // List with precreated 'locked' elements
  112. PoolIterator m_PoolIterator;
  113. };
  114. #endif // PARTICLE_POOL_H