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(), a
.cend(), b
.cbegin(),
26 [](typename
FieldTypeSet::const_reference field_a
,
27 typename
FieldTypeSet::const_reference field_b
) {
28 return *field_a
== *field_b
;
33 lst::type::type(unsigned int in_alignment
) : alignment
{in_alignment
}
41 bool lst::type::operator==(const lst::type
& other
) const noexcept
43 return typeid(*this) == typeid(other
) &&
44 alignment
== other
.alignment
&&
45 /* defer to concrete type comparison */
46 this->_is_equal(other
);
49 bool lst::type::operator!=(const lst::type
& other
) const noexcept
51 return !(*this == other
);
54 lst::field::field(std::string in_name
, lst::type::cuptr in_type
) :
55 name
{std::move(in_name
)}, _type
{std::move(in_type
)}
59 void lst::field::accept(lst::field_visitor
& visitor
) const
64 bool lst::field::operator==(const lst::field
& other
) const noexcept
66 return name
== other
.name
&& *_type
== *other
._type
;
69 lst::integer_type::integer_type(unsigned int in_alignment
,
70 enum lst::byte_order in_byte_order
,
72 enum lst::integer_type::signedness in_signedness
,
73 enum lst::integer_type::base in_base
) :
75 byte_order
{in_byte_order
},
77 signedness
{in_signedness
},
82 bool lst::integer_type::_is_equal(const type
&base_other
) const noexcept
84 const auto& other
= static_cast<decltype(*this)&>(base_other
);
86 return this->byte_order
== other
.byte_order
&&
87 this->size
== other
.size
&&
88 this->signedness
== other
.signedness
&&
89 this->base
== other
.base
;
92 void lst::integer_type::accept(type_visitor
& visitor
) const
97 lst::byte_order
lst::type::reverse_byte_order(lst::byte_order byte_order
) noexcept
99 if (byte_order
== lst::byte_order::BIG_ENDIAN_
) {
100 return lst::byte_order::LITTLE_ENDIAN_
;
102 return lst::byte_order::BIG_ENDIAN_
;
106 lst::floating_point_type::floating_point_type(unsigned int in_alignment
,
107 lst::byte_order in_byte_order
,
108 unsigned int in_exponent_digits
,
109 unsigned int in_mantissa_digits
) :
111 byte_order(in_byte_order
),
112 exponent_digits
{in_exponent_digits
},
113 mantissa_digits(in_mantissa_digits
)
115 /* Allowed (exponent, mantissa) pairs. */
116 static const std::set
<std::pair
<unsigned int, unsigned int>> allowed_pairs
{
117 {5, 11}, /* binary16 */
118 {8, 24}, /* binary32 */
119 {11, 53}, /* binary64 */
120 {15, 113}, /* binary128 */
123 if (allowed_pairs
.find({exponent_digits
, mantissa_digits
}) != allowed_pairs
.end()) {
124 /* mantissa and exponent digits is a valid pair. */
128 LTTNG_THROW_INVALID_ARGUMENT_ERROR(
129 fmt::format("Invalid exponent/mantissa values provided while creating {}",
133 void lst::floating_point_type::accept(type_visitor
& visitor
) const
135 visitor
.visit(*this);
138 bool lst::floating_point_type::_is_equal(const type
& base_other
) const noexcept
140 const auto& other
= static_cast<decltype(*this)&>(base_other
);
142 return this->byte_order
== other
.byte_order
&&
143 this->exponent_digits
== other
.exponent_digits
&&
144 this->mantissa_digits
== other
.mantissa_digits
;
147 lst::enumeration_type::enumeration_type(unsigned int in_alignment
,
148 enum lst::byte_order in_byte_order
,
149 unsigned int in_size
,
150 enum signedness in_signedness
,
152 integer_type(in_alignment
, in_byte_order
, in_size
, in_signedness
, in_base
)
157 void lst::signed_enumeration_type::accept(type_visitor
& visitor
) const
159 visitor
.visit(*this);
163 void lst::unsigned_enumeration_type::accept(type_visitor
& visitor
) const
165 visitor
.visit(*this);
168 lst::array_type::array_type(unsigned int in_alignment
, type::cuptr in_element_type
) :
169 type(in_alignment
), element_type
{std::move(in_element_type
)}
173 bool lst::array_type::_is_equal(const type
& base_other
) const noexcept
175 const auto& other
= static_cast<decltype(*this)&>(base_other
);
177 return *this->element_type
== *other
.element_type
;
180 lst::static_length_array_type::static_length_array_type(unsigned int in_alignment
,
181 type::cuptr in_element_type
,
182 uint64_t in_length
) :
183 array_type(in_alignment
, std::move(in_element_type
)),
188 bool lst::static_length_array_type::_is_equal(const type
& base_other
) const noexcept
190 const auto& other
= static_cast<decltype(*this)&>(base_other
);
192 return array_type::_is_equal(base_other
) && this->length
== other
.length
;
195 void lst::static_length_array_type::accept(type_visitor
& visitor
) const
197 visitor
.visit(*this);
200 lst::dynamic_length_array_type::dynamic_length_array_type(unsigned int in_alignment
,
201 type::cuptr in_element_type
,
202 std::string in_length_field_name
) :
203 array_type(in_alignment
, std::move(in_element_type
)),
204 length_field_name
{std::move(in_length_field_name
)}
208 bool lst::dynamic_length_array_type::_is_equal(const type
& base_other
) const noexcept
210 const auto& other
= static_cast<decltype(*this)&>(base_other
);
212 return array_type::_is_equal(base_other
) &&
213 this->length_field_name
== other
.length_field_name
;
216 void lst::dynamic_length_array_type::accept(type_visitor
& visitor
) const
218 visitor
.visit(*this);
221 lst::string_type::string_type(unsigned int in_alignment
, enum encoding in_encoding
) :
222 type(in_alignment
), encoding
{in_encoding
}
226 bool lst::string_type::_is_equal(const type
& base_other
) const noexcept
228 const auto& other
= static_cast<decltype(*this)&>(base_other
);
230 return this->encoding
== other
.encoding
;
233 lst::static_length_string_type::static_length_string_type(
234 unsigned int in_alignment
, enum encoding in_encoding
, uint64_t in_length
) :
235 string_type(in_alignment
, in_encoding
), length
{in_length
}
239 bool lst::static_length_string_type::_is_equal(const type
& base_other
) const noexcept
241 const auto& other
= static_cast<decltype(*this)&>(base_other
);
243 return string_type::_is_equal(base_other
) && this->length
== other
.length
;
246 void lst::static_length_string_type::accept(type_visitor
& visitor
) const
248 visitor
.visit(*this);
251 lst::dynamic_length_string_type::dynamic_length_string_type(unsigned int in_alignment
,
252 enum encoding in_encoding
,
253 std::string in_length_field_name
) :
254 string_type(in_alignment
, in_encoding
), length_field_name
{std::move(in_length_field_name
)}
258 bool lst::dynamic_length_string_type::_is_equal(const type
& base_other
) const noexcept
260 const auto& other
= static_cast<decltype(*this)&>(base_other
);
262 return string_type::_is_equal(base_other
) &&
263 this->length_field_name
== other
.length_field_name
;
266 void lst::dynamic_length_string_type::accept(type_visitor
& visitor
) const
268 visitor
.visit(*this);
271 lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment
,
272 enum encoding in_encoding
) :
273 string_type(in_alignment
, in_encoding
)
277 void lst::null_terminated_string_type::accept(type_visitor
& visitor
) const
279 visitor
.visit(*this);
282 lst::structure_type::structure_type(unsigned int in_alignment
, fields in_fields
) :
283 type(in_alignment
), _fields
{std::move(in_fields
)}
287 bool lst::structure_type::_is_equal(const type
& base_other
) const noexcept
289 const auto &other
= static_cast<decltype(*this)&>(base_other
);
291 return fields_are_equal(this->_fields
, other
._fields
);
294 void lst::structure_type::accept(type_visitor
& visitor
) const
296 visitor
.visit(*this);
299 lst::variant_type::variant_type(unsigned int in_alignment
,
300 std::string in_tag_name
,
301 choices in_choices
) :
303 tag_name
{std::move(in_tag_name
)},
304 _choices
{std::move(in_choices
)}
308 bool lst::variant_type::_is_equal(const type
& base_other
) const noexcept
310 const auto &other
= static_cast<decltype(*this)&>(base_other
);
312 return this->tag_name
== other
.tag_name
&&
313 fields_are_equal(this->_choices
, other
._choices
);
316 void lst::variant_type::accept(type_visitor
& visitor
) const
318 visitor
.visit(*this);