2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5 * @file StoreGate/SGIterator.icc
6 * @brief A standard conforming forward iterator over items in StoreGate.
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.
16 * Will skip ahead to the first valid proxy.
18template <SG::detail::DataProxyIterator ITERATOR>
19SG::detail::IteratorBase::IteratorBase (ITERATOR itr, ITERATOR itrEnd, bool isConst)
21 setState (itr, itrEnd, isConst).ignore();
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?
31 * Will skip ahead to the first valid proxy.
32 * Returns FAILURE if the range is empty.
34template <SG::detail::DataProxyIterator ITERATOR>
36SG::detail::IteratorBase::setState (ITERATOR itr, ITERATOR itrEnd, bool isConst)
38 if (!m_proxies.empty()) {
43 // Save all valid proxies in reverse order. Also take out a refcount on each.
44 while (itr != itrEnd) {
46 DataProxy* dp = *itrEnd;
47 if (dp->isValid() && (isConst || !dp->isConst())) {
48 m_proxies.push_back (dp);
53 if (m_proxies.empty()) {
54 return StatusCode::FAILURE;
57 return StatusCode::SUCCESS;
62 * @brief Equality check.
65bool SG::detail::IteratorBase::eql (const IteratorBase& rhs) const
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();
74 * @brief The proxy pointed at by this iterator.
77SG::DataProxy* SG::detail::IteratorBase::proxy() const
79 return m_proxies.back();
84 * @brief Add a reference count to all proxies in our range.
87void SG::detail::IteratorBase::addRef()
89 for (DataProxy* dp : m_proxies) {
96 * @brief Remove a reference count from all proxies in our range.
99void SG::detail::IteratorBase::release()
101 for (DataProxy* dp : m_proxies) {
108 * @brief Constructor from proxy iterator pair.
109 * @param itr Starting proxy iterator.
110 * @param itrEnd Ending proxy iterator.
112 * Will skip ahead to the first valid proxy.
115SG::TypelessIterator::TypelessIterator (SG::ConstProxyIterator itr,
116 SG::ConstProxyIterator itrEnd)
117 : detail::IteratorBase (itr, itrEnd, true)
123 * @brief Constructor from iterator pair over DataProxy*.
124 * @param itr Starting iterator.
125 * @param itrEnd Ending iterator.
127 * Will skip ahead to the first valid proxy.
129template <SG::detail::DataProxyIterator ITERATOR>
131SG::TypelessIterator::TypelessIterator (ITERATOR itr, ITERATOR itrEnd)
132 : detail::IteratorBase (itr, itrEnd, true)
138 * @brief Reset state of the iterator.
139 * @param itr Starting proxy iterator.
140 * @param itrEnd Ending proxy iterator.
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.
147SG::TypelessIterator::setState (SG::ConstProxyIterator itr,
148 SG::ConstProxyIterator itrEnd)
150 return detail::IteratorBase::setState (itr, itrEnd, true);
155 * @brief Reset state of the iterator.
156 * @param itr Starting iterator over DataProxy*.
157 * @param itrEnd Ending iterator.
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.
162template <SG::detail::DataProxyIterator ITERATOR>
165SG::TypelessIterator::setState (ITERATOR itr, ITERATOR itrEnd)
167 return detail::IteratorBase::setState (itr, itrEnd, true);
172 * @brief Equality check.
174 * Used by the boost iterator adapter.
177bool SG::TypelessIterator::equal(const TypelessIterator& rhs) const
179 return detail::IteratorBase::eql(rhs);
184 * @brief Dereference the iterator.
186 * Used by the boost iterator adapter.
189SG::DataProxy* SG::TypelessIterator::dereference() const
191 return this->proxy();
196 * @brief Default constructor.
200SG::ConstIterator<DATA>::ConstIterator()
201 : detail::IteratorBase()
207 * @brief Constructor from proxy iterator pair.
208 * @param itr Starting proxy iterator.
209 * @param itrEnd Ending proxy iterator.
211 * Will skip ahead to the first valid proxy.
215SG::ConstIterator<DATA>::ConstIterator (SG::ConstProxyIterator itr,
216 SG::ConstProxyIterator itrEnd)
217 : detail::IteratorBase (itr, itrEnd, true)
223 * @brief Reset state of the iterator.
224 * @param itr Starting proxy iterator.
225 * @param itrEnd Ending proxy iterator.
227 * Will skip ahead to the first valid proxy.
228 * Will return failure if the range is empty.
233SG::ConstIterator<DATA>::setState (SG::ConstProxyIterator itr,
234 SG::ConstProxyIterator itrEnd)
236 return detail::IteratorBase::setState (itr, itrEnd, true);
241 * @brief Equality check.
243 * Used by the boost iterator adapter.
247bool SG::ConstIterator<DATA>::equal(const ConstIterator& rhs) const
249 return detail::IteratorBase::eql(rhs);
254 * @brief Dereference the iterator.
256 * Used by the boost iterator adapter.
258 * Will throw SG::ExcInvalidIterator() if the proxy is not valid.
261const DATA& SG::ConstIterator<DATA>::dereference() const
263 const DATA* ptr = SG::DataProxy_cast<DATA> (this->proxy());
265 throw SG::ExcInvalidIterator();
272 * @brief Default constructor.
276SG::Iterator<DATA>::Iterator()
277 : detail::IteratorBase()
283 * @brief Constructor from proxy iterator pair.
284 * @param itr Starting proxy iterator.
285 * @param itrEnd Ending proxy iterator.
287 * Will skip ahead to the first valid proxy.
291SG::Iterator<DATA>::Iterator (SG::ConstProxyIterator itr,
292 SG::ConstProxyIterator itrEnd)
293 : detail::IteratorBase (itr, itrEnd, false)
299 * @brief Reset state of the iterator.
300 * @param itr Starting proxy iterator.
301 * @param itrEnd Ending proxy iterator.
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.
309SG::Iterator<DATA>::setState (SG::ConstProxyIterator itr,
310 SG::ConstProxyIterator itrEnd)
312 return detail::IteratorBase::setState (itr, itrEnd, false);
317 * @brief Equality check.
319 * Used by the boost iterator adapter.
323bool SG::Iterator<DATA>::equal(const Iterator<DATA>& rhs) const
325 return detail::IteratorBase::eql(rhs);
330 * @brief Dereference the iterator.
332 * Used by the boost iterator adapter.
334 * Will throw SG::ExcInvalidIterator() if the proxy is not valid.
335 * Will throw SG::ExcConstObject if the proxy is const.
338DATA& SG::Iterator<DATA>::dereference() const
341 DATA* ptr = SG::DataProxy_cast<DATA> (this->proxy());
343 throw SG::ExcInvalidIterator();