www.mooseframework.org
Classes | Public Types | Public Member Functions | Private Attributes | List of all members
MooseUtils::SharedPool< T > Class Template Reference

Originally From https://stackoverflow.com/a/27837534/2042320. More...

#include <SharedPool.h>

Classes

struct  ExternalDeleter
 

Public Types

using ptr_type = std::unique_ptr< T, ExternalDeleter >
 

Public Member Functions

 SharedPool ()
 
virtual ~SharedPool ()
 
void add (std::unique_ptr< T > t)
 
template<typename... Args>
ptr_type acquire (Args &&... args)
 
bool empty () const
 
size_t size () const
 
size_t num_created () const
 

Private Attributes

std::shared_ptr< SharedPool< T > * > _this_ptr
 
std::stack< std::unique_ptr< T > > _pool
 
size_t _num_created = 0
 

Detailed Description

template<class T>
class MooseUtils::SharedPool< T >

Originally From https://stackoverflow.com/a/27837534/2042320.

friedmud added variadic templated perfect forwarding to acquire()

For an object to be resetable it needs to define a reset() function that takes the same arguments as its constructor.

Definition at line 43 of file SharedPool.h.

Member Typedef Documentation

◆ ptr_type

template<class T >
using MooseUtils::SharedPool< T >::ptr_type = std::unique_ptr<T, ExternalDeleter>

Definition at line 71 of file SharedPool.h.

Constructor & Destructor Documentation

◆ SharedPool()

template<class T >
MooseUtils::SharedPool< T >::SharedPool ( )
inline

Definition at line 73 of file SharedPool.h.

73 : _this_ptr(new SharedPool<T> *(this)) {}
std::shared_ptr< SharedPool< T > * > _this_ptr
Definition: SharedPool.h:107

◆ ~SharedPool()

template<class T >
virtual MooseUtils::SharedPool< T >::~SharedPool ( )
inlinevirtual

Definition at line 74 of file SharedPool.h.

74 {}

Member Function Documentation

◆ acquire()

template<class T >
template<typename... Args>
ptr_type MooseUtils::SharedPool< T >::acquire ( Args &&...  args)
inline

Definition at line 79 of file SharedPool.h.

80  {
81  // if the pool is empty - create one
82  if (_pool.empty())
83  {
84  _num_created++;
85  return std::move(ptr_type(new T(std::forward<Args>(args)...),
86  ExternalDeleter{std::weak_ptr<SharedPool<T> *>{_this_ptr}}));
87  }
88  else
89  {
90  ptr_type tmp(_pool.top().release(),
91  ExternalDeleter{std::weak_ptr<SharedPool<T> *>{_this_ptr}});
92  _pool.pop();
93 
94  reset(1, *tmp, std::forward<Args>(args)...);
95 
96  return std::move(tmp);
97  }
98  }
std::stack< std::unique_ptr< T > > _pool
Definition: SharedPool.h:108
auto reset(int, T &obj, Args... args) -> decltype(obj.reset(args...), void())
Definition: SharedPool.h:22
std::unique_ptr< T, ExternalDeleter > ptr_type
Definition: SharedPool.h:71
std::shared_ptr< SharedPool< T > * > _this_ptr
Definition: SharedPool.h:107

◆ add()

template<class T >
void MooseUtils::SharedPool< T >::add ( std::unique_ptr< T >  t)
inline

Definition at line 76 of file SharedPool.h.

Referenced by MooseUtils::SharedPool< T >::ExternalDeleter::operator()().

76 { _pool.push(std::move(t)); }
std::stack< std::unique_ptr< T > > _pool
Definition: SharedPool.h:108

◆ empty()

template<class T >
bool MooseUtils::SharedPool< T >::empty ( ) const
inline

Definition at line 100 of file SharedPool.h.

100 { return _pool.empty(); }
std::stack< std::unique_ptr< T > > _pool
Definition: SharedPool.h:108

◆ num_created()

template<class T >
size_t MooseUtils::SharedPool< T >::num_created ( ) const
inline

Definition at line 104 of file SharedPool.h.

104 { return _num_created; }

◆ size()

template<class T >
size_t MooseUtils::SharedPool< T >::size ( ) const
inline

Definition at line 102 of file SharedPool.h.

102 { return _pool.size(); }
std::stack< std::unique_ptr< T > > _pool
Definition: SharedPool.h:108

Member Data Documentation

◆ _num_created

template<class T >
size_t MooseUtils::SharedPool< T >::_num_created = 0
private

◆ _pool

template<class T >
std::stack<std::unique_ptr<T> > MooseUtils::SharedPool< T >::_pool
private

◆ _this_ptr

template<class T >
std::shared_ptr<SharedPool<T> *> MooseUtils::SharedPool< T >::_this_ptr
private

Definition at line 107 of file SharedPool.h.

Referenced by MooseUtils::SharedPool< T >::acquire().


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