LPCGame
A 2d game 'engine' written in C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator
ObjectPool< T > Class Template Reference

An object pool. More...

#include <objectpool.h>

Public Member Functions

 ObjectPool (size_t chunkSize=kDefaultChunkSize) throw (std::invalid_argument, std::bad_alloc)
 
std::shared_ptr< T > AcquireObject ()
 
void ReturnObject (std::shared_ptr< T > obj)
 

Protected Member Functions

void AllocateChunk ()
 Allocate mChunkSize objects for the pool to use.
 

Protected Attributes

std::queue< std::shared_ptr< T > > mFreeList
 The list of objects not currently in use.
 
size_t mChunkSize
 

Static Protected Attributes

static const size_t kDefaultChunkSize = 10
 

Detailed Description

template<typename T>
class ObjectPool< T >

An object pool.

An object pool where users can request objects, run them, and return them too the pool

Constructor & Destructor Documentation

template<typename T >
ObjectPool< T >::ObjectPool ( size_t  chunkSize = kDefaultChunkSize) throw (std::invalid_argument, std::bad_alloc)

Initialize an object pool with chunkSize objects

Exceptions
invalid_argumentif chunksize is 0
bad_allocif allocation fails

Member Function Documentation

template<typename T >
std::shared_ptr< T > ObjectPool< T >::AcquireObject ( )

Reserve an object for use Note: Users must NOT free the object

Returns
The object's shared pointer
template<typename T >
void ObjectPool< T >::AllocateChunk ( )
protected

Allocate mChunkSize objects for the pool to use.

template<typename T >
void ObjectPool< T >::ReturnObject ( std::shared_ptr< T >  obj)

Return an object to the pool Note: Users must NOT use the object after returning

Parameters
objThe object to return to the pool

Member Data Documentation

template<typename T>
const size_t ObjectPool< T >::kDefaultChunkSize = 10
staticprotected
template<typename T>
size_t ObjectPool< T >::mChunkSize
protected
template<typename T>
std::queue<std::shared_ptr<T> > ObjectPool< T >::mFreeList
protected

The list of objects not currently in use.


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