@@ -23,11 +23,13 @@ namespace o2 {
2323
2424namespace algorithm {
2525
26- // TODO there is probably a standard way?
26+ // / helper function returning size of type with a specialization for
27+ // / void returning 0
2728template <typename T>
2829struct typesize {
2930 static const size_t size = sizeof (T);
3031};
32+ // specialization for void
3133template <>
3234struct typesize <void > {
3335 static const size_t size = 0 ;
@@ -38,8 +40,12 @@ struct typesize<void> {
3840 * Parser for a sequence of frames with header, trailer and variable payload.
3941 * The size is expected to be part of the header.
4042 *
43+ * Trailer type can be void, which is also the default template parameter. That
44+ * allows to define a frame consisting of only header and data.
45+ *
4146 * Usage:
42- * ForwardParser<SomeHeaderType, SomeTrailerType> SomeParser;
47+ * <pre>
48+ * using SomeParser = ForwardParser<SomeHeaderType, SomeTrailerType>;
4349 * SomeParser parser;
4450 * std::vector<typename SomeParser::FrameInfo> frames;
4551 * parser.parse(ptr, size,
@@ -62,6 +68,28 @@ struct typesize<void> {
6268 * return true;
6369 * }
6470 * )
71+ *
72+ * // a reduced version without trailer check callback
73+ * using SomeParser = ForwardParser<SomeHeaderType>;
74+ * SomeParser parser;
75+ * std::vector<typename SomeParser::FrameInfo> frames;
76+ * parser.parse(ptr, size,
77+ * [] (const typename SomeParser::HeaderType& h) {
78+ * // check the header
79+ * return true;
80+ * },
81+ * [] (const typename SomeParser::HeaderType& h) {
82+ * // get the size of the frame including payload
83+ * // and header and trailer size, e.g. payload size
84+ * // from a header member
85+ * return h.payloadSize + SomeParser::totalOffset;
86+ * },
87+ * [&frames] (typename SomeParser::FrameInfo& info) {
88+ * frames.emplace_back(info);
89+ * return true;
90+ * }
91+ * )
92+ * </pre>
6593 */
6694template <typename HeaderT,
6795 typename TrailerT = void
@@ -72,6 +100,8 @@ class ForwardParser {
72100 using TrailerType = TrailerT;
73101 using PayloadType = unsigned char ;
74102
103+ // / @struct FrameInfo
104+ // / a compound of header, data, and trailer
75105 struct FrameInfo {
76106 using PtrT = const PayloadType*;
77107
@@ -81,37 +111,42 @@ class ForwardParser {
81111 size_t length = 0 ;
82112 };
83113
84- // the length offset due to header and trailer
114+ // / the length offset due to header
85115 static const size_t headOffset = typesize<HeaderType>::size;
116+ // / the length offset due to trailer
86117 static const size_t tailOffset = typesize<TrailerType>::size;
118+ // / total length offset due to header and trailer
87119 static const size_t totalOffset = headOffset + tailOffset;
88120
89- // alias for callback checking the header, return true if the object
90- // is a valid header
121+ // / alias for callback checking the header, return true if the object
122+ // / is a valid header
91123 using CheckHeaderFct = std::function<bool (const HeaderType&)>;
92124
93- // alias for the argument type to be used in the CheckTrailer function
94- // have to forward to a valid type in case of void TrailerType in order
95- // to allow passing by reference
125+ // / alias for the argument type to be used in the CheckTrailer function
126+ // / have to forward to a valid type in case of void TrailerType in order
127+ // / to allow passing by reference
96128 using CheckTrailerFctArgumentT = typename std::conditional<
97129 !std::is_void<TrailerType>::value, TrailerType, int >::type;
98130
99- // alias for callback checking the trailer, takes reference to trailer
100- // object if TrailerType is a valid type, no argument otherwise
131+ // / alias for callback checking the trailer, takes reference to trailer
132+ // / object if TrailerType is a valid type, no argument otherwise
101133 template <typename U>
102134 using CheckTrailerFct = typename std::conditional<
103135 !std::is_void<U>::value,
104136 std::function<bool (const CheckTrailerFctArgumentT&)>,
105137 std::function<bool ()>>::type;
106138
107- // alias for callback to get the complete frame size including header,
108- // trailer and the data
139+ // / alias for callback to get the complete frame size including header,
140+ // / trailer and the data
109141 using GetFrameSizeFct = std::function<size_t (const HeaderType& )>;
110142
111- // function callback to insert/handle one frame into, sequentially called
112- // for all frames if the whole block has a valid format
143+ // / function callback to insert/handle one frame into, sequentially called
144+ // / for all frames if the whole block has a valid format
113145 using InsertFct = std::function<bool (FrameInfo&)>;
114146
147+ // / Parse buffer of size bufferSize, requires callbacks to check header
148+ // / trailer, the frame size, and insert callback to handle a FrameInfo
149+ // / object.
115150 template <typename InputType>
116151 int parse (const InputType* buffer, size_t bufferSize,
117152 CheckHeaderFct checkHeader,
@@ -172,6 +207,10 @@ class ForwardParser {
172207 return -1 ;
173208 }
174209
210+ // / Parse buffer of size bufferSize, specialization skipping the trailer
211+ // / check, e.g. when its type is void, or when the integrity of the trailer
212+ // / is not relevant. Requires callbacks to check header, frame size, and
213+ // / insert callback to handle a FrameInfo object.
175214 template <typename InputType, typename U = TrailerType>
176215 typename std::enable_if<std::is_void<U>::value, int >::type
177216 parse (const InputType* buffer, size_t bufferSize,
@@ -183,6 +222,8 @@ class ForwardParser {
183222 }
184223
185224private:
225+ // / internal function to check the trailer, distinguishes void and non-void
226+ // / trailer type.
186227 template <typename U = TrailerType>
187228 typename std::enable_if<!std::is_void<U>::value, bool >::type
188229 CheckTrailer (const FrameInfo& entry, CheckTrailerFct<TrailerType>& checkTrailer) const {
@@ -200,10 +241,14 @@ class ForwardParser {
200241 * @class ReverseParser
201242 * Parser for a sequence of frames with header, trailer and variable payload.
202243 * The size is expected to be part of the trailer, the parsing is thus in
203- * reverse direction.
244+ * reverse direction. Also the insert callback is called with the entries
245+ * starting form the end of the buffer.
246+ * TODO: an easy extension can be to reverse the order of the inserts, meaning
247+ * that the entries are read from the beginning.
204248 *
205249 * Usage:
206- * ReverseParser<SomeHeaderType, SomeTrailerType> SomeParser;
250+ * <pre>
251+ * using SomeParser = ReverseParser<SomeHeaderType, SomeTrailerType>;
207252 * SomeParser parser;
208253 * std::vector<typename SomeParser::FrameInfo> frames;
209254 * parser.parse(ptr, size,
@@ -226,6 +271,7 @@ class ForwardParser {
226271 * return true;
227272 * }
228273 * )
274+ * </pre>
229275 */
230276template <typename HeaderT, typename TrailerT>
231277class ReverseParser {
@@ -234,6 +280,7 @@ class ReverseParser {
234280 using TrailerType = TrailerT;
235281 using PayloadType = unsigned char ;
236282
283+ // / @struct FrameInfo a compound of header, data, and trailer
237284 struct FrameInfo {
238285 using PtrT = const PayloadType*;
239286
@@ -249,11 +296,21 @@ class ReverseParser {
249296 // / total length offset due to header and trailer
250297 static const size_t totalOffset = headOffset + tailOffset;
251298
299+ // / alias for callback checking the header, return true if the object
300+ // / is a valid header
252301 using CheckHeaderFct = std::function<bool (const HeaderType&)>;
302+ // / alias for callback checking the trailer
253303 using CheckTrailerFct = std::function<bool (const TrailerType&)>;
304+ // / alias for callback to get the complete frame size including header,
305+ // / trailer and the data
254306 using GetFrameSizeFct = std::function<size_t (const TrailerType&)>;
307+ // / function callback to insert/handle one frame into, sequentially called
308+ // / for all frames if the whole block has a valid format
255309 using InsertFct = std::function<bool (const FrameInfo&)>;
256310
311+ // / Parse buffer of size bufferSize, requires callbacks to check header
312+ // / trailer, the frame size, and insert callback to handle a FrameInfo
313+ // / object.
257314 template <typename InputType>
258315 int parse (const InputType* buffer, size_t bufferSize,
259316 CheckHeaderFct checkHeader,
0 commit comments