ATLAS Offline Software
ArenaHandleBaseT.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id: ArenaHandleBaseT.icc 470529 2011-11-24 23:54:22Z ssnyder $
6 /**
7  * @file AthAllocators/ArenaHandleBaseT.icc
8  * @author scott snyder
9  * @date May 2007
10  * @brief Base class for @c Handle classes, containing parts that
11  * are independent of how the Allocator gets created.
12  * Inline and template implementations.
13  */
14 
15 
16 namespace SG {
17 
18 
19 /**
20  * @brief Constructor, passing in an index.
21  * @param header The group of Arenas which this Handle may reference.
22  * May be null to select the global default.
23  * @param index The index of this Handle's Allocator type.
24  */
25 template <typename T, typename ALLOC>
26 ArenaHandleBaseT<T, ALLOC>::ArenaHandleBaseT (ArenaHeader* header,
27  size_t index)
28  : Base (header, index)
29 {
30 }
31 
32 
33 /**
34  * @brief Constructor, passing in an index, for a specific event slot.
35  * @param header The group of Arenas which this Handle may reference.
36  * May be null to select the global default.
37  * @param ctx Event context identifying the event slot.
38  * @param index The index of this Handle's Allocator type.
39  */
40 template <typename T, typename ALLOC>
41 ArenaHandleBaseT<T, ALLOC>::ArenaHandleBaseT (ArenaHeader* header,
42  const EventContext& ctx,
43  size_t index)
44  : Base (header, ctx, index)
45 {
46 }
47 
48 
49 /**
50  * @brief Constructor, passing in an index, for a specific Arena.
51  * @param arena The Arena in which to find the allocator.
52  * @param index The index of this Handle's Allocator type.
53  */
54 template <typename T, typename ALLOC>
55 ArenaHandleBaseT<T, ALLOC>::ArenaHandleBaseT (ArenaBase* arena, size_t index)
56  : Base (arena, index)
57 {
58 }
59 
60 
61 /**
62  * @brief Constructor.
63  * @param it The base iterator.
64  */
65 template <typename T, typename ALLOC>
66 inline
67 ArenaHandleBaseT<T, ALLOC>::iterator::iterator
68  (const typename ALLOC::iterator& it)
69  : iterator::iterator_adaptor_ (it)
70 {
71 }
72 
73 
74 /**
75  * @brief Dereference the iterator.
76  */
77 template <typename T, typename ALLOC>
78 inline
79 typename ArenaHandleBaseT<T, ALLOC>::iterator::reference
80 ArenaHandleBaseT<T, ALLOC>::iterator::dereference() const
81 {
82  return reinterpret_cast<T&> (*this->base_reference());
83 }
84 
85 
86 /**
87  * @brief Constructor.
88  * @param it The base iterator.
89  */
90 template <typename T, typename ALLOC>
91 inline
92 ArenaHandleBaseT<T, ALLOC>::const_iterator::const_iterator
93  (const typename ALLOC::const_iterator& it)
94  : const_iterator::iterator_adaptor_ (it)
95 {
96 }
97 
98 
99 /**
100  * @brief Constructor from a non-const iterator.
101  * @param it The non-const iterator.
102  */
103 template <typename T, typename ALLOC>
104 inline
105 ArenaHandleBaseT<T, ALLOC>::const_iterator::const_iterator
106  (const iterator& it)
107  : const_iterator::iterator_adaptor_ (it.base_reference())
108 {
109 }
110 
111 
112 /**
113  * @brief Dereference the iterator.
114  */
115 template <typename T, typename ALLOC>
116 inline
117 typename ArenaHandleBaseT<T, ALLOC>::const_iterator::reference
118 ArenaHandleBaseT<T, ALLOC>::const_iterator::dereference() const
119 {
120  return reinterpret_cast<const T&> (*this->base_reference());
121 }
122 
123 
124 /**
125  * @brief Starting iterator.
126  *
127  * This will iterate over all allocated elements (in unspecified order).
128  * It is at least a @c forward_iterator.
129  * This may only be instantiated if the underlying Allocator
130  * supports iterators.
131  */
132 template <class T, class ALLOC>
133 typename ArenaHandleBaseT<T, ALLOC>::iterator
134 ArenaHandleBaseT<T, ALLOC>::begin()
135 {
136  return const_cast<ALLOC*>(this->allocator())->begin();
137 }
138 
139 
140 /**
141  * @brief Starting const iterator.
142  *
143  * This will iterate over all allocated elements (in unspecified order).
144  * It is at least a @c forward_iterator.
145  * This may only be instantiated if the underlying Allocator
146  * supports iterators.
147  */
148 template <class T, class ALLOC>
149 typename ArenaHandleBaseT<T, ALLOC>::const_iterator
150 ArenaHandleBaseT<T, ALLOC>::begin() const
151 {
152  return this->allocator()->begin();
153 }
154 
155 
156 /**
157  * @brief Ending iterator.
158  *
159  * This may only be instantiated if the underlying Allocator
160  * supports iterators.
161  */
162 template <class T, class ALLOC>
163 typename ArenaHandleBaseT<T, ALLOC>::iterator
164 ArenaHandleBaseT<T, ALLOC>::end()
165 {
166  return this->allocator()->end();
167 }
168 
169 
170 /**
171  * @brief Ending const iterator.
172  *
173  * This may only be instantiated if the underlying Allocator
174  * supports iterators.
175  */
176 template <class T, class ALLOC>
177 typename ArenaHandleBaseT<T, ALLOC>::const_iterator
178 ArenaHandleBaseT<T, ALLOC>::end() const
179 {
180  return const_cast<const ALLOC*>(this->allocator())->end();
181 }
182 
183 
184 /**
185  * @brief Free an element.
186  * @param p The element to be freed.
187  *
188  * @c clear() will be called on the element at this point,
189  * if it has been defined.
190  *
191  * This may only be instantiated if it's supported
192  * by the underlying Allocator.
193  */
194 template <class T, class ALLOC>
195 inline
196 void ArenaHandleBaseT<T, ALLOC>::free (pointer p)
197 {
198  this->allocator()->free
199  (reinterpret_cast<typename ALLOC::pointer>(p));
200 }
201 
202 
203 /**
204  * @brief Reset pool back to a previous state.
205  * @param p The pointer back to which to reset.
206  *
207  * This will free (a la @c reset) the element @c p and all elements
208  * that have been allocated after it from this allocator.
209  *
210  * This may only be instantiated if it's supported
211  * by the underlying Allocator.
212  */
213 template <class T, class ALLOC>
214 void ArenaHandleBaseT<T, ALLOC>::resetTo(pointer p)
215 {
216  return this->allocator()->resetTo
217  (reinterpret_cast<typename ALLOC::pointer>(p));
218 }
219 
220 
221 } // namespace SG