Build fix: unknown warning -Wduplicated-branches
[lttng-tools.git] / src / bin / lttng-sessiond / field.hpp
CommitLineData
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#ifndef LTTNG_FIELD_H
9#define LTTNG_FIELD_H
10
11#include "trace-class.hpp"
12
13#include <memory>
14#include <string>
15#include <type_traits>
16#include <vector>
17
18#include <vendor/optional.hpp>
19
20namespace lttng {
21namespace sessiond {
22namespace trace {
23
24class field_visitor;
25class type_visitor;
26
27/*
28 * Field, and the various field types, represents fields as exposed by the
29 * LTTng tracers. These classes do not attempt to describe the complete spectrum of the CTF
30 * specification.
31 */
32
33class type {
34public:
35 using cuptr = std::unique_ptr<const type>;
36
37 static byte_order reverse_byte_order(byte_order byte_order) noexcept;
38
39 bool operator==(const type& other) const noexcept;
40 bool operator!=(const type& other) const noexcept;
41 virtual ~type();
42 virtual void accept(type_visitor& visitor) const = 0;
43
44 const unsigned int alignment;
45
46protected:
47 type(unsigned int alignment);
48
49private:
50 virtual bool _is_equal(const type& rhs) const noexcept = 0;
51};
52
53class field {
54public:
55 using cuptr = std::unique_ptr<const field>;
56
57 field(std::string name, type::cuptr type);
58 void accept(field_visitor& visitor) const;
59 bool operator==(const field& other) const noexcept;
60
61 const std::string name;
62 const type::cuptr _type;
63};
64
65class integer_type : public type {
66public:
67 enum class signedness {
68 SIGNED,
69 UNSIGNED,
70 };
71
72 enum class base {
73 BINARY = 2,
74 OCTAL = 8,
75 DECIMAL = 10,
76 HEXADECIMAL = 16,
77 };
78
79 integer_type(unsigned int alignment,
80 byte_order byte_order,
81 unsigned int size,
82 signedness signedness,
83 base base);
84
85 virtual void accept(type_visitor& visitor) const override;
86
87 const enum byte_order byte_order;
88 const unsigned int size;
89 const signedness signedness;
90 const base base;
91
92protected:
93 virtual bool _is_equal(const type& other) const noexcept override;
94};
95
96class floating_point_type : public type {
97public:
98 floating_point_type(unsigned int alignment,
99 byte_order byte_order,
100 unsigned int exponent_digits,
101 unsigned int mantissa_digits);
102
103 virtual void accept(type_visitor& visitor) const override final;
104
105 const enum byte_order byte_order;
106 const unsigned int exponent_digits;
107 const unsigned int mantissa_digits;
108
109private:
110 virtual bool _is_equal(const type& other) const noexcept override final;
111};
112
113class enumeration_type : public integer_type {
114protected:
115 enumeration_type(unsigned int alignment,
116 enum byte_order byte_order,
117 unsigned int size,
118 enum signedness signedness,
119 enum base base);
120
121 virtual void accept(type_visitor& visitor) const = 0;
122};
123
124namespace details {
125template <class MappingIntegerType>
126class enumeration_mapping_range {
127public:
128 using range_integer_t = MappingIntegerType;
129
130 enumeration_mapping_range(MappingIntegerType in_begin, MappingIntegerType in_end) :
131 begin{in_begin}, end{in_end}
132 {
133 }
134
135 const range_integer_t begin, end;
136};
137
138template <class MappingIntegerType>
139bool operator==(const enumeration_mapping_range<MappingIntegerType>& lhs,
140 const enumeration_mapping_range<MappingIntegerType>& rhs) noexcept
141{
142 return lhs.begin == rhs.begin && lhs.end == rhs.end;
143}
144
145template <class MappingIntegerType>
146class enumeration_mapping {
147public:
148 using range_t = enumeration_mapping_range<MappingIntegerType>;
149
150 enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = delete;
151 enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) :
152 name{std::move(other.name)}, range{other.range}
153 {
154 }
155
156 /* Mapping with an implicit value. */
157 enumeration_mapping(std::string in_name) : name{std::move(in_name)}
158 {
159 }
160
161 enumeration_mapping(std::string in_name, range_t in_range) : name{std::move(in_name)}, range{in_range}
162 {
163 }
164
165 const std::string name;
166 const nonstd::optional<range_t> range;
167};
168
169template <class MappingIntegerType>
170bool operator==(const enumeration_mapping<MappingIntegerType>& lhs,
171 const enumeration_mapping<MappingIntegerType>& rhs) noexcept
172{
173 return lhs.name == rhs.name && lhs.range == rhs.range;
174}
175} /* namespace details */
176
177template <class MappingIntegerType>
178class typed_enumeration_type : public enumeration_type {
179public:
180 using mapping = details::enumeration_mapping<MappingIntegerType>;
181 using mappings = std::vector<mapping>;
182
183 static_assert(std::is_integral<MappingIntegerType>::value &&
184 sizeof(MappingIntegerType) == 8,
185 "MappingIntegerType must be either int64_t or uint64_t");
186
187 typed_enumeration_type(unsigned int in_alignment,
188 enum byte_order in_byte_order,
189 unsigned int in_size,
190 enum signedness in_signedness,
191 enum base in_base,
192 const std::shared_ptr<const mappings>& in_mappings) :
193 enumeration_type(in_alignment,
194 in_byte_order,
195 in_size,
196 in_signedness,
197 in_base),
198 _mappings{std::move(in_mappings)}
199 {
200 }
201
202 virtual void accept(type_visitor& visitor) const override final;
203
204 const std::shared_ptr<const mappings> _mappings;
205
206private:
207 virtual bool _is_equal(const type& base_other) const noexcept override final
208 {
209 const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
210 base_other);
211
212 return integer_type::_is_equal(base_other) && *this->_mappings == *other._mappings;
213 }
214};
215
216/* Aliases for all allowed enumeration mapping types. */
217using signed_enumeration_type = typed_enumeration_type<int64_t>;
218using unsigned_enumeration_type = typed_enumeration_type<uint64_t>;
219
220class array_type : public type {
221public:
222 array_type(unsigned int alignment, type::cuptr element_type);
223
d7bfb9b0 224 const type::cuptr element_type;
0220be14
JG
225
226protected:
227 virtual bool _is_equal(const type& base_other) const noexcept override;
228};
229
230class static_length_array_type : public array_type {
231public:
232 static_length_array_type(unsigned int alignment,
233 type::cuptr element_type,
234 uint64_t in_length);
235
236 virtual void accept(type_visitor& visitor) const override final;
237
238 const uint64_t length;
239
240private:
241 virtual bool _is_equal(const type& base_other) const noexcept override final;
242};
243
244class dynamic_length_array_type : public array_type {
245public:
246 dynamic_length_array_type(unsigned int alignment,
247 type::cuptr element_type,
248 std::string length_field_name);
249
250 virtual void accept(type_visitor& visitor) const override final;
251
252 const std::string length_field_name;
253
254private:
255 virtual bool _is_equal(const type& base_other) const noexcept override final;
256};
257
258class string_type : public type {
259public:
260 enum class encoding {
261 ASCII,
262 UTF8,
263 };
264
265 string_type(unsigned int alignment, enum encoding encoding);
266
267 const encoding encoding;
268
269protected:
270 virtual bool _is_equal(const type& base_other) const noexcept override;
271};
272
273class static_length_string_type : public string_type {
274public:
275 static_length_string_type(
276 unsigned int alignment, enum encoding in_encoding, uint64_t length);
277 virtual void accept(type_visitor& visitor) const override final;
278
279 const uint64_t length;
280
281private:
282 virtual bool _is_equal(const type& base_other) const noexcept override final;
283};
284
285class dynamic_length_string_type : public string_type {
286public:
287 dynamic_length_string_type(unsigned int alignment,
288 enum encoding in_encoding,
289 std::string length_field_name);
290 virtual void accept(type_visitor& visitor) const override final;
291
292 const std::string length_field_name;
293
294private:
295 virtual bool _is_equal(const type& base_other) const noexcept override final;
296};
297
298class null_terminated_string_type : public string_type {
299public:
300 null_terminated_string_type(unsigned int alignment, enum encoding in_encoding);
301 virtual void accept(type_visitor& visitor) const override final;
302};
303
304class structure_type : public type {
305public:
306 using fields = std::vector<field::cuptr>;
307
308 structure_type(unsigned int alignment, fields in_fields);
309
310 virtual void accept(type_visitor& visitor) const override final;
311
312 const fields _fields;
313
314private:
315 virtual bool _is_equal(const type& base_other) const noexcept override final;
316};
317
318class variant_type : public type {
319public:
320 using choices = std::vector<field::cuptr>;
321
322 variant_type(unsigned int alignment, std::string tag_name, choices in_choices);
323
324 virtual void accept(type_visitor& visitor) const override final;
325
326 const std::string tag_name;
327 const choices _choices;
328
329private:
330 virtual bool _is_equal(const type& base_other) const noexcept override final;
331};
332
333class field_visitor {
334public:
335 virtual ~field_visitor() = default;
336 virtual void visit(const field& field) = 0;
337
338protected:
339 field_visitor() = default;
340};
341
342class type_visitor {
343public:
344 virtual ~type_visitor() = default;
345 virtual void visit(const integer_type& type) = 0;
346 virtual void visit(const floating_point_type& type) = 0;
347 virtual void visit(const signed_enumeration_type& type) = 0;
348 virtual void visit(const unsigned_enumeration_type& type) = 0;
349 virtual void visit(const static_length_array_type& type) = 0;
350 virtual void visit(const dynamic_length_array_type& type) = 0;
351 virtual void visit(const null_terminated_string_type& type) = 0;
352 virtual void visit(const static_length_string_type& type) = 0;
353 virtual void visit(const dynamic_length_string_type& type) = 0;
354 virtual void visit(const structure_type& type) = 0;
355 virtual void visit(const variant_type& type) = 0;
356
357protected:
358 type_visitor() = default;
359};
360
361} /* namespace trace */
362} /* namespace sessiond */
363} /* namespace lttng */
364
365#endif /* LTTNG_FIELD_H */
This page took 0.035607 seconds and 4 git commands to generate.