ObjectPool.h
Classes
- ObjectPool -- A parameterized stack of re-usable objects (full description)
template <class T, class Key> class ObjectPool
Interface
- Public Members
- ObjectPool()
- ~ObjectPool()
- T *get(const Key key=Key())
- PoolStack<T, Key> &getStack(const Key key)
- void release(T *obj, const Key key=Key())
- uInt nelements() const
- void clearStacks()
- void clearStack(const Key key=Key())
- void clear()
- Private Members
- ObjectPool(const ObjectPool<T, Key> &other)
- ObjectPool<T, Key> &operator=(const ObjectPool<T, Key> &other)
Review Status
- Reviewed By:
- Ger van Diepen
- Date Reviewed:
- 2001/07/04
- Programs:
- Tests:
Prerequisite
Synopsis
An ObjectPool contains a set of pre-allocated Objects of the type
T. A Map based on the Key values contains
a stack of objects for each key value.
As an example, a <Vector<Double>, uInt> ObjectPool contains
a SimpleOrderedMap<uInt,PoolStack<Vector<Double>,uInt>* >
map. Each Stack will contain a stack of Vector<Double>
objects, with a length of the key value each.
When an object is asked for with the get method, the
correct stack is found using the parameter key. If no entry in the map
exists, a new stack is created. If the relevant stack is empty, new elements
are added to the stack.
Example
// Create a pool of vectors
ObjectPool<Vector<Double>, uInt> pool;
// Get a pointer to a pre-defined vector of length 5
Vector<Double> *el5(pool.get(5));
// and one of length 10
Vector<Double> *el10(pool.get(10));
...
// Release the objects for re-use
pool.release(el5, 5);
pool.release(el10, 10);
Motivation
To improve the speed for the auto differentiating class.
Template Type Argument Requirements (T)
- the class T must have a constructor with a Key argument
Template Type Argument Requirements (Key)
- the class Key must be sortable to be used as a key in the Map
To Do
Member Description
Create the pool
Delete the pool
T *get(const Key key=Key())
Get a pointer to an object in the pool with the specified parameter. The
object is detached from the stack, and has to be returned with the
release method. The object should not be deleted by caller.
Get the object stack for the given key
void release(T *obj, const Key key=Key())
Release an object obtained from the pool through get for
re-use.
Get the number of object stacks in the pool
Decimate the stacks by deleting all unused objects.
Decimate the stacks and remove any map entry that is completely unused
ObjectPool(const ObjectPool<T, Key> &other)
ObjectPool<T, Key> &operator=(const ObjectPool<T, Key> &other)
Copy and assignment constructors and assignment (not implemented)