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
)
160 void lst::signed_enumeration_type::accept(type_visitor
& visitor
) const
162 visitor
.visit(*this);
166 void lst::unsigned_enumeration_type::accept(type_visitor
& visitor
) const
168 visitor
.visit(*this);
170 } /* namespace trace */
171 } /* namespace sessiond */
172 } /* namespace lttng */
174 lst::array_type::array_type(unsigned int in_alignment
, type::cuptr in_element_type
) :
175 type(in_alignment
), element_type
{std::move(in_element_type
)}
179 bool lst::array_type::_is_equal(const type
& base_other
) const noexcept
181 const auto& other
= static_cast<decltype(*this)&>(base_other
);
183 return *this->element_type
== *other
.element_type
;
186 lst::static_length_array_type::static_length_array_type(unsigned int in_alignment
,
187 type::cuptr in_element_type
,
188 uint64_t in_length
) :
189 array_type(in_alignment
, std::move(in_element_type
)),
194 bool lst::static_length_array_type::_is_equal(const type
& base_other
) const noexcept
196 const auto& other
= static_cast<decltype(*this)&>(base_other
);
198 return array_type::_is_equal(base_other
) && this->length
== other
.length
;
201 void lst::static_length_array_type::accept(type_visitor
& visitor
) const
203 visitor
.visit(*this);
206 lst::dynamic_length_array_type::dynamic_length_array_type(unsigned int in_alignment
,
207 type::cuptr in_element_type
,
208 std::string in_length_field_name
) :
209 array_type(in_alignment
, std::move(in_element_type
)),
210 length_field_name
{std::move(in_length_field_name
)}
214 bool lst::dynamic_length_array_type::_is_equal(const type
& base_other
) const noexcept
216 const auto& other
= static_cast<decltype(*this)&>(base_other
);
218 return array_type::_is_equal(base_other
) &&
219 this->length_field_name
== other
.length_field_name
;
222 void lst::dynamic_length_array_type::accept(type_visitor
& visitor
) const
224 visitor
.visit(*this);
227 lst::string_type::string_type(unsigned int in_alignment
, enum encoding in_encoding
) :
228 type(in_alignment
), encoding_
{in_encoding
}
232 bool lst::string_type::_is_equal(const type
& base_other
) const noexcept
234 const auto& other
= static_cast<decltype(*this)&>(base_other
);
236 return this->encoding_
== other
.encoding_
;
239 lst::static_length_string_type::static_length_string_type(
240 unsigned int in_alignment
, enum encoding in_encoding
, uint64_t in_length
) :
241 string_type(in_alignment
, in_encoding
), length
{in_length
}
245 bool lst::static_length_string_type::_is_equal(const type
& base_other
) const noexcept
247 const auto& other
= static_cast<decltype(*this)&>(base_other
);
249 return string_type::_is_equal(base_other
) && this->length
== other
.length
;
252 void lst::static_length_string_type::accept(type_visitor
& visitor
) const
254 visitor
.visit(*this);
257 lst::dynamic_length_string_type::dynamic_length_string_type(unsigned int in_alignment
,
258 enum encoding in_encoding
,
259 std::string in_length_field_name
) :
260 string_type(in_alignment
, in_encoding
), length_field_name
{std::move(in_length_field_name
)}
264 bool lst::dynamic_length_string_type::_is_equal(const type
& base_other
) const noexcept
266 const auto& other
= static_cast<decltype(*this)&>(base_other
);
268 return string_type::_is_equal(base_other
) &&
269 this->length_field_name
== other
.length_field_name
;
272 void lst::dynamic_length_string_type::accept(type_visitor
& visitor
) const
274 visitor
.visit(*this);
277 lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment
,
278 enum encoding in_encoding
) :
279 string_type(in_alignment
, in_encoding
)
283 void lst::null_terminated_string_type::accept(type_visitor
& visitor
) const
285 visitor
.visit(*this);
288 lst::structure_type::structure_type(unsigned int in_alignment
, fields in_fields
) :
289 type(in_alignment
), _fields
{std::move(in_fields
)}
293 bool lst::structure_type::_is_equal(const type
& base_other
) const noexcept
295 const auto &other
= static_cast<decltype(*this)&>(base_other
);
297 return fields_are_equal(this->_fields
, other
._fields
);
300 void lst::structure_type::accept(type_visitor
& visitor
) const
302 visitor
.visit(*this);
305 lst::variant_type::variant_type(unsigned int in_alignment
,
306 std::string in_tag_name
,
307 choices in_choices
) :
309 tag_name
{std::move(in_tag_name
)},
310 _choices
{std::move(in_choices
)}
314 bool lst::variant_type::_is_equal(const type
& base_other
) const noexcept
316 const auto &other
= static_cast<decltype(*this)&>(base_other
);
318 return this->tag_name
== other
.tag_name
&&
319 fields_are_equal(this->_choices
, other
._choices
);
322 void lst::variant_type::accept(type_visitor
& visitor
) const
324 visitor
.visit(*this);