ATLAS Offline Software
RecyclableDataObject.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 // $Id$
5 /*
6  */
7 /**
8  * @file AthenaKernel/RecyclableDataObject.icc
9  * @author scott snyder <snyder@bnl.gov>
10  * @date Oct, 2018
11  * @brief Helper for recycling objects from event to event.
12  */
13 
14 
15 namespace Athena {
16 
17 
18 /**
19  * @brief Constructor.
20  * @param queue Queue on which this object will be entered when it is released.
21  * @param args... Additional arguments to pass to the @c DOBJ constructor.
22  */
23 template <class DOBJ>
24 template <typename... ARGS>
25 RecyclableDataObject<DOBJ>::RecyclableDataObject (std::shared_ptr<queue_t> queue, ARGS&&... args)
26  : DOBJ (std::forward<ARGS>(args)...),
27  m_queue (queue)
28 {
29  // Bump reference count by one so that these objects won't be deleted by StoreGate.
30  this->addRef();
31 }
32 
33 
34 /**
35  * @brief DataObject release method.
36  *
37  * This overrides the @c release() method of @c DataObject.
38  * Once the reference count falls back to 1 (meaning that the object is no longer
39  * referenced from StoreGate), we recycle the object back to the queue.
40  */
41 template <class DOBJ>
42 unsigned long RecyclableDataObject<DOBJ>::release()
43 {
44  unsigned long cnt = DOBJ::release();
45  if (cnt == 1) {
46  this->recycle();
47  m_queue->push (this);
48  }
49  return cnt;
50 }
51 
52 
53 /**
54  * @brief Constructor.
55  */
56 template <class DOBJ>
57 RecyclableDataQueue<DOBJ>::RecyclableDataQueue()
58  : m_queue (std::make_shared<queue_t>())
59 {
60 }
61 
62 
63 /**
64  * @brief Get an object, either a new one or one recycled from a previous event.
65  * @param args... Arguments to pass to the @c DOBJ constructor if we make a new one.
66  */
67 template <class DOBJ>
68 template <typename... ARGS>
69 DOBJ* RecyclableDataQueue<DOBJ>::get (ARGS&&... args)
70 {
71  DOBJ* obj = nullptr;
72  if (m_queue->try_pop (obj)) {
73  return obj;
74  }
75  return new RecyclableDataObject<DOBJ> (m_queue, std::forward<ARGS>(args)...);
76 }
77 
78 
79 /**
80  * @brief Destructor.
81  *
82  * Free all objects in the queue.
83  */
84 template <class DOBJ>
85 RecyclableDataQueue<DOBJ>::~RecyclableDataQueue()
86 {
87  DOBJ* obj = nullptr;
88  while (m_queue->try_pop (obj)) {
89  delete obj;
90  }
91 }
92 
93 
94 } // namespace Athena