CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Member Functions | Private Attributes
edm::ReusableObjectHolder< T, Deleter > Class Template Reference

#include <ReusableObjectHolder.h>

Public Types

using deleter_type = Deleter
 

Public Member Functions

void add (std::unique_ptr< T, Deleter > iItem)
 
template<typename F >
std::shared_ptr< TmakeOrGet (F iFunc)
 If there isn't an object already available, creates a new one using iFunc. More...
 
template<typename FM , typename FC >
std::shared_ptr< TmakeOrGetAndClear (FM iMakeFunc, FC iClearFunc)
 
 ReusableObjectHolder ()
 
 ReusableObjectHolder (ReusableObjectHolder &&iOther)
 
std::shared_ptr< TtryToGet ()
 
 ~ReusableObjectHolder ()
 

Private Member Functions

void addBack (std::unique_ptr< T, Deleter > iItem)
 
std::unique_ptr< T, Deleter > makeUnique (std::unique_ptr< T, Deleter > ptr)
 
std::unique_ptr< TmakeUnique (T *ptr)
 

Private Attributes

tbb::concurrent_queue< std::unique_ptr< T, Deleter > > m_availableQueue
 
std::atomic< size_t > m_outstandingObjects
 

Detailed Description

template<class T, class Deleter = std::default_delete<T>>
class edm::ReusableObjectHolder< T, Deleter >

Description: Thread safe way to do create and reuse a group of the same object type.

Usage: This class can be used to safely reuse a series of objects created on demand. The reuse of the objects is safe even across different threads since one can safely call all member functions of this class on the same instance of this class from multiple threads.

This class manages the cache of reusable objects and therefore an instance of this class must live as long as you want the cache to live.

The primary way of using the class it to call makeOrGetAndClear An example use would be

auto objectToUse = holder.makeOrGetAndClear(
[]() { return new MyObject(10); }, //makes new one
[](MyObject* old) {old->reset(); } //resets old one
);

If you always want to set the values you can use makeOrGet

auto objectToUse = holder.makeOrGet(
[]() { return new MyObject(); });
objectToUse->setValue(3);

NOTE: If you hold onto the std::shared_ptr<> until another call to the ReusableObjectHolder, make sure to release the shared_ptr before the call. That way the object you were just using can go back into the cache and be reused for the call you are going to make. An example

std::shared_ptr<MyObject> obj;
while(someCondition()) {
//release object so it can re-enter the cache
obj.release();
obj = holder.makeOrGet([]{ return new MyObject();} );
obj->setValue(someNewValue());
useTheObject(obj);
}

The above example is very contrived, since the better way to do the above is

while(someCondition()) {
auto obj = holder.makeOrGet([]{ return new MyObject();} );
obj->setValue(someNewValue());
useTheObject(obj);
//obj goes out of scope and returns the object to the cache
}

When a custom deleter is used, the deleter type must be the same to all objects. The deleter is allowed to have state that depends on the object. The deleter object is passed along the std::unique_ptr, and is internally kept along the object. The deleter object must be copyable.

Definition at line 80 of file ReusableObjectHolder.h.

Member Typedef Documentation

◆ deleter_type

template<class T, class Deleter = std::default_delete<T>>
using edm::ReusableObjectHolder< T, Deleter >::deleter_type = Deleter

Definition at line 82 of file ReusableObjectHolder.h.

Constructor & Destructor Documentation

◆ ReusableObjectHolder() [1/2]

template<class T, class Deleter = std::default_delete<T>>
edm::ReusableObjectHolder< T, Deleter >::ReusableObjectHolder ( )
inline

Definition at line 84 of file ReusableObjectHolder.h.

◆ ReusableObjectHolder() [2/2]

template<class T, class Deleter = std::default_delete<T>>
edm::ReusableObjectHolder< T, Deleter >::ReusableObjectHolder ( ReusableObjectHolder< T, Deleter > &&  iOther)
inline

Definition at line 85 of file ReusableObjectHolder.h.

86  : m_availableQueue(std::move(iOther.m_availableQueue)), m_outstandingObjects(0) {
87  assert(0 == iOther.m_outstandingObjects);
88  }

◆ ~ReusableObjectHolder()

template<class T, class Deleter = std::default_delete<T>>
edm::ReusableObjectHolder< T, Deleter >::~ReusableObjectHolder ( )
inline

Definition at line 89 of file ReusableObjectHolder.h.

89  {
91  std::unique_ptr<T, Deleter> item;
92  while (m_availableQueue.try_pop(item)) {
93  item.reset();
94  }
95  }

Member Function Documentation

◆ add()

template<class T, class Deleter = std::default_delete<T>>
void edm::ReusableObjectHolder< T, Deleter >::add ( std::unique_ptr< T, Deleter >  iItem)
inline

Adds the item to the cache. Use this function if you know ahead of time how many cached items you will need.

Definition at line 100 of file ReusableObjectHolder.h.

100  {
101  if (nullptr != iItem) {
102  m_availableQueue.push(std::move(iItem));
103  }
104  }

