DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TriggerObjectOverlay.hpp
Go to the documentation of this file.
1
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->n_inputs = object.inputs.size();
59 for (size_t i = 0; i < object.inputs.size(); ++i) {
60 overlay->inputs[i] = object.inputs[i];
61 }
62}
63
64// Calculate the size of buffer (in bytes) required to store an
65// overlay created from `object`
66template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t>
67size_t
68get_overlay_nbytes(const Object& object)
69{
70 // Need to check that this is actually right: what does sizeof
71 // return when the class contains a flexible array member? Seems to
72 // work in unit tests, so probably this is right
73 return sizeof(Overlay) + object.inputs.size() * sizeof(typename Overlay::input_t);
74}
75
76// Given an overlay object (dunedaq::trgdataformats::TriggerActivity or
77// dunedaq::trgdataformats::TriggerCandidate), create a corresponding non-overlay object
78// (triggeralgs::TriggerActivity or triggeralgs::TriggerCandidate) with the same contents, and return it
79template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t,
80 class Data = typename TypeToOverlayType<Object>::data_t>
81Object
82read_overlay(const Overlay& overlay)
83{
84 Object ret;
85 // overlay.data is a Trigger(Activity|Candidate)Data, which is the
86 // base class of Trigger(Activity|Candidate). So we want to set the
87 // base-class part of `ret` to `overlay.data`, which is what the
88 // static_cast is for. We have to do the cast on the pointer to get
89 // reference semantics (without the &, we get a _copy_ of ret, which
90 // is not what we want)
91 *static_cast<Data*>(&ret) = overlay.data;
92 for (uint64_t i = 0; i < overlay.n_inputs; ++i) {
93 ret.inputs.push_back(overlay.inputs[i]);
94 }
95 return ret;
96}
97
98// Given a buffer containing an overlay object (dunedaq::trgdataformats::TriggerActivity
99// or dunedaq::trgdataformats::TriggerCandidate), create a corresponding non-overlay
100// object (triggeralgs::TriggerActivity or triggeralgs::TriggerCandidate) with the same
101// contents, and return it. This function requires the return type to
102// be explicitly specified as a template argument, unlike the other
103// functions in this file. Eg
104//
105// TriggerActivity activity_read = read_overlay_from_buffer<TriggerActivity>(buffer);
106template<class Object, class Overlay = typename TypeToOverlayType<Object>::overlay_t>
107Object
108read_overlay_from_buffer(const void* buffer)
109{
110 return read_overlay<Object, Overlay>(*reinterpret_cast<const Overlay*>(buffer));
111}
112
113} // namespace triggeralgs
114
115#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)