DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
DNDS::ObjectPool< T > Class Template Reference

Generic object pool: caches unique_ptr<T> instances and hands them out with RAII return-on-destruction semantics. More...

#include <ObjectPool.hpp>

Collaboration diagram for DNDS::ObjectPool< T >:
[legend]

Classes

class  ObjectPoolAllocated
 RAII handle returned by ObjectPoolget (and friends). More...
 
struct  Pool
 Internal storage shared among all handles. More...
 

Public Types

using uPtrResource = std::unique_ptr< T >
 

Public Member Functions

 ObjectPool ()
 Construct an empty pool; populate with resize.
 
size_t size ()
 Number of objects currently available (not checked out).
 
template<class... _CtorArgs>
void resize (size_t N, _CtorArgs &&...__ctorArgs)
 Pre-allocate N objects, forwarding __ctorArgs to each ctor. Clears any previously pooled instances.
 
template<class TFInit , class... _CtorArgs>
void resizeInit (size_t N, TFInit &&FInit, _CtorArgs &&...__ctorArgs)
 Like resize but calls FInit(obj) on each newly-created object.
 
ObjectPoolAllocated get ()
 Take an object out of the pool. Returns an empty handle if exhausted.
 
template<class... _CtorArgs>
ObjectPoolAllocated getAlloc (_CtorArgs &&...__ctorArgs)
 Like get, but allocates a brand-new object if the pool is empty.
 
template<class TFInit , class... _CtorArgs>
ObjectPoolAllocated getAllocInit (TFInit &&FInit, _CtorArgs &&...__ctorArgs)
 Like getAlloc, but additionally runs FInit(obj) on newly-allocated objects.
 

Public Attributes

std::shared_ptr< PoolpPool
 Shared pointer to the underlying storage.
 

Detailed Description

template<class T = int>
class DNDS::ObjectPool< T >

Generic object pool: caches unique_ptr<T> instances and hands them out with RAII return-on-destruction semantics.

Intended for expensive-to-construct objects used in hot loops (e.g., Eigen solvers, pre-allocated scratch matrices). Typical pattern:

pool.resize(nThreads, ctorArgs...);
{
auto handle = pool.get(); // returns from pool, or empty if exhausted
handle->doWork();
} // handle goes out of scope -> worker returned to pool
Generic object pool: caches unique_ptr<T> instances and hands them out with RAII return-on-destructio...
void resize(size_t N, _CtorArgs &&...__ctorArgs)
Pre-allocate N objects, forwarding __ctorArgs to each ctor. Clears any previously pooled instances.
ObjectPoolAllocated get()
Take an object out of the pool. Returns an empty handle if exhausted.

The pool shares ownership with every outstanding ObjectPoolAllocated via a shared_ptr<Pool>; workers correctly skip the return step if the pool itself has been destroyed meanwhile.

Template Parameters
TPooled object type.

Definition at line 32 of file ObjectPool.hpp.

Member Typedef Documentation

◆ uPtrResource

template<class T = int>
using DNDS::ObjectPool< T >::uPtrResource = std::unique_ptr<T>

Definition at line 35 of file ObjectPool.hpp.

Constructor & Destructor Documentation

◆ ObjectPool()

template<class T = int>
DNDS::ObjectPool< T >::ObjectPool ( )
inline

Construct an empty pool; populate with resize.

Definition at line 51 of file ObjectPool.hpp.

Member Function Documentation

◆ get()

template<class T = int>
ObjectPoolAllocated DNDS::ObjectPool< T >::get ( )
inline

Take an object out of the pool. Returns an empty handle if exhausted.

Definition at line 135 of file ObjectPool.hpp.

◆ getAlloc()

template<class T = int>
template<class... _CtorArgs>
ObjectPoolAllocated DNDS::ObjectPool< T >::getAlloc ( _CtorArgs &&...  __ctorArgs)
inline

Like get, but allocates a brand-new object if the pool is empty.

Definition at line 148 of file ObjectPool.hpp.

◆ getAllocInit()

template<class T = int>
template<class TFInit , class... _CtorArgs>
ObjectPoolAllocated DNDS::ObjectPool< T >::getAllocInit ( TFInit &&  FInit,
_CtorArgs &&...  __ctorArgs 
)
inline

Like getAlloc, but additionally runs FInit(obj) on newly-allocated objects.

Definition at line 162 of file ObjectPool.hpp.

◆ resize()

template<class T = int>
template<class... _CtorArgs>
void DNDS::ObjectPool< T >::resize ( size_t  N,
_CtorArgs &&...  __ctorArgs 
)
inline

Pre-allocate N objects, forwarding __ctorArgs to each ctor. Clears any previously pooled instances.

Definition at line 109 of file ObjectPool.hpp.

◆ resizeInit()

template<class T = int>
template<class TFInit , class... _CtorArgs>
void DNDS::ObjectPool< T >::resizeInit ( size_t  N,
TFInit &&  FInit,
_CtorArgs &&...  __ctorArgs 
)
inline

Like resize but calls FInit(obj) on each newly-created object.

Definition at line 122 of file ObjectPool.hpp.

◆ size()

template<class T = int>
size_t DNDS::ObjectPool< T >::size ( )
inline

Number of objects currently available (not checked out).

Definition at line 57 of file ObjectPool.hpp.

Member Data Documentation

◆ pPool

template<class T = int>
std::shared_ptr<Pool> DNDS::ObjectPool< T >::pPool

Shared pointer to the underlying storage.

Definition at line 47 of file ObjectPool.hpp.


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