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

typedef std::unique_ptr< T, ExternalDeleterPtrType
 

Public Member Functions

 SharedPool ()
 
virtual ~SharedPool ()
 
void add (std::unique_ptr< T > t)
 
template<typename... Args>
PtrType 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 42 of file SharedPool.h.

Member Typedef Documentation

◆ PtrType

template<class T >
typedef std::unique_ptr<T, ExternalDeleter> MooseUtils::SharedPool< T >::PtrType

Definition at line 70 of file SharedPool.h.

Constructor & Destructor Documentation

◆ SharedPool()

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

Definition at line 72 of file SharedPool.h.

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

◆ ~SharedPool()

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

Definition at line 73 of file SharedPool.h.

73 {}

Member Function Documentation

◆ acquire()

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

Definition at line 78 of file SharedPool.h.

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

◆ add()

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

Definition at line 75 of file SharedPool.h.

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

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

◆ empty()

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

Definition at line 99 of file SharedPool.h.

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

◆ num_created()

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

Definition at line 103 of file SharedPool.h.

103 { return _num_created; }

◆ size()

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

Definition at line 101 of file SharedPool.h.

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

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 106 of file SharedPool.h.

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


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