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

Creates objects in a shared memory region. More...

#include <ObjectPool.h>

Public Member Functions

 ObjectPoolTracked ()
 
 ~ObjectPoolTracked ()
 
template<typename... Args>
ElementType * ConstructNew (KeyType forentity, Args &&... args)
 Constructs a new component of the held type for entity. More...
 
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 id, bool addtoremoved, Args &&... args)
 Calls Release on an object and then removes it from the pool. More...
 
template<typename... Args>
bool ReleaseIfExists (KeyType id, bool addtoremoved, Args &&... args)
 Calls Release on an object if it is in this pool and then removes it from the pool. More...
 
void ClearQueued ()
 Removes elements that are queued for destruction without calling release. More...
 
const auto & GetRemoved () const
 Returns a reference to the vector of removed elements. More...
 
auto & GetAdded ()
 Returns a reference to the vector of added elements. More...
 
void ClearAdded ()
 Clears the added list. More...
 
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...
 
template<typename... Args>
void ReleaseAllAndClear (Args &&... args)
 Calls release on all objects and clears everything. More...
 
void RemoveFromAdded (KeyType id)
 Removes a specific id from the added list. More...
 
ElementType * Find (KeyType id) const
 
void Destroy (KeyType id, bool addtoremoved=true)
 Destroys a component based on id. More...
 
bool DestroyIfExists (KeyType id, bool addtoremoved=true)
 Destroys a component based on id if exists. More...
 
void QueueDestroy (KeyType id)
 Queues destruction of an element. More...
 
