2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/exception.hpp>
11 #include <common/format.hpp>
15 namespace lst
= lttng::sessiond::trace
;
18 template <class FieldTypeSet
>
19 bool fields_are_equal(const FieldTypeSet
& a
, const FieldTypeSet
& b
)
21 if (a
.size() != b
.size()) {
25 return std::equal(a
.cbegin(),
28 [](typename
FieldTypeSet::const_reference field_a
,
29 typename
FieldTypeSet::const_reference field_b
) {
30 return *field_a
== *field_b
;
35 lst::field_location::field_location(lst::field_location::root in_lookup_root
,
36 lst::field_location::elements in_elements
) :
37 root_
{ in_lookup_root
}, elements_
{ std::move(in_elements
) }
41 bool lst::field_location::operator==(const lst::field_location
& other
) const noexcept
43 return root_
== other
.root_
&& elements_
== other
.elements_
;
46 lst::type::type(unsigned int in_alignment
) : alignment
{ in_alignment
}
54 bool lst::type::operator==(const lst::type
& other
) const noexcept
56 return typeid(*this) == typeid(other
) && alignment
== other
.alignment
&&
57 /* defer to concrete type comparison */
58 this->_is_equal(other
);
61 bool lst::type::operator!=(const lst::type
& other
) const noexcept
63 return !(*this == other
);
66 lst::field::field(std::string in_name
, lst::type::cuptr in_type
) :
67 name
{ std::move(in_name
) }, _type
{ std::move(in_type
) }
71 fmt::format("Invalid type used to create field: field name = `{}`", name
));
75 void lst::field::accept(lst::field_visitor
& visitor
) const
80 bool lst::field::operator==(const lst::field
& other
) const noexcept
82 return name
== other
.name
&& *_type
== *other
._type
;
85 lst::type::cuptr
lst::field::move_type() noexcept
87 return std::move(_type
);
90 const lst::type
& lst::field::get_type() const
95 LTTNG_THROW_ERROR(fmt::format(
96 "Invalid attempt to access field type after transfer: field name = `{}`",
101 lst::integer_type::integer_type(unsigned int in_alignment
,
102 enum lst::byte_order in_byte_order
,
103 unsigned int in_size
,
104 enum lst::integer_type::signedness in_signedness
,
105 enum lst::integer_type::base in_base
,
108 byte_order
{ in_byte_order
},
110 signedness_
{ in_signedness
},
112 roles_
{ std::move(in_roles
) }
116 lst::type::cuptr
lst::integer_type::copy() const
118 return lttng::make_unique
<integer_type
>(
119 alignment
, byte_order
, size
, signedness_
, base_
, roles_
);
122 bool lst::integer_type::_is_equal(const type
& base_other
) const noexcept
124 const auto& other
= static_cast<decltype(*this)&>(base_other
);
126 return this->byte_order
== other
.byte_order
&& this->size
== other
.size
&&
127 this->signedness_
== other
.signedness_
&& this->base_
== other
.base_
&&
128 this->roles_
== other
.roles_
;
131 void lst::integer_type::accept(type_visitor
& visitor
) const
133 visitor
.visit(*this);
136 lst::byte_order
lst::type::reverse_byte_order(lst::byte_order byte_order
) noexcept
138 if (byte_order
== lst::byte_order::BIG_ENDIAN_
) {
139 return lst::byte_order::LITTLE_ENDIAN_
;
141 return lst::byte_order::BIG_ENDIAN_
;
145 lst::floating_point_type::floating_point_type(unsigned int in_alignment
,
146 lst::byte_order in_byte_order
,
147 unsigned int in_exponent_digits
,
148 unsigned int in_mantissa_digits
) :
150 byte_order(in_byte_order
),
151 exponent_digits
{ in_exponent_digits
},
152 mantissa_digits(in_mantissa_digits
)
154 /* Allowed (exponent, mantissa) pairs. */
155 static const std::set
<std::pair
<unsigned int, unsigned int>> allowed_pairs
{
156 { 5, 11 }, /* binary16 */
157 { 8, 24 }, /* binary32 */
158 { 11, 53 }, /* binary64 */
159 { 15, 113 }, /* binary128 */
162 if (allowed_pairs
.find({ exponent_digits
, mantissa_digits
}) != allowed_pairs
.end()) {
163 /* mantissa and exponent digits is a valid pair. */
167 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
168 "Invalid exponent/mantissa values provided while creating {}", typeid(*this)));
171 lst::type::cuptr
lst::floating_point_type::copy() const
173 return lttng::make_unique
<floating_point_type
>(
174 alignment
, byte_order
, exponent_digits
, mantissa_digits
);
177 void lst::floating_point_type::accept(type_visitor
& visitor
) const
179 visitor
.visit(*this);
182 bool lst::floating_point_type::_is_equal(const type
& base_other
) const noexcept
184 const auto& other
= static_cast<decltype(*this)&>(base_other
);
186 return this->byte_order
== other
.byte_order
&&
187 this->exponent_digits
== other
.exponent_digits
&&
188 this->mantissa_digits
== other
.mantissa_digits
;
191 lst::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
,
196 lst::integer_type::roles in_roles
) :
198 in_alignment
, in_byte_order
, in_size
, in_signedness
, in_base
, std::move(in_roles
))
203 * Due to a bug in g++ < 7.1, these specializations must be enclosed in the namespaces
204 * rather than using the usual `namespace::namespace::function` notation:
205 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
211 void signed_enumeration_type::accept(type_visitor
& visitor
) const
213 visitor
.visit(*this);
217 void unsigned_enumeration_type::accept(type_visitor
& visitor
) const
219 visitor
.visit(*this);
223 void variant_type
<lst::signed_enumeration_type::mapping::range_t::range_integer_t
>::accept(
224 lst::type_visitor
& visitor
) const
226 visitor
.visit(*this);
230 void variant_type
<lst::unsigned_enumeration_type::mapping::range_t::range_integer_t
>::accept(
231 lst::type_visitor
& visitor
) const
233 visitor
.visit(*this);
235 } /* namespace trace */
236 } /* namespace sessiond */
237 } /* namespace lttng */
239 lst::array_type::array_type(unsigned int in_alignment
, type::cuptr in_element_type
) :
240 type(in_alignment
), element_type
{ std::move(in_element_type
) }
244 bool lst::array_type::_is_equal(const type
& base_other
) const noexcept
246 const auto& other
= static_cast<decltype(*this)&>(base_other
);
248 return *this->element_type
== *other
.element_type
;
251 lst::static_length_array_type::static_length_array_type(unsigned int in_alignment
,
252 type::cuptr in_element_type
,
253 uint64_t in_length
) :
254 array_type(in_alignment
, std::move(in_element_type
)), length
{ in_length
}
258 bool lst::static_length_array_type::_is_equal(const type
& base_other
) const noexcept
260 const auto& other
= static_cast<decltype(*this)&>(base_other
);
262 return array_type::_is_equal(base_other
) && this->length
== other
.length
;
265 lst::type::cuptr
lst::static_length_array_type::copy() const
267 return lttng::make_unique
<static_length_array_type
>(
268 alignment
, element_type
->copy(), length
);
271 void lst::static_length_array_type::accept(type_visitor
& visitor
) const
273 visitor
.visit(*this);
276 lst::dynamic_length_array_type::dynamic_length_array_type(
277 unsigned int in_alignment
,
278 type::cuptr in_element_type
,
279 lst::field_location in_length_field_location
) :
280 array_type(in_alignment
, std::move(in_element_type
)),
281 length_field_location
{ std::move(in_length_field_location
) }
285 bool lst::dynamic_length_array_type::_is_equal(const type
& base_other
) const noexcept
287 const auto& other
= static_cast<decltype(*this)&>(base_other
);
289 return array_type::_is_equal(base_other
) &&
290 this->length_field_location
== other
.length_field_location
;
293 lst::type::cuptr
lst::dynamic_length_array_type::copy() const
295 return lttng::make_unique
<dynamic_length_array_type
>(
296 alignment
, element_type
->copy(), length_field_location
);
299 void lst::dynamic_length_array_type::accept(type_visitor
& visitor
) const
301 visitor
.visit(*this);
304 lst::static_length_blob_type::static_length_blob_type(unsigned int in_alignment
,
305 uint64_t in_length_bytes
,
307 type(in_alignment
), length_bytes
{ in_length_bytes
}, roles_
{ std::move(in_roles
) }
311 bool lst::static_length_blob_type::_is_equal(const type
& base_other
) const noexcept
313 const auto& other
= static_cast<decltype(*this)&>(base_other
);
315 return length_bytes
== other
.length_bytes
&& roles_
== other
.roles_
;
318 lst::type::cuptr
lst::static_length_blob_type::copy() const
320 return lttng::make_unique
<static_length_blob_type
>(alignment
, length_bytes
, roles_
);
323 void lst::static_length_blob_type::accept(type_visitor
& visitor
) const
325 visitor
.visit(*this);
328 lst::dynamic_length_blob_type::dynamic_length_blob_type(
329 unsigned int in_alignment
, lst::field_location in_length_field_location
) :
330 type(in_alignment
), length_field_location
{ std::move(in_length_field_location
) }
334 bool lst::dynamic_length_blob_type::_is_equal(const type
& base_other
) const noexcept
336 const auto& other
= dynamic_cast<decltype(*this)&>(base_other
);
338 return length_field_location
== other
.length_field_location
;
341 lst::type::cuptr
lst::dynamic_length_blob_type::copy() const
343 return lttng::make_unique
<dynamic_length_blob_type
>(alignment
, length_field_location
);
346 void lst::dynamic_length_blob_type::accept(type_visitor
& visitor
) const
348 visitor
.visit(*this);
351 lst::string_type::string_type(unsigned int in_alignment
, enum encoding in_encoding
) :
352 type(in_alignment
), encoding_
{ in_encoding
}
356 bool lst::string_type::_is_equal(const type
& base_other
) const noexcept
358 const auto& other
= static_cast<decltype(*this)&>(base_other
);
360 return this->encoding_
== other
.encoding_
;
363 lst::static_length_string_type::static_length_string_type(unsigned int in_alignment
,
364 enum encoding in_encoding
,
365 uint64_t in_length
) :
366 string_type(in_alignment
, in_encoding
), length
{ in_length
}
370 bool lst::static_length_string_type::_is_equal(const type
& base_other
) const noexcept
372 const auto& other
= static_cast<decltype(*this)&>(base_other
);
374 return string_type::_is_equal(base_other
) && this->length
== other
.length
;
377 lst::type::cuptr
lst::static_length_string_type::copy() const
379 return lttng::make_unique
<static_length_string_type
>(alignment
, encoding_
, length
);
382 void lst::static_length_string_type::accept(type_visitor
& visitor
) const
384 visitor
.visit(*this);
387 lst::dynamic_length_string_type::dynamic_length_string_type(
388 unsigned int in_alignment
,
389 enum encoding in_encoding
,
390 field_location in_length_field_location
) :
391 string_type(in_alignment
, in_encoding
),
392 length_field_location
{ std::move(in_length_field_location
) }
396 bool lst::dynamic_length_string_type::_is_equal(const type
& base_other
) const noexcept
398 const auto& other
= static_cast<decltype(*this)&>(base_other
);
400 return string_type::_is_equal(base_other
) &&
401 this->length_field_location
== other
.length_field_location
;
404 lst::type::cuptr
lst::dynamic_length_string_type::copy() const
406 return lttng::make_unique
<dynamic_length_string_type
>(
407 alignment
, encoding_
, length_field_location
);
410 void lst::dynamic_length_string_type::accept(type_visitor
& visitor
) const
412 visitor
.visit(*this);
415 lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment
,
416 enum encoding in_encoding
) :
417 string_type(in_alignment
, in_encoding
)
421 lst::type::cuptr
lst::null_terminated_string_type::copy() const
423 return lttng::make_unique
<null_terminated_string_type
>(alignment
, encoding_
);
426 void lst::null_terminated_string_type::accept(type_visitor
& visitor
) const
428 visitor
.visit(*this);
431 lst::structure_type::structure_type(unsigned int in_alignment
, fields in_fields
) :
432 type(in_alignment
), fields_
{ std::move(in_fields
) }
436 bool lst::structure_type::_is_equal(const type
& base_other
) const noexcept
438 const auto& other
= static_cast<decltype(*this)&>(base_other
);
440 return fields_are_equal(this->fields_
, other
.fields_
);
443 lst::type::cuptr
lst::structure_type::copy() const
445 structure_type::fields copy_of_fields
;
447 copy_of_fields
.reserve(fields_
.size());
448 for (const auto& field
: fields_
) {
449 copy_of_fields
.emplace_back(
450 lttng::make_unique
<lst::field
>(field
->name
, field
->get_type().copy()));
453 return lttng::make_unique
<structure_type
>(alignment
, std::move(copy_of_fields
));
456 void lst::structure_type::accept(type_visitor
& visitor
) const
458 visitor
.visit(*this);