Project
Loading...
Searching...
No Matches
CallbackRegistry.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 O2_FRAMEWORK_CALLBACKREGISTRY_H_
12#define O2_FRAMEWORK_CALLBACKREGISTRY_H_
13
15#include "Framework/Pack.h"
16#include <functional>
17
18namespace o2::framework
19{
20
21template <typename KeyT, KeyT _id, typename CallbackT>
23 using id = std::integral_constant<KeyT, _id>;
24 using type = CallbackT;
26};
27
28template <typename CallbackId, typename... Args>
30{
31 public:
32 static constexpr std::size_t size = sizeof...(Args);
33 using CallbackTypes = o2::framework::pack<typename Args::type...>;
34 using CallbackStore = std::array<std::vector<void*>, size>;
35 CallbackRegistry() = default;
36
37 // set callback for slot
38 template <CallbackId ID, typename U>
39 void set(U&& cb)
40 {
41 using CallbackType = typename o2::framework::pack_element_t<(int)ID, CallbackTypes>;
42 mStore[(int)ID].push_back(reinterpret_cast<void*>(new CallbackType(std::forward<U>(cb))));
43 }
44
45 // execute callback at specified slot with argument pack
46 template <CallbackId ID, typename... TArgs>
47 void call(TArgs&&... args)
48 {
49 using CallbackType = typename o2::framework::pack_element_t<(int)ID, CallbackTypes>;
50 static_assert(std::is_same_v<CallbackType, std::function<void(TArgs...)>>, "callback type mismatch");
51 auto& v = mStore[(int)ID];
52 for (auto& ptr : v) {
53 auto cb = reinterpret_cast<CallbackType*>(ptr);
54 if (cb == nullptr) {
55 continue;
56 }
57 (*cb)(std::forward<TArgs>(args)...);
58 }
59 }
60
61 private:
62 // store of RegistryPairs
63 CallbackStore mStore;
64};
65} // namespace o2::framework
66#endif // O2_FRAMEWORK_CALLBACKREGISTRY_H_
TBranch * ptr
std::array< std::vector< void * >, size > CallbackStore
GLsizeiptr size
Definition glcorearb.h:659
const GLdouble * v
Definition glcorearb.h:832
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
typename pack_element< I, T >::type pack_element_t
Definition Pack.h:56
std::integral_constant< KeyT, _id > id