Shadowrun: Awakened 29 September 2011 - Build 871
ObjectPool.h
Go to the documentation of this file.
00001 #ifndef OBJECTPOOL_H
00002 #define OBJECTPOOL_H
00003 
00004 #include <vector>
00005 
00006 //#include "queuing_mutex.h"
00007 
00008 #include "CodeGenMacros.h"
00009 
00023 template <class ClassType>
00024 class ObjectPool
00025 {
00026 protected:
00027 
00031     std::vector<ClassType*> _instances;
00032 
00036     std::vector<ClassType*> _freeInstances;
00037 
00041     //mutable tbb::queuing_mutex _mutex;
00042 
00043     bool _deleteAllInstances;
00044 
00045     virtual ClassType* createInstance() = 0;
00046 
00047 public:
00048 
00052     ObjectPool(unsigned int initialSize) : 
00053         _instances(initialSize),
00054         _freeInstances(initialSize),
00055         _deleteAllInstances(true)
00056     {
00057         //create the number of instances given in the constructor
00058         ClassType* instance;
00059         for(unsigned int i = 0; i<initialSize; ++i)
00060         {
00061             instance = new ClassType();
00062             _instances.push_back(instance);
00063             recycle(instance);
00064         }
00065     }
00066 
00070     ObjectPool() : _deleteAllInstances(true)
00071     {
00072     }
00073 
00078     virtual ~ObjectPool()
00079     {
00080         if(_deleteAllInstances)
00081         {
00082             //delete all instances, even those that are not free
00083             FOREACH(iter, std::vector<ClassType*>, _instances)
00084                 delete (*iter);
00085         }
00086         else
00087         {
00088             //delete only instances currently in the pool
00089             FOREACH(iter, std::vector<ClassType*>, _freeInstances)
00090                 delete (*iter);
00091         }
00092     }
00093 
00094     //controls whether or not all instances allocated by the pool are deleted at destruction time
00095     //of if only those currently in the pool are deleted
00096     INLINE GET(getDeleteAllInstances, bool, _deleteAllInstances)
00097     INLINE SET(setDeleteAllInstances, bool, _deleteAllInstances)
00098 
00103     virtual ClassType* getInstance()
00104     {
00105         //tbb::queuing_mutex::scoped_lock lock(_mutex);
00106 
00107         ClassType* ret = NULL;
00108         //for thread safety, add lock around this later
00109         if(_freeInstances.size() > 0)
00110         {
00111             ret = _freeInstances.back();
00112             _freeInstances.pop_back();
00113         }
00114 
00115         //if we didn't have an instance in the pool, make a new one
00116         if(ret == NULL)
00117         {
00118             //make a new instances and save it in the instances list
00119             ret = createInstance();
00120             _instances.push_back(ret);
00121         }
00122 
00123         return ret;
00124     }
00125 
00129     virtual void recycle(ClassType* instance)
00130     {
00131         //tbb::queuing_mutex::scoped_lock lock(_mutex);
00132         //for thread safety, lock around this statement
00133         _freeInstances.push_back(instance);
00134     }
00135 };
00136 
00137 #endif

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