#include "ObjectPool.h" template const int ObjectPool::kDefaultChunkSize = 10; template ObjectPool::ObjectPool(int chunkSize) throw(invalid_argument, bad_alloc) : mChunkSize(chunkSize) { if (mChunkSize <= 0) { throw invalid_argument("chunk size must be positive"); } // Create mChunkSize objects to start allocateChunk(); } // // Allocates an array of mChunkSize objects because that's // more efficient than allocating each of them individually. // Stores a pointer to the first element of the array in the mAllObjects // vector. Adds a pointer to each new object to the mFreeList. // template void ObjectPool::allocateChunk() { T *newObjects = new T[mChunkSize]; mAllObjects.push_back(newObjects); for (int i = 0; i < mChunkSize; i++) { mFreeList.push(&newObjects[i]); } } // // Freeing function for use in the for_each algorithm in the // destructor. // template void ObjectPool::arrayDeleteObject(T *obj) { delete [] obj; } template ObjectPool::~ObjectPool() { // free each of the allocation chunks for_each(mAllObjects.begin(), mAllObjects.end(), arrayDeleteObject); } template T &ObjectPool::acquireObject() { if (mFreeList.empty()) { allocateChunk(); } T *obj = mFreeList.front(); mFreeList.pop(); return (*obj); } template void ObjectPool::releaseObject(T &obj) { mFreeList.push(&obj); }