Commit | Line | Data |
---|---|---|
0220be14 JG |
1 | /* |
2 | * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "field.hpp" | |
9 | ||
10 | #include <common/exception.hpp> | |
11 | #include <common/format.hpp> | |
12 | ||
d7bfb9b0 JG |
13 | #include <set> |
14 | ||
0220be14 JG |
15 | namespace lst = lttng::sessiond::trace; |
16 | ||
17 | namespace { | |
18 | template <class FieldTypeSet> | |
19 | bool fields_are_equal(const FieldTypeSet& a, const FieldTypeSet& b) | |
20 | { | |
21 | if (a.size() != b.size()) { | |
22 | return false; | |
23 | } | |
24 | ||
25 | return std::equal(a.cbegin(), a.cend(), b.cbegin(), | |
26 | [](typename FieldTypeSet::const_reference field_a, | |
27 | typename FieldTypeSet::const_reference field_b) { | |
28 | return *field_a == *field_b; | |
29 | }); | |
30 | } | |
31 | } /* namespace */ | |
32 | ||
eda1aa02 JG |
33 | lst::field_location::field_location(lst::field_location::root in_lookup_root, |
34 | lst::field_location::elements in_elements) : | |
35 | root_{in_lookup_root}, elements_{std::move(in_elements)} | |
36 | { | |
37 | } | |
38 | ||
39 | bool lst::field_location::operator==(const lst::field_location& other) const noexcept | |
40 | { | |
41 | return root_ == other.root_ && | |
42 | elements_ == other.elements_; | |
43 | } | |
44 | ||
0220be14 JG |
45 | lst::type::type(unsigned int in_alignment) : alignment{in_alignment} |
46 | { | |
47 | } | |
48 | ||
49 | lst::type::~type() | |
50 | { | |
51 | } | |
52 | ||
53 | bool lst::type::operator==(const lst::type& other) const noexcept | |
54 | { | |
55 | return typeid(*this) == typeid(other) && | |
56 | alignment == other.alignment && | |
57 | /* defer to concrete type comparison */ | |
58 | this->_is_equal(other); | |
59 | } | |
60 | ||
61 | bool lst::type::operator!=(const lst::type& other) const noexcept | |
62 | { | |
63 | return !(*this == other); | |
64 | } | |
65 | ||
66 | lst::field::field(std::string in_name, lst::type::cuptr in_type) : | |
67 | name{std::move(in_name)}, _type{std::move(in_type)} | |
68 | { | |
45110cdd JG |
69 | if (!_type) { |
70 | LTTNG_THROW_ERROR(fmt::format("Invalid type used to create field: field name = `{}`", name)); | |
71 | } | |
0220be14 JG |
72 | } |
73 | ||
74 | void lst::field::accept(lst::field_visitor& visitor) const | |
75 | { | |
76 | visitor.visit(*this); | |
77 | } | |
78 | ||
79 | bool lst::field::operator==(const lst::field& other) const noexcept | |
80 | { | |
81 | return name == other.name && *_type == *other._type; | |
82 | } | |
83 | ||
45110cdd JG |
84 | lst::type::cuptr lst::field::move_type() noexcept |
85 | { | |
86 | return std::move(_type); | |
87 | } | |
88 | ||
89 | const lst::type &lst::field::get_type() const | |
90 | { | |
91 | if (_type) { | |
92 | return *_type; | |
93 | } else { | |
94 | LTTNG_THROW_ERROR(fmt::format("Invalid attempt to access field type after transfer: field name = `{}`", name)); | |
95 | } | |
96 | } | |
97 | ||
0220be14 JG |
98 | lst::integer_type::integer_type(unsigned int in_alignment, |
99 | enum lst::byte_order in_byte_order, | |
100 | unsigned int in_size, | |
101 | enum lst::integer_type::signedness in_signedness, | |
e7360180 JG |
102 | enum lst::integer_type::base in_base, |
103 | roles in_roles) : | |
0220be14 JG |
104 | type(in_alignment), |
105 | byte_order{in_byte_order}, | |
106 | size{in_size}, | |
65cd3c0c | 107 | signedness_{in_signedness}, |
e7360180 JG |
108 | base_{in_base}, |
109 | roles_{std::move(in_roles)} | |
0220be14 JG |
110 | { |
111 | } | |
112 | ||
113 | bool lst::integer_type::_is_equal(const type &base_other) const noexcept | |
114 | { | |
115 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
116 | ||
117 | return this->byte_order == other.byte_order && | |
118 | this->size == other.size && | |
65cd3c0c | 119 | this->signedness_ == other.signedness_ && |
e7360180 JG |
120 | this->base_ == other.base_ && |
121 | this->roles_ == other.roles_; | |
0220be14 JG |
122 | } |
123 | ||
124 | void lst::integer_type::accept(type_visitor& visitor) const | |
125 | { | |
126 | visitor.visit(*this); | |
127 | } | |
128 | ||
129 | lst::byte_order lst::type::reverse_byte_order(lst::byte_order byte_order) noexcept | |
130 | { | |
131 | if (byte_order == lst::byte_order::BIG_ENDIAN_) { | |
132 | return lst::byte_order::LITTLE_ENDIAN_; | |
133 | } else { | |
134 | return lst::byte_order::BIG_ENDIAN_; | |
135 | } | |
136 | } | |
137 | ||
138 | lst::floating_point_type::floating_point_type(unsigned int in_alignment, | |
139 | lst::byte_order in_byte_order, | |
140 | unsigned int in_exponent_digits, | |
141 | unsigned int in_mantissa_digits) : | |
142 | type(in_alignment), | |
143 | byte_order(in_byte_order), | |
144 | exponent_digits{in_exponent_digits}, | |
145 | mantissa_digits(in_mantissa_digits) | |
146 | { | |
147 | /* Allowed (exponent, mantissa) pairs. */ | |
d7bfb9b0 | 148 | static const std::set<std::pair<unsigned int, unsigned int>> allowed_pairs{ |
0220be14 JG |
149 | {5, 11}, /* binary16 */ |
150 | {8, 24}, /* binary32 */ | |
151 | {11, 53}, /* binary64 */ | |
152 | {15, 113}, /* binary128 */ | |
153 | }; | |
154 | ||
d7bfb9b0 JG |
155 | if (allowed_pairs.find({exponent_digits, mantissa_digits}) != allowed_pairs.end()) { |
156 | /* mantissa and exponent digits is a valid pair. */ | |
157 | return; | |
0220be14 JG |
158 | } |
159 | ||
160 | LTTNG_THROW_INVALID_ARGUMENT_ERROR( | |
161 | fmt::format("Invalid exponent/mantissa values provided while creating {}", | |
162 | typeid(*this))); | |
163 | } | |
164 | ||
165 | void lst::floating_point_type::accept(type_visitor& visitor) const | |
166 | { | |
167 | visitor.visit(*this); | |
168 | } | |
169 | ||
170 | bool lst::floating_point_type::_is_equal(const type& base_other) const noexcept | |
171 | { | |
172 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
173 | ||
174 | return this->byte_order == other.byte_order && | |
175 | this->exponent_digits == other.exponent_digits && | |
176 | this->mantissa_digits == other.mantissa_digits; | |
177 | } | |
178 | ||
179 | lst::enumeration_type::enumeration_type(unsigned int in_alignment, | |
180 | enum lst::byte_order in_byte_order, | |
181 | unsigned int in_size, | |
182 | enum signedness in_signedness, | |
e7360180 JG |
183 | enum base in_base, |
184 | lst::integer_type::roles in_roles) : | |
185 | integer_type(in_alignment, | |
186 | in_byte_order, | |
187 | in_size, | |
188 | in_signedness, | |
189 | in_base, | |
190 | std::move(in_roles)) | |
0220be14 JG |
191 | { |
192 | } | |
193 | ||
e2c2bec2 JR |
194 | namespace lttng { |
195 | namespace sessiond { | |
196 | namespace trace { | |
0220be14 JG |
197 | template <> |
198 | void lst::signed_enumeration_type::accept(type_visitor& visitor) const | |
199 | { | |
200 | visitor.visit(*this); | |
201 | } | |
202 | ||
203 | template <> | |
204 | void lst::unsigned_enumeration_type::accept(type_visitor& visitor) const | |
205 | { | |
206 | visitor.visit(*this); | |
207 | } | |
e2c2bec2 JR |
208 | } /* namespace trace */ |
209 | } /* namespace sessiond */ | |
210 | } /* namespace lttng */ | |
0220be14 | 211 | |
45110cdd JG |
212 | template <> |
213 | void lst::variant_type<lst::signed_enumeration_type::mapping::range_t::range_integer_t>::accept( | |
214 | lst::type_visitor& visitor) const | |
215 | { | |
216 | visitor.visit(*this); | |
217 | } | |
218 | ||
219 | template <> | |
220 | void lst::variant_type<lst::unsigned_enumeration_type::mapping::range_t::range_integer_t>::accept( | |
221 | lst::type_visitor& visitor) const | |
222 | { | |
223 | visitor.visit(*this); | |
224 | } | |
225 | ||
0220be14 JG |
226 | lst::array_type::array_type(unsigned int in_alignment, type::cuptr in_element_type) : |
227 | type(in_alignment), element_type{std::move(in_element_type)} | |
228 | { | |
229 | } | |
230 | ||
231 | bool lst::array_type::_is_equal(const type& base_other) const noexcept | |
232 | { | |
233 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
234 | ||
235 | return *this->element_type == *other.element_type; | |
236 | } | |
237 | ||
238 | lst::static_length_array_type::static_length_array_type(unsigned int in_alignment, | |
239 | type::cuptr in_element_type, | |
240 | uint64_t in_length) : | |
241 | array_type(in_alignment, std::move(in_element_type)), | |
242 | length{in_length} | |
243 | { | |
244 | } | |
245 | ||
246 | bool lst::static_length_array_type::_is_equal(const type& base_other) const noexcept | |
247 | { | |
248 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
249 | ||
250 | return array_type::_is_equal(base_other) && this->length == other.length; | |
251 | } | |
252 | ||
253 | void lst::static_length_array_type::accept(type_visitor& visitor) const | |
254 | { | |
255 | visitor.visit(*this); | |
256 | } | |
257 | ||
258 | lst::dynamic_length_array_type::dynamic_length_array_type(unsigned int in_alignment, | |
259 | type::cuptr in_element_type, | |
eda1aa02 | 260 | lst::field_location in_length_field_location) : |
0220be14 | 261 | array_type(in_alignment, std::move(in_element_type)), |
eda1aa02 | 262 | length_field_location{std::move(in_length_field_location)} |
0220be14 JG |
263 | { |
264 | } | |
265 | ||
266 | bool lst::dynamic_length_array_type::_is_equal(const type& base_other) const noexcept | |
267 | { | |
268 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
269 | ||
270 | return array_type::_is_equal(base_other) && | |
eda1aa02 | 271 | this->length_field_location == other.length_field_location; |
0220be14 JG |
272 | } |
273 | ||
274 | void lst::dynamic_length_array_type::accept(type_visitor& visitor) const | |
275 | { | |
276 | visitor.visit(*this); | |
277 | } | |
278 | ||
e7360180 JG |
279 | lst::static_length_blob_type::static_length_blob_type( |
280 | unsigned int in_alignment, uint64_t in_length_bytes, roles in_roles) : | |
281 | type(in_alignment), length_bytes{in_length_bytes}, roles_{std::move(in_roles)} | |
282 | { | |
283 | } | |
284 | ||
285 | bool lst::static_length_blob_type::_is_equal(const type& base_other) const noexcept | |
286 | { | |
287 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
288 | ||
289 | return length_bytes == other.length_bytes && roles_ == other.roles_; | |
290 | } | |
291 | ||
292 | void lst::static_length_blob_type::accept(type_visitor& visitor) const | |
293 | { | |
294 | visitor.visit(*this); | |
295 | } | |
296 | ||
297 | lst::dynamic_length_blob_type::dynamic_length_blob_type( | |
eda1aa02 JG |
298 | unsigned int in_alignment, lst::field_location in_length_field_location) : |
299 | type(in_alignment), length_field_location{std::move(in_length_field_location)} | |
e7360180 JG |
300 | { |
301 | } | |
302 | ||
303 | bool lst::dynamic_length_blob_type::_is_equal(const type& base_other) const noexcept | |
304 | { | |
305 | const auto& other = dynamic_cast<decltype(*this)&>(base_other); | |
306 | ||
eda1aa02 | 307 | return length_field_location == other.length_field_location; |
e7360180 JG |
308 | } |
309 | ||
310 | void lst::dynamic_length_blob_type::accept(type_visitor& visitor) const | |
311 | { | |
312 | visitor.visit(*this); | |
313 | } | |
314 | ||
0220be14 | 315 | lst::string_type::string_type(unsigned int in_alignment, enum encoding in_encoding) : |
65cd3c0c | 316 | type(in_alignment), encoding_{in_encoding} |
0220be14 JG |
317 | { |
318 | } | |
319 | ||
320 | bool lst::string_type::_is_equal(const type& base_other) const noexcept | |
321 | { | |
322 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
323 | ||
65cd3c0c | 324 | return this->encoding_ == other.encoding_; |
0220be14 JG |
325 | } |
326 | ||
327 | lst::static_length_string_type::static_length_string_type( | |
328 | unsigned int in_alignment, enum encoding in_encoding, uint64_t in_length) : | |
329 | string_type(in_alignment, in_encoding), length{in_length} | |
330 | { | |
331 | } | |
332 | ||
333 | bool lst::static_length_string_type::_is_equal(const type& base_other) const noexcept | |
334 | { | |
335 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
336 | ||
337 | return string_type::_is_equal(base_other) && this->length == other.length; | |
338 | } | |
339 | ||
340 | void lst::static_length_string_type::accept(type_visitor& visitor) const | |
341 | { | |
342 | visitor.visit(*this); | |
343 | } | |
344 | ||
345 | lst::dynamic_length_string_type::dynamic_length_string_type(unsigned int in_alignment, | |
346 | enum encoding in_encoding, | |
eda1aa02 JG |
347 | field_location in_length_field_location) : |
348 | string_type(in_alignment, in_encoding), | |
349 | length_field_location{std::move(in_length_field_location)} | |
0220be14 JG |
350 | { |
351 | } | |
352 | ||
353 | bool lst::dynamic_length_string_type::_is_equal(const type& base_other) const noexcept | |
354 | { | |
355 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
356 | ||
357 | return string_type::_is_equal(base_other) && | |
eda1aa02 | 358 | this->length_field_location == other.length_field_location; |
0220be14 JG |
359 | } |
360 | ||
361 | void lst::dynamic_length_string_type::accept(type_visitor& visitor) const | |
362 | { | |
363 | visitor.visit(*this); | |
364 | } | |
365 | ||
366 | lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment, | |
367 | enum encoding in_encoding) : | |
368 | string_type(in_alignment, in_encoding) | |
369 | { | |
370 | } | |
371 | ||
372 | void lst::null_terminated_string_type::accept(type_visitor& visitor) const | |
373 | { | |
374 | visitor.visit(*this); | |
375 | } | |
376 | ||
377 | lst::structure_type::structure_type(unsigned int in_alignment, fields in_fields) : | |
da9dd521 | 378 | type(in_alignment), fields_{std::move(in_fields)} |
0220be14 JG |
379 | { |
380 | } | |
381 | ||
382 | bool lst::structure_type::_is_equal(const type& base_other) const noexcept | |
383 | { | |
384 | const auto &other = static_cast<decltype(*this)&>(base_other); | |
385 | ||
da9dd521 | 386 | return fields_are_equal(this->fields_, other.fields_); |
0220be14 JG |
387 | } |
388 | ||
389 | void lst::structure_type::accept(type_visitor& visitor) const | |
390 | { | |
391 | visitor.visit(*this); | |
da9dd521 | 392 | } |