AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::ResourcePool< T, C, make_ptr > Class Template Reference

Generic resource pool utility class. More...

#include <resourcePool.hpp>

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

Public Member Functions

 ResourcePool (const C &config, std::size_t maxSize, std::size_t initialSize=0, std::chrono::milliseconds timeout=std::chrono::milliseconds(100))
 ResourcePool (const ResourcePool &)=delete
 ResourcePool (ResourcePool &&)=delete
 ~ResourcePool ()
ResourcePool & operator= (const ResourcePool &)=delete
ResourcePool & operator= (ResourcePool &&)=delete
anch::PoolableResource< T > borrowResource ()
void setTimeout (std::chrono::milliseconds timeout)

Detailed Description

template<anch::Validable 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, outside the pool lock.
If no resource is available, pool will wait until timeout (default to 100ms) is reached.
Waiting borrowers are served in arrival order (FIFO): a borrower can not be overtaken by later ones, so waiting times stay bounded under contention.
Resources are automatically released through their destructor: release() is called on resources which implement it (Releasable), then invalid resources are destroyed and will be replaced on demand.
If you have only one action to do, you can use pool.borrowResource().get().doAction() ; otherwise you should to keep anch::PoolableResource until all actions have been done (with auto res = pool.borrowResource()).
Borrowed resources can outlive their pool: they will be destroyed when released.
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

◆ ResourcePool() [1/3]

template<anch::Validable 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 initialSize = 0,
std::chrono::milliseconds timeout = std::chrono::milliseconds(100) )

ResourcePool constructor
Resources which can not be created at initialization will be created on demand.

Parameters
configthe resource configuration
maxSizethe pool maximum size
initialSizethe initialize size
timeoutthe timeout
Exceptions
std::invalid_argumentwhen maxSize is 0

◆ ResourcePool() [2/3]

template<anch::Validable 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

◆ ResourcePool() [3/3]

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

Prohibits ResourcePool move constructor

◆ ~ResourcePool()

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

ResourcePool destructor
Destroy every available resource. Borrowed resources will be destroyed when released.

Member Function Documentation

◆ borrowResource()

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

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)

◆ operator=() [1/2]

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

Prohibits ResourcePool copy assignment

◆ operator=() [2/2]

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

Prohibits ResourcePool move assignment

◆ setTimeout()

template<anch::Validable T, typename C, std::shared_ptr< T >(*)(const C &) make_ptr = std::make_shared<T>>
void anch::ResourcePool< T, C, make_ptr >::setTimeout ( std::chrono::milliseconds timeout)

Timeout setter

Parameters
timeoutthe timeout to set

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