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

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

#include <ObjectPool.h>

Public Member Functions

 ObjectPool ()
 
 ~ObjectPool ()
 
template<typename... Args>
ElementType * ConstructNew (KeyType forentity, Args &&... args)
 Constructs a new component of the held type for entity. More...
 
template<typename... Args>
void Release (KeyType entity, Args &&... args)
 Calls Release on an object and then removes it from the pool. More...
 
ElementType * Find (KeyType id) const
 
void Destroy (KeyType id)
 Destroys a component based on id. More...
 
template<typename Any >
void RemoveBasedOnKeyTupleList (const std::vector< std::tuple< Any, KeyType >> &values)
 Destroys without releasing elements based on ids in vector. 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...
 

Protected Member Functions

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

Protected Attributes

std::unordered_map< KeyType, ElementType * > Index
 Used for looking up element belonging to id. More...
 
boost::pool Elements
 Pool for objects. More...
 

Detailed Description

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

Creates objects in a shared memory region.

Todo:
Should this class use the ordered_malloc family of methods to allow better use of memory blocks (but is slightly slower for a large number of allocations)

Definition at line 70 of file ObjectPool.h.

Constructor & Destructor Documentation

◆ ObjectPool()

template<class ElementType, typename KeyType, bool AutoCleanupObjects = true>
Leviathan::ObjectPool< ElementType, KeyType, AutoCleanupObjects >::ObjectPool ( )
inline
Todo:
Figure out the optimal value for the Elements constructor (initial size)

Definition at line 73 of file ObjectPool.h.

73 : Elements(sizeof(ElementType), 16) {}
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ ~ObjectPool()

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

Definition at line 75 of file ObjectPool.h.

76  {
77 
78  if(!AutoCleanupObjects)
79  return;
80 
81  Clear();
82  }
void Clear()
Clears the index and replaces the pool with a new one.
Definition: ObjectPool.h:206

Member Function Documentation

◆ Call()

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

187  {
188 
189  for(auto iter = Index.begin(); iter != Index.end();) {
190 
191  if(function(*iter->second, iter->first)) {
192 
193  iter->second->~ElementType();
194  Elements.free(iter->second);
195  iter = Index.erase(iter);
196 
197  } else {
198 
199  ++iter;
200  }
201  }
202  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ Clear()

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

207  {
208 
209  for(auto iter = Index.begin(); iter != Index.end(); ++iter) {
210 
211  iter->second->~ElementType();
212  Elements.free(iter->second);
213  }
214 
215  Index.clear();
216  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ ConstructNew()

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

88  {
89 
90  if(Find(forentity))
91  throw Exception("Entity with ID already has object in pool of this type");
92 
93  // Get memory to hold the object //
94  void* memoryForObject = Elements.malloc();
95 
96  if(!memoryForObject)
97  throw Exception("Out of memory for element");
98 
99  // Construct the object //
100  ElementType* created;
101  try {
102  created = new(memoryForObject) ElementType(std::forward<Args>(args)...);
103  } catch(...) {
104 
105  Elements.free(memoryForObject);
106  throw;
107  }
108 
109  // Add to index for finding later //
110  Index.insert(std::make_pair(forentity, created));
111  return created;
112  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:134
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ Destroy()

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

Destroys a component based on id.

Definition at line 146 of file ObjectPool.h.

147  {
148 
149  auto object = Find(id);
150 
151  if(!object)
152  throw InvalidArgument("ID is not in index");
153 
154  object->~ElementType();
155  Elements.free(object);
156 
157  RemoveFromIndex(id);
158  RemoveFromAdded(id);
159  }
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:134
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn&#39;t destruct it.
Definition: ObjectPool.h:236
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ Find()

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

Definition at line 134 of file ObjectPool.h.

135  {
136 
137  auto iter = Index.find(id);
138 
139  if(iter == Index.end())
140  return nullptr;
141 
142  return iter->second;
143  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254

◆ GetIndex()

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

228  {
229 
230  return Index;
231  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254

◆ GetObjectCount()

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

Definition at line 218 of file ObjectPool.h.

219  {
220 
221  return Index.size();
222  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254

◆ Release()

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

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

Definition at line 116 of file ObjectPool.h.

117  {
118 
119  auto* object = Find(entity);
120 
121  if(!object)
122  throw NotFound("entity not in pool");
123 
124  object->Release(std::forward<Args>(args)...);
125 
126  object->~ElementType();
127  Elements.free(object);
128 
129  RemoveFromIndex(entity);
130  RemoveFromAdded(entity);
131  }
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:134
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn&#39;t destruct it.
Definition: ObjectPool.h:236
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ RemoveBasedOnKeyTupleList()

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

Destroys without releasing elements based on ids in vector.

Definition at line 163 of file ObjectPool.h.

164  {
165  for(auto iter = values.begin(); iter != values.end(); ++iter) {
166 
167  auto todelete = Index.find(std::get<1>(*iter));
168 
169  if(todelete == Index.end())
170  continue;
171 
172  todelete->second->~ElementType();
173  Elements.free(todelete->second);
174 
175  Index.erase(todelete);
176  }
177  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:257

◆ RemoveFromIndex()

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

237  {
238 
239  auto end = Index.end();
240  for(auto iter = Index.begin(); iter != end; ++iter) {
241 
242  if(iter->first == id) {
243 
244  Index.erase(iter);
245  return true;
246  }
247  }
248 
249  return false;
250  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:254

Member Data Documentation

◆ Elements

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

Pool for objects.

Definition at line 257 of file ObjectPool.h.

◆ Index

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

Used for looking up element belonging to id.

Definition at line 254 of file ObjectPool.h.


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