sessiond: ust: conditionally enable the underscore prefix variant quirk
[lttng-tools.git] / src / bin / lttng-sessiond / field.cpp
CommitLineData
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
15namespace lst = lttng::sessiond::trace;
16
17namespace {
18template <class FieldTypeSet>
19bool 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
33lst::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
39bool 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
45lst::type::type(unsigned int in_alignment) : alignment{in_alignment}
46{
47}
48
49lst::type::~type()
50{
51}
52
53bool 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
61bool lst::type::operator!=(const lst::type& other) const noexcept
62{
63 return !(*this == other);
64}
65
66lst::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
74void lst::field::accept(lst::field_visitor& visitor) const
75{
76 visitor.visit(*this);
77}
78
79bool lst::field::operator==(const lst::field& other) const noexcept
80{
81 return name == other.name && *_type == *other._type;
82}
83
45110cdd
JG
84lst::type::cuptr lst::field::move_type() noexcept
85{
86 return std::move(_type);
87}
88
89const 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
98lst::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
b6bbb1d6
JG
113lst::type::cuptr lst::integer_type::copy() const
114{
115 return lttng::make_unique<integer_type>(
116 alignment, byte_order, size, signedness_, base_, roles_);
117}
118
0220be14
JG
119bool lst::integer_type::_is_equal(const type &base_other) const noexcept
120{
121 const auto& other = static_cast<decltype(*this)&>(base_other);
122
123 return this->byte_order == other.byte_order &&
124 this->size == other.size &&
65cd3c0c 125 this->signedness_ == other.signedness_ &&
e7360180
JG
126 this->base_ == other.base_ &&
127 this->roles_ == other.roles_;
0220be14
JG
128}
129
130void lst::integer_type::accept(type_visitor& visitor) const
131{
132 visitor.visit(*this);
133}
134
135lst::byte_order lst::type::reverse_byte_order(lst::byte_order byte_order) noexcept
136{
137 if (byte_order == lst::byte_order::BIG_ENDIAN_) {
138 return lst::byte_order::LITTLE_ENDIAN_;
139 } else {
140 return lst::byte_order::BIG_ENDIAN_;
141 }
142}
143
144lst::floating_point_type::floating_point_type(unsigned int in_alignment,
145 lst::byte_order in_byte_order,
146 unsigned int in_exponent_digits,
147 unsigned int in_mantissa_digits) :
148 type(in_alignment),
149 byte_order(in_byte_order),
150 exponent_digits{in_exponent_digits},
151 mantissa_digits(in_mantissa_digits)
152{
153 /* Allowed (exponent, mantissa) pairs. */
d7bfb9b0 154 static const std::set<std::pair<unsigned int, unsigned int>> allowed_pairs{
0220be14
JG
155 {5, 11}, /* binary16 */
156 {8, 24}, /* binary32 */
157 {11, 53}, /* binary64 */
158 {15, 113}, /* binary128 */
159 };
160
d7bfb9b0
JG
161 if (allowed_pairs.find({exponent_digits, mantissa_digits}) != allowed_pairs.end()) {
162 /* mantissa and exponent digits is a valid pair. */
163 return;
0220be14
JG
164 }
165
166 LTTNG_THROW_INVALID_ARGUMENT_ERROR(
167 fmt::format("Invalid exponent/mantissa values provided while creating {}",
168 typeid(*this)));
169}
170
b6bbb1d6
JG
171lst::type::cuptr lst::floating_point_type::copy() const
172{
173 return lttng::make_unique<floating_point_type>(
174 alignment, byte_order, exponent_digits, mantissa_digits);
175}
176
0220be14
JG
177void lst::floating_point_type::accept(type_visitor& visitor) const
178{
179 visitor.visit(*this);
180}
181
182bool lst::floating_point_type::_is_equal(const type& base_other) const noexcept
183{
184 const auto& other = static_cast<decltype(*this)&>(base_other);
185
186 return this->byte_order == other.byte_order &&
187 this->exponent_digits == other.exponent_digits &&
188 this->mantissa_digits == other.mantissa_digits;
189}
190
191lst::enumeration_type::enumeration_type(unsigned int in_alignment,
192 enum lst::byte_order in_byte_order,
193 unsigned int in_size,
194 enum signedness in_signedness,
e7360180
JG
195 enum base in_base,
196 lst::integer_type::roles in_roles) :
197 integer_type(in_alignment,
198 in_byte_order,
199 in_size,
200 in_signedness,
201 in_base,
202 std::move(in_roles))
0220be14
JG
203{
204}
205
7a73918f
JG
206/*
207 * Due to a bug in g++ < 7.1, these specializations must be enclosed in the namespaces
208 * rather than using the usual `namespace::namespace::function` notation:
209 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
210 */
e2c2bec2
JR
211namespace lttng {
212namespace sessiond {
213namespace trace {
0220be14 214template <>
7a73918f 215void signed_enumeration_type::accept(type_visitor& visitor) const
0220be14
JG
216{
217 visitor.visit(*this);
218}
219
220template <>
7a73918f 221void unsigned_enumeration_type::accept(type_visitor& visitor) const
0220be14
JG
222{
223 visitor.visit(*this);
224}
225
45110cdd 226template <>
7a73918f 227void variant_type<lst::signed_enumeration_type::mapping::range_t::range_integer_t>::accept(
45110cdd
JG
228 lst::type_visitor& visitor) const
229{
230 visitor.visit(*this);
231}
232
233template <>
7a73918f 234void variant_type<lst::unsigned_enumeration_type::mapping::range_t::range_integer_t>::accept(
45110cdd
JG
235 lst::type_visitor& visitor) const
236{
237 visitor.visit(*this);
238}
7a73918f
JG
239} /* namespace trace */
240} /* namespace sessiond */
241} /* namespace lttng */
45110cdd 242
0220be14
JG
243lst::array_type::array_type(unsigned int in_alignment, type::cuptr in_element_type) :
244 type(in_alignment), element_type{std::move(in_element_type)}
245{
246}
247
248bool lst::array_type::_is_equal(const type& base_other) const noexcept
249{
250 const auto& other = static_cast<decltype(*this)&>(base_other);
251
252 return *this->element_type == *other.element_type;
253}
254
255lst::static_length_array_type::static_length_array_type(unsigned int in_alignment,
256 type::cuptr in_element_type,
257 uint64_t in_length) :
258 array_type(in_alignment, std::move(in_element_type)),
259 length{in_length}
260{
261}
262
263bool lst::static_length_array_type::_is_equal(const type& base_other) const noexcept
264{
265 const auto& other = static_cast<decltype(*this)&>(base_other);
266
267 return array_type::_is_equal(base_other) && this->length == other.length;
268}
269
b6bbb1d6
JG
270lst::type::cuptr lst::static_length_array_type::copy() const
271{
272 return lttng::make_unique<static_length_array_type>(
273 alignment, element_type->copy(), length);
274}
275
0220be14
JG
276void lst::static_length_array_type::accept(type_visitor& visitor) const
277{
278 visitor.visit(*this);
279}
280
281lst::dynamic_length_array_type::dynamic_length_array_type(unsigned int in_alignment,
282 type::cuptr in_element_type,
eda1aa02 283 lst::field_location in_length_field_location) :
0220be14 284 array_type(in_alignment, std::move(in_element_type)),
eda1aa02 285 length_field_location{std::move(in_length_field_location)}
0220be14
JG
286{
287}
288
289bool lst::dynamic_length_array_type::_is_equal(const type& base_other) const noexcept
290{
291 const auto& other = static_cast<decltype(*this)&>(base_other);
292
293 return array_type::_is_equal(base_other) &&
eda1aa02 294 this->length_field_location == other.length_field_location;
0220be14
JG
295}
296
b6bbb1d6
JG
297lst::type::cuptr lst::dynamic_length_array_type::copy() const
298{
299 return lttng::make_unique<dynamic_length_array_type>(
300 alignment, element_type->copy(), length_field_location);
301}
302
0220be14
JG
303void lst::dynamic_length_array_type::accept(type_visitor& visitor) const
304{
305 visitor.visit(*this);
306}
307
e7360180
JG
308lst::static_length_blob_type::static_length_blob_type(
309 unsigned int in_alignment, uint64_t in_length_bytes, roles in_roles) :
310 type(in_alignment), length_bytes{in_length_bytes}, roles_{std::move(in_roles)}
311{
312}
313
314bool lst::static_length_blob_type::_is_equal(const type& base_other) const noexcept
315{
316 const auto& other = static_cast<decltype(*this)&>(base_other);
317
318 return length_bytes == other.length_bytes && roles_ == other.roles_;
319}
320
b6bbb1d6
JG
321lst::type::cuptr lst::static_length_blob_type::copy() const
322{
323 return lttng::make_unique<static_length_blob_type>(alignment, length_bytes, roles_);
324}
325
e7360180
JG
326void lst::static_length_blob_type::accept(type_visitor& visitor) const
327{
328 visitor.visit(*this);
329}
330
331lst::dynamic_length_blob_type::dynamic_length_blob_type(
eda1aa02
JG
332 unsigned int in_alignment, lst::field_location in_length_field_location) :
333 type(in_alignment), length_field_location{std::move(in_length_field_location)}
e7360180
JG
334{
335}
336
337bool lst::dynamic_length_blob_type::_is_equal(const type& base_other) const noexcept
338{
339 const auto& other = dynamic_cast<decltype(*this)&>(base_other);
340
eda1aa02 341 return length_field_location == other.length_field_location;
e7360180
JG
342}
343
b6bbb1d6
JG
344lst::type::cuptr lst::dynamic_length_blob_type::copy() const
345{
346 return lttng::make_unique<dynamic_length_blob_type>(alignment, length_field_location);
347}
348
e7360180
JG
349void lst::dynamic_length_blob_type::accept(type_visitor& visitor) const
350{
351 visitor.visit(*this);
352}
353
0220be14 354lst::string_type::string_type(unsigned int in_alignment, enum encoding in_encoding) :
65cd3c0c 355 type(in_alignment), encoding_{in_encoding}
0220be14
JG
356{
357}
358
359bool lst::string_type::_is_equal(const type& base_other) const noexcept
360{
361 const auto& other = static_cast<decltype(*this)&>(base_other);
362
65cd3c0c 363 return this->encoding_ == other.encoding_;
0220be14
JG
364}
365
366lst::static_length_string_type::static_length_string_type(
367 unsigned int in_alignment, enum encoding in_encoding, uint64_t in_length) :
368 string_type(in_alignment, in_encoding), length{in_length}
369{
370}
371
372bool lst::static_length_string_type::_is_equal(const type& base_other) const noexcept
373{
374 const auto& other = static_cast<decltype(*this)&>(base_other);
375
376 return string_type::_is_equal(base_other) && this->length == other.length;
377}
378
b6bbb1d6
JG
379lst::type::cuptr lst::static_length_string_type::copy() const
380{
381 return lttng::make_unique<static_length_string_type>(alignment, encoding_, length);
382}
383
0220be14
JG
384void lst::static_length_string_type::accept(type_visitor& visitor) const
385{
386 visitor.visit(*this);
387}
388
389lst::dynamic_length_string_type::dynamic_length_string_type(unsigned int in_alignment,
390 enum encoding in_encoding,
eda1aa02
JG
391 field_location in_length_field_location) :
392 string_type(in_alignment, in_encoding),
393 length_field_location{std::move(in_length_field_location)}
0220be14
JG
394{
395}
396
397bool lst::dynamic_length_string_type::_is_equal(const type& base_other) const noexcept
398{
399 const auto& other = static_cast<decltype(*this)&>(base_other);
400
401 return string_type::_is_equal(base_other) &&
eda1aa02 402 this->length_field_location == other.length_field_location;
0220be14
JG
403}
404
b6bbb1d6
JG
405lst::type::cuptr lst::dynamic_length_string_type::copy() const
406{
407 return lttng::make_unique<dynamic_length_string_type>(
408 alignment, encoding_, length_field_location);
409}
410
0220be14
JG
411void lst::dynamic_length_string_type::accept(type_visitor& visitor) const
412{
413 visitor.visit(*this);
414}
415
416lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment,
417 enum encoding in_encoding) :
418 string_type(in_alignment, in_encoding)
419{
420}
421
b6bbb1d6
JG
422lst::type::cuptr lst::null_terminated_string_type::copy() const
423{
424 return lttng::make_unique<null_terminated_string_type>(alignment, encoding_);
425}
426
0220be14
JG
427void lst::null_terminated_string_type::accept(type_visitor& visitor) const
428{
429 visitor.visit(*this);
430}
431
432lst::structure_type::structure_type(unsigned int in_alignment, fields in_fields) :
da9dd521 433 type(in_alignment), fields_{std::move(in_fields)}
0220be14
JG
434{
435}
436
437bool lst::structure_type::_is_equal(const type& base_other) const noexcept
438{
439 const auto &other = static_cast<decltype(*this)&>(base_other);
440
da9dd521 441 return fields_are_equal(this->fields_, other.fields_);
0220be14
JG
442}
443
b6bbb1d6
JG
444lst::type::cuptr lst::structure_type::copy() const
445{
446 structure_type::fields copy_of_fields;
447
448 copy_of_fields.reserve(fields_.size());
449 for (const auto& field : fields_) {
450 copy_of_fields.emplace_back(lttng::make_unique<lst::field>(
451 field->name, field->get_type().copy()));
452 }
453
454 return lttng::make_unique<structure_type>(alignment, std::move(copy_of_fields));
455}
456
0220be14
JG
457void lst::structure_type::accept(type_visitor& visitor) const
458{
459 visitor.visit(*this);
7a73918f 460}
This page took 0.043848 seconds and 4 git commands to generate.