void Call (std::function< bool(ElementType &, KeyType)> 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...
 
auto GetObjectCount () const
 
std::unordered_map< KeyType, ElementType * > & GetIndex ()
 Returns a direct access to Index. More...
 
auto GetIndexSize () const
 
ElementType * GetAtIndex (size_t index)
 Returns a created object by index. More...
 

Protected Member Functions

bool RemoveFromIndex (KeyType id)
 Removes an component from the index but doesn't destruct it. More...
 
template<typename... Args>
void _ReleaseCommon (ElementType *object, KeyType id, bool addtoremoved, Args &&... args)
 
void _DestroyCommon (ElementType *object, KeyType id, bool addtoremoved)
 

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...
 

Detailed Description

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

Creates objects in a shared memory region.

Tracks the creation and destruction of elements as needed by components in a GameWorld

Todo:
Created and destroyed vectors could use swap with last and remove the last to remove more effectively

Definition at line 312 of file ObjectPool.h.

Constructor & Destructor Documentation

◆ ObjectPoolTracked()

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

Definition at line 314 of file ObjectPool.h.

316  :
317  Elements(sizeof(ElementType), 16)
318 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
319  {}
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766

◆ ~ObjectPoolTracked()

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

Definition at line 321 of file ObjectPool.h.

322  {
323  if(!AutoCleanupObjects)
324  return;
325 
326  Clear();
327  }
void Clear()
Clears the index and replaces the pool with a new one.
Definition: ObjectPool.h:639

Member Function Documentation

◆ _DestroyCommon()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
void Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::_DestroyCommon ( ElementType *  object,
KeyType  id,
bool  addtoremoved 
)
inlineprotected

Definition at line 731 of file ObjectPool.h.

732  {
733 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
734  object->~ElementType();
735  Elements.free(object);
736 #else
737  delete object;
738 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
739 
740  if(addtoremoved)
741  Removed.push_back(std::make_tuple(object, id));
742 
743  RemoveFromIndex(id);
744  RemoveFromAdded(id);
745  }
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn't destruct it.
Definition: ObjectPool.h:695
void RemoveFromAdded(KeyType id)
Removes a specific id from the added list.
Definition: ObjectPool.h:542
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ _ReleaseCommon()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
template<typename... Args>
void Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::_ReleaseCommon ( ElementType *  object,
KeyType  id,
bool  addtoremoved,
Args &&...  args 
)
inlineprotected

Definition at line 711 of file ObjectPool.h.

712  {
713  object->Release(std::forward<Args>(args)...);
714 
715 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
716  object->~ElementType();
717  Elements.free(object);
718 #else
719  delete object;
720 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
721 
722 
723 
724  if(addtoremoved)
725  Removed.push_back(std::make_tuple(object, id));
726 
727  RemoveFromIndex(id);
728  RemoveFromAdded(id);
729  }
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn't destruct it.
Definition: ObjectPool.h:695
void RemoveFromAdded(KeyType id)
Removes a specific id from the added list.
Definition: ObjectPool.h:542
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ Call()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
void Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::Call ( std::function< bool(ElementType &, KeyType)>  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, the return value specifies if the component should be destroyed (true being yes and false being no)

Definition at line 615 of file ObjectPool.h.

616  {
617  for(auto iter = Index.begin(); iter != Index.end();) {
618 
619  if(function(*iter->second, iter->first)) {
620 
621 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
622  iter->second->~ElementType();
623  Elements.free(iter->second);
624 #else
625  delete iter->second;
626 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
627 
628  iter = Index.erase(iter);
629 
630  } else {
631 
632  ++iter;
633  }
634  }
635  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766

◆ Clear()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
void Leviathan::ObjectPoolTracked< 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 639 of file ObjectPool.h.

640  {
641  for(auto iter = Index.begin(); iter != Index.end(); ++iter) {
642 
643 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
644  iter->second->~ElementType();
645  Elements.free(iter->second);
646 #else
647  delete iter->second;
648 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
649  }
650 
651  Index.clear();
652  Removed.clear();
653  Queued.clear();
654  Added.clear();
655  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749
std::vector< std::tuple< ElementType *, KeyType > > Queued
Definition: ObjectPool.h:759
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
Definition: ObjectPool.h:762

◆ ClearAdded()

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

Clears the added list.

Definition at line 471 of file ObjectPool.h.

472  {
473  Added.clear();
474  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
Definition: ObjectPool.h:762

◆ ClearQueued()

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

Removes elements that are queued for destruction without calling release.

Definition at line 437 of file ObjectPool.h.

438  {
439  for(auto iter = Queued.begin(); iter != Queued.end(); ++iter) {
440 
441  auto object = std::get<0>(*iter);
442  const auto id = std::get<1>(*iter);
443 
444 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
445  object->~ElementType();
446  Elements.free(object);
447 #else
448  delete object;
449 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
450 
451  Removed.push_back(std::make_tuple(object, id));
452  RemoveFromIndex(id);
453  }
454 
455  Queued.clear();
456  }
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn't destruct it.
Definition: ObjectPool.h:695
std::vector< std::tuple< ElementType *, KeyType > > Queued
Definition: ObjectPool.h:759
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ ClearRemoved()

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

Clears the removed list.

Definition at line 477 of file ObjectPool.h.

478  {
479  Removed.clear();
480  }
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ ConstructNew()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
template<typename... Args>
ElementType* Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::ConstructNew ( 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 332 of file ObjectPool.h.

333  {
334  if(Find(forentity))
335  throw Exception("Entity with ID already has object in pool of this type");
336 
337 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
338  // Get memory to hold the object //
339  void* memoryForObject = Elements.malloc();
340 
341  if(!memoryForObject)
342  throw Exception("Out of memory for element");
343 
344  // Construct the object //
345  ElementType* created;
346  try {
347  created = new(memoryForObject) ElementType(std::forward<Args>(args)...);
348  } catch(...) {
349 
350  Elements.free(memoryForObject);
351  throw;
352  }
353 #else
354  // Construct the object //
355  ElementType* created = new ElementType(std::forward<Args>(args)...);
356 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
357 
358  // Add to index for finding later //
359  Index.insert(std::make_pair(forentity, created));
360 
361  Added.push_back(std::make_tuple(created, forentity));
362 
363  return created;
364  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:555
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
Definition: ObjectPool.h:762

◆ Destroy()

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

Destroys a component based on id.

Definition at line 566 of file ObjectPool.h.

567  {
568  auto object = Find(id);
569 
570  if(!object)
571  throw InvalidArgument("ID is not in index");
572 
573  _DestroyCommon(object, id, addtoremoved);
574  }
void _DestroyCommon(ElementType *object, KeyType id, bool addtoremoved)
Definition: ObjectPool.h:731
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:555

◆ DestroyIfExists()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
bool Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::DestroyIfExists ( KeyType  id,
bool  addtoremoved = true 
)
inline

Destroys a component based on id if exists.

Definition at line 577 of file ObjectPool.h.

578  {
579  auto object = Find(id);
580 
581  if(!object)
582  return false;
583 
584  _DestroyCommon(object, id, addtoremoved);
585  return true;
586  }
void _DestroyCommon(ElementType *object, KeyType id, bool addtoremoved)
Definition: ObjectPool.h:731
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:555

◆ Find()

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

Definition at line 555 of file ObjectPool.h.

556  {
557  auto iter = Index.find(id);
558 
559  if(iter == Index.end())
560  return nullptr;
561 
562  return iter->second;
563  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749

◆ GetAdded()

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

Returns a reference to the vector of added elements.

Definition at line 465 of file ObjectPool.h.

466  {
467  return Added;
468  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
Definition: ObjectPool.h:762

◆ GetAtIndex()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
ElementType* Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::GetAtIndex ( size_t  index)
inline

Returns a created object by index.

Helper for exposing object pools to scripts

Note
This is not optimal. It seems that with the map approach it's not possible to have good random iterator access

Definition at line 680 of file ObjectPool.h.

681  {
682  if(index >= Index.size())
683  throw InvalidArgument("index out of range");
684 
685  auto iter = Index.begin();
686 
687  for(size_t i = 0; i < index; ++i)
688  ++iter;
689  return iter->second;
690  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749

◆ GetIndex()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
std::unordered_map<KeyType, ElementType*>& Leviathan::ObjectPoolTracked< 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 665 of file ObjectPool.h.

666  {
667  return Index;
668  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749

◆ GetIndexSize()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
auto Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::GetIndexSize ( ) const
inline

Definition at line 670 of file ObjectPool.h.

671  {
672  return Index.size();
673  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749

◆ GetObjectCount()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
auto Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::GetObjectCount ( ) const
inline

Definition at line 657 of file ObjectPool.h.

658  {
659  return Index.size();
660  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749

◆ GetRemoved()

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

Returns a reference to the vector of removed elements.

Definition at line 459 of file ObjectPool.h.

460  {
461  return Removed;
462  }
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ HasElementsInAdded()

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

Returns true if there are objects in Added.

Definition at line 373 of file ObjectPool.h.

374  {
375  return !Added.empty();
376  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
Definition: ObjectPool.h:762

◆ HasElementsInQueued()

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

Returns true if there are objects in Queued.

Definition at line 379 of file ObjectPool.h.

380  {
381  return !Queued.empty();
382  }
std::vector< std::tuple< ElementType *, KeyType > > Queued
Definition: ObjectPool.h:759

◆ HasElementsInRemoved()

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

Returns true if there are objects in Removed.

Definition at line 367 of file ObjectPool.h.

368  {
369  return !Removed.empty();
370  }
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ QueueDestroy()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
void Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::QueueDestroy ( 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 591 of file ObjectPool.h.

592  {
593  auto end = Index.end();
594  for(auto iter = Index.begin(); iter != end; ++iter) {
595 
596  if(iter->first == id) {
597 
598  Queued.push_back(std::make_tuple(iter->second, id));
599 
600  RemoveFromAdded(id);
601 
602  return;
603  }
604  }
605 
606  throw InvalidArgument("ID is not in index");
607  }
void RemoveFromAdded(KeyType id)
Removes a specific id from the added list.
Definition: ObjectPool.h:542
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749
std::vector< std::tuple< ElementType *, KeyType > > Queued
Definition: ObjectPool.h:759

◆ Release()

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

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

Definition at line 411 of file ObjectPool.h.

412  {
413  auto* object = Find(id);
414 
415  if(!object)
416  throw NotFound("id not in pool");
417 
418  _ReleaseCommon(object, id, addtoremoved, std::forward<Args>(args)...);
419  }
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:555
void _ReleaseCommon(ElementType *object, KeyType id, bool addtoremoved, Args &&... args)
Definition: ObjectPool.h:711

◆ ReleaseAllAndClear()

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

Calls release on all objects and clears everything.

Definition at line 514 of file ObjectPool.h.

515  {
516  for(auto iter = Index.begin(); iter != Index.end(); ++iter) {
517 
518  auto object = iter->second;
519 
520  object->Release(std::forward<Args>(args)...);
521 
522 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
523  object->~ElementType();
524  Elements.free(object);
525 #else
526  delete object;
527 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
528  }
529 
530  // Skip double free
531  Index.clear();
532 
533  // And then clear the rest of things
534  Clear();
535  }
void Clear()
Clears the index and replaces the pool with a new one.
Definition: ObjectPool.h:639
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766

◆ ReleaseIfExists()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
template<typename... Args>
bool Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::ReleaseIfExists ( KeyType  id,
bool  addtoremoved,
Args &&...  args 
)
inline

Calls Release on an object if it is in this pool and then removes it from the pool.

Definition at line 424 of file ObjectPool.h.

425  {
426  auto* object = Find(id);
427 
428  if(!object)
429  return false;
430 
431  _ReleaseCommon(object, id, addtoremoved, std::forward<Args>(args)...);
432  return true;
433  }
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:555
void _ReleaseCommon(ElementType *object, KeyType id, bool addtoremoved, Args &&... args)
Definition: ObjectPool.h:711

◆ ReleaseQueued()

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

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

Definition at line 387 of file ObjectPool.h.

388  {
389  for(auto iter = Queued.begin(); iter != Queued.end(); ++iter) {
390 
391  auto object = std::get<0>(*iter);
392  const auto id = std::get<1>(*iter);
393 
394  object->Release(std::forward<Args>(args)...);
395 
396 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
397  object->~ElementType();
398  Elements.free(object);
399 #else
400  delete object;
401 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
402  Removed.push_back(std::make_tuple(object, id));
403  RemoveFromIndex(id);
404  }
405 
406  Queued.clear();
407  }
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn't destruct it.
Definition: ObjectPool.h:695
std::vector< std::tuple< ElementType *, KeyType > > Queued
Definition: ObjectPool.h:759
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ RemoveBasedOnKeyTupleList()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
template<typename Any >
void Leviathan::ObjectPoolTracked< 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 486 of file ObjectPool.h.

488  {
489  for(auto iter = values.begin(); iter != values.end(); ++iter) {
490 
491  auto todelete = Index.find(std::get<1>(*iter));
492 
493  if(todelete == Index.end())
494  continue;
495 
496 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
497  todelete->second->~ElementType();
498  Elements.free(todelete->second);
499 #else
500  delete todelete->second;
501 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
502 
503  if(addtoremoved)
504  Removed.push_back(std::make_tuple(todelete->second, todelete->first));
505 
506  RemoveFromAdded(todelete->first);
507 
508  Index.erase(todelete);
509  }
510  }
void RemoveFromAdded(KeyType id)
Removes a specific id from the added list.
Definition: ObjectPool.h:542
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:766
std::vector< std::tuple< ElementType *, KeyType > > Removed
Definition: ObjectPool.h:754

◆ RemoveFromAdded()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
void Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::RemoveFromAdded ( 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 542 of file ObjectPool.h.

543  {
544  for(auto iter = Added.begin(); iter != Added.end(); ++iter) {
545 
546  if(std::get<1>(*iter) == id) {
547 
548  Added.erase(iter);
549  return;
550  }
551  }
552  }
std::vector< std::tuple< ElementType *, KeyType > > Added
Used for detecting created elements.
Definition: ObjectPool.h:762

◆ RemoveFromIndex()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
bool Leviathan::ObjectPoolTracked< ElementType, KeyType, AutoCleanupObjects >::RemoveFromIndex ( 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 695 of file ObjectPool.h.

696  {
697  auto end = Index.end();
698  for(auto iter = Index.begin(); iter != end; ++iter) {
699 
700  if(iter->first == id) {
701 
702  Index.erase(iter);
703  return true;
704  }
705  }
706 
707  return false;
708  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:749

Member Data Documentation

◆ Added

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

Used for detecting created elements.

Definition at line 762 of file ObjectPool.h.

◆ Elements

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

Pool for objects.

Definition at line 766 of file ObjectPool.h.

◆ Index

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

Used for looking up element belonging to id.

Definition at line 749 of file ObjectPool.h.

◆ Queued

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
std::vector<std::tuple<ElementType*, KeyType> > Leviathan::ObjectPoolTracked< 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 759 of file ObjectPool.h.

◆ Removed

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
std::vector<std::tuple<ElementType*, KeyType> > Leviathan::ObjectPoolTracked< 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 754 of file ObjectPool.h.


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