cbritopacheco / object_pool

Generic object pool for reusing and recycling object instances.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This project is still under development.

The full documentation can be found here.

object_pool

Header-only Build Status Coverage Status Documentation GitHub license

An object_pool is a container which provides shared access to a collection of instances of one object of type T. One only needs to include the header object_pool.hpp to be able to use it. Even more, the interface is designed á la STL for ease of use! Take the following example for demonstration purposes:

#include "object_pool.hpp"

using namespace carlosb;

struct expensive_object
{
	expensive_object()
	{
		/* very expensive construction */
	}
};

int main()
{
	object_pool<expensive_object> pool(10); // pool of 10 objects!

	if (auto obj = pool.acquire())
		doSomething(*obj);
	else
		doSomethingElse();
	
	return 0;
}
// when the object goes out of scope, it will get returned to the pool
// when the pool goes out of scope, it will delete all objects

Basic Example

To illustrate a typical call to an access function we provide the following example:

#include <iostream>
#include "object_pool.hpp"

using namespace std;
using namespace carlosb;

int main()
{
    object_pool<int> pool;  // empty pool
    pool.push(10); // push 10 into pool

    if (auto obj = pool.acquire())
        cout << "We acquired: " << *obj << "\n";
    else
        cout << "We didn't acquire an object" << "\n";

    return 0;
}

Output:

We acquired: 10

License

The software is licensed under the Boost Software License 1.0.

About

Generic object pool for reusing and recycling object instances.

License:Boost Software License 1.0


Languages

Language:C++ 97.9%Language:CMake 1.9%Language:Shell 0.2%