|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +#ifndef ALGORITHM_O2FORMATPARSER_H |
| 12 | +#define ALGORITHM_O2FORMATPARSER_H |
| 13 | + |
| 14 | +/// @file O2FormatParser.h |
| 15 | +/// @author Matthias Richter |
| 16 | +/// @since 2017-10-18 |
| 17 | +/// @brief Parser for the O2 data format |
| 18 | + |
| 19 | +#include "HeaderStack.h" |
| 20 | + |
| 21 | +namespace o2 { |
| 22 | + |
| 23 | +namespace algorithm { |
| 24 | + |
| 25 | +/** |
| 26 | + * parse an input list and try to interpret in O2 data format |
| 27 | + * O2 format consist of header-payload message pairs. The header message |
| 28 | + * always starts with the DataHeader, optionally there can be more |
| 29 | + * headers in the header stack. |
| 30 | + * |
| 31 | + * The following callbacks are mandadory to be provided, e.g. through lambdas |
| 32 | + * - insert function with signature (const DataHeader&, ptr, size) |
| 33 | + * auto insertFct = [&] (const auto & dataheader, |
| 34 | + * auto ptr, |
| 35 | + * auto size) { |
| 36 | + * // do something with dataheader and buffer |
| 37 | + * }; |
| 38 | + * - getter for the message pointer, e.g. provided std::pair is used |
| 39 | + * auto getPointerFct = [] (const auto & arg) {return arg.first;}; |
| 40 | + * - getter for the message size, e.g. provided std::pair is used |
| 41 | + * auto getSizeFct = [] (const auto & arg) {return arg.second;}; |
| 42 | + * |
| 43 | + * Optionally, also the header stack can be parsed by specifying further |
| 44 | + * arguments. For every header supposed to be parsed, a pair of a dummy object |
| 45 | + * and callback has to be specified, e.g. |
| 46 | + * // handler callback for MyHeaderStruct |
| 47 | + * auto onMyHeaderStruct = [&] (const auto & mystruct) { |
| 48 | + * // do something with mystruct |
| 49 | + * }; // end handler callback |
| 50 | + * |
| 51 | + * parseO2Format(list, insertFct, MyHeaderStruct(), onMyHeaderStruct); |
| 52 | + * |
| 53 | + */ |
| 54 | +template< |
| 55 | + typename InputListT |
| 56 | + , typename GetPointerFctT |
| 57 | + , typename GetSizeFctT |
| 58 | + , typename InsertFctT // (const auto&, ptr, size) |
| 59 | + , typename... HeaderStackTypes // pairs of HeaderType and CallbackType |
| 60 | + > |
| 61 | +int parseO2Format(const InputListT& list, |
| 62 | + GetPointerFctT getPointer, |
| 63 | + GetSizeFctT getSize, |
| 64 | + InsertFctT insert, |
| 65 | + HeaderStackTypes&&... stackArgs |
| 66 | + ) |
| 67 | +{ |
| 68 | + const o2::Header::DataHeader* dh = nullptr; |
| 69 | + for (auto & part : list) { |
| 70 | + if (!dh) { |
| 71 | + // new header - payload pair, read DataHeader |
| 72 | + dh = o2::Header::get<o2::Header::DataHeader>(getPointer(part), getSize(part)); |
| 73 | + if (!dh) { |
| 74 | + return -ENOMSG; |
| 75 | + } |
| 76 | + o2::algorithm::dispatchHeaderStackCallback(getPointer(part), |
| 77 | + getSize(part), |
| 78 | + stackArgs... |
| 79 | + ); |
| 80 | + } else { |
| 81 | + insert(*dh, getPointer(part), getSize(part)); |
| 82 | + dh = nullptr; |
| 83 | + } |
| 84 | + } |
| 85 | + if (dh) { |
| 86 | + return -ENOMSG; |
| 87 | + } |
| 88 | + return list.size()/2; |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace algorithm |
| 92 | + |
| 93 | +} // namespace o2 |
| 94 | + |
| 95 | +#endif // ALGORITHM_O2FORMATPARSER_H |