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 | ||
33 | lst::type::type(unsigned int in_alignment) : alignment{in_alignment} | |
34 | { | |
35 | } | |
36 | ||
37 | lst::type::~type() | |
38 | { | |
39 | } | |
40 | ||
41 | bool lst::type::operator==(const lst::type& other) const noexcept | |
42 | { | |
43 | return typeid(*this) == typeid(other) && | |
44 | alignment == other.alignment && | |
45 | /* defer to concrete type comparison */ | |
46 | this->_is_equal(other); | |
47 | } | |
48 | ||
49 | bool lst::type::operator!=(const lst::type& other) const noexcept | |
50 | { | |
51 | return !(*this == other); | |
52 | } | |
53 | ||
54 | lst::field::field(std::string in_name, lst::type::cuptr in_type) : | |
55 | name{std::move(in_name)}, _type{std::move(in_type)} | |
56 | { | |
57 | } | |
58 | ||
59 | void lst::field::accept(lst::field_visitor& visitor) const | |
60 | { | |
61 | visitor.visit(*this); | |
62 | } | |
63 | ||
64 | bool lst::field::operator==(const lst::field& other) const noexcept | |
65 | { | |
66 | return name == other.name && *_type == *other._type; | |
67 | } | |
68 | ||
69 | lst::integer_type::integer_type(unsigned int in_alignment, | |
70 | enum lst::byte_order in_byte_order, | |
71 | unsigned int in_size, | |
72 | enum lst::integer_type::signedness in_signedness, | |
73 | enum lst::integer_type::base in_base) : | |
74 | type(in_alignment), | |
75 | byte_order{in_byte_order}, | |
76 | size{in_size}, | |
65cd3c0c JG |
77 | signedness_{in_signedness}, |
78 | base_{in_base} | |
0220be14 JG |
79 | { |
80 | } | |
81 | ||
82 | bool lst::integer_type::_is_equal(const type &base_other) const noexcept | |
83 | { | |
84 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
85 | ||
86 | return this->byte_order == other.byte_order && | |
87 | this->size == other.size && | |
65cd3c0c JG |
88 | this->signedness_ == other.signedness_ && |
89 | this->base_ == other.base_; | |
0220be14 JG |
90 | } |
91 | ||
92 | void lst::integer_type::accept(type_visitor& visitor) const | |
93 | { | |
94 | visitor.visit(*this); | |
95 | } | |
96 | ||
97 | lst::byte_order lst::type::reverse_byte_order(lst::byte_order byte_order) noexcept | |
98 | { | |
99 | if (byte_order == lst::byte_order::BIG_ENDIAN_) { | |
100 | return lst::byte_order::LITTLE_ENDIAN_; | |
101 | } else { | |
102 | return lst::byte_order::BIG_ENDIAN_; | |
103 | } | |
104 | } | |
105 | ||
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) : | |
110 | type(in_alignment), | |
111 | byte_order(in_byte_order), | |
112 | exponent_digits{in_exponent_digits}, | |
113 | mantissa_digits(in_mantissa_digits) | |
114 | { | |
115 | /* Allowed (exponent, mantissa) pairs. */ | |
d7bfb9b0 | 116 | static const std::set<std::pair<unsigned int, unsigned int>> allowed_pairs{ |
0220be14 JG |
117 | {5, 11}, /* binary16 */ |
118 | {8, 24}, /* binary32 */ | |
119 | {11, 53}, /* binary64 */ | |
120 | {15, 113}, /* binary128 */ | |
121 | }; | |
122 | ||
d7bfb9b0 JG |
123 | if (allowed_pairs.find({exponent_digits, mantissa_digits}) != allowed_pairs.end()) { |
124 | /* mantissa and exponent digits is a valid pair. */ | |
125 | return; | |
0220be14 JG |
126 | } |
127 | ||
128 | LTTNG_THROW_INVALID_ARGUMENT_ERROR( | |
129 | fmt::format("Invalid exponent/mantissa values provided while creating {}", | |
130 | typeid(*this))); | |
131 | } | |
132 | ||
133 | void lst::floating_point_type::accept(type_visitor& visitor) const | |
134 | { | |
135 | visitor.visit(*this); | |
136 | } | |
137 | ||
138 | bool lst::floating_point_type::_is_equal(const type& base_other) const noexcept | |
139 | { | |
140 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
141 | ||
142 | return this->byte_order == other.byte_order && | |
143 | this->exponent_digits == other.exponent_digits && | |
144 | this->mantissa_digits == other.mantissa_digits; | |
145 | } | |
146 | ||
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, | |
151 | enum base in_base) : | |
152 | integer_type(in_alignment, in_byte_order, in_size, in_signedness, in_base) | |
153 | { | |
154 | } | |
155 | ||
e2c2bec2 JR |
156 | namespace lttng { |
157 | namespace sessiond { | |
158 | namespace trace { | |
0220be14 JG |
159 | template <> |
160 | void lst::signed_enumeration_type::accept(type_visitor& visitor) const | |
161 | { | |
162 | visitor.visit(*this); | |
163 | } | |
164 | ||
165 | template <> | |
166 | void lst::unsigned_enumeration_type::accept(type_visitor& visitor) const | |
167 | { | |
168 | visitor.visit(*this); | |
169 | } | |
e2c2bec2 JR |
170 | } /* namespace trace */ |
171 | } /* namespace sessiond */ | |
172 | } /* namespace lttng */ | |
0220be14 JG |
173 | |
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)} | |
176 | { | |
177 | } | |
178 | ||
179 | bool lst::array_type::_is_equal(const type& base_other) const noexcept | |
180 | { | |
181 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
182 | ||
183 | return *this->element_type == *other.element_type; | |
184 | } | |
185 | ||
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)), | |
190 | length{in_length} | |
191 | { | |
192 | } | |
193 | ||
194 | bool lst::static_length_array_type::_is_equal(const type& base_other) const noexcept | |
195 | { | |
196 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
197 | ||
198 | return array_type::_is_equal(base_other) && this->length == other.length; | |
199 | } | |
200 | ||
201 | void lst::static_length_array_type::accept(type_visitor& visitor) const | |
202 | { | |
203 | visitor.visit(*this); | |
204 | } | |
205 | ||
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)} | |
211 | { | |
212 | } | |
213 | ||
214 | bool lst::dynamic_length_array_type::_is_equal(const type& base_other) const noexcept | |
215 | { | |
216 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
217 | ||
218 | return array_type::_is_equal(base_other) && | |
219 | this->length_field_name == other.length_field_name; | |
220 | } | |
221 | ||
222 | void lst::dynamic_length_array_type::accept(type_visitor& visitor) const | |
223 | { | |
224 | visitor.visit(*this); | |
225 | } | |
226 | ||
227 | lst::string_type::string_type(unsigned int in_alignment, enum encoding in_encoding) : | |
65cd3c0c | 228 | type(in_alignment), encoding_{in_encoding} |
0220be14 JG |
229 | { |
230 | } | |
231 | ||
232 | bool lst::string_type::_is_equal(const type& base_other) const noexcept | |
233 | { | |
234 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
235 | ||
65cd3c0c | 236 | return this->encoding_ == other.encoding_; |
0220be14 JG |
237 | } |
238 | ||
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} | |
242 | { | |
243 | } | |
244 | ||
245 | bool lst::static_length_string_type::_is_equal(const type& base_other) const noexcept | |
246 | { | |
247 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
248 | ||
249 | return string_type::_is_equal(base_other) && this->length == other.length; | |
250 | } | |
251 | ||
252 | void lst::static_length_string_type::accept(type_visitor& visitor) const | |
253 | { | |
254 | visitor.visit(*this); | |
255 | } | |
256 | ||
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)} | |
261 | { | |
262 | } | |
263 | ||
264 | bool lst::dynamic_length_string_type::_is_equal(const type& base_other) const noexcept | |
265 | { | |
266 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
267 | ||
268 | return string_type::_is_equal(base_other) && | |
269 | this->length_field_name == other.length_field_name; | |
270 | } | |
271 | ||
272 | void lst::dynamic_length_string_type::accept(type_visitor& visitor) const | |
273 | { | |
274 | visitor.visit(*this); | |
275 | } | |
276 | ||
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) | |
280 | { | |
281 | } | |
282 | ||
283 | void lst::null_terminated_string_type::accept(type_visitor& visitor) const | |
284 | { | |
285 | visitor.visit(*this); | |
286 | } | |
287 | ||
288 | lst::structure_type::structure_type(unsigned int in_alignment, fields in_fields) : | |
289 | type(in_alignment), _fields{std::move(in_fields)} | |
290 | { | |
291 | } | |
292 | ||
293 | bool lst::structure_type::_is_equal(const type& base_other) const noexcept | |
294 | { | |
295 | const auto &other = static_cast<decltype(*this)&>(base_other); | |
296 | ||
297 | return fields_are_equal(this->_fields, other._fields); | |
298 | } | |
299 | ||
300 | void lst::structure_type::accept(type_visitor& visitor) const | |
301 | { | |
302 | visitor.visit(*this); | |
303 | } | |
304 | ||
305 | lst::variant_type::variant_type(unsigned int in_alignment, | |
306 | std::string in_tag_name, | |
307 | choices in_choices) : | |
308 | type(in_alignment), | |
309 | tag_name{std::move(in_tag_name)}, | |
310 | _choices{std::move(in_choices)} | |
311 | { | |
312 | } | |
313 | ||
314 | bool lst::variant_type::_is_equal(const type& base_other) const noexcept | |
315 | { | |
316 | const auto &other = static_cast<decltype(*this)&>(base_other); | |
317 | ||
318 | return this->tag_name == other.tag_name && | |
319 | fields_are_equal(this->_choices, other._choices); | |
320 | } | |
321 | ||
322 | void lst::variant_type::accept(type_visitor& visitor) const | |
323 | { | |
324 | visitor.visit(*this); | |
d7bfb9b0 | 325 | } |