Skip to content

Commit 64cd1e6

Browse files
matthiasrichterktf
authored andcommitted
Starting module 'Algorithm' for generic tools and utilities
First algorithms: - parsing of O2 header stack - forward and reverse parsers for frame sequences consisting of header-payload-trailer composites
1 parent e4f6428 commit 64cd1e6

File tree

7 files changed

+808
-0
lines changed

7 files changed

+808
-0
lines changed

Algorithm/CMakeLists.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# @author Matthias Richter
2+
# @brief cmake setup for module Algorithm
3+
4+
set(MODULE_NAME "Algorithm")
5+
6+
O2_SETUP(NAME ${MODULE_NAME})
7+
8+
set(SRCS
9+
)
10+
11+
set(LIBRARY_NAME ${MODULE_NAME})
12+
13+
set(BUCKET_NAME Algorithm_bucket)
14+
15+
# no library for the moment
16+
#O2_GENERATE_LIBRARY()
17+
18+
Set(Exe_Names
19+
)
20+
21+
set(Exe_Source
22+
)
23+
24+
list(LENGTH Exe_Names _length)
25+
if (LENGTH)
26+
math(EXPR _length ${_length}-1)
27+
28+
ForEach (_file RANGE 0 ${_length})
29+
list(GET Exe_Names ${_file} _name)
30+
list(GET Exe_Source ${_file} _src)
31+
O2_GENERATE_EXECUTABLE(
32+
EXE_NAME ${_name}
33+
SOURCES ${_src}
34+
MODULE_LIBRARY_NAME ${LIBRARY_NAME}
35+
BUCKET_NAME ${BUCKET_NAME}
36+
)
37+
EndForEach (_file RANGE 0 ${_length})
38+
endif()
39+
40+
set(TEST_SRCS
41+
test/headerstack.cxx
42+
test/parser.cxx
43+
)
44+
45+
O2_GENERATE_TESTS(
46+
BUCKET_NAME ${BUCKET_NAME}
47+
TEST_SRCS ${TEST_SRCS}
48+
)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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_HEADERSTACK_H
12+
#define ALGORITHM_HEADERSTACK_H
13+
14+
/// @file HeaderStack.h
15+
/// @author Matthias Richter
16+
/// @since 2017-09-19
17+
/// @brief Utilities for the O2 header stack
18+
19+
// the implemented functionality relies on o2::Header::get defined in
20+
// DataHeader.h; all O2 headers inherit from BaseHeader, this is required
21+
// to check the consistency of the header stack, also the next-header-flag
22+
// is part of the BaseHeader
23+
#include "Headers/DataHeader.h" // for o2::Header::get
24+
25+
namespace o2 {
26+
27+
namespace algorithm {
28+
29+
/**
30+
* Generic utility for the O2 header stack, redirect to header specific callbacks
31+
*
32+
* The O2 header stack consists of one or more headers. The DataHeader is the
33+
* first one and mandatory. Other optional headers can be recursively extracted
34+
* from a buffer with one call to this utility function. For each header a pair
35+
* of type and callback has to be provided. The header type can be provided as
36+
* a dummy parameter, the callback with a lambda or any callable object.
37+
*
38+
* Usage:
39+
* dispatchHeaderStackCallback(ptr, size,
40+
* MyHeader(),
41+
* [] (const auto & h) {
42+
* // do something with h
43+
* }
44+
*/
45+
template<
46+
typename PtrType,
47+
typename SizeType,
48+
typename HeaderType,
49+
typename HeaderCallbackType,
50+
typename... MoreTypes
51+
>
52+
void dispatchHeaderStackCallback(PtrType ptr,
53+
SizeType size,
54+
HeaderType header,
55+
HeaderCallbackType onHeader,
56+
MoreTypes&&... types);
57+
58+
// template specialization: handler for one pair of type and callback
59+
template<
60+
typename PtrType,
61+
typename SizeType,
62+
typename HeaderType,
63+
typename HeaderCallbackType
64+
>
65+
void dispatchHeaderStackCallback(PtrType ptr,
66+
SizeType size,
67+
HeaderType /*dummy*/,
68+
HeaderCallbackType onHeader)
69+
{
70+
const HeaderType* h = o2::Header::get<HeaderType>(ptr, size);
71+
if (h) {
72+
onHeader(*h);
73+
}
74+
}
75+
76+
// an empty function in case no StackTypes have been provided to call
77+
template<typename PtrType, typename SizeType>
78+
void dispatchHeaderStackCallback(PtrType ptr, SizeType size) {}
79+
80+
// actual implementation
81+
template<
82+
typename PtrType,
83+
typename SizeType,
84+
typename HeaderType,
85+
typename HeaderCallbackType,
86+
typename... MoreTypes
87+
>
88+
void dispatchHeaderStackCallback(PtrType ptr,
89+
SizeType size,
90+
HeaderType header,
91+
HeaderCallbackType onHeader,
92+
MoreTypes&&... types)
93+
{
94+
// call for current
95+
dispatchHeaderStackCallback(ptr, size, header, onHeader);
96+
// call for next
97+
dispatchHeaderStackCallback(ptr, size, types...);
98+
}
99+
100+
/**
101+
* Generic utility for the O2 header stack, extract headers
102+
*
103+
* The O2 header stack consists of one or more headers. The DataHeader is the
104+
* first one and mandatory. Other optional headers can be recursively extracted
105+
* from a buffer with one call to this utility function. For each header to be
106+
* extracted, a variable can be passed be reference. If a header of corresponding
107+
* type is in the stack, its content will be assigned to the variable.
108+
*
109+
* Usage:
110+
* parseHeaderStack(ptr, size,
111+
* MyHeader(),
112+
* [] (const auto & h) {
113+
* // do something with h
114+
* }
115+
*/
116+
template<
117+
typename PtrType,
118+
typename SizeType,
119+
typename HeaderType,
120+
typename... MoreTypes
121+
>
122+
void parseHeaderStack(PtrType ptr,
123+
SizeType size,
124+
HeaderType & header,
125+
MoreTypes&&... types);
126+
127+
// template specialization: handler for one type
128+
template<
129+
typename PtrType,
130+
typename SizeType,
131+
typename HeaderType
132+
>
133+
void parseHeaderStack(PtrType ptr,
134+
SizeType size,
135+
HeaderType & header)
136+
{
137+
const HeaderType* h = o2::Header::get<HeaderType>(ptr, size);
138+
if (h) {
139+
header = *h;
140+
}
141+
}
142+
143+
// an empty function in case no StackTypes have been provided to call
144+
template<typename PtrType, typename SizeType>
145+
void parseHeaderStack(PtrType ptr, SizeType size) {}
146+
147+
// generic implementation
148+
template<
149+
typename PtrType,
150+
typename SizeType,
151+
typename HeaderType,
152+
typename... MoreTypes
153+
>
154+
void parseHeaderStack(PtrType ptr,
155+
SizeType size,
156+
HeaderType & header,
157+
MoreTypes&&... types)
158+
{
159+
// call for current
160+
parseHeaderStack(ptr, size, header);
161+
// call for next
162+
parseHeaderStack(ptr, size, types...);
163+
}
164+
165+
}; // namespace algorithm
166+
167+
}; // namespace o2
168+
169+
#endif //ALGORITHM_HEADERSTACK_H

0 commit comments

Comments
 (0)