# Poco

# template < class C, class P = C *, class F = PoolableObjectFactory < C, P > >

# class ObjectPool

Library: Foundation
Package: Core
Header: Poco/ObjectPool.h

## Description

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.

## Member Summary

Member Functions: activateObject, available, borrowObject, capacity, peakCapacity, returnObject, size

## Constructors

### ObjectPool

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

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.

## Destructor

### ~ObjectPool

~ObjectPool();

Destroys the ObjectPool.

## Member Functions

### available

std::size_t available() const;

### borrowObject

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.

### capacity

std::size_t capacity() const;

### peakCapacity

std::size_t peakCapacity() const;

### returnObject

void returnObject(
P pObject
);

Returns an object to the pool.

### size

std::size_t size() const;

### activateObject

P activateObject(
P pObject
);