DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Fragment_serialization.hpp
Go to the documentation of this file.
1
10#ifndef DFMESSAGES_INCLUDE_DFMESSAGES_FRAGMENT_SERIALIZATION_HPP_
11#define DFMESSAGES_INCLUDE_DFMESSAGES_FRAGMENT_SERIALIZATION_HPP_
12
15#include "logging/Logging.hpp" // NOTE: if ISSUES ARE DECLARED BEFORE include logging/Logging.hpp, TLOG_DEBUG<<issue wont work.
16
17#include <memory>
18#include <vector>
19
20// MsgPack serialization functions (which just put the raw bytes of
21// the fragment array into a MsgPack message)
22namespace msgpack {
23MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
24{
25 namespace adaptor {
26
27 template<>
28 struct pack<dunedaq::daqdataformats::Fragment>
29 {
30 template<typename Stream>
31 packer<Stream>& operator()(msgpack::packer<Stream>& o, dunedaq::daqdataformats::Fragment const& frag) const
32 {
33 o.pack_bin(frag.get_size()); // pack header and size
34 o.pack_bin_body(static_cast<const char*>(frag.get_storage_location()), frag.get_size()); // pack payload
35 return o;
36 }
37 };
38
39 // Typically we use convert<> for deserialization, but Fragment isn't
40 // default constructible, so we have to use as<>. See:
41 // https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_adaptor#non-default-constructible-class-support-c11-only-since-120
42 template<>
43 struct as<dunedaq::daqdataformats::Fragment>
44 {
45 dunedaq::daqdataformats::Fragment operator()(msgpack::object const& o) const
46 {
47 // The second argument to the Fragment ctor is whether to copy
48 // the data array into the Fragment's own storage. Putting false
49 // here would be faster, but we have to copy, since the returned
50 // Fragment might outlast the msgpack::object which owns/points
51 // to the underlying data.
52 return dunedaq::daqdataformats::Fragment(const_cast<char*>(o.via.bin.ptr),
54 }
55 };
56
57 template<>
58 struct pack<std::unique_ptr<dunedaq::daqdataformats::Fragment>>
59 {
60 template<typename Stream>
61 packer<Stream>& operator()(msgpack::packer<Stream>& o,
62 std::unique_ptr<dunedaq::daqdataformats::Fragment> const& frag) const
63 {
64 o.pack_bin(frag->get_size()); // pack header and size
65 o.pack_bin_body(static_cast<const char*>(frag->get_storage_location()), frag->get_size()); // pack payload
66 return o;
67 }
68 };
69
70 // Typically we use convert<> for deserialization, but Fragment isn't
71 // default constructible, so we have to use as<>. See:
72 // https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_adaptor#non-default-constructible-class-support-c11-only-since-120
73 template<>
74 struct as<std::unique_ptr<dunedaq::daqdataformats::Fragment>>
75 {
76 std::unique_ptr<dunedaq::daqdataformats::Fragment> operator()(msgpack::object const& o) const
77 {
78 // The second argument to the Fragment ctor is whether to copy
79 // the data array into the Fragment's own storage. Putting false
80 // here would be faster, but we have to copy, since the returned
81 // Fragment might outlast the msgpack::object which owns/points
82 // to the underlying data.
83 return std::make_unique<dunedaq::daqdataformats::Fragment>(
85 }
86 };
87 } // namespace adaptor
88} // namespace MSGPACK_DEFAULT_API_NS
89} // namespace msgpack
90
92DUNE_DAQ_SERIALIZABLE(std::unique_ptr<dunedaq::daqdataformats::Fragment>, "Fragment");
93
94#endif // DFMESSAGES_INCLUDE_DFMESSAGES_FRAGMENT_SERIALIZATION_HPP_
#define DUNE_DAQ_SERIALIZABLE(Type, typestring)
C++ Representation of a DUNE Fragment, wrapping the flat byte array that is the Fragment's "actual" f...
Definition Fragment.hpp:38
@ kCopyFromBuffer
Copy the contents of the buffer into a new Fragment array.
fragment_size_t get_size() const
Get the total size of the Fragment.
Definition Fragment.hpp:242
const void * get_storage_location() const
Get a pointer to the Fragment's data array to read its contents directly.
Definition Fragment.hpp:105
Including Qt Headers.
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)