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 93 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 97 of file ObjectPool.h.

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

◆ ~ObjectPool()

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

Definition at line 102 of file ObjectPool.h.

103  {
104  if(!AutoCleanupObjects)
105  return;
106 
107  Clear();
108  }
void Clear()
Clears the index and replaces the pool with a new one.
Definition: ObjectPool.h:249

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 225 of file ObjectPool.h.

226  {
227  for(auto iter = Index.begin(); iter != Index.end();) {
228 
229  if(function(*iter->second, iter->first)) {
230 
231 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
232  iter->second->~ElementType();
233  Elements.free(iter->second);
234 #else
235  delete iter->second;
236 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
237 
238  iter = Index.erase(iter);
239 
240  } else {
241 
242  ++iter;
243  }
244  }
245  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:301

◆ 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 249 of file ObjectPool.h.

250  {
251  for(auto iter = Index.begin(); iter != Index.end(); ++iter) {
252 
253 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
254  iter->second->~ElementType();
255  Elements.free(iter->second);
256 #else
257  delete iter->second;
258 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
259  }
260 
261  Index.clear();
262  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:301

◆ 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 113 of file ObjectPool.h.

114  {
115  if(Find(forentity))
116  throw Exception("Entity with ID already has object in pool of this type");
117 
118 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
119  // Get memory to hold the object //
120  void* memoryForObject = Elements.malloc();
121 
122  if(!memoryForObject)
123  throw Exception("Out of memory for element");
124 
125  // Construct the object //
126  ElementType* created;
127  try {
128  created = new(memoryForObject) ElementType(std::forward<Args>(args)...);
129  } catch(...) {
130 
131  Elements.free(memoryForObject);
132  throw;
133  }
134 #else
135  // Construct the object //
136  ElementType* created = new ElementType(std::forward<Args>(args)...);
137 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
138 
139  // Add to index for finding later //
140  Index.insert(std::make_pair(forentity, created));
141  return created;
142  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:167
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:301

◆ 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 178 of file ObjectPool.h.

179  {
180  auto object = Find(id);
181 
182  if(!object)
183  throw InvalidArgument("ID is not in index");
184 
185 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
186  object->~ElementType();
187  Elements.free(object);
188 #else
189  delete object;
190 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
191 
192  RemoveFromIndex(id);
193  RemoveFromAdded(id);
194  }
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:167
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn't destruct it.
Definition: ObjectPool.h:280
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:301

◆ 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 167 of file ObjectPool.h.

168  {
169  auto iter = Index.find(id);
170 
171  if(iter == Index.end())
172  return nullptr;
173 
174  return iter->second;
175  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297

◆ 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 272 of file ObjectPool.h.

273  {
274  return Index;
275  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297

◆ GetObjectCount()

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

Definition at line 264 of file ObjectPool.h.

265  {
266  return Index.size();
267  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297

◆ 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 146 of file ObjectPool.h.

147  {
148  auto* object = Find(entity);
149 
150  if(!object)
151  throw NotFound("entity not in pool");
152 
153  object->Release(std::forward<Args>(args)...);
154 
155 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
156  object->~ElementType();
157  Elements.free(object);
158 #else
159  delete object;
160 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
161 
162  RemoveFromIndex(entity);
163  RemoveFromAdded(entity);
164  }
ElementType * Find(KeyType id) const
Definition: ObjectPool.h:167
bool RemoveFromIndex(KeyType id)
Removes an component from the index but doesn't destruct it.
Definition: ObjectPool.h:280
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:301

◆ 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 198 of file ObjectPool.h.

199  {
200  for(auto iter = values.begin(); iter != values.end(); ++iter) {
201 
202  auto todelete = Index.find(std::get<1>(*iter));
203 
204  if(todelete == Index.end())
205  continue;
206 
207 #ifdef LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
208  todelete->second->~ElementType();
209  Elements.free(todelete->second);
210 #else
211  delete todelete->second;
212 #endif // LEVIATHAN_USE_ACTUAL_OBJECT_POOLS
213 
214  Index.erase(todelete);
215  }
216  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297
boost::pool Elements
Pool for objects.
Definition: ObjectPool.h:301

◆ 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 280 of file ObjectPool.h.

281  {
282  auto end = Index.end();
283  for(auto iter = Index.begin(); iter != end; ++iter) {
284 
285  if(iter->first == id) {
286 
287  Index.erase(iter);
288  return true;
289  }
290  }
291 
292  return false;
293  }
std::unordered_map< KeyType, ElementType * > Index
Used for looking up element belonging to id.
Definition: ObjectPool.h:297

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 301 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 297 of file ObjectPool.h.


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