Referenced by edm::ReusableObjectHolder< edm::ESProductHost >::makeOrGet(), edm::ReusableObjectHolder< edm::ESProductHost >::makeOrGetAndClear(), and counter.Counter::register().

◆ addBack()

template<class T, class Deleter = std::default_delete<T>>
void edm::ReusableObjectHolder< T, Deleter >::addBack ( std::unique_ptr< T, Deleter >  iItem)
inlineprivate

Definition at line 156 of file ReusableObjectHolder.h.

156  {
157  m_availableQueue.push(std::move(iItem));
159  }

◆ makeOrGet()

template<class T, class Deleter = std::default_delete<T>>
template<typename F >
std::shared_ptr<T> edm::ReusableObjectHolder< T, Deleter >::makeOrGet ( F  iFunc)
inline

◆ makeOrGetAndClear()

template<class T, class Deleter = std::default_delete<T>>
template<typename FM , typename FC >
std::shared_ptr<T> edm::ReusableObjectHolder< T, Deleter >::makeOrGetAndClear ( FM  iMakeFunc,
FC  iClearFunc 
)
inline

If there is an object already available, passes the object to iClearFunc and then returns the object. If there is not an object already available, creates a new one using iMakeFunc

Definition at line 138 of file ReusableObjectHolder.h.

138  {
139  std::shared_ptr<T> returnValue;
140  while (!(returnValue = tryToGet())) {
141  add(makeUnique(iMakeFunc()));
142  }
143  iClearFunc(returnValue.get());
144  return returnValue;
145  }

◆ makeUnique() [1/2]

template<class T, class Deleter = std::default_delete<T>>
std::unique_ptr<T, Deleter> edm::ReusableObjectHolder< T, Deleter >::makeUnique ( std::unique_ptr< T, Deleter >  ptr)
inlineprivate

Definition at line 154 of file ReusableObjectHolder.h.

154 { return ptr; }

◆ makeUnique() [2/2]

template<class T, class Deleter = std::default_delete<T>>
std::unique_ptr<T> edm::ReusableObjectHolder< T, Deleter >::makeUnique ( T ptr)
inlineprivate

Definition at line 148 of file ReusableObjectHolder.h.

148  {
149  static_assert(std::is_same_v<Deleter, std::default_delete<T>>,
150  "Generating functions returning raw pointers are supported only with std::default_delete<T>");
151  return std::unique_ptr<T>{ptr};
152  }

Referenced by edm::ReusableObjectHolder< edm::ESProductHost >::makeOrGet(), and edm::ReusableObjectHolder< edm::ESProductHost >::makeOrGetAndClear().

◆ tryToGet()

template<class T, class Deleter = std::default_delete<T>>
std::shared_ptr<T> edm::ReusableObjectHolder< T, Deleter >::tryToGet ( )
inline

Tries to get an already created object, if none are available, returns an empty shared_ptr. Use this function in conjunction with add()

Definition at line 109 of file ReusableObjectHolder.h.

109  {
110  std::unique_ptr<T, Deleter> item;
111  m_availableQueue.try_pop(item);
112  if (nullptr == item) {
113  return std::shared_ptr<T>{};
114  }
115  //instead of deleting, hand back to queue
116  auto pHolder = this;
117  auto deleter = item.get_deleter();
119  return std::shared_ptr<T>{item.release(), [pHolder, deleter](T* iItem) {
120  pHolder->addBack(std::unique_ptr<T, Deleter>{iItem, deleter});
121  }};
122  }

Referenced by edm::ReusableObjectHolder< edm::ESProductHost >::makeOrGet(), and edm::ReusableObjectHolder< edm::ESProductHost >::makeOrGetAndClear().

Member Data Documentation

◆ m_availableQueue

template<class T, class Deleter = std::default_delete<T>>
tbb::concurrent_queue<std::unique_ptr<T, Deleter> > edm::ReusableObjectHolder< T, Deleter >::m_availableQueue
private

◆ m_outstandingObjects

template<class T, class Deleter = std::default_delete<T>>
std::atomic<size_t> edm::ReusableObjectHolder< T, Deleter >::m_outstandingObjects
private
edm::ReusableObjectHolder::makeUnique
std::unique_ptr< T > makeUnique(T *ptr)
Definition: ReusableObjectHolder.h:148
deleter
Definition: QcdLowPtDQM.cc:34
edm::ReusableObjectHolder::m_outstandingObjects
std::atomic< size_t > m_outstandingObjects
Definition: ReusableObjectHolder.h:162
cms::cuda::assert
assert(be >=bs)
edm::ReusableObjectHolder::add
void add(std::unique_ptr< T, Deleter > iItem)
Definition: ReusableObjectHolder.h:100
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
edm::ReusableObjectHolder::m_availableQueue
tbb::concurrent_queue< std::unique_ptr< T, Deleter > > m_availableQueue
Definition: ReusableObjectHolder.h:161
eostools.move
def move(src, dest)
Definition: eostools.py:511
T
long double T
Definition: Basic3DVectorLD.h:48
edm::ReusableObjectHolder::tryToGet
std::shared_ptr< T > tryToGet()
Definition: ReusableObjectHolder.h:109