srs-control 0.2.2
Loading...
Searching...
No Matches
StructSerializer.cpp
Go to the documentation of this file.
7#include <algorithm>
8#include <bit>
9#include <bitset>
10#include <cassert>
11#include <concepts>
12#include <cstddef>
13#include <cstdint>
14#include <expected>
15#include <string_view>
16#include <type_traits>
17#include <vector>
18#include <zpp_bits.h>
19
20namespace srs::process
21{
22 namespace
23 {
24 template <class T>
25 auto check_compact_max_size(const T& value, size_t length) -> std::expected<void, std::string_view>
26 {
27 if (value >= 1ULL << length)
28 {
29 return std::unexpected("Serialization: exceeding size limit!");
30 }
31 return {};
32 }
33
34 auto marker_to_compact(const MarkerData& marker_data, internal::MarkerDataCompact& marker_data_compact)
35 -> std::expected<void, std::string_view>
36 {
37 auto is_correct_sizes =
38 check_compact_max_size(marker_data.vmm_id, internal::VMM_ID_BIT_LENGTH)
39 .and_then(
40 [&]
41 {
42 return check_compact_max_size(
43 marker_data.srs_timestamp,
45 });
46 if (!is_correct_sizes)
47 {
48 return std::unexpected(is_correct_sizes.error());
49 }
50#pragma GCC diagnostic push
51#pragma GCC diagnostic ignored "-Wconversion"
52 auto timestamp_bits =
53 std::bitset<common::SRS_TIMESTAMP_HIGH_BIT_LENGTH + common::SRS_TIMESTAMP_LOW_BIT_LENGTH>(
54 marker_data.srs_timestamp);
55 auto timestamp_low_bits = common::get_low_bits<common::SRS_TIMESTAMP_LOW_BIT_LENGTH>(timestamp_bits);
56 auto timestamp_high_bits = common::get_high_bits<common::SRS_TIMESTAMP_HIGH_BIT_LENGTH>(timestamp_bits);
57 marker_data_compact.timestamp_low_bits =
58 static_cast<decltype(marker_data_compact.timestamp_low_bits)>(timestamp_low_bits.to_ulong());
59 marker_data_compact.timestamp_high_bits =
60 static_cast<decltype(marker_data_compact.timestamp_high_bits)>(timestamp_high_bits.to_ullong());
61 marker_data_compact.flag = static_cast<decltype(marker_data_compact.flag)>(0);
62 marker_data_compact.vmm_id = static_cast<decltype(marker_data_compact.vmm_id)>(marker_data.vmm_id);
63 return {};
64#pragma GCC diagnostic pop
65 }
66
67 auto hit_to_compact(const HitData& hit_data, internal::HitDataCompact& hit_data_compact)
68 -> std::expected<void, std::string_view>
69 {
70 auto is_correct_sizes =
71 check_compact_max_size(hit_data.channel_num, internal::CHANNEL_NUM_BIT_LENGTH)
72 .and_then([&] { return check_compact_max_size(hit_data.bc_id, internal::BC_ID_BIT_LENGTH); })
73 .and_then([&] { return check_compact_max_size(hit_data.adc, internal::ADC_BIT_LENGTH); })
74 .and_then([&] { return check_compact_max_size(hit_data.vmm_id, internal::VMM_ID_BIT_LENGTH); })
75 .and_then([&] { return check_compact_max_size(hit_data.offset, internal::OFFSET_BIT_LENGTH); });
76 if (!is_correct_sizes)
77 {
78 return std::unexpected(is_correct_sizes.error());
79 }
80#pragma GCC diagnostic push
81#pragma GCC diagnostic ignored "-Wconversion"
82 hit_data_compact.vmm_id = static_cast<decltype(hit_data_compact.vmm_id)>(hit_data.vmm_id);
83 hit_data_compact.flag = static_cast<decltype(hit_data_compact.flag)>(1);
84 hit_data_compact.adc = static_cast<decltype(hit_data_compact.adc)>(hit_data.adc);
85 hit_data_compact.bc_id = static_cast<decltype(hit_data_compact.bc_id)>(hit_data.bc_id);
86 hit_data_compact.bc_id = common::binary_to_gray(hit_data_compact.bc_id);
87 hit_data_compact.channel_num = static_cast<decltype(hit_data_compact.channel_num)>(hit_data.channel_num);
88 hit_data_compact.is_over_threshold =
89 static_cast<decltype(hit_data_compact.is_over_threshold)>(hit_data.is_over_threshold);
90 hit_data_compact.offset = static_cast<decltype(hit_data_compact.offset)>(hit_data.offset);
91 hit_data_compact.tdc = static_cast<decltype(hit_data_compact.tdc)>(hit_data.tdc);
92#pragma GCC diagnostic pop
93 return {};
94 }
95
96 template <class T>
97 requires(std::same_as<std::remove_cvref_t<T>, internal::HitDataCompact> ||
98 std::same_as<std::remove_cvref_t<T>, internal::MarkerDataCompact>)
99 auto compact_to_vector(const T& compact_data, std::vector<char>& output) -> void
100 {
101 auto compact_bitset =
102 std::bitset<sizeof(std::uint64_t) * common::BYTE_BIT_LENGTH>{ std::bit_cast<uint64_t>(compact_data) };
103 output.resize(sizeof(std::uint64_t));
104 auto write_to_output = zpp::bits::out{ output, zpp::bits::endian::network{}, zpp::bits::no_size{} };
105 write_to_output(compact_bitset).or_throw();
106 output.erase(output.begin(), output.begin() + 2);
107 std::ranges::reverse(output);
108 }
109 }; // namespace
110
112 : ConverterTask{ "Struct deserializer", none, n_lines }
113 {
114 output_data_.resize(n_lines);
115 compact_data_buffer_.resize(n_lines);
116 }
117
118 // NOLINTBEGIN
119 auto StructSerializer::convert([[maybe_unused]] const StructData* input,
120 [[maybe_unused]] std::vector<char>& output,
121 std::vector<char>& compact_data_buffer)
122 -> std::expected<std::size_t, std::string_view>
123 {
124 compact_data_buffer.resize(0, 0);
125 auto serialize_to = zpp::bits::out{ output, zpp::bits::endian::network{}, zpp::bits::no_size{} };
126 serialize_to(input->header).or_throw();
127 for (auto hit : input->hit_data)
128 {
129 std::ranges::fill(compact_data_buffer, 0);
130 auto hit_compact = internal::HitDataCompact{};
131 if (auto res = hit_to_compact(hit, hit_compact); !res)
132 {
133 return std::unexpected(res.error());
134 }
135 compact_to_vector(hit_compact, compact_data_buffer);
136 serialize_to(compact_data_buffer_).or_throw();
137 }
138 for (auto marker : input->marker_data)
139 {
140 std::ranges::fill(compact_data_buffer, 0);
141 auto marker_compact = internal::MarkerDataCompact{};
142 if (auto res = marker_to_compact(marker, marker_compact); !res)
143 {
144 return std::unexpected(res.error());
145 }
146 compact_to_vector(marker_compact, compact_data_buffer);
147 serialize_to(compact_data_buffer).or_throw();
148 }
149 return input->marker_data.size() + input->hit_data.size();
150 }
151 // NOLINTEND
152} // namespace srs::process
ConverterTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition BaseTask.hpp:98
std::vector< std::vector< char > > output_data_
auto convert(const StructData *input, std::vector< char > &output, std::vector< char > &compact_data_buffer) -> std::expected< std::size_t, std::string_view >
std::vector< std::vector< char > > compact_data_buffer_
constexpr auto binary_to_gray(T bin_val)
constexpr auto get_high_bits(const std::bitset< total_size > &bits) -> std::bitset< high_size >
constexpr auto SRS_TIMESTAMP_LOW_BIT_LENGTH
constexpr auto get_low_bits(const std::bitset< total_size > &bits) -> std::bitset< low_size >
constexpr auto BYTE_BIT_LENGTH
constexpr auto SRS_TIMESTAMP_HIGH_BIT_LENGTH
constexpr auto OFFSET_BIT_LENGTH
constexpr auto ADC_BIT_LENGTH
constexpr auto BC_ID_BIT_LENGTH
constexpr auto VMM_ID_BIT_LENGTH
constexpr auto CHANNEL_NUM_BIT_LENGTH