AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Classes | Public Member Functions | List of all members
anch::ResourcePool< T, C, make_ptr > Class Template Reference

Generic resource pool utility class. More...

#include <resourcePool.hpp>

Collaboration diagram for anch::ResourcePool< T, C, make_ptr >:

Public Member Functions

 ResourcePool (const C &config, std::size_t maxSize, std::size_t initialiSize=0, std::chrono::milliseconds timeout=std::chrono::milliseconds(100))
 
 ResourcePool (const ResourcePool &)=delete
 
virtual ~ResourcePool ()
 
PoolableResource borrowResource ()
 
void setTimeout (const std::chrono::milliseconds timeout)
 

Detailed Description

template<typename T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
class anch::ResourcePool< T, C, make_ptr >

Generic resource pool utility class.

Maximum number of resources is mandatory.
Intialize pool size will instanciate resources for imediate use.
Resources will be instanciated with their configuration.
If no resource is available, pool will wait until timeout (default to 100ms) is reached.
Resources are automatically released through their destructor.
If you have only one action to do, you can use pool.borrowResource.get().doAction() ; otherwise you should to keep reference to anch::PoolableResource until all actions have been done (with auto res = pool.borrowResource()).
You can specifiy an std::shared_ptr creation function as third template parameter. By default, std::make_shared<T> will be used. It can be usefull for polymorphism dynamic allocation.

Author
Vincent Lachenal
Since
0.1

Constructor & Destructor Documentation

template<typename T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
anch::ResourcePool< T, C, make_ptr >::ResourcePool ( const C &  config,
std::size_t  maxSize,
std::size_t  initialiSize = 0,
std::chrono::milliseconds  timeout = std::chrono::milliseconds(100) 
)
inline

ResourcePool constructor

Parameters
configthe resource configuration
maxSizethe pool maximum size
initialiSizethe initialize size
timeoutthe timeout
template<typename T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
anch::ResourcePool< T, C, make_ptr >::ResourcePool ( const ResourcePool< T, C, make_ptr > &  )
delete

Prohibits ResourcePool copy constructor

template<typename T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
virtual anch::ResourcePool< T, C, make_ptr >::~ResourcePool ( )
inlinevirtual

ResourcePool destructor
Destroy every resource in pool.

Member Function Documentation

template<typename T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
PoolableResource anch::ResourcePool< T, C, make_ptr >::borrowResource ( )
inline

Retrieve available resource if there is one.
Create a new resource if maximum size has not been reached.

This method will raise TimeoutException if timeout has been reached while waiting available resource or any other exception thrown by the resource constructor.

Returns
the poolable resource (anch::PoolableResource)
template<typename T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
void anch::ResourcePool< T, C, make_ptr >::setTimeout ( const std::chrono::milliseconds  timeout)
inline

Timeout setter

Parameters
timeoutthe timeout to set

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