Skip to content

Generic object pool for reusing and recycling object instances.

License

Notifications You must be signed in to change notification settings

cbritopacheco/object_pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published