An ObjectPool manages a pool of objects of a certain class.
The number of objects managed by the pool can be restricted.
When an object is requested from the pool:
- If an object is available from the pool, an object from the pool is removed from the pool, activated (using the factory) and returned.
- Otherwise, if the peak capacity of the pool has not yet been reached, a new object is created and activated, using the object factory, and returned.
- If the peak capacity has already been reached, null is returned after timeout.
When an object is returned to the pool:
- If the object is valid (checked by calling validateObject() from the object factory), the object is deactivated. If the number of objects in the pool is below the capacity, the object is added to the pool. Otherwise it is destroyed.
- If the object is not valid, it is destroyed immediately.
Creates a new ObjectPool with the given capacity and peak capacity.
The PoolableObjectFactory must have a public default constructor.
const F & factory,
Destroys the ObjectPool.
std::size_t available() const;
long timeoutMilliseconds = 0
Obtains an object from the pool, or creates a new object if possible.
Returns null if no object is available after timeout.
If activating the object fails, the object is destroyed and the exception is passed on to the caller.
std::size_t capacity() const;
std::size_t peakCapacity() const;
Returns an object to the pool.
std::size_t size() const;