2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <type_traits>
16 #include <vendor/optional.hpp>
25 enum class byte_order {
30 class field_location {
36 EVENT_RECORD_COMMON_CONTEXT,
37 EVENT_RECORD_SPECIFIC_CONTEXT,
41 using elements = std::vector<std::string>;
43 field_location(root lookup_root, elements elements);
44 bool operator==(const field_location& other) const noexcept;
47 const elements elements_;
51 * Field, and the various field types, represents fields as exposed by the
52 * LTTng tracers. These classes do not attempt to describe the complete spectrum of the CTF
58 using cuptr = std::unique_ptr<const type>;
60 static byte_order reverse_byte_order(byte_order byte_order) noexcept;
62 bool operator==(const type& other) const noexcept;
63 bool operator!=(const type& other) const noexcept;
65 virtual void accept(type_visitor& visitor) const = 0;
67 const unsigned int alignment;
70 type(unsigned int alignment);
73 virtual bool _is_equal(const type& rhs) const noexcept = 0;
78 using cuptr = std::unique_ptr<const field>;
80 field(std::string name, type::cuptr type);
81 void accept(field_visitor& visitor) const;
82 bool operator==(const field& other) const noexcept;
84 const std::string name;
85 const type::cuptr _type;
88 class integer_type : public type {
90 enum class signedness {
103 DEFAULT_CLOCK_TIMESTAMP,
104 /* Packet header field class specific roles. */
105 DATA_STREAM_CLASS_ID,
108 /* Packet context field class specific roles. */
109 DISCARDED_EVENT_RECORD_COUNTER_SNAPSHOT,
110 PACKET_CONTENT_LENGTH,
111 PACKET_END_DEFAULT_CLOCK_TIMESTAMP,
112 PACKET_SEQUENCE_NUMBER,
114 /* Event record field class roles. */
115 EVENT_RECORD_CLASS_ID,
118 using roles = std::vector<role>;
120 integer_type(unsigned int alignment,
121 byte_order byte_order,
123 signedness signedness,
127 virtual void accept(type_visitor& visitor) const override;
129 const enum byte_order byte_order;
130 const unsigned int size;
132 * signedness and base are suffixed with '_' to work-around a bug in older
133 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
134 * nested-name-specifiers.
136 const signedness signedness_;
141 virtual bool _is_equal(const type& other) const noexcept override;
144 class floating_point_type : public type {
146 floating_point_type(unsigned int alignment,
147 byte_order byte_order,
148 unsigned int exponent_digits,
149 unsigned int mantissa_digits);
151 virtual void accept(type_visitor& visitor) const override final;
153 const enum byte_order byte_order;
154 const unsigned int exponent_digits;
155 const unsigned int mantissa_digits;
158 virtual bool _is_equal(const type& other) const noexcept override final;
161 class enumeration_type : public integer_type {
163 enumeration_type(unsigned int alignment,
164 enum byte_order byte_order,
166 enum signedness signedness,
168 integer_type::roles roles = {});
170 virtual void accept(type_visitor& visitor) const = 0;
174 template <class MappingIntegerType>
175 class enumeration_mapping_range {
177 using range_integer_t = MappingIntegerType;
179 enumeration_mapping_range(MappingIntegerType in_begin, MappingIntegerType in_end) :
180 begin{in_begin}, end{in_end}
184 const range_integer_t begin, end;
187 template <class MappingIntegerType>
188 bool operator==(const enumeration_mapping_range<MappingIntegerType>& lhs,
189 const enumeration_mapping_range<MappingIntegerType>& rhs) noexcept
191 return lhs.begin == rhs.begin && lhs.end == rhs.end;
194 template <class MappingIntegerType>
195 class enumeration_mapping {
197 using range_t = enumeration_mapping_range<MappingIntegerType>;
199 enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = delete;
200 enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) :
201 name{std::move(other.name)}, range{other.range}
205 /* Mapping with an implicit value. */
206 enumeration_mapping(std::string in_name) : name{std::move(in_name)}
210 enumeration_mapping(std::string in_name, range_t in_range) : name{std::move(in_name)}, range{in_range}
214 const std::string name;
215 const nonstd::optional<range_t> range;
218 template <class MappingIntegerType>
219 bool operator==(const enumeration_mapping<MappingIntegerType>& lhs,
220 const enumeration_mapping<MappingIntegerType>& rhs) noexcept
222 return lhs.name == rhs.name && lhs.range == rhs.range;
224 } /* namespace details */
226 template <class MappingIntegerType>
227 class typed_enumeration_type : public enumeration_type {
229 using mapping = details::enumeration_mapping<MappingIntegerType>;
230 using mappings = std::vector<mapping>;
232 static_assert(std::is_integral<MappingIntegerType>::value &&
233 sizeof(MappingIntegerType) == 8,
234 "MappingIntegerType must be either int64_t or uint64_t");
236 typed_enumeration_type(unsigned int in_alignment,
237 enum byte_order in_byte_order,
238 unsigned int in_size,
240 const std::shared_ptr<const mappings>& in_mappings,
241 integer_type::roles in_roles = {}) :
242 enumeration_type(in_alignment,
245 std::is_signed<MappingIntegerType>::value ?
246 integer_type::signedness::SIGNED :
247 integer_type::signedness::UNSIGNED,
249 std::move(in_roles)),
250 _mappings{std::move(in_mappings)}
254 virtual void accept(type_visitor& visitor) const override final;
256 const std::shared_ptr<const mappings> _mappings;
259 virtual bool _is_equal(const type& base_other) const noexcept override final
261 const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
264 return integer_type::_is_equal(base_other) && *this->_mappings == *other._mappings;
268 /* Aliases for all allowed enumeration mapping types. */
269 using signed_enumeration_type = typed_enumeration_type<int64_t>;
270 using unsigned_enumeration_type = typed_enumeration_type<uint64_t>;
272 class array_type : public type {
274 array_type(unsigned int alignment, type::cuptr element_type);
276 const type::cuptr element_type;
279 virtual bool _is_equal(const type& base_other) const noexcept override;
282 class static_length_array_type : public array_type {
284 static_length_array_type(unsigned int alignment,
285 type::cuptr element_type,
288 virtual void accept(type_visitor& visitor) const override final;
290 const uint64_t length;
293 virtual bool _is_equal(const type& base_other) const noexcept override final;
296 class dynamic_length_array_type : public array_type {
298 dynamic_length_array_type(unsigned int alignment,
299 type::cuptr element_type,
300 field_location length_field_location);
302 virtual void accept(type_visitor& visitor) const override final;
304 const field_location length_field_location;
307 virtual bool _is_equal(const type& base_other) const noexcept override final;
310 class static_length_blob_type : public type {
313 /* Packet header field class specific role. */
317 using roles = std::vector<role>;
319 static_length_blob_type(unsigned int alignment, uint64_t in_length_bytes, roles roles = {});
321 virtual void accept(type_visitor& visitor) const override final;
323 const uint64_t length_bytes;
327 virtual bool _is_equal(const type& base_other) const noexcept override final;
330 class dynamic_length_blob_type : public type {
332 dynamic_length_blob_type(unsigned int alignment, field_location length_field_location);
334 virtual void accept(type_visitor& visitor) const override final;
336 const field_location length_field_location;
339 virtual bool _is_equal(const type& base_other) const noexcept override final;
342 class string_type : public type {
344 enum class encoding {
349 string_type(unsigned int alignment, enum encoding encoding);
352 * encoding is suffixed with '_' to work-around a bug in older
353 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
354 * nested-name-specifiers.
356 const encoding encoding_;
359 virtual bool _is_equal(const type& base_other) const noexcept override;
362 class static_length_string_type : public string_type {
364 static_length_string_type(
365 unsigned int alignment, enum encoding in_encoding, uint64_t length);
366 virtual void accept(type_visitor& visitor) const override final;
368 const uint64_t length;
371 virtual bool _is_equal(const type& base_other) const noexcept override final;
374 class dynamic_length_string_type : public string_type {
376 dynamic_length_string_type(unsigned int alignment,
377 enum encoding in_encoding,
378 field_location length_field_location);
379 virtual void accept(type_visitor& visitor) const override final;
381 const field_location length_field_location;
384 virtual bool _is_equal(const type& base_other) const noexcept override final;
387 class null_terminated_string_type : public string_type {
389 null_terminated_string_type(unsigned int alignment, enum encoding in_encoding);
390 virtual void accept(type_visitor& visitor) const override final;
393 class structure_type : public type {
395 using fields = std::vector<field::cuptr>;
397 structure_type(unsigned int alignment, fields in_fields);
399 virtual void accept(type_visitor& visitor) const override final;
401 const fields _fields;
404 virtual bool _is_equal(const type& base_other) const noexcept override final;
407 class variant_type : public type {
409 using choices = std::vector<field::cuptr>;
411 variant_type(unsigned int alignment,
412 field_location selector_field_location,
415 virtual void accept(type_visitor& visitor) const override final;
417 const field_location selector_field_location;
418 const choices _choices;
422 virtual bool _is_equal(const type& base_other) const noexcept override final;
425 class field_visitor {
427 virtual ~field_visitor() = default;
428 virtual void visit(const field& field) = 0;
431 field_visitor() = default;
436 virtual ~type_visitor() = default;
437 virtual void visit(const integer_type& type) = 0;
438 virtual void visit(const floating_point_type& type) = 0;
439 virtual void visit(const signed_enumeration_type& type) = 0;
440 virtual void visit(const unsigned_enumeration_type& type) = 0;
441 virtual void visit(const static_length_array_type& type) = 0;
442 virtual void visit(const dynamic_length_array_type& type) = 0;
443 virtual void visit(const static_length_blob_type& type) = 0;
444 virtual void visit(const dynamic_length_blob_type& type) = 0;
445 virtual void visit(const null_terminated_string_type& type) = 0;
446 virtual void visit(const static_length_string_type& type) = 0;
447 virtual void visit(const dynamic_length_string_type& type) = 0;
448 virtual void visit(const structure_type& type) = 0;
449 virtual void visit(const variant_type& type) = 0;
452 type_visitor() = default;
455 } /* namespace trace */
456 } /* namespace sessiond */
457 } /* namespace lttng */
459 #endif /* LTTNG_FIELD_H */