template class Poco::ObjectPool

Overview

An ObjectPool manages a pool of objects of a certain class. Moreā€¦

#include <ObjectPool.h>

template <
    class C,
    class P = C*,
    class F = PoolableObjectFactory<C, P>
    >
class ObjectPool
{
public:
    // construction

    ObjectPool(
        std::size_t capacity,
        std::size_t peakCapacity
        );

    ObjectPool(
        const F& factory,
        std::size_t capacity,
        std::size_t peakCapacity
        );

    // methods

    P
    borrowObject();

    void
    returnObject(P pObject);

    std::size_t
    capacity() const;

    std::size_t
    peakCapacity() const;

    std::size_t
    size() const;

    std::size_t
    available() const;

protected:
    // methods

    P
    activateObject(P pObject);
};

Detailed Documentation

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.

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.

Construction

ObjectPool(
    std::size_t capacity,
    std::size_t peakCapacity
    )

Creates a new ObjectPool with the given capacity and peak capacity.

The PoolableObjectFactory must have a public default constructor.

ObjectPool(
    const F& factory,
    std::size_t capacity,
    std::size_t peakCapacity
    )

Creates a new ObjectPool with the given PoolableObjectFactory, capacity and peak capacity.

The PoolableObjectFactory must have a public copy constructor.

Methods

P
borrowObject()

Obtains an object from the pool, or creates a new object if possible.

Returns null if no object is available.

If activating the object fails, the object is destroyed and the exception is passed on to the caller.

void
returnObject(P pObject)

Returns an object to the pool.