Shadowrun: Awakened 29 September 2011 - Build 871
Public Member Functions | Protected Member Functions | Protected Attributes
ObjectPool< ClassType > Class Template Reference

#include <ObjectPool.h>

Inheritance diagram for ObjectPool< ClassType >:

List of all members.

Public Member Functions

__inline bool getDeleteAllInstances () const
virtual ClassType * getInstance ()
 ObjectPool (unsigned int initialSize)
 ObjectPool ()
virtual void recycle (ClassType *instance)
__inline void setDeleteAllInstances (bool val)
virtual ~ObjectPool ()

Protected Member Functions

virtual ClassType * createInstance ()=0

Protected Attributes

bool _deleteAllInstances
std::vector< ClassType * > _freeInstances
std::vector< ClassType * > _instances

Detailed Description

template<class ClassType>
class ObjectPool< ClassType >

This template class implements an object pool The pooled class must have a default constructor NOTE: By default, the ObjectPool deletes all its instances on destruction, even those currently in use this means users of the class are required to ensure they are done when instances of this class are destroyed This behavior can be changed by calling setDeleteAllInstance(false)

If an object performs a repetitive task each instance, each instance is intended to exist for a short time, perform a small amount of work then use a pooled instance. If an object is intended to exist the entire duration of the application, it is not a good idea to use a pooled instance!

Object pooling usually needs thread safety Object pooling usually needs some means of reducing pool size while idle

Definition at line 24 of file ObjectPool.h.


Constructor & Destructor Documentation

template<class ClassType>
ObjectPool< ClassType >::ObjectPool ( unsigned int  initialSize) [inline]

Constructs pool with initialSize instances ready for use

Definition at line 52 of file ObjectPool.h.

                                         : 
        _instances(initialSize),
        _freeInstances(initialSize),
        _deleteAllInstances(true)
    {
        //create the number of instances given in the constructor
        ClassType* instance;
        for(unsigned int i = 0; i<initialSize; ++i)
        {
            instance = new ClassType();
            _instances.push_back(instance);
            recycle(instance);
        }
    }
template<class ClassType>
ObjectPool< ClassType >::ObjectPool ( ) [inline]

Constructs an empty pool

Definition at line 70 of file ObjectPool.h.

                 : _deleteAllInstances(true)
    {
    }
template<class ClassType>
virtual ObjectPool< ClassType >::~ObjectPool ( ) [inline, virtual]

Destructor Deletes all instances in the pool

Definition at line 78 of file ObjectPool.h.

    {
        if(_deleteAllInstances)
        {
            //delete all instances, even those that are not free
            FOREACH(iter, std::vector<ClassType*>, _instances)
                delete (*iter);
        }
        else
        {
            //delete only instances currently in the pool
            FOREACH(iter, std::vector<ClassType*>, _freeInstances)
                delete (*iter);
        }
    }

Member Function Documentation

template<class ClassType>
virtual ClassType* ObjectPool< ClassType >::createInstance ( ) [protected, pure virtual]
template<class ClassType>
__inline bool ObjectPool< ClassType >::getDeleteAllInstances ( ) const [inline]

Definition at line 96 of file ObjectPool.h.

{
template<class ClassType>
virtual ClassType* ObjectPool< ClassType >::getInstance ( ) [inline, virtual]

Returns an instance from the object pool or, if no instances available, creates a new instance

Definition at line 103 of file ObjectPool.h.

Referenced by Support::TimedEventManager::checkTimers().

    {
        //tbb::queuing_mutex::scoped_lock lock(_mutex);

        ClassType* ret = NULL;
        //for thread safety, add lock around this later
        if(_freeInstances.size() > 0)
        {
            ret = _freeInstances.back();
            _freeInstances.pop_back();
        }

        //if we didn't have an instance in the pool, make a new one
        if(ret == NULL)
        {
            //make a new instances and save it in the instances list
            ret = createInstance();
            _instances.push_back(ret);
        }

        return ret;
    }
template<class ClassType>
virtual void ObjectPool< ClassType >::recycle ( ClassType *  instance) [inline, virtual]

Returns an instance to the object pool

Definition at line 129 of file ObjectPool.h.

Referenced by ObjectPool< TimedTask >::ObjectPool().

    {
        //tbb::queuing_mutex::scoped_lock lock(_mutex);
        //for thread safety, lock around this statement
        _freeInstances.push_back(instance);
    }
template<class ClassType>
__inline void ObjectPool< ClassType >::setDeleteAllInstances ( bool  val) [inline]

Definition at line 97 of file ObjectPool.h.

{

Member Data Documentation

template<class ClassType>
bool ObjectPool< ClassType >::_deleteAllInstances [protected]

Lock on this mutex when modifying either _instances or _freeInstances

Definition at line 43 of file ObjectPool.h.

Referenced by ObjectPool< TimedTask >::~ObjectPool().

template<class ClassType>
std::vector<ClassType*> ObjectPool< ClassType >::_freeInstances [protected]

Vector of all instances currently available inside the pool

Definition at line 36 of file ObjectPool.h.

Referenced by ObjectPool< TimedTask >::getInstance(), ObjectPool< TimedTask >::recycle(), and ObjectPool< TimedTask >::~ObjectPool().

template<class ClassType>
std::vector<ClassType*> ObjectPool< ClassType >::_instances [protected]

Vector of all instances created from within this pool

Definition at line 31 of file ObjectPool.h.

Referenced by ObjectPool< TimedTask >::getInstance(), ObjectPool< TimedTask >::ObjectPool(), and ObjectPool< TimedTask >::~ObjectPool().


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

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net