Project
Loading...
Searching...
No Matches
InputSpan.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef O2_FRAMEWORK_INPUTSPAN_H_
12#define O2_FRAMEWORK_INPUTSPAN_H_
13
14#include "Framework/DataRef.h"
15#include <functional>
16
17extern template class std::function<o2::framework::DataRef(size_t)>;
18extern template class std::function<o2::framework::DataRef(size_t, size_t)>;
19
20namespace o2::framework
21{
22
29{
30 public:
31 InputSpan() = delete;
32 InputSpan(InputSpan const&) = delete;
33 InputSpan(InputSpan&&) = default;
34
38 InputSpan(std::function<DataRef(size_t)> getter, size_t size);
39
43 InputSpan(std::function<DataRef(size_t, size_t)> getter, size_t size);
44
49 InputSpan(std::function<DataRef(size_t, size_t)> getter, std::function<size_t(size_t)> nofPartsGetter, size_t size);
50
52 [[nodiscard]] DataRef get(size_t i, size_t partidx = 0) const
53 {
54 return mGetter(i, partidx);
55 }
56
58 [[nodiscard]] size_t getNofParts(size_t i) const
59 {
60 if (i >= mSize) {
61 return 0;
62 }
63 if (!mNofPartsGetter) {
64 return 1;
65 }
66 return mNofPartsGetter(i);
67 }
68
70 [[nodiscard]] size_t size() const
71 {
72 return mSize;
73 }
74
75 [[nodiscard]] const char* header(size_t i) const
76 {
77 return get(i).header;
78 }
79
80 [[nodiscard]] const char* payload(size_t i) const
81 {
82 return get(i).payload;
83 }
84
86 template <typename ParentT, typename T>
88 {
89 public:
90 using ParentType = ParentT;
92 using iterator_category = std::forward_iterator_tag;
93 using value_type = T;
94 using reference = T&;
95 using pointer = T*;
96 using difference_type = std::ptrdiff_t;
97 using ElementType = typename std::remove_const<value_type>::type;
98
99 Iterator() = delete;
100
101 Iterator(ParentType const* parent, size_t position = 0, size_t size = 0)
102 : mPosition(position), mSize(size > position ? size : position), mParent(parent), mElement{}
103 {
104 if (mPosition < mSize) {
105 mElement = mParent->get(mPosition);
106 }
107 }
108
109 ~Iterator() = default;
110
111 // prefix increment
113 {
114 if (mPosition < mSize && ++mPosition < mSize) {
115 mElement = mParent->get(mPosition);
116 } else {
117 // reset the element to the default value of the type
118 mElement = ElementType{};
119 }
120 return *this;
121 }
122 // postfix increment
123 SelfType operator++(int /*unused*/)
124 {
125 SelfType copy(*this);
126 operator++();
127 return copy;
128 }
129
130 // return reference
132 {
133 return mElement;
134 }
135
136 // comparison
137 bool operator==(const SelfType& rh) const
138 {
139 return mPosition == rh.mPosition;
140 }
141
142 // comparison
143 bool operator!=(const SelfType& rh) const
144 {
145 return mPosition != rh.mPosition;
146 }
147
148 // return pointer to parent instance
149 [[nodiscard]] ParentType const* parent() const
150 {
151 return mParent;
152 }
153
154 // return current position
155 [[nodiscard]] size_t position() const
156 {
157 return mPosition;
158 }
159
160 private:
161 size_t mPosition;
162 size_t mSize;
163 ParentType const* mParent;
164 ElementType mElement;
165 };
166
170 template <typename T>
171 class InputSpanIterator : public Iterator<InputSpan, T>
172 {
173 public:
178 using pointer = typename BaseType::pointer;
179 using ElementType = typename std::remove_const<value_type>::type;
182
183 InputSpanIterator(InputSpan const* parent, size_t position = 0, size_t size = 0)
185 {
186 }
187
189 [[nodiscard]] ElementType get(size_t pos) const
190 {
191 return this->parent()->get(this->position(), pos);
192 }
193
195 [[nodiscard]] bool isValid(size_t = 0) const
196 {
197 if (this->position() < this->parent()->size()) {
198 return this->parent()->isValid(this->position());
199 }
200 return false;
201 }
202
204 [[nodiscard]] size_t size() const
205 {
206 return this->parent()->getNofParts(this->position());
207 }
208
209 // iterator for the part access
210 [[nodiscard]] const_iterator begin() const
211 {
212 return const_iterator(this, 0, size());
213 }
214
215 [[nodiscard]] const_iterator end() const
216 {
217 return const_iterator(this, size());
218 }
219 };
220
223
224 // supporting read-only access and returning const_iterator
225 [[nodiscard]] const_iterator begin() const
226 {
227 return {this, 0, size()};
228 }
229
230 // supporting read-only access and returning const_iterator
231 [[nodiscard]] const_iterator end() const
232 {
233 return {this, size()};
234 }
235
236 private:
237 std::function<DataRef(size_t, size_t)> mGetter;
238 std::function<size_t(size_t)> mNofPartsGetter;
239 size_t mSize;
240};
241
242} // namespace o2::framework
243
244#endif // FRAMEWORK_INPUTSSPAN_H
int32_t i
uint16_t pos
Definition RawData.h:3
ElementType get(size_t pos) const
Get element at {slotindex, partindex}.
Definition InputSpan.h:189
bool isValid(size_t=0) const
Check if slot is valid, index of part is not used.
Definition InputSpan.h:195
typename BaseType::value_type value_type
Definition InputSpan.h:176
InputSpanIterator(InputSpan const *parent, size_t position=0, size_t size=0)
Definition InputSpan.h:183
Iterator< SelfType, const T > const_iterator
Definition InputSpan.h:181
typename BaseType::reference reference
Definition InputSpan.h:177
size_t size() const
Get number of parts in input slot.
Definition InputSpan.h:204
typename std::remove_const< value_type >::type ElementType
Definition InputSpan.h:179
typename BaseType::pointer pointer
Definition InputSpan.h:178
an iterator class working on position within the a parent class
Definition InputSpan.h:88
bool operator==(const SelfType &rh) const
Definition InputSpan.h:137
typename std::remove_const< value_type >::type ElementType
Definition InputSpan.h:97
std::forward_iterator_tag iterator_category
Definition InputSpan.h:92
bool operator!=(const SelfType &rh) const
Definition InputSpan.h:143
ParentType const * parent() const
Definition InputSpan.h:149
Iterator(ParentType const *parent, size_t position=0, size_t size=0)
Definition InputSpan.h:101
const char * header(size_t i) const
Definition InputSpan.h:75
const_iterator end() const
Definition InputSpan.h:231
const_iterator begin() const
Definition InputSpan.h:225
InputSpan(InputSpan const &)=delete
size_t size() const
Number of elements in the InputSpan.
Definition InputSpan.h:70
InputSpan(InputSpan &&)=default
size_t getNofParts(size_t i) const
number of parts in the i-th element of the InputSpan
Definition InputSpan.h:58
DataRef get(size_t i, size_t partidx=0) const
i-th element of the InputSpan
Definition InputSpan.h:52
const char * payload(size_t i) const
Definition InputSpan.h:80
GLsizeiptr size
Definition glcorearb.h:659
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
const char * header
Definition DataRef.h:27
const char * payload
Definition DataRef.h:28