PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
RelationRange.h
Go to the documentation of this file.
1#ifndef PODIO_RELATIONRANGE_H
2#define PODIO_RELATIONRANGE_H
3
4#include <iterator>
5#include <vector>
6
7namespace podio {
8/**
9 * A simple helper class that allows to return related objects in a way that
10 * makes it possible to use the return type in a range-based for loop.
11 */
12template <typename ReferenceType>
14public:
15 using ConstIteratorType = typename std::vector<ReferenceType>::const_iterator;
16
17 RelationRange() = delete;
18
20 m_begin(begin), m_end(end), m_size(std::distance(m_begin, m_end)) {
21 }
22
23 /// begin of the range (necessary for range-based for loop)
25 return m_begin;
26 }
27 /// end of the range (necessary for range-based for loop)
29 return m_end;
30 }
31 /// convenience overload for size
32 size_t size() const {
33 return m_size;
34 }
35 /// convenience overload to check if the range is empty
36 bool empty() const {
37 return m_begin == m_end;
38 }
39 /// Indexed access
40 ReferenceType operator[](size_t i) const {
41 auto it = m_begin;
42 std::advance(it, i);
43 return *it;
44 }
45 /// Indexed access with range check
46 ReferenceType at(size_t i) const {
47 if (i < m_size) {
48 auto it = m_begin;
49 std::advance(it, i);
50 return *it;
51 }
52 throw std::out_of_range("index out of bounds for RelationRange");
53 }
54
55private:
56 ConstIteratorType m_begin;
58 size_t m_size{0};
59};
60} // namespace podio
61
62#endif // PODIO_RELATIONRANGE_H
typename std::vector< ReferenceType >::const_iterator ConstIteratorType
Definition: RelationRange.h:15
bool empty() const
convenience overload to check if the range is empty
Definition: RelationRange.h:36
ReferenceType operator[](size_t i) const
Indexed access.
Definition: RelationRange.h:40
ConstIteratorType end() const
end of the range (necessary for range-based for loop)
Definition: RelationRange.h:28
RelationRange(ConstIteratorType begin, ConstIteratorType end)
Definition: RelationRange.h:19
size_t size() const
convenience overload for size
Definition: RelationRange.h:32
ReferenceType at(size_t i) const
Indexed access with range check.
Definition: RelationRange.h:46
ConstIteratorType begin() const
begin of the range (necessary for range-based for loop)
Definition: RelationRange.h:24