V-Gears 0
Free Final Fantasy VII engine.
ParticlePool.h
Go to the documentation of this file.
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
16#pragma once
17
18#include <list>
19
28template <typename T>
30
31 public:
32
33 typedef std::list<T*> PoolList;
34
35 // The 'typename' MUST be added, since T is not a fixed type
36 typedef typename PoolList::iterator PoolIterator;
37
42
46 virtual ~ParticlePool(){};
47
56 bool IsEmpty(){return released_.empty();};
57
65 size_t GetSize(){return released_.size();};
66
71
81 T* GetFirst() {
83 if (End()) return NULL;
84 T* t = *pool_iterator_;
85 return t;
86 };
87
97 T* GetNext(){
98 if (End()) return NULL;
100 if (End()) return NULL;
101 T* t = *pool_iterator_;
102 return t;
103 };
104
113 bool End(){
114 return pool_iterator_ == released_.end();
115 };
116
122 void Clear(){
123 locked_.clear();
124 released_.clear();
125 };
126
134 void AddElement(T* element) {
135 locked_.push_back(element);
136 };
137
147 // Return with 0 if no elements left
148 if (locked_.empty()) return 0;
149
150 // Move element from locked elements to released elements and return it
151 T* t = locked_.front();
152 released_.splice(released_.end(), locked_, locked_.begin());
153 return t;
154 };
155
162 // Move all elements from locked elements to released elements
163 released_.splice(released_.end(), locked_);
165 };
166
173 if (End() == false){
174 locked_.push_back(*pool_iterator_);
176 }
177 };
178
186 // Move all elements from released elements to locked elements
187 locked_.splice(locked_.end(), released_);
189 };
190
196 std::list<T*>& GetActiveElementsList(){
197 return released_;
198 };
199
200 protected:
201
208
215
223};
224
A particle pool.
Definition: ParticlePool.h:29
virtual ~ParticlePool()
Destructor.
Definition: ParticlePool.h:46
PoolIterator pool_iterator_
The pool iterator.
Definition: ParticlePool.h:222
PoolList::iterator PoolIterator
Definition: ParticlePool.h:36
void LockLatestElement()
Locks the released particle pointed by the iterator.
Definition: ParticlePool.h:172
T * ReleaseElement()
Releases a locked particle.
Definition: ParticlePool.h:146
PoolList released_
List with released particles.
Definition: ParticlePool.h:207
std::list< T * > PoolList
Definition: ParticlePool.h:33
bool IsEmpty()
Checks if the pool is empty.
Definition: ParticlePool.h:56
void Clear()
Removes all particles.
Definition: ParticlePool.h:122
T * GetFirst()
Retrieves the first particle.
Definition: ParticlePool.h:81
size_t GetSize()
Retrieves the pool size.
Definition: ParticlePool.h:65
void AddElement(T *element)
Adds a particle to the pool.
Definition: ParticlePool.h:134
std::list< T * > & GetActiveElementsList()
Retrieves the list of released particles.
Definition: ParticlePool.h:196
bool End()
Checks if the iterator is at the end of the pool.
Definition: ParticlePool.h:113
void LockAllElements()
Locks all particles.
Definition: ParticlePool.h:185
void ResetIterator()
Resets the pool iterator.
Definition: ParticlePool.h:70
void ReleaseAllElements()
Releases all locked particles.
Definition: ParticlePool.h:161
PoolList locked_
List with locked particles.
Definition: ParticlePool.h:214
T * GetNext()
Retrieves the next particle in the pool.
Definition: ParticlePool.h:97
ParticlePool()
Constructor.
Definition: ParticlePool.h:41