commons::pool< T > Class Template Reference

A fixed-size pool of resources. More...

#include <pool.h>

List of all members.

Public Member Functions

 pool (int size)
 Create a pool of size size.
T * take ()
 Take an item from the pool.
void drop (T *p)
 Release an item back into the pool.
size_t size ()
 Get the number of (remaining) items in the pool.


Detailed Description

template<typename T>
class commons::pool< T >

A fixed-size pool of resources.

Definition at line 15 of file pool.h.


Constructor & Destructor Documentation

template<typename T>
commons::pool< T >::pool ( int  size  )  [inline]

Create a pool of size size.

These resources are created and stored in an array.

Parameters:
[in] size The number of elements to create.

Definition at line 22 of file pool.h.

00022                        : xs(size), ps(size) {
00023           for (size_t i = 0; i < xs.size(); i++)
00024             ps.push_back(&xs[i]);
00025         }


Member Function Documentation

template<typename T>
T* commons::pool< T >::take (  )  [inline]

Take an item from the pool.

Exceptions:
exception The pool is empty.

Definition at line 31 of file pool.h.

00031                   {
00032           check(!ps.empty(), "taking from empty pool");
00033           T *p = ps[0];
00034           ps.pop_front();
00035           return p;
00036         }

template<typename T>
void commons::pool< T >::drop ( T *  p  )  [inline]

Release an item back into the pool.

This doesn't check that the pointer being released was one that was originally taken from this pool.

Parameters:
[in] p The pointer to release.
Exceptions:
exception The pool is full.

Definition at line 44 of file pool.h.

00044                         {
00045           check(ps.size() < ps.max_size(), "dropping into full pool");
00046           ps.push_back(p);
00047         }


The documentation for this class was generated from the following file:

Generated on Mon Mar 2 22:13:23 2009 for C++ Commons by  doxygen 1.5.6