ATLAS Offline Software
Loading...
Searching...
No Matches
TracccEdmSerialization.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
12
13#ifndef TRACCCTRITONCLIENT_TRACCCEDMSERIALIZATION_H
14#define TRACCCTRITONCLIENT_TRACCCEDMSERIALIZATION_H
15
16#include <cstddef>
17#include <cstdint>
18#include <cstring>
19#include <stdexcept>
20#include <type_traits>
21#include <utility>
22#include <vector>
23
24#include <vecmem/edm/host.hpp>
25
26namespace TracccTriton {
27
35namespace detail {
36
37inline void putBytes(std::vector<std::uint8_t>& out, const void* data,
38 std::size_t size) {
39 const auto* p = static_cast<const std::uint8_t*>(data);
40 out.insert(out.end(), p, p + size);
41}
42
43inline void putSize(std::vector<std::uint8_t>& out, std::uint64_t size) {
44 putBytes(out, &size, sizeof(size));
45}
46
47class Reader {
48public:
49 Reader(const std::uint8_t* data, std::size_t size)
50 : m_pos(data), m_end(data + size) {}
51
52 std::uint64_t size() {
53 std::uint64_t value;
54 bytes(&value, sizeof(value));
55 return value;
56 }
57
58 void bytes(void* dst, std::size_t size) {
59 if (static_cast<std::size_t>(m_end - m_pos) < size) {
60 throw std::runtime_error(
61 "TracccEdmSerialization: buffer too short while reading");
62 }
63 if (size != 0) {
64 std::memcpy(dst, m_pos, size);
65 }
66 m_pos += size;
67 }
68
69private:
70 const std::uint8_t* m_pos;
71 const std::uint8_t* m_end;
72};
73
74template <typename Element, typename Allocator>
75void write(std::vector<std::uint8_t>& out,
76 const std::vector<Element, Allocator>& variable) {
77 static_assert(std::is_trivially_copyable_v<Element>);
78 putSize(out, variable.size());
79 putBytes(out, variable.data(), variable.size() * sizeof(Element));
80}
81
82template <typename Element, typename InnerAllocator, typename OuterAllocator>
83void write(std::vector<std::uint8_t>& out,
84 const std::vector<std::vector<Element, InnerAllocator>,
85 OuterAllocator>& variable) {
86 static_assert(std::is_trivially_copyable_v<Element>);
87 putSize(out, variable.size());
88 for (const auto& inner : variable) {
89 putSize(out, inner.size());
90 }
91 for (const auto& inner : variable) {
92 putBytes(out, inner.data(), inner.size() * sizeof(Element));
93 }
94}
95
96template <typename Element, typename Allocator>
97void read(Reader& in, std::vector<Element, Allocator>& variable) {
98 variable.resize(in.size());
99 in.bytes(variable.data(), variable.size() * sizeof(Element));
100}
101
102template <typename Element, typename InnerAllocator, typename OuterAllocator>
103void read(Reader& in,
104 std::vector<std::vector<Element, InnerAllocator>, OuterAllocator>&
105 variable) {
106 variable.resize(in.size());
107 for (auto& inner : variable) {
108 inner.resize(in.size());
109 }
110 for (auto& inner : variable) {
111 in.bytes(inner.data(), inner.size() * sizeof(Element));
112 }
113}
114
115} // namespace detail
116
118template <typename... VARTYPES, template <typename> class INTERFACE>
120 const vecmem::edm::host<vecmem::edm::schema<VARTYPES...>, INTERFACE>& container,
121 std::vector<std::uint8_t>& out) {
122 [&]<std::size_t... Index>(std::index_sequence<Index...>) {
123 (detail::write(out, container.template get<Index>()), ...);
124 }(std::make_index_sequence<sizeof...(VARTYPES)>{});
125}
126
128template <typename... VARTYPES, template <typename> class INTERFACE>
130 const std::uint8_t* data, std::size_t size,
131 vecmem::edm::host<vecmem::edm::schema<VARTYPES...>, INTERFACE>& container) {
132 detail::Reader in(data, size);
133 [&]<std::size_t... Index>(std::index_sequence<Index...>) {
134 (detail::read(in, container.template get<Index>()), ...);
135 }(std::make_index_sequence<sizeof...(VARTYPES)>{});
136}
137
138} // namespace TracccTriton
139
140#endif // TRACCCTRITONCLIENT_TRACCCEDMSERIALIZATION_H
IndexedConstituentUserInfo::Index Index
size_t size() const
Number of registered mappings.
void bytes(void *dst, std::size_t size)
Reader(const std::uint8_t *data, std::size_t size)
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
void putSize(std::vector< std::uint8_t > &out, std::uint64_t size)
void putBytes(std::vector< std::uint8_t > &out, const void *data, std::size_t size)
void write(std::vector< std::uint8_t > &out, const std::vector< Element, Allocator > &variable)
void read(Reader &in, std::vector< Element, Allocator > &variable)
void serialize(const vecmem::edm::host< vecmem::edm::schema< VARTYPES... >, INTERFACE > &container, std::vector< std::uint8_t > &out)
Append container to out in the format described above.
void deserialize(const std::uint8_t *data, std::size_t size, vecmem::edm::host< vecmem::edm::schema< VARTYPES... >, INTERFACE > &container)
Fill container from size bytes at data.