Project
Loading...
Searching...
No Matches
AlgorithmSpec.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef FRAMEWORK_ALGORITHMSPEC_H
12#define FRAMEWORK_ALGORITHMSPEC_H
13
17
19
20#include <functional>
21
22namespace o2::framework
23{
24
44 using ProcessCallback = std::function<void(ProcessingContext&)>;
45 using InitCallback = std::function<ProcessCallback(InitContext&)>;
46 using ErrorCallback = std::function<void(ErrorContext&)>;
47 using InitErrorCallback = std::function<void(InitErrorContext&)>;
48
52
53 AlgorithmSpec() = default;
54
56 AlgorithmSpec(const AlgorithmSpec&) = default;
59
61 : onInit{nullptr},
63 onError{error},
64 onInitError{nullptr}
65 {
66 }
67
69 : onInit{init},
70 onProcess{nullptr},
71 onError{error},
72 onInitError{initError}
73 {
74 }
75
80};
81
84 virtual AlgorithmSpec create(ConfigContext const&) = 0;
85};
86// Allow fetching inputs from the context using a string literal.
87template <StringLiteral lit, typename T>
88struct Input {
89 // The size of the string is available as a constant expression.
90 static constexpr auto size = sizeof(lit.value);
91 // and so is the string's content.
92 static constexpr auto contents = lit.value;
95 {
96 }
97 operator T const&() const
98 {
99 return ctx.inputs().template get<T>(lit.value);
100 }
101};
102
103template <typename T, typename S = std::void_t<>>
105 static decltype(auto) get(ProcessingContext& ctx)
106 {
107 return ctx.services().get<T>();
108 }
109 static decltype(auto) get(InitContext& ctx)
110 {
111 return ctx.services().get<T>();
112 }
113};
114
115template <>
118 {
119 return ctx.options();
120 }
121};
122
123template <typename S>
126 {
127 static_assert(always_static_assert_v<S>, "Should be ConfigParamRegistry const&");
128 return ctx.options();
129 }
130};
131
132template <>
135 {
136 return ctx.inputs();
137 }
138};
139
140template <>
143 {
144 return ctx.outputs();
145 }
146};
147
148template <>
151 {
152 return ctx;
153 }
154};
155
156template <>
159 {
160 return ctx;
161 }
162};
163
164template <StringLiteral L, typename T>
165struct ContextElementTraits<Input<L, T> const> {
167 {
168 return Input<L, T>{ctx};
169 }
170};
171
172template <StringLiteral L, typename T>
175 {
176 static_assert(always_static_assert_v<Input<L, T>>, "Should be Input<L, T> const&");
177 }
178};
179
180template <typename... CONTEXTELEMENT>
181AlgorithmSpec::ProcessCallback adaptStatelessF(std::function<void(CONTEXTELEMENT&...)> callback)
182{
183 return [callback](ProcessingContext& ctx) {
184 return callback(ContextElementTraits<CONTEXTELEMENT>::get(ctx)...);
185 };
186}
187
188template <typename... CONTEXTELEMENT>
190{
191 return [callback](InitContext& ctx) {
192 return callback(ContextElementTraits<CONTEXTELEMENT>::get(ctx)...);
193 };
194}
195
196template <typename R, typename... ARGS>
198{
199 std::function<R(ARGS...)> f = callback;
200 return adaptStatelessF(f);
201}
202
228template <typename LAMBDA>
230{
231 // MAGIC: this makes the lambda decay into a function / method pointer
232 return adaptStatelessF(FFL(l));
233}
234
235template <typename LAMBDA>
240
241} // namespace o2::framework
242
243#endif // FRAMEWORK_ALGORITHMSPEC_H
uint32_t c
Definition RawData.h:2
ServiceRegistryRef services()
Definition InitContext.h:34
ConfigParamRegistry const & options()
Definition InitContext.h:33
The input API of the Data Processing Layer This class holds the inputs which are valid for processing...
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
InputRecord & inputs()
The inputs associated with this processing context.
ServiceRegistryRef services()
The services registry associated with this processing context.
GLsizeiptr size
Definition glcorearb.h:659
GLdouble f
Definition glcorearb.h:310
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
AlgorithmSpec::InitCallback adaptStatefulF(std::function< AlgorithmSpec::ProcessCallback(CONTEXTELEMENT &...)> callback)
AlgorithmSpec::ProcessCallback adaptStatelessP(R(*callback)(ARGS...))
AlgorithmSpec::ProcessCallback adaptStateless(LAMBDA l)
AlgorithmSpec::ProcessCallback adaptStatelessF(std::function< void(CONTEXTELEMENT &...)> callback)
memfun_type< decltype(&F::operator())>::type FFL(F const &func)
AlgorithmSpec::InitCallback adaptStateful(LAMBDA l)
Helper class for an algorithm which is loaded as a plugin.
virtual AlgorithmSpec create(ConfigContext const &)=0
std::function< ProcessCallback(InitContext &)> InitCallback
AlgorithmSpec(AlgorithmSpec &)=default
static AlgorithmSpec dummyAlgorithm()
std::function< void(InitErrorContext &)> InitErrorCallback
AlgorithmSpec(InitCallback init, ErrorCallback &error=emptyErrorCallback(), InitErrorCallback &initError=emptyInitErrorCallback())
AlgorithmSpec & operator=(const AlgorithmSpec &)=default
std::function< void(ProcessingContext &)> ProcessCallback
static ErrorCallback & emptyErrorCallback()
AlgorithmSpec(const AlgorithmSpec &)=default
static InitErrorCallback & emptyInitErrorCallback()
std::function< void(ErrorContext &)> ErrorCallback
AlgorithmSpec(ProcessCallback process, ErrorCallback &error=emptyErrorCallback())
InitErrorCallback onInitError
AlgorithmSpec(AlgorithmSpec &&)=default
static ConfigParamRegistry const & get(InitContext &ctx)
static ConfigParamRegistry const & get(InitContext &ctx)
static DataAllocator & get(ProcessingContext &ctx)
static InitContext & get(InitContext &ctx)
static InputRecord & get(ProcessingContext &ctx)
static Input< L, T > get(ProcessingContext &ctx)
static Input< L, T > get(ProcessingContext &ctx)
static ProcessingContext & get(ProcessingContext &ctx)
static decltype(auto) get(ProcessingContext &ctx)
static decltype(auto) get(InitContext &ctx)
static constexpr auto contents
Input(ProcessingContext &c)
ProcessingContext & ctx