Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects > Class Template Reference

Thread safe version of ObjectPool. More...

#include <ObjectPoolThreadSafe.h>

+ Inheritance diagram for Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >:

Public Member Functions

 ThreadSafeObjectPool ()
 
 ~ThreadSafeObjectPool ()
 
template<typename... Args>
ElementType * ConstructNew (Lock &guard, KeyType forentity, Args &&... args)
 Constructs a new component of the held type for entity. More...
 
template<typename... Args>
ElementType * ConstructNew (KeyType forentity, Args &&... args)
 
bool HasElementsInRemoved () const
 Returns true if there are objects in Removed. More...
 
bool HasElementsInAdded () const
 Returns true if there are objects in Added. More...
 
bool HasElementsInQueued () const
 Returns true if there are objects in Queued. More...
 
template<typename... Args>
void ReleaseQueued (Args &&... args)
 Calls Release with the specified arguments on elements that are queued for destruction. More...
 
template<typename... Args>
void Release (KeyType entity, Args &&... args)
 Calls Release on an object and then removes it from the pool. More...
 
void ClearQueued ()
 Removes elements that are queued for destruction without calling release. More...
 
const auto & GetRemoved (Lock &locked) const
 Returns a reference to the vector of removed elements. More...
 
auto & GetAdded (Lock &locked)
 Returns a reference to the vector of added elements. More...
 
void ClearAdded ()
 Clears the added list. More...
 
void ClearAdded (Lock &guard)
 
void ClearRemoved ()
 Clears the removed list. More...
 
template<typename Any >
void RemoveBasedOnKeyTupleList (const std::vector< std::tuple< Any, KeyType >> &values, bool addtoremoved=false)
 Destroys without releasing elements based on ids in vector. More...
 
void RemoveFromAdded (Lock &guard, KeyType id)
 Removes a specific id from the added list. More...
 
ElementType * Find (Lock &guard, KeyType id) const
 
ElementType * Find (KeyType id) const
 
void Destroy (Lock &guard, KeyType id, bool addtoremoved=true)
 Destroys a component based on id. More...
 
void Destroy (KeyType id, bool addtoremoved=true)
 
void QueueDestroy (Lock &guard, KeyType id)
 Queues destruction of an element. More...
 
void QueueDestroy (KeyType id)
 
void Call (std::function< bool(ElementType &, KeyType, Lock &)> function)
 Calls an function on all the objects in the pool. More...
 
void Clear ()
 Clears the index and replaces the pool with a new one. More...
 
std::unordered_map< KeyType, ElementType * > & GetIndex ()
 Returns a direct access to Index. More...
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Protected Member Functions

bool RemoveFromIndex (Lock &guard, KeyType id)
 Removes an component from the index but doesn't destruct it. More...
 
bool RemoveFromIndex (KeyType id)
 

Protected Attributes

std::unordered_map< KeyType, ElementType * > Index
 Used for looking up element belonging to id. More...
 
std::vector< std::tuple< ElementType *, KeyType > > Removed
 
std::vector< std::tuple< ElementType *, KeyType > > Queued
 
std::vector< std::tuple< ElementType *, KeyType > > Added
 Used for detecting created elements. More...
 
boost::pool Elements
 Pool for objects. More...
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Additional Inherited Members

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 

Detailed Description

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
class Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >

Thread safe version of ObjectPool.

Definition at line 25 of file ObjectPoolThreadSafe.h.

Constructor & Destructor Documentation

◆ ThreadSafeObjectPool()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ThreadSafeObjectPool ( )
inline

Definition at line 27 of file ObjectPoolThreadSafe.h.

29  :
30  Elements(sizeof(ElementType), 100, 200)
31 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
32  {}
boost::pool Elements
Pool for objects.

◆ ~ThreadSafeObjectPool()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::~ThreadSafeObjectPool ( )
inline

Definition at line 34 of file ObjectPoolThreadSafe.h.

35  {
36  if(!AutoCleanupObjects)
37  return;
38 
39  Clear();
40  }
void Clear()
Clears the index and replaces the pool with a new one.

Member Function Documentation

◆ Call()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Call ( std::function< bool(ElementType &, KeyType, Lock &)>  function)
inline

