DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TriggerObjectOverlay.hpp
Go to the documentation of this file.
1
8
9#ifndef TRIGGERALGS_INCLUDE_TRIGGERALGS_TRIGGEROBJECTOVERLAY_HPP_
10#define TRIGGERALGS_INCLUDE_TRIGGERALGS_TRIGGEROBJECTOVERLAY_HPP_
11
12
15
19
20namespace triggeralgs {
21
22// TypeToOverlayType acts as a "map" from a type to its overlay type
23// (eg from TriggerCandidate to dunedaq::trgdataformats::TriggerCandidate). The point of
24// having this is so that we can call the various overlay conversion
25// functions below without having to explicitly specify the template
26// arguments. An alternative way to do this would be to have
27// TriggerActivity and TriggerCandidate each contain a using declaration
28// like `using overlay_t = dunedaq::trgdataformats::Trigger(Activity|Candidate);`, but I
29// couldn't figure out the right forward declaration for the overlay
30// types in the TriggerCandidate/TriggerActivity header files
31template<class T>
33
34template<>
40
41template<>
47
48// Populate a TriggerObjectOverlay in `buffer`, created from
49// `object`. The necessary size for the buffer can be found with
50// `get_overlay_nbytes()`
51template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t,
52 class Data = typename TypeToOverlayType<Object>::data_t>
53void
54write_overlay(const Object& object, void* buffer)
55{
56 Overlay* overlay = reinterpret_cast<Overlay*>(buffer);
57 overlay->data = static_cast<Data>(object);
58 overlay->set_inputs(object.inputs);
59}
60
61// Calculate the size of buffer (in bytes) required to store an
62// overlay created from `object`
63template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t>
64size_t
65get_overlay_nbytes(const Object& object)
66{
67 // Need to check that this is actually right: what does sizeof
68 // return when the class contains a flexible array member? Seems to
69 // work in unit tests, so probably this is right
70 return sizeof(Overlay) + object.inputs.size() * sizeof(typename Overlay::input_t);
71}
72
73// Given an overlay object (dunedaq::trgdataformats::TriggerActivity or
74// dunedaq::trgdataformats::TriggerCandidate), create a corresponding non-overlay object
75// (triggeralgs::TriggerActivity or triggeralgs::TriggerCandidate) with the same contents, and return it
76template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t,
77 class Data = typename TypeToOverlayType<Object>::data_t>
78Object
79read_overlay(const Overlay& overlay)
80{
81 Object ret;
82 // overlay.data is a Trigger(Activity|Candidate)Data, which is the
83 // base class of Trigger(Activity|Candidate). So we want to set the
84 // base-class part of `ret` to `overlay.data`, which is what the
85 // static_cast is for. We have to do the cast on the pointer to get
86 // reference semantics (without the &, we get a _copy_ of ret, which
87 // is not what we want)
88 *static_cast<Data*>(&ret) = overlay.data;
89 for (uint64_t i = 0; i < overlay.n_inputs; ++i) {
90 ret.inputs.push_back(overlay.inputs[i]);
91 }
92 return ret;
93}
94
95// Given a buffer containing an overlay object (dunedaq::trgdataformats::TriggerActivity
96// or dunedaq::trgdataformats::TriggerCandidate), create a corresponding non-overlay
97// object (triggeralgs::TriggerActivity or triggeralgs::TriggerCandidate) with the same
98// contents, and return it. This function requires the return type to
99// be explicitly specified as a template argument, unlike the other
100// functions in this file. Eg
101//
102// TriggerActivity activity_read = read_overlay_from_buffer<TriggerActivity>(buffer);
103template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t>
104Object
105read_overlay_from_buffer(const void* buffer)
106{
107 return read_overlay<Object, Overlay>(*reinterpret_cast<const Overlay*>(buffer));
108}
109
110} // namespace triggeralgs
111
112#endif // TRIGGERALGS_INCLUDE_TRIGGERALGS_TRIGGEROBJECTOVERLAY_HPP_
TriggerObjectOverlay< TriggerActivityData, TriggerPrimitive > TriggerActivity
TriggerObjectOverlay< TriggerCandidateData, TriggerActivityData > TriggerCandidate
Object read_overlay_from_buffer(const void *buffer)
void write_overlay(const Object &object, void *buffer)
Object read_overlay(const Overlay &overlay)
size_t get_overlay_nbytes(const Object &object)
dunedaq::trgdataformats::TriggerActivityData data_t
dunedaq::trgdataformats::TriggerActivity overlay_t
dunedaq::trgdataformats::TriggerCandidateData data_t
dunedaq::trgdataformats::TriggerCandidate overlay_t