Skip to content

Commit 7ae04ca

Browse files
DPL: Improving InputRecord iterator
- filtering out invalid input slots from the iteration - nullptr as default values of DataRef members - fixing const'ness of isValid method
1 parent 1ac17a6 commit 7ae04ca

4 files changed

Lines changed: 66 additions & 10 deletions

File tree

Framework/Core/include/Framework/DataRef.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ struct InputSpec;
2020
struct DataRef {
2121
// FIXME: had to remove the second 'const' in const T* const
2222
// to allow assignment
23-
const InputSpec* spec;
24-
const char* header;
25-
const char* payload;
23+
const InputSpec* spec = nullptr;
24+
const char* header = nullptr;
25+
const char* payload = nullptr;
2626
};
2727

2828
} // namespace framework

Framework/Core/include/Framework/InputRecord.h

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,14 @@ class InputRecord
422422
}
423423

424424
/// Helper method to be used to check if a given part of the InputRecord is present.
425-
bool isValid(std::string const& s)
425+
bool isValid(std::string const& s) const
426426
{
427427
return isValid(s.c_str());
428428
}
429429

430430
/// Helper method to be used to check if a given part of the InputRecord is present.
431-
bool isValid(char const* s);
432-
bool isValid(int pos);
431+
bool isValid(char const* s) const;
432+
bool isValid(int pos) const;
433433

434434
size_t size() const
435435
{
@@ -456,7 +456,11 @@ class InputRecord
456456
: mParent(parent), mPosition(position), mSize(size > position ? size : position), mElement{nullptr, nullptr, nullptr}
457457
{
458458
if (mPosition < mSize) {
459-
mElement = mParent->getByPos(mPosition);
459+
if (mParent->isValid(mPosition)) {
460+
mElement = mParent->getByPos(mPosition);
461+
} else {
462+
(*this)++;
463+
}
460464
}
461465
}
462466

@@ -465,8 +469,16 @@ class InputRecord
465469
// prefix increment
466470
SelfType& operator++()
467471
{
468-
if (mPosition < mSize && ++mPosition < mSize) {
472+
while (mPosition < mSize && ++mPosition < mSize) {
473+
if (!mParent->isValid(mPosition)) {
474+
continue;
475+
}
469476
mElement = mParent->getByPos(mPosition);
477+
break;
478+
}
479+
if (mPosition >= mSize) {
480+
// reset the element to the default value of the type
481+
mElement = ElementType{};
470482
}
471483
return *this;
472484
}
@@ -493,6 +505,33 @@ class InputRecord
493505
return mPosition != rh.mPosition;
494506
}
495507

508+
bool matches(o2::header::DataHeader matcher) const
509+
{
510+
if (mPosition >= mSize || mElement.header == nullptr) {
511+
return false;
512+
}
513+
// at this point there must be a DataHeader, this has been checked by the DPL
514+
// input cache
515+
const auto* dh = DataRefUtils::getHeader<o2::header::DataHeader*>(mElement);
516+
return *dh == matcher;
517+
}
518+
519+
bool matches(o2::header::DataOrigin origin, o2::header::DataDescription description = o2::header::gDataDescriptionInvalid) const
520+
{
521+
if (mPosition >= mSize || mElement.header == nullptr) {
522+
return false;
523+
}
524+
// at this point there must be a DataHeader, this has been checked by the DPL
525+
// input cache
526+
const auto* dh = DataRefUtils::getHeader<o2::header::DataHeader*>(mElement);
527+
return dh->dataOrigin == origin && (description == o2::header::gDataDescriptionInvalid || dh->dataDescription == description);
528+
}
529+
530+
bool matches(o2::header::DataOrigin origin, o2::header::DataDescription description, o2::header::DataHeader::SubSpecificationType subspec) const
531+
{
532+
return matches(o2::header::DataHeader{description, origin, subspec});
533+
}
534+
496535
private:
497536
size_t mPosition;
498537
size_t mSize;

Framework/Core/src/InputRecord.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int InputRecord::getPos(std::string const& binding) const
6161
return -1;
6262
}
6363

64-
bool InputRecord::isValid(char const* s)
64+
bool InputRecord::isValid(char const* s) const
6565
{
6666
DataRef ref = get(s);
6767
if (ref.header == nullptr || ref.payload == nullptr) {
@@ -70,7 +70,7 @@ bool InputRecord::isValid(char const* s)
7070
return true;
7171
}
7272

73-
bool InputRecord::isValid(int s)
73+
bool InputRecord::isValid(int s) const
7474
{
7575
DataRef ref = getByPos(s);
7676
if (ref.header == nullptr || ref.payload == nullptr) {

Framework/Core/test/test_InputRecord.cxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ BOOST_AUTO_TEST_CASE(TestInputRecord)
127127
// A few more time just to make sure we are not stateful..
128128
BOOST_CHECK_EQUAL(record.get<int>("x"), 1);
129129
BOOST_CHECK_EQUAL(record.get<int>("x"), 1);
130+
131+
// test the iterator
132+
int position = 0;
133+
for (auto input = record.begin(), end = record.end(); input != end; input++, position++) {
134+
if (position == 0) {
135+
BOOST_CHECK(input.matches("TPC") == true);
136+
BOOST_CHECK(input.matches("TPC", "CLUSTERS") == true);
137+
BOOST_CHECK(input.matches("ITS", "CLUSTERS") == false);
138+
}
139+
if (position == 1) {
140+
BOOST_CHECK(input.matches("ITS") == true);
141+
BOOST_CHECK(input.matches("ITS", "CLUSTERS") == true);
142+
BOOST_CHECK(input.matches("TPC", "CLUSTERS") == false);
143+
}
144+
// check if invalid slots are filtered out by the iterator
145+
BOOST_CHECK(position != 2);
146+
}
130147
}
131148

132149
// TODO:

0 commit comments

Comments
 (0)