Skip to content

Commit 6ceb3ff

Browse files
Adding some helper methods to RawParser iterator
Adding methods to access the raw buffer at position and payload offset. Renaming length() -> size() The raw buffer points to the beginning of the RDH which can be different versions and thus we can not return a concrete type. See get_if for this.
1 parent 9a55f8d commit 6ceb3ff

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

Framework/Utils/include/DPLUtils/RawParser.h

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class ConcreteRawParser
110110
return *reinterpret_cast<header_type const*>(mPosition);
111111
}
112112

113-
/// Get length of payload at current position
114-
size_t length() const
113+
/// Get size of payload at current position
114+
size_t size() const
115115
{
116116
if (mPosition == mRawBuffer + mSize) {
117117
return 0;
@@ -124,17 +124,36 @@ class ConcreteRawParser
124124
/// Get pointer to payload data at current position
125125
buffer_type const* data() const
126126
{
127-
size_t len = length();
128-
if (len == 0) {
127+
size_t size = this->size();
128+
if (size == 0) {
129129
return nullptr;
130130
}
131131
header_type const& h = header();
132-
if (mPosition + len + h.headerSize > mRawBuffer + mSize) {
132+
if (mPosition + size + h.headerSize > mRawBuffer + mSize) {
133133
throw std::runtime_error("not enough data at position " + std::to_string(mPosition - mRawBuffer));
134134
}
135135
return mPosition + h.headerSize;
136136
}
137137

138+
/// Get pointer to raw buffer at current position
139+
buffer_type const* raw() const
140+
{
141+
if (mPosition < mRawBuffer + mSize) {
142+
return mPosition;
143+
}
144+
return nullptr;
145+
}
146+
147+
/// Get offset of payload in the raw buffer at current position
148+
size_t offset() const
149+
{
150+
if (mPosition < mRawBuffer + mSize) {
151+
header_type const& h = header();
152+
return h.headerSize;
153+
}
154+
return 0;
155+
}
156+
138157
/// Parse the complete buffer
139158
/// For each page, the processor function is called with the payload buffer and size,
140159
/// processor has signature
@@ -145,7 +164,7 @@ class ConcreteRawParser
145164
reset();
146165
//auto deleter = [](buffer_type*) {};
147166
do {
148-
processor(data(), length());
167+
processor(data(), size());
149168
//processor(std::unique_ptr<buffer_type, decltype(deleter)>(data(), deleter), size());
150169
} while (next());
151170
}
@@ -299,15 +318,15 @@ U const* get_if(T& instances)
299318
///
300319
/// // option 1: parse method
301320
/// RawParser parser(buffer, size);
302-
/// auto processor = [&count](auto data, size_t length) {
303-
/// std::cout << "Processing block of length " << length << std::endl;
321+
/// auto processor = [&count](auto data, size_t size) {
322+
/// std::cout << "Processing block of size " << size << std::endl;
304323
/// };
305324
/// parser.parse(processor);
306325
///
307326
/// // option 2: iterator
308327
/// RawParser parser(buffer, size);
309328
/// for (auto it = parser.begin(), end = parser.end(); it != end; ++it, ++count) {
310-
/// std::cout << "Iterating block of length " << it.length() << std::endl;
329+
/// std::cout << "Iterating block of size " << it.size() << std::endl;
311330
/// auto dataptr = it.data();
312331
/// }
313332
///
@@ -366,7 +385,7 @@ class RawParser
366385
/// - increment (there is no decrement, its not a bidirectional parser)
367386
/// - dereference operator returns @a RawDataHeaderInfo as common header
368387
/// - member function data() returns pointer to payload at current position
369-
/// - member function length() return size of payload at current position
388+
/// - member function size() return size of payload at current position
370389
template <typename T, typename ParentType>
371390
class Iterator : public IteratorBase<T>
372391
{
@@ -417,16 +436,28 @@ class RawParser
417436
return not operator==(rh);
418437
}
419438

439+
/// get pointer to raw block at current position, rdh starts here
440+
buffer_type const* raw() const
441+
{
442+
return std::visit([](auto& parser) { return parser.raw(); }, mParser);
443+
}
444+
420445
/// get pointer to payload at current position
421446
buffer_type const* data() const
422447
{
423448
return std::visit([](auto& parser) { return parser.data(); }, mParser);
424449
}
425450

426-
/// get length of payload at current position
427-
size_t length() const
451+
/// offset of payload at current position
452+
size_t offset() const
453+
{
454+
return std::visit([](auto& parser) { return parser.offset(); }, mParser);
455+
}
456+
457+
/// get size of payload at current position
458+
size_t size() const
428459
{
429-
return std::visit([](auto& parser) { return parser.length(); }, mParser);
460+
return std::visit([](auto& parser) { return parser.size(); }, mParser);
430461
}
431462

432463
/// get header as specific type

Framework/Utils/src/raw-parser.cxx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,15 @@ WorkflowSpec defineDataProcessing(ConfigContext const& config)
5151
<< dh->subSpecification << " payload size " << dh->payloadSize;
5252
}
5353

54-
// there is a bug in InpuRecord::get for vectors of simple types, not catched in
55-
// DataAllocator unit test
56-
//auto data = inputs.get<std::vector<char>>(input.spec->binding.c_str());
57-
//LOG(INFO) << "data size " << data.size();
54+
auto raw = inputs.get<std::span<char>>(input.spec->binding.c_str());
5855

5956
try {
60-
o2::framework::RawParser parser(input.payload, dh->payloadSize);
57+
o2::framework::RawParser parser(raw.data(), raw.size());
6158

6259
std::stringstream rdhprintout;
6360
rdhprintout << parser;
6461
for (auto it = parser.begin(), end = parser.end(); it != end; ++it) {
65-
rdhprintout << it << ": block length " << it.length() << std::endl;
62+
rdhprintout << it << ": payload size " << it.size() << std::endl;
6663
}
6764
if (loglevel > 1) {
6865
LOG(INFO) << rdhprintout.str();

Framework/Utils/test/test_RawParser.cxx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ BOOST_AUTO_TEST_CASE(test_RawParser)
3838
}
3939

4040
size_t count = 0;
41-
auto processor = [&count](auto data, size_t length) {
42-
BOOST_CHECK(length == PageSize - sizeof(V5));
41+
auto processor = [&count](auto data, size_t size) {
42+
BOOST_CHECK(size == PageSize - sizeof(V5));
4343
BOOST_CHECK(*reinterpret_cast<size_t const*>(data) == count);
44-
std::cout << "Processing block of length " << length << std::endl;
44+
std::cout << "Processing block of size " << size << std::endl;
4545
count++;
4646
};
4747
RawParser parser(buffer.data(), buffer.size());
@@ -53,11 +53,12 @@ BOOST_AUTO_TEST_CASE(test_RawParser)
5353
std::cout << parser << std::endl;
5454
count = 0;
5555
for (auto it = parser.begin(), end = parser.end(); it != end; ++it, ++count) {
56-
BOOST_CHECK(it.length() == PageSize - sizeof(V5));
56+
BOOST_CHECK(it.size() == PageSize - sizeof(V5));
5757
BOOST_CHECK(*reinterpret_cast<size_t const*>(it.data()) == count);
5858
BOOST_CHECK(it.get_if<V5>() != nullptr);
5959
BOOST_CHECK(it.get_if<V4>() == nullptr);
60-
std::cout << it << ": block length " << it.length() << std::endl;
60+
BOOST_CHECK(it.raw() + it.offset() == it.data());
61+
std::cout << it << ": block size " << it.size() << std::endl;
6162
}
6263
}
6364

0 commit comments

Comments
 (0)