ATLAS Offline Software
Loading...
Searching...
No Matches
SGIterator.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4/**
5 * @file StoreGate/SGIterator.icc
6 * @brief A standard conforming forward iterator over items in StoreGate.
7 */
8
9
10/**
11 * @brief Constructor from iterator pair over DataProxy*.
12 * @param itr Starting iterator.
13 * @param itrEnd Ending iterator.
14 * @param isConst If false, take only non-const proxies.
15 *
16 * Will skip ahead to the first valid proxy.
17 */
18template <SG::detail::DataProxyIterator ITERATOR>
19SG::detail::IteratorBase::IteratorBase (ITERATOR itr, ITERATOR itrEnd, bool isConst)
20{
21 setState (itr, itrEnd, isConst).ignore();
22}
23
24
25/**
26 * @brief Reset state of the iterator.
27 * @param itr Starting iterator over DataProxy*.
28 * @param itrEnd Ending iterator.
29 * @param isConst Is this for a const iterator?
30 *
31 * Will skip ahead to the first valid proxy.
32 * Returns FAILURE if the range is empty.
33 */
34template <SG::detail::DataProxyIterator ITERATOR>
35StatusCode
36SG::detail::IteratorBase::setState (ITERATOR itr, ITERATOR itrEnd, bool isConst)
37{
38 if (!m_proxies.empty()) {
39 this->release();
40 m_proxies.clear();
41 }
42
43 // Save all valid proxies in reverse order. Also take out a refcount on each.
44 while (itr != itrEnd) {
45 --itrEnd;
46 DataProxy* dp = *itrEnd;
47 if (dp->isValid() && (isConst || !dp->isConst())) {
48 m_proxies.push_back (dp);
49 dp->addRef();
50 }
51 }
52
53 if (m_proxies.empty()) {
54 return StatusCode::FAILURE;
55 }
56
57 return StatusCode::SUCCESS;
58}
59
60
61/**
62 * @brief Equality check.
63 */
64inline
65bool SG::detail::IteratorBase::eql (const IteratorBase& rhs) const
66{
67 if (m_proxies.size() != rhs.m_proxies.size()) return false;
68 if (m_proxies.empty()) return true;
69 return m_proxies.back() == rhs.m_proxies.back();
70}
71
72
73/**
74 * @brief The proxy pointed at by this iterator.
75 */
76inline
77SG::DataProxy* SG::detail::IteratorBase::proxy() const
78{
79 return m_proxies.back();
80}
81
82
83/**
84 * @brief Add a reference count to all proxies in our range.
85 */
86inline
87void SG::detail::IteratorBase::addRef()
88{
89 for (DataProxy* dp : m_proxies) {
90 dp->addRef();
91 }
92}
93
94
95/**
96 * @brief Remove a reference count from all proxies in our range.
97 */
98inline
99void SG::detail::IteratorBase::release()
100{
101 for (DataProxy* dp : m_proxies) {
102 dp->release();
103 }
104}
105
106
107/**
108 * @brief Constructor from proxy iterator pair.
109 * @param itr Starting proxy iterator.
110 * @param itrEnd Ending proxy iterator.
111 *
112 * Will skip ahead to the first valid proxy.
113 */
114inline
115SG::TypelessIterator::TypelessIterator (SG::ConstProxyIterator itr,
116 SG::ConstProxyIterator itrEnd)
117 : detail::IteratorBase (itr, itrEnd, true)
118{
119}
120
121
122/**
123 * @brief Constructor from iterator pair over DataProxy*.
124 * @param itr Starting iterator.
125 * @param itrEnd Ending iterator.
126 *
127 * Will skip ahead to the first valid proxy.
128 */
129template <SG::detail::DataProxyIterator ITERATOR>
130inline
131SG::TypelessIterator::TypelessIterator (ITERATOR itr, ITERATOR itrEnd)
132 : detail::IteratorBase (itr, itrEnd, true)
133{
134}
135
136
137/**
138 * @brief Reset state of the iterator.
139 * @param itr Starting proxy iterator.
140 * @param itrEnd Ending proxy iterator.
141 *
142 * Will skip ahead to the first valid proxy.
143 * Will return failure if the first proxy is const or if the range is empty.
144 */
145inline
146StatusCode
147SG::TypelessIterator::setState (SG::ConstProxyIterator itr,
148 SG::ConstProxyIterator itrEnd)
149{
150 return detail::IteratorBase::setState (itr, itrEnd, true);
151}
152
153
154/**
155 * @brief Reset state of the iterator.
156 * @param itr Starting iterator over DataProxy*.
157 * @param itrEnd Ending iterator.
158 *
159 * Will skip ahead to the first valid proxy.
160 * Will return failure if the first proxy is const or if the range is empty.
161 */
162template <SG::detail::DataProxyIterator ITERATOR>
163inline
164StatusCode
165SG::TypelessIterator::setState (ITERATOR itr, ITERATOR itrEnd)
166{
167 return detail::IteratorBase::setState (itr, itrEnd, true);
168}
169
170
171/**
172 * @brief Equality check.
173 *
174 * Used by the boost iterator adapter.
175 */
176inline
177bool SG::TypelessIterator::equal(const TypelessIterator& rhs) const
178{
179 return detail::IteratorBase::eql(rhs);
180}
181
182
183/**
184 * @brief Dereference the iterator.
185 *
186 * Used by the boost iterator adapter.
187 */
188inline
189SG::DataProxy* SG::TypelessIterator::dereference() const
190{
191 return this->proxy();
192}
193
194
195/**
196 * @brief Default constructor.
197 */
198template <class DATA>
199inline
200SG::ConstIterator<DATA>::ConstIterator()
201 : detail::IteratorBase()
202{
203}
204
205
206/**
207 * @brief Constructor from proxy iterator pair.
208 * @param itr Starting proxy iterator.
209 * @param itrEnd Ending proxy iterator.
210 *
211 * Will skip ahead to the first valid proxy.
212 */
213template <class DATA>
214inline
215SG::ConstIterator<DATA>::ConstIterator (SG::ConstProxyIterator itr,
216 SG::ConstProxyIterator itrEnd)
217 : detail::IteratorBase (itr, itrEnd, true)
218{
219}
220
221
222/**
223 * @brief Reset state of the iterator.
224 * @param itr Starting proxy iterator.
225 * @param itrEnd Ending proxy iterator.
226 *
227 * Will skip ahead to the first valid proxy.
228 * Will return failure if the range is empty.
229 */
230template <class DATA>
231inline
232StatusCode
233SG::ConstIterator<DATA>::setState (SG::ConstProxyIterator itr,
234 SG::ConstProxyIterator itrEnd)
235{
236 return detail::IteratorBase::setState (itr, itrEnd, true);
237}
238
239
240/**
241 * @brief Equality check.
242 *
243 * Used by the boost iterator adapter.
244 */
245template <class DATA>
246inline
247bool SG::ConstIterator<DATA>::equal(const ConstIterator& rhs) const
248{
249 return detail::IteratorBase::eql(rhs);
250}
251
252
253/**
254 * @brief Dereference the iterator.
255 *
256 * Used by the boost iterator adapter.
257 *
258 * Will throw SG::ExcInvalidIterator() if the proxy is not valid.
259 */
260template <class DATA>
261const DATA& SG::ConstIterator<DATA>::dereference() const
262{
263 const DATA* ptr = SG::DataProxy_cast<DATA> (this->proxy());
264 if (!ptr) {
265 throw SG::ExcInvalidIterator();
266 }
267 return *ptr;
268}
269
270
271/**
272 * @brief Default constructor.
273 */
274template <class DATA>
275inline
276SG::Iterator<DATA>::Iterator()
277 : detail::IteratorBase()
278{
279}
280
281
282/**
283 * @brief Constructor from proxy iterator pair.
284 * @param itr Starting proxy iterator.
285 * @param itrEnd Ending proxy iterator.
286 *
287 * Will skip ahead to the first valid proxy.
288 */
289template <class DATA>
290inline
291SG::Iterator<DATA>::Iterator (SG::ConstProxyIterator itr,
292 SG::ConstProxyIterator itrEnd)
293 : detail::IteratorBase (itr, itrEnd, false)
294{
295}
296
297
298/**
299 * @brief Reset state of the iterator.
300 * @param itr Starting proxy iterator.
301 * @param itrEnd Ending proxy iterator.
302 *
303 * Will skip ahead to the first valid proxy.
304 * Will return failure if the first proxy is const or if the range is empty.
305 */
306template <class DATA>
307inline
308StatusCode
309SG::Iterator<DATA>::setState (SG::ConstProxyIterator itr,
310 SG::ConstProxyIterator itrEnd)
311{
312 return detail::IteratorBase::setState (itr, itrEnd, false);
313}
314
315
316/**
317 * @brief Equality check.
318 *
319 * Used by the boost iterator adapter.
320 */
321template <class DATA>
322inline
323bool SG::Iterator<DATA>::equal(const Iterator<DATA>& rhs) const
324{
325 return detail::IteratorBase::eql(rhs);
326}
327
328
329/**
330 * @brief Dereference the iterator.
331 *
332 * Used by the boost iterator adapter.
333 *
334 * Will throw SG::ExcInvalidIterator() if the proxy is not valid.
335 * Will throw SG::ExcConstObject if the proxy is const.
336 */
337template <class DATA>
338DATA& SG::Iterator<DATA>::dereference() const
339{
340 this->const_check();
341 DATA* ptr = SG::DataProxy_cast<DATA> (this->proxy());
342 if (!ptr) {
343 throw SG::ExcInvalidIterator();
344 }
345 return *ptr;
346}