Calls an function on all the objects in the pool.

Note
The order of the objects is not guaranteed and can change between runs
Parameters
functionThe function that is called with all of the components of this type the first parameter is the component, the second is the id of the entity owning the component and the last one is a lock to this ComponentHolder, the return value specifies if the component should be destroyed (true being yes and false being no)

Definition at line 351 of file ObjectPoolThreadSafe.h.

352  {
353  GUARD_LOCK();
354 
355  for(auto iter = Index.begin(); iter != Index.end();) {
356 
357  if(function(*iter->second, iter->first, guard)) {
358 
359 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
360  iter->second->~ElementType();
361  Elements.free(iter->second);
362 #else
363  delete iter->second;
364 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
365 
366  iter = Index.erase(iter);
367 
368  } else {
369 
370  ++iter;
371  }
372  }
373  }
boost::pool Elements
Pool for objects.
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Clear()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Clear ( )
inline

Clears the index and replaces the pool with a new one.

Warning
All objects after this call are invalid

Definition at line 377 of file ObjectPoolThreadSafe.h.

378  {
379  GUARD_LOCK();
380 
381  for(auto iter = Index.begin(); iter != Index.end(); ++iter) {
382 
383 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
384  iter->second->~ElementType();
385  Elements.free(iter->second);
386 #else
387  delete iter->second;
388 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
389  }
390 
391  Index.clear();
392  Removed.clear();
393  Queued.clear();
394  Added.clear();
395  }
boost::pool Elements
Pool for objects.
std::vector< std::tuple< ElementType *, KeyType > > Removed
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
std::vector< std::tuple< ElementType *, KeyType > > Queued
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ClearAdded() [1/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ClearAdded ( )
inline

Clears the added list.

Definition at line 203 of file ObjectPoolThreadSafe.h.

204  {
205  GUARD_LOCK();
206  ClearAdded(guard);
207  }
void ClearAdded()
Clears the added list.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ClearAdded() [2/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ClearAdded ( Lock guard)
inline

Definition at line 209 of file ObjectPoolThreadSafe.h.

210  {
211  Added.clear();
212  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.

◆ ClearQueued()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ClearQueued ( )
inline

Removes elements that are queued for destruction without calling release.

Definition at line 161 of file ObjectPoolThreadSafe.h.

162  {
163  GUARD_LOCK();
164 
165  for(auto iter = Queued.begin(); iter != Queued.end(); ++iter) {
166 
167  auto object = std::get<0>(*iter);
168  const auto id = std::get<1>(*iter);
169 
170 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
171  object->~ElementType();
172  Elements.free(object);
173 #else
174  delete object;
175 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
176 
177  Removed.push_back(std::make_tuple(object, id));
178  RemoveFromIndex(guard, id);
179  }
180 
181  Queued.clear();
182  }
bool RemoveFromIndex(Lock &guard, KeyType id)
Removes an component from the index but doesn't destruct it.
boost::pool Elements
Pool for objects.
std::vector< std::tuple< ElementType *, KeyType > > Removed
std::vector< std::tuple< ElementType *, KeyType > > Queued
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ClearRemoved()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ClearRemoved ( )
inline

Clears the removed list.

Definition at line 215 of file ObjectPoolThreadSafe.h.

216  {
217  GUARD_LOCK();
218  Removed.clear();
219  }
std::vector< std::tuple< ElementType *, KeyType > > Removed
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ConstructNew() [1/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
template<typename... Args>
ElementType* Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ConstructNew ( Lock guard,
KeyType  forentity,
Args &&...  args 
)
inline

Constructs a new component of the held type for entity.

Exceptions
Exceptionwhen component has not been created

Definition at line 45 of file ObjectPoolThreadSafe.h.

46  {
47  if(Find(guard, forentity))
48  throw Exception("Entity with ID already has object in pool of this type");
49 
50 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
51  // Get memory to hold the object //
52  void* memoryForObject = Elements.malloc();
53 
54  if(!memoryForObject)
55  throw Exception("Out of memory for element");
56 
57  // Construct the object //
58  ElementType* created;
59  try {
60  created = new(memoryForObject) ElementType(std::forward<Args>(args)...);
61  } catch(...) {
62 
63  Elements.free(memoryForObject);
64  throw;
65  }
66 #else
67  // Construct the object //
68  ElementType* created = new ElementType(std::forward<Args>(args)...);
69 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
70 
71  // Add to index for finding later //
72  Index.insert(std::make_pair(forentity, created));
73 
74  Added.push_back(std::make_tuple(created, forentity));
75 
76  return created;
77  }
ElementType * Find(Lock &guard, KeyType id) const
boost::pool Elements
Pool for objects.
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.

◆ ConstructNew() [2/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
template<typename... Args>
ElementType* Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ConstructNew ( KeyType  forentity,
Args &&...  args 
)
inline

Definition at line 80 of file ObjectPoolThreadSafe.h.

81  {
82  GUARD_LOCK();
83  return ConstructNew(guard, forentity, args...);
84  }
ElementType * ConstructNew(Lock &guard, KeyType forentity, Args &&... args)
Constructs a new component of the held type for entity.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Destroy() [1/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Destroy ( Lock guard,
KeyType  id,
bool  addtoremoved = true 
)
inline

Destroys a component based on id.

Definition at line 290 of file ObjectPoolThreadSafe.h.

291  {
292  auto object = Find(guard, id);
293 
294  if(!object)
295  throw InvalidArgument("ID is not in index");
296 
297 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
298  object->~ElementType();
299  Elements.free(object);
300 #else
301  delete object;
302 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
303 
304  if(addtoremoved)
305  Removed.push_back(std::make_tuple(object, id));
306 
307  RemoveFromIndex(guard, id);
308  RemoveFromAdded(guard, id);
309  }
void RemoveFromAdded(Lock &guard, KeyType id)
Removes a specific id from the added list.
bool RemoveFromIndex(Lock &guard, KeyType id)
Removes an component from the index but doesn't destruct it.
ElementType * Find(Lock &guard, KeyType id) const
boost::pool Elements
Pool for objects.
std::vector< std::tuple< ElementType *, KeyType > > Removed

◆ Destroy() [2/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Destroy ( KeyType  id,
bool  addtoremoved = true 
)
inline

Definition at line 311 of file ObjectPoolThreadSafe.h.

312  {
313  GUARD_LOCK();
314  Destroy(guard, id, addtoremoved);
315  }
void Destroy(Lock &guard, KeyType id, bool addtoremoved=true)
Destroys a component based on id.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Find() [1/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
ElementType* Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Find ( Lock guard,
KeyType  id 
) const
inline
Returns
The found component or NULL

Definition at line 273 of file ObjectPoolThreadSafe.h.

274  {
275  auto iter = Index.find(id);
276 
277  if(iter == Index.end())
278  return nullptr;
279 
280  return iter->second;
281  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.

◆ Find() [2/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
ElementType* Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Find ( KeyType  id) const
inline

Definition at line 283 of file ObjectPoolThreadSafe.h.

284  {
285  GUARD_LOCK();
286  return Find(guard, id);
287  }
ElementType * Find(Lock &guard, KeyType id) const
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ GetAdded()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
auto& Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::GetAdded ( Lock locked)
inline

Returns a reference to the vector of added elements.

Note
This object needs to be locked and kept locked while using the result of this call

Definition at line 197 of file ObjectPoolThreadSafe.h.

198  {
199  return Added;
200  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.

◆ GetIndex()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
std::unordered_map<KeyType, ElementType*>& Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::GetIndex ( )
inline

Returns a direct access to Index.

Note
Do not change the returned index it is intended only for looping. Okay, you may change it but you have to be extremely careful

Definition at line 400 of file ObjectPoolThreadSafe.h.

401  {
402  return Index;
403  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.

◆ GetRemoved()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
const auto& Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::GetRemoved ( Lock locked) const
inline

Returns a reference to the vector of removed elements.

Note
This object needs to be locked and kept locked while using the result of this call

Definition at line 187 of file ObjectPoolThreadSafe.h.

188  {
189  return Removed;
190  }
std::vector< std::tuple< ElementType *, KeyType > > Removed

◆ HasElementsInAdded()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
bool Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::HasElementsInAdded ( ) const
inline

Returns true if there are objects in Added.

Definition at line 94 of file ObjectPoolThreadSafe.h.

95  {
96  GUARD_LOCK();
97  return !Added.empty();
98  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ HasElementsInQueued()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
bool Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::HasElementsInQueued ( ) const
inline

Returns true if there are objects in Queued.

Definition at line 101 of file ObjectPoolThreadSafe.h.

102  {
103  GUARD_LOCK();
104  return !Queued.empty();
105  }
std::vector< std::tuple< ElementType *, KeyType > > Queued
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ HasElementsInRemoved()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
bool Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::HasElementsInRemoved ( ) const
inline

Returns true if there are objects in Removed.

Definition at line 87 of file ObjectPoolThreadSafe.h.

88  {
89  GUARD_LOCK();
90  return !Removed.empty();
91  }
std::vector< std::tuple< ElementType *, KeyType > > Removed
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ QueueDestroy() [1/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::QueueDestroy ( Lock guard,
KeyType  id 
)
inline

Queues destruction of an element.

Exceptions
InvalidArgumentwhen key is not found (is already deleted)
Note
This has to be used for objects that require calling Release

Definition at line 320 of file ObjectPoolThreadSafe.h.

321  {
322  auto end = Index.end();
323  for(auto iter = Index.begin(); iter != end; ++iter) {
324 
325  if(iter->first == id) {
326 
327  Queued.push_back(std::make_tuple(iter->second, id));
328 
329  RemoveFromAdded(guard, id);
330 
331  return;
332  }
333  }
334 
335  throw InvalidArgument("ID is not in index");
336  }
void RemoveFromAdded(Lock &guard, KeyType id)
Removes a specific id from the added list.
std::vector< std::tuple< ElementType *, KeyType > > Queued
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.

◆ QueueDestroy() [2/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::QueueDestroy ( KeyType  id)
inline

Definition at line 338 of file ObjectPoolThreadSafe.h.

339  {
340  GUARD_LOCK();
341  QueueDestroy(guard, id);
342  }
void QueueDestroy(Lock &guard, KeyType id)
Queues destruction of an element.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Release()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
template<typename... Args>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Release ( KeyType  entity,
Args &&...  args 
)
inline

Calls Release on an object and then removes it from the pool.

Definition at line 137 of file ObjectPoolThreadSafe.h.

138  {
139  GUARD_LOCK();
140 
141  auto* object = Find(guard, entity);
142 
143  if(!object)
144  throw NotFound("entity not in pool");
145 
146  object->Release(std::forward<Args>(args)...);
147 
148 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
149  object->~ElementType();
150  Elements.free(object);
151 #else
152  delete object;
153 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
154 
155  RemoveFromIndex(guard, entity);
156  RemoveFromAdded(guard, entity);
157  }
void RemoveFromAdded(Lock &guard, KeyType id)
Removes a specific id from the added list.
bool RemoveFromIndex(Lock &guard, KeyType id)
Removes an component from the index but doesn't destruct it.
ElementType * Find(Lock &guard, KeyType id) const
boost::pool Elements
Pool for objects.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ ReleaseQueued()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
template<typename... Args>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::ReleaseQueued ( Args &&...  args)
inline

Calls Release with the specified arguments on elements that are queued for destruction.

Definition at line 110 of file ObjectPoolThreadSafe.h.

111  {
112  GUARD_LOCK();
113 
114  for(auto iter = Queued.begin(); iter != Queued.end(); ++iter) {
115 
116  auto object = std::get<0>(*iter);
117  const auto id = std::get<1>(*iter);
118 
119  object->Release(std::forward<Args>(args)...);
120 
121 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
122  object->~ElementType();
123  Elements.free(object);
124 #else
125  delete object;
126 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
127 
128  Removed.push_back(std::make_tuple(object, id));
129  RemoveFromIndex(guard, id);
130  }
131 
132  Queued.clear();
133  }
bool RemoveFromIndex(Lock &guard, KeyType id)
Removes an component from the index but doesn't destruct it.
boost::pool Elements
Pool for objects.
std::vector< std::tuple< ElementType *, KeyType > > Removed
std::vector< std::tuple< ElementType *, KeyType > > Queued
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ RemoveBasedOnKeyTupleList()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
template<typename Any >
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::RemoveBasedOnKeyTupleList ( const std::vector< std::tuple< Any, KeyType >> &  values,
bool  addtoremoved = false 
)
inline

Destroys without releasing elements based on ids in vector.

Parameters
addtoremovedIf true will add the elements to the Removed index for (possibly) using them to remove attached resources

Definition at line 225 of file ObjectPoolThreadSafe.h.

227  {
228  GUARD_LOCK();
229 
230  for(auto iter = values.begin(); iter != values.end(); ++iter) {
231 
232  auto todelete = Index.find(std::get<1>(*iter));
233 
234  if(todelete == Index.end())
235  continue;
236 
237  if(addtoremoved) {
238 
239  Removed.push_back(std::make_tuple(todelete->second, todelete->first));
240  }
241 
242 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
243  todelete->second->~ElementType();
244  Elements.free(todelete->second);
245 #else
246  delete todelete->second;
247 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
248 
249  RemoveFromAdded(guard, todelete->first);
250 
251  Index.erase(todelete);
252  }
253  }
void RemoveFromAdded(Lock &guard, KeyType id)
Removes a specific id from the added list.
boost::pool Elements
Pool for objects.
std::vector< std::tuple< ElementType *, KeyType > > Removed
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ RemoveFromAdded()

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
void Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::RemoveFromAdded ( Lock guard,
KeyType  id 
)
inline

Removes a specific id from the added list.

Used to remove entries that have been deleted before clearing the added ones

Todo:
Check if this gives better performance than noticing missing elements during node construction

Definition at line 260 of file ObjectPoolThreadSafe.h.

261  {
262  for(auto iter = Added.begin(); iter != Added.end(); ++iter) {
263 
264  if(std::get<1>(*iter) == id) {
265 
266  Added.erase(iter);
267  return;
268  }
269  }
270  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.

◆ RemoveFromIndex() [1/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
bool Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::RemoveFromIndex ( Lock guard,
KeyType  id 
)
inlineprotected

Removes an component from the index but doesn't destruct it.

Note
The component will only be deallocated once this object is destructed

Definition at line 408 of file ObjectPoolThreadSafe.h.

409  {
410  auto end = Index.end();
411  for(auto iter = Index.begin(); iter != end; ++iter) {
412 
413  if(iter->first == id) {
414 
415  Index.erase(iter);
416  return true;
417  }
418  }
419 
420  return false;
421  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.

◆ RemoveFromIndex() [2/2]

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
bool Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::RemoveFromIndex ( KeyType  id)
inlineprotected

Definition at line 423 of file ObjectPoolThreadSafe.h.

424  {
425  GUARD_LOCK();
426  return RemoveFromIndex(id);
427  }
bool RemoveFromIndex(Lock &guard, KeyType id)
Removes an component from the index but doesn't destruct it.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

Member Data Documentation

◆ Added

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
std::vector<std::tuple<ElementType*, KeyType> > Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Added
protected

Used for detecting created elements.

Definition at line 444 of file ObjectPoolThreadSafe.h.

◆ Elements

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
boost::pool Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Elements
protected

Pool for objects.

Definition at line 448 of file ObjectPoolThreadSafe.h.

◆ Index

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
std::unordered_map<KeyType, ElementType*> Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Index
protected

Used for looking up element belonging to id.

Definition at line 431 of file ObjectPoolThreadSafe.h.

◆ Queued

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
std::vector<std::tuple<ElementType*, KeyType> > Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Queued
protected

Used for marking elements to be deleted later at a suitable time GameWorld uses this to control when components are deleted

Definition at line 441 of file ObjectPoolThreadSafe.h.

◆ Removed

template<class ElementType , typename KeyType , bool AutoCleanupObjects = true>
std::vector<std::tuple<ElementType*, KeyType> > Leviathan::ThreadSafeObjectPool< ElementType, KeyType, AutoCleanupObjects >::Removed
protected

Used for detecting deleted elements later Can be used to unlink resources that have pointers to elements

Definition at line 436 of file ObjectPoolThreadSafe.h.


The documentation for this class was generated from the following file: