Commit | Line | Data |
---|---|---|
a1e31878 PP |
1 | /* |
2 | * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP | |
9 | ||
a1e31878 | 10 | #include <cstdint> |
c802cacb SM |
11 | #include <type_traits> |
12 | ||
a1e31878 PP |
13 | #include <babeltrace2/babeltrace.h> |
14 | ||
15 | #include "common/assert.h" | |
a1e31878 PP |
16 | #include "cpp-common/optional.hpp" |
17 | #include "cpp-common/string_view.hpp" | |
c802cacb | 18 | |
0d218157 | 19 | #include "borrowed-object.hpp" |
a1e31878 | 20 | #include "field-class.hpp" |
c802cacb | 21 | #include "internal/utils.hpp" |
a1e31878 PP |
22 | |
23 | namespace bt2 { | |
24 | ||
25 | template <typename LibObjT> | |
26 | class CommonBoolField; | |
27 | ||
28 | template <typename LibObjT> | |
29 | class CommonBitArrayField; | |
30 | ||
31 | template <typename LibObjT> | |
32 | class CommonUnsignedIntegerField; | |
33 | ||
34 | template <typename LibObjT> | |
35 | class CommonSignedIntegerField; | |
36 | ||
37 | template <typename LibObjT> | |
38 | class CommonUnsignedEnumerationField; | |
39 | ||
40 | template <typename LibObjT> | |
41 | class CommonSignedEnumerationField; | |
42 | ||
43 | template <typename LibObjT> | |
44 | class CommonSinglePrecisionRealField; | |
45 | ||
46 | template <typename LibObjT> | |
47 | class CommonDoublePrecisionRealField; | |
48 | ||
49 | template <typename LibObjT> | |
50 | class CommonStringField; | |
51 | ||
52 | template <typename LibObjT> | |
53 | class CommonStructureField; | |
54 | ||
55 | template <typename LibObjT> | |
56 | class CommonArrayField; | |
57 | ||
58 | template <typename LibObjT> | |
59 | class CommonDynamicArrayField; | |
60 | ||
61 | template <typename LibObjT> | |
62 | class CommonOptionField; | |
63 | ||
64 | template <typename LibObjT> | |
65 | class CommonVariantField; | |
66 | ||
67 | namespace internal { | |
68 | ||
69 | template <typename LibObjT> | |
70 | struct CommonFieldSpec; | |
71 | ||
b5f55e9f | 72 | /* Functions specific to mutable fields */ |
a1e31878 PP |
73 | template <> |
74 | struct CommonFieldSpec<bt_field> final | |
75 | { | |
76 | static bt_field_class *cls(bt_field * const libObjPtr) noexcept | |
77 | { | |
78 | return bt_field_borrow_class(libObjPtr); | |
79 | } | |
80 | }; | |
81 | ||
b5f55e9f | 82 | /* Functions specific to constant fields */ |
a1e31878 PP |
83 | template <> |
84 | struct CommonFieldSpec<const bt_field> final | |
85 | { | |
86 | static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept | |
87 | { | |
88 | return bt_field_borrow_class_const(libObjPtr); | |
89 | } | |
90 | }; | |
91 | ||
b5f55e9f | 92 | } /* namespace internal */ |
a1e31878 PP |
93 | |
94 | template <typename LibObjT> | |
0d218157 | 95 | class CommonField : public BorrowedObject<LibObjT> |
a1e31878 PP |
96 | { |
97 | private: | |
0d218157 | 98 | using typename BorrowedObject<LibObjT>::_ThisBorrowedObject; |
a1e31878 PP |
99 | |
100 | protected: | |
0d218157 | 101 | using typename BorrowedObject<LibObjT>::_LibObjPtr; |
a1e31878 PP |
102 | using _ThisCommonField = CommonField<LibObjT>; |
103 | ||
104 | public: | |
105 | using Class = | |
106 | typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type; | |
107 | ||
0d218157 | 108 | explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} |
a1e31878 PP |
109 | { |
110 | } | |
111 | ||
112 | template <typename OtherLibObjT> | |
0d218157 | 113 | CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val} |
a1e31878 PP |
114 | { |
115 | } | |
116 | ||
117 | template <typename OtherLibObjT> | |
100fa861 | 118 | _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept |
a1e31878 | 119 | { |
0d218157 | 120 | _ThisBorrowedObject::operator=(val); |
a1e31878 PP |
121 | return *this; |
122 | } | |
123 | ||
124 | FieldClassType classType() const noexcept | |
125 | { | |
341a67c4 | 126 | return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr())); |
a1e31878 PP |
127 | } |
128 | ||
dcb8ae9b | 129 | Class cls() const noexcept |
a1e31878 | 130 | { |
341a67c4 | 131 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
132 | } |
133 | ||
134 | bool isBool() const noexcept | |
135 | { | |
136 | return this->cls().isBool(); | |
137 | } | |
138 | ||
139 | bool isBitArray() const noexcept | |
140 | { | |
141 | return this->cls().isBitArray(); | |
142 | } | |
143 | ||
144 | bool isUnsignedInteger() const noexcept | |
145 | { | |
146 | return this->cls().isUnsignedInteger(); | |
147 | } | |
148 | ||
149 | bool isSignedInteger() const noexcept | |
150 | { | |
151 | return this->cls().isSignedInteger(); | |
152 | } | |
153 | ||
154 | bool isUnsignedEnumeration() const noexcept | |
155 | { | |
156 | return this->cls().isUnsignedEnumeration(); | |
157 | } | |
158 | ||
159 | bool isSignedEnumeration() const noexcept | |
160 | { | |
161 | return this->cls().isSignedEnumeration(); | |
162 | } | |
163 | ||
164 | bool isSinglePrecisionReal() const noexcept | |
165 | { | |
166 | return this->cls().isSinglePrecisionReal(); | |
167 | } | |
168 | ||
169 | bool isDoublePrecisionReal() const noexcept | |
170 | { | |
171 | return this->cls().isDoublePrecisionReal(); | |
172 | } | |
173 | ||
174 | bool isString() const noexcept | |
175 | { | |
176 | return this->cls().isString(); | |
177 | } | |
178 | ||
179 | bool isStructure() const noexcept | |
180 | { | |
181 | return this->cls().isStructure(); | |
182 | } | |
183 | ||
184 | bool isArray() const noexcept | |
185 | { | |
186 | return this->cls().isArray(); | |
187 | } | |
188 | ||
189 | bool isDynamicArray() const noexcept | |
190 | { | |
191 | return this->cls().isDynamicArray(); | |
192 | } | |
193 | ||
194 | bool isOption() const noexcept | |
195 | { | |
196 | return this->cls().isOption(); | |
197 | } | |
198 | ||
199 | bool isVariant() const noexcept | |
200 | { | |
201 | return this->cls().isVariant(); | |
202 | } | |
203 | ||
45e0ded5 PP |
204 | template <typename FieldT> |
205 | FieldT as() const noexcept | |
206 | { | |
207 | return FieldT {this->libObjPtr()}; | |
208 | } | |
209 | ||
a1e31878 PP |
210 | CommonBoolField<LibObjT> asBool() const noexcept; |
211 | CommonBitArrayField<LibObjT> asBitArray() const noexcept; | |
212 | CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept; | |
213 | CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept; | |
214 | CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept; | |
215 | CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept; | |
216 | CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept; | |
217 | CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept; | |
218 | CommonStringField<LibObjT> asString() const noexcept; | |
219 | CommonStructureField<LibObjT> asStructure() const noexcept; | |
220 | CommonArrayField<LibObjT> asArray() const noexcept; | |
221 | CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept; | |
222 | CommonOptionField<LibObjT> asOption() const noexcept; | |
223 | CommonVariantField<LibObjT> asVariant() const noexcept; | |
224 | }; | |
225 | ||
226 | using Field = CommonField<bt_field>; | |
227 | using ConstField = CommonField<const bt_field>; | |
228 | ||
4927bae7 PP |
229 | namespace internal { |
230 | ||
231 | struct FieldTypeDescr | |
232 | { | |
233 | using Const = ConstField; | |
234 | using NonConst = Field; | |
235 | }; | |
236 | ||
237 | template <> | |
238 | struct TypeDescr<Field> : public FieldTypeDescr | |
239 | { | |
240 | }; | |
241 | ||
242 | template <> | |
243 | struct TypeDescr<ConstField> : public FieldTypeDescr | |
244 | { | |
245 | }; | |
246 | ||
247 | } /* namespace internal */ | |
248 | ||
a1e31878 PP |
249 | template <typename LibObjT> |
250 | class CommonBoolField final : public CommonField<LibObjT> | |
251 | { | |
252 | private: | |
253 | using typename CommonField<LibObjT>::_LibObjPtr; | |
254 | using typename CommonField<LibObjT>::_ThisCommonField; | |
255 | ||
256 | public: | |
257 | using Value = bool; | |
258 | ||
259 | explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
260 | { | |
261 | BT_ASSERT_DBG(this->isBool()); | |
262 | } | |
263 | ||
264 | template <typename OtherLibObjT> | |
100fa861 | 265 | CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val} |
a1e31878 PP |
266 | { |
267 | } | |
268 | ||
269 | template <typename OtherLibObjT> | |
100fa861 | 270 | CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept |
a1e31878 PP |
271 | { |
272 | _ThisCommonField::operator=(val); | |
273 | return *this; | |
274 | } | |
275 | ||
dcb8ae9b | 276 | CommonBoolField<LibObjT> operator=(const Value val) const noexcept |
a1e31878 PP |
277 | { |
278 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
279 | ||
341a67c4 | 280 | bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val)); |
a1e31878 PP |
281 | return *this; |
282 | } | |
283 | ||
284 | Value value() const noexcept | |
285 | { | |
341a67c4 | 286 | return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr())); |
a1e31878 PP |
287 | } |
288 | ||
289 | operator Value() const noexcept | |
290 | { | |
291 | return this->value(); | |
292 | } | |
293 | }; | |
294 | ||
295 | using BoolField = CommonBoolField<bt_field>; | |
296 | using ConstBoolField = CommonBoolField<const bt_field>; | |
297 | ||
4927bae7 PP |
298 | namespace internal { |
299 | ||
300 | struct BoolFieldTypeDescr | |
301 | { | |
302 | using Const = ConstBoolField; | |
303 | using NonConst = BoolField; | |
304 | }; | |
305 | ||
306 | template <> | |
307 | struct TypeDescr<BoolField> : public BoolFieldTypeDescr | |
308 | { | |
309 | }; | |
310 | ||
311 | template <> | |
312 | struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr | |
313 | { | |
314 | }; | |
315 | ||
316 | } /* namespace internal */ | |
317 | ||
a1e31878 PP |
318 | template <typename LibObjT> |
319 | class CommonBitArrayField final : public CommonField<LibObjT> | |
320 | { | |
321 | private: | |
322 | using typename CommonField<LibObjT>::_LibObjPtr; | |
323 | using typename CommonField<LibObjT>::_ThisCommonField; | |
324 | ||
325 | public: | |
326 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass, | |
327 | BitArrayFieldClass>::type; | |
328 | ||
329 | explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
330 | { | |
331 | BT_ASSERT_DBG(this->isBitArray()); | |
332 | } | |
333 | ||
334 | template <typename OtherLibObjT> | |
100fa861 | 335 | CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept : |
a1e31878 PP |
336 | _ThisCommonField {val} |
337 | { | |
338 | } | |
339 | ||
340 | template <typename OtherLibObjT> | |
100fa861 | 341 | CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept |
a1e31878 PP |
342 | { |
343 | _ThisCommonField::operator=(val); | |
344 | return *this; | |
345 | } | |
346 | ||
347 | ConstBitArrayFieldClass cls() const noexcept | |
348 | { | |
349 | return ConstBitArrayFieldClass { | |
341a67c4 | 350 | internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())}; |
a1e31878 PP |
351 | } |
352 | ||
353 | Class cls() noexcept | |
354 | { | |
341a67c4 | 355 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
356 | } |
357 | ||
dcb8ae9b | 358 | CommonBitArrayField<LibObjT> operator=(const std::uint64_t bits) const noexcept |
a1e31878 PP |
359 | { |
360 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
361 | ||
341a67c4 | 362 | bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits); |
a1e31878 PP |
363 | return *this; |
364 | } | |
365 | ||
366 | std::uint64_t valueAsInteger() const noexcept | |
367 | { | |
341a67c4 | 368 | return bt_field_bit_array_get_value_as_integer(this->libObjPtr()); |
a1e31878 PP |
369 | } |
370 | ||
371 | bool bitValue(const std::uint64_t index) const noexcept | |
372 | { | |
373 | BT_ASSERT_DBG(index < this->cls().length()); | |
374 | return static_cast<bool>(this->valueAsInteger() & (1ULL << index)); | |
375 | } | |
376 | }; | |
377 | ||
378 | using BitArrayField = CommonBitArrayField<bt_field>; | |
379 | using ConstBitArrayField = CommonBitArrayField<const bt_field>; | |
380 | ||
4927bae7 PP |
381 | namespace internal { |
382 | ||
383 | struct BitArrayFieldTypeDescr | |
384 | { | |
385 | using Const = ConstBitArrayField; | |
386 | using NonConst = BitArrayField; | |
387 | }; | |
388 | ||
389 | template <> | |
390 | struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr | |
391 | { | |
392 | }; | |
393 | ||
394 | template <> | |
395 | struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr | |
396 | { | |
397 | }; | |
398 | ||
399 | } /* namespace internal */ | |
400 | ||
a1e31878 | 401 | template <typename LibObjT> |
edfe13aa | 402 | class CommonUnsignedIntegerField : public CommonField<LibObjT> |
a1e31878 PP |
403 | { |
404 | private: | |
405 | using typename CommonField<LibObjT>::_ThisCommonField; | |
406 | ||
407 | protected: | |
408 | using typename CommonField<LibObjT>::_LibObjPtr; | |
409 | using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>; | |
410 | ||
411 | public: | |
412 | using Value = std::uint64_t; | |
413 | ||
414 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass, | |
415 | IntegerFieldClass>::type; | |
416 | ||
417 | explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept : | |
418 | _ThisCommonField {libObjPtr} | |
419 | { | |
420 | BT_ASSERT_DBG(this->isUnsignedInteger()); | |
421 | } | |
422 | ||
423 | template <typename OtherLibObjT> | |
100fa861 | 424 | CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept : |
a1e31878 PP |
425 | _ThisCommonField {val} |
426 | { | |
427 | } | |
428 | ||
429 | template <typename OtherLibObjT> | |
430 | _ThisCommonUnsignedIntegerField& | |
100fa861 | 431 | operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept |
a1e31878 PP |
432 | { |
433 | _ThisCommonField::operator=(val); | |
434 | return *this; | |
435 | } | |
436 | ||
dcb8ae9b | 437 | Class cls() const noexcept |
a1e31878 | 438 | { |
341a67c4 | 439 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
440 | } |
441 | ||
dcb8ae9b | 442 | CommonUnsignedIntegerField<LibObjT> operator=(const Value val) const noexcept |
a1e31878 PP |
443 | { |
444 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
445 | ||
341a67c4 | 446 | bt_field_integer_unsigned_set_value(this->libObjPtr(), val); |
a1e31878 PP |
447 | return *this; |
448 | } | |
449 | ||
450 | Value value() const noexcept | |
451 | { | |
341a67c4 | 452 | return bt_field_integer_unsigned_get_value(this->libObjPtr()); |
a1e31878 PP |
453 | } |
454 | ||
455 | operator Value() const noexcept | |
456 | { | |
457 | return this->value(); | |
458 | } | |
459 | }; | |
460 | ||
461 | using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>; | |
462 | using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>; | |
463 | ||
4927bae7 PP |
464 | namespace internal { |
465 | ||
466 | struct UnsignedIntegerFieldTypeDescr | |
467 | { | |
468 | using Const = ConstUnsignedIntegerField; | |
469 | using NonConst = UnsignedIntegerField; | |
470 | }; | |
471 | ||
472 | template <> | |
473 | struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr | |
474 | { | |
475 | }; | |
476 | ||
477 | template <> | |
478 | struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr | |
479 | { | |
480 | }; | |
481 | ||
482 | } /* namespace internal */ | |
483 | ||
a1e31878 | 484 | template <typename LibObjT> |
edfe13aa | 485 | class CommonSignedIntegerField : public CommonField<LibObjT> |
a1e31878 PP |
486 | { |
487 | private: | |
488 | using typename CommonField<LibObjT>::_ThisCommonField; | |
489 | ||
490 | protected: | |
491 | using typename CommonField<LibObjT>::_LibObjPtr; | |
492 | using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>; | |
493 | ||
494 | public: | |
edfe13aa | 495 | using Value = std::int64_t; |
a1e31878 PP |
496 | |
497 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass, | |
498 | IntegerFieldClass>::type; | |
499 | ||
500 | explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept : | |
501 | _ThisCommonField {libObjPtr} | |
502 | { | |
503 | BT_ASSERT_DBG(this->isSignedInteger()); | |
504 | } | |
505 | ||
506 | template <typename OtherLibObjT> | |
100fa861 | 507 | CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept : |
a1e31878 PP |
508 | _ThisCommonField {val} |
509 | { | |
510 | } | |
511 | ||
512 | template <typename OtherLibObjT> | |
513 | _ThisCommonSignedIntegerField& | |
100fa861 | 514 | operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept |
a1e31878 PP |
515 | { |
516 | _ThisCommonField::operator=(val); | |
517 | return *this; | |
518 | } | |
519 | ||
dcb8ae9b | 520 | Class cls() const noexcept |
a1e31878 | 521 | { |
341a67c4 | 522 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
523 | } |
524 | ||
dcb8ae9b | 525 | CommonSignedIntegerField<LibObjT> operator=(const Value val) const noexcept |
a1e31878 PP |
526 | { |
527 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
528 | ||
341a67c4 | 529 | bt_field_integer_signed_set_value(this->libObjPtr(), val); |
a1e31878 PP |
530 | return *this; |
531 | } | |
532 | ||
533 | Value value() const noexcept | |
534 | { | |
341a67c4 | 535 | return bt_field_integer_signed_get_value(this->libObjPtr()); |
a1e31878 PP |
536 | } |
537 | ||
538 | operator Value() const noexcept | |
539 | { | |
540 | return this->value(); | |
541 | } | |
542 | }; | |
543 | ||
544 | using SignedIntegerField = CommonSignedIntegerField<bt_field>; | |
545 | using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>; | |
546 | ||
4927bae7 PP |
547 | namespace internal { |
548 | ||
549 | struct SignedIntegerFieldTypeDescr | |
550 | { | |
551 | using Const = ConstSignedIntegerField; | |
552 | using NonConst = SignedIntegerField; | |
553 | }; | |
554 | ||
555 | template <> | |
556 | struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr | |
557 | { | |
558 | }; | |
559 | ||
560 | template <> | |
561 | struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr | |
562 | { | |
563 | }; | |
564 | ||
565 | } /* namespace internal */ | |
566 | ||
a1e31878 PP |
567 | class EnumerationFieldClassMappingLabels |
568 | { | |
569 | public: | |
570 | explicit EnumerationFieldClassMappingLabels( | |
571 | const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) : | |
572 | _mLabels {labels}, | |
573 | _mSize {size} | |
574 | { | |
575 | } | |
576 | ||
577 | EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept = | |
578 | default; | |
579 | ||
580 | EnumerationFieldClassMappingLabels& | |
581 | operator=(const EnumerationFieldClassMappingLabels&) noexcept = default; | |
582 | ||
583 | std::uint64_t size() const noexcept | |
584 | { | |
585 | return _mSize; | |
586 | } | |
587 | ||
588 | bpstd::string_view operator[](const std::uint64_t index) const noexcept | |
589 | { | |
590 | return _mLabels[index]; | |
591 | } | |
592 | ||
593 | private: | |
594 | bt_field_class_enumeration_mapping_label_array _mLabels; | |
595 | std::uint64_t _mSize; | |
596 | }; | |
597 | ||
598 | template <typename LibObjT> | |
599 | class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT> | |
600 | { | |
601 | private: | |
602 | using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField; | |
603 | using typename CommonField<LibObjT>::_LibObjPtr; | |
604 | ||
605 | public: | |
606 | using Class = | |
607 | typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass, | |
608 | UnsignedEnumerationFieldClass>::type; | |
609 | ||
610 | explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : | |
611 | _ThisCommonUnsignedIntegerField {libObjPtr} | |
612 | { | |
613 | BT_ASSERT_DBG(this->isUnsignedEnumeration()); | |
614 | } | |
615 | ||
616 | template <typename OtherLibObjT> | |
100fa861 | 617 | CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept |
a1e31878 PP |
618 | : |
619 | _ThisCommonUnsignedIntegerField {val} | |
620 | { | |
621 | } | |
622 | ||
623 | template <typename OtherLibObjT> | |
624 | CommonUnsignedEnumerationField<LibObjT>& | |
100fa861 | 625 | operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept |
a1e31878 PP |
626 | { |
627 | _ThisCommonUnsignedIntegerField::operator=(val); | |
628 | return *this; | |
629 | } | |
630 | ||
a44a9938 FD |
631 | using CommonUnsignedIntegerField<LibObjT>::operator=; |
632 | ||
dcb8ae9b | 633 | Class cls() const noexcept |
a1e31878 | 634 | { |
341a67c4 | 635 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
636 | } |
637 | ||
638 | EnumerationFieldClassMappingLabels labels() const | |
639 | { | |
640 | bt_field_class_enumeration_mapping_label_array labelArray; | |
641 | std::uint64_t count; | |
341a67c4 | 642 | const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(), |
a1e31878 PP |
643 | &labelArray, &count); |
644 | ||
645 | if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) { | |
39278ebc | 646 | throw MemoryError {}; |
a1e31878 PP |
647 | } |
648 | ||
649 | return EnumerationFieldClassMappingLabels {labelArray, count}; | |
650 | } | |
651 | }; | |
652 | ||
653 | using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>; | |
654 | using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>; | |
655 | ||
4927bae7 PP |
656 | namespace internal { |
657 | ||
658 | struct UnsignedEnumerationFieldTypeDescr | |
659 | { | |
660 | using Const = ConstUnsignedEnumerationField; | |
661 | using NonConst = UnsignedEnumerationField; | |
662 | }; | |
663 | ||
664 | template <> | |
665 | struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr | |
666 | { | |
667 | }; | |
668 | ||
669 | template <> | |
670 | struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr | |
671 | { | |
672 | }; | |
673 | ||
674 | } /* namespace internal */ | |
675 | ||
a1e31878 PP |
676 | template <typename LibObjT> |
677 | class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT> | |
678 | { | |
679 | private: | |
680 | using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField; | |
681 | using typename CommonField<LibObjT>::_LibObjPtr; | |
682 | ||
683 | public: | |
684 | using Class = | |
685 | typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass, | |
686 | SignedEnumerationFieldClass>::type; | |
687 | ||
688 | explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : | |
689 | _ThisCommonSignedIntegerField {libObjPtr} | |
690 | { | |
691 | BT_ASSERT_DBG(this->isSignedEnumeration()); | |
692 | } | |
693 | ||
694 | template <typename OtherLibObjT> | |
100fa861 | 695 | CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept : |
a1e31878 PP |
696 | _ThisCommonSignedIntegerField {val} |
697 | { | |
698 | } | |
699 | ||
700 | template <typename OtherLibObjT> | |
701 | CommonSignedEnumerationField<LibObjT>& | |
100fa861 | 702 | operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept |
a1e31878 PP |
703 | { |
704 | _ThisCommonSignedIntegerField::operator=(val); | |
705 | return *this; | |
706 | } | |
707 | ||
a44a9938 FD |
708 | using CommonSignedIntegerField<LibObjT>::operator=; |
709 | ||
dcb8ae9b | 710 | Class cls() const noexcept |
a1e31878 | 711 | { |
341a67c4 | 712 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
713 | } |
714 | ||
715 | EnumerationFieldClassMappingLabels labels() const | |
716 | { | |
717 | bt_field_class_enumeration_mapping_label_array labelArray; | |
718 | std::uint64_t count; | |
719 | const auto status = | |
341a67c4 | 720 | bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count); |
a1e31878 PP |
721 | |
722 | if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) { | |
39278ebc | 723 | throw MemoryError {}; |
a1e31878 PP |
724 | } |
725 | ||
726 | return EnumerationFieldClassMappingLabels {labelArray, count}; | |
727 | } | |
728 | }; | |
729 | ||
730 | using SignedEnumerationField = CommonSignedEnumerationField<bt_field>; | |
731 | using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>; | |
732 | ||
4927bae7 PP |
733 | namespace internal { |
734 | ||
735 | struct SignedEnumerationFieldTypeDescr | |
736 | { | |
737 | using Const = ConstSignedEnumerationField; | |
738 | using NonConst = SignedEnumerationField; | |
739 | }; | |
740 | ||
741 | template <> | |
742 | struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr | |
743 | { | |
744 | }; | |
745 | ||
746 | template <> | |
747 | struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr | |
748 | { | |
749 | }; | |
750 | ||
751 | } /* namespace internal */ | |
752 | ||
a1e31878 PP |
753 | template <typename LibObjT> |
754 | class CommonSinglePrecisionRealField final : public CommonField<LibObjT> | |
755 | { | |
756 | private: | |
757 | using typename CommonField<LibObjT>::_LibObjPtr; | |
758 | using typename CommonField<LibObjT>::_ThisCommonField; | |
759 | ||
760 | public: | |
761 | using Value = float; | |
762 | ||
763 | explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept : | |
764 | _ThisCommonField {libObjPtr} | |
765 | { | |
766 | BT_ASSERT_DBG(this->isSinglePrecisionReal()); | |
767 | } | |
768 | ||
769 | template <typename OtherLibObjT> | |
100fa861 | 770 | CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept |
a1e31878 PP |
771 | : |
772 | _ThisCommonField {val} | |
773 | { | |
774 | } | |
775 | ||
776 | template <typename OtherLibObjT> | |
777 | CommonSinglePrecisionRealField<LibObjT>& | |
100fa861 | 778 | operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept |
a1e31878 PP |
779 | { |
780 | _ThisCommonField::operator=(val); | |
781 | return *this; | |
782 | } | |
783 | ||
dcb8ae9b | 784 | CommonSinglePrecisionRealField<LibObjT> operator=(const Value val) const noexcept |
a1e31878 PP |
785 | { |
786 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
787 | ||
341a67c4 | 788 | bt_field_real_single_precision_set_value(this->libObjPtr(), val); |
a1e31878 PP |
789 | return *this; |
790 | } | |
791 | ||
792 | Value value() const noexcept | |
793 | { | |
341a67c4 | 794 | return bt_field_real_single_precision_get_value(this->libObjPtr()); |
a1e31878 PP |
795 | } |
796 | ||
797 | operator Value() const noexcept | |
798 | { | |
799 | return this->value(); | |
800 | } | |
801 | }; | |
802 | ||
803 | using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>; | |
804 | using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>; | |
805 | ||
4927bae7 PP |
806 | namespace internal { |
807 | ||
808 | struct SinglePrecisionRealFieldTypeDescr | |
809 | { | |
810 | using Const = ConstSinglePrecisionRealField; | |
811 | using NonConst = SinglePrecisionRealField; | |
812 | }; | |
813 | ||
814 | template <> | |
815 | struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr | |
816 | { | |
817 | }; | |
818 | ||
819 | template <> | |
820 | struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr | |
821 | { | |
822 | }; | |
823 | ||
824 | } /* namespace internal */ | |
825 | ||
a1e31878 PP |
826 | template <typename LibObjT> |
827 | class CommonDoublePrecisionRealField final : public CommonField<LibObjT> | |
828 | { | |
829 | private: | |
830 | using typename CommonField<LibObjT>::_LibObjPtr; | |
831 | using typename CommonField<LibObjT>::_ThisCommonField; | |
832 | ||
833 | public: | |
834 | using Value = double; | |
835 | ||
836 | explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept : | |
837 | _ThisCommonField {libObjPtr} | |
838 | { | |
839 | BT_ASSERT_DBG(this->isDoublePrecisionReal()); | |
840 | } | |
841 | ||
842 | template <typename OtherLibObjT> | |
100fa861 | 843 | CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept |
a1e31878 PP |
844 | : |
845 | _ThisCommonField {val} | |
846 | { | |
847 | } | |
848 | ||
849 | template <typename OtherLibObjT> | |
850 | CommonDoublePrecisionRealField<LibObjT>& | |
100fa861 | 851 | operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept |
a1e31878 PP |
852 | { |
853 | _ThisCommonField::operator=(val); | |
854 | return *this; | |
855 | } | |
856 | ||
dcb8ae9b | 857 | CommonDoublePrecisionRealField<LibObjT> operator=(const Value val) const noexcept |
a1e31878 PP |
858 | { |
859 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
860 | ||
edfe13aa | 861 | bt_field_real_double_precision_set_value(this->libObjPtr(), val); |
a1e31878 PP |
862 | return *this; |
863 | } | |
864 | ||
865 | Value value() const noexcept | |
866 | { | |
edfe13aa | 867 | return bt_field_real_double_precision_get_value(this->libObjPtr()); |
a1e31878 PP |
868 | } |
869 | ||
870 | operator Value() const noexcept | |
871 | { | |
872 | return this->value(); | |
873 | } | |
874 | }; | |
875 | ||
876 | using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>; | |
877 | using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>; | |
878 | ||
4927bae7 PP |
879 | namespace internal { |
880 | ||
881 | struct DoublePrecisionRealFieldTypeDescr | |
882 | { | |
883 | using Const = ConstDoublePrecisionRealField; | |
884 | using NonConst = DoublePrecisionRealField; | |
885 | }; | |
886 | ||
887 | template <> | |
888 | struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr | |
889 | { | |
890 | }; | |
891 | ||
892 | template <> | |
893 | struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr | |
894 | { | |
895 | }; | |
896 | ||
897 | } /* namespace internal */ | |
898 | ||
a1e31878 PP |
899 | template <typename LibObjT> |
900 | class CommonStringField final : public CommonField<LibObjT> | |
901 | { | |
902 | private: | |
903 | using typename CommonField<LibObjT>::_LibObjPtr; | |
904 | using typename CommonField<LibObjT>::_ThisCommonField; | |
905 | ||
906 | public: | |
907 | explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
908 | { | |
909 | BT_ASSERT_DBG(this->isString()); | |
910 | } | |
911 | ||
912 | template <typename OtherLibObjT> | |
100fa861 | 913 | CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val} |
a1e31878 PP |
914 | { |
915 | } | |
916 | ||
917 | template <typename OtherLibObjT> | |
100fa861 | 918 | CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept |
a1e31878 PP |
919 | { |
920 | _ThisCommonField::operator=(val); | |
921 | return *this; | |
922 | } | |
923 | ||
dcb8ae9b | 924 | CommonStringField<LibObjT> operator=(const char * const val) const |
a1e31878 PP |
925 | { |
926 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
927 | ||
341a67c4 | 928 | const auto status = bt_field_string_set_value(this->libObjPtr(), val); |
a1e31878 PP |
929 | |
930 | if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) { | |
39278ebc | 931 | throw MemoryError {}; |
a1e31878 PP |
932 | } |
933 | ||
934 | return *this; | |
935 | } | |
936 | ||
dcb8ae9b | 937 | CommonStringField<LibObjT> operator=(const std::string& val) const |
a1e31878 PP |
938 | { |
939 | return *this = val.data(); | |
940 | } | |
941 | ||
dcb8ae9b | 942 | void append(const char * const begin, const std::uint64_t len) const |
8a79c777 FD |
943 | { |
944 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
945 | ||
946 | const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len); | |
947 | ||
948 | if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) { | |
949 | throw MemoryError {}; | |
950 | } | |
951 | } | |
952 | ||
dcb8ae9b | 953 | void append(const std::string& val) const |
8a79c777 FD |
954 | { |
955 | this->append(val.data(), val.size()); | |
956 | } | |
957 | ||
dcb8ae9b | 958 | void clear() const noexcept |
a1e31878 PP |
959 | { |
960 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
961 | ||
341a67c4 | 962 | bt_field_string_clear(this->libObjPtr()); |
a1e31878 PP |
963 | } |
964 | ||
965 | bpstd::string_view value() const noexcept | |
966 | { | |
341a67c4 | 967 | return bt_field_string_get_value(this->libObjPtr()); |
a1e31878 PP |
968 | } |
969 | }; | |
970 | ||
971 | using StringField = CommonStringField<bt_field>; | |
972 | using ConstStringField = CommonStringField<const bt_field>; | |
973 | ||
974 | namespace internal { | |
975 | ||
4927bae7 PP |
976 | struct StringFieldTypeDescr |
977 | { | |
978 | using Const = ConstStringField; | |
979 | using NonConst = StringField; | |
980 | }; | |
981 | ||
982 | template <> | |
983 | struct TypeDescr<StringField> : public StringFieldTypeDescr | |
984 | { | |
985 | }; | |
986 | ||
987 | template <> | |
988 | struct TypeDescr<ConstStringField> : public StringFieldTypeDescr | |
989 | { | |
990 | }; | |
991 | ||
a1e31878 PP |
992 | template <typename LibObjT> |
993 | struct CommonStructureFieldSpec; | |
994 | ||
b5f55e9f | 995 | /* Functions specific to mutable structure fields */ |
a1e31878 PP |
996 | template <> |
997 | struct CommonStructureFieldSpec<bt_field> final | |
998 | { | |
999 | static bt_field *memberFieldByIndex(bt_field * const libObjPtr, | |
1000 | const std::uint64_t index) noexcept | |
1001 | { | |
1002 | return bt_field_structure_borrow_member_field_by_index(libObjPtr, index); | |
1003 | } | |
1004 | ||
1005 | static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept | |
1006 | { | |
1007 | return bt_field_structure_borrow_member_field_by_name(libObjPtr, name); | |
1008 | } | |
1009 | }; | |
1010 | ||
b5f55e9f | 1011 | /* Functions specific to constant structure fields */ |
a1e31878 PP |
1012 | template <> |
1013 | struct CommonStructureFieldSpec<const bt_field> final | |
1014 | { | |
1015 | static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr, | |
1016 | const std::uint64_t index) noexcept | |
1017 | { | |
1018 | return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index); | |
1019 | } | |
1020 | ||
1021 | static const bt_field *memberFieldByName(const bt_field * const libObjPtr, | |
1022 | const char * const name) noexcept | |
1023 | { | |
1024 | return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name); | |
1025 | } | |
1026 | }; | |
1027 | ||
b5f55e9f | 1028 | } /* namespace internal */ |
a1e31878 PP |
1029 | |
1030 | template <typename LibObjT> | |
1031 | class CommonStructureField final : public CommonField<LibObjT> | |
1032 | { | |
1033 | private: | |
1034 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1035 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1036 | using _Spec = internal::CommonStructureFieldSpec<LibObjT>; | |
1037 | ||
1038 | public: | |
1039 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass, | |
1040 | StructureFieldClass>::type; | |
1041 | ||
1042 | explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept : | |
1043 | _ThisCommonField {libObjPtr} | |
1044 | { | |
1045 | BT_ASSERT_DBG(this->isStructure()); | |
1046 | } | |
1047 | ||
1048 | template <typename OtherLibObjT> | |
100fa861 | 1049 | CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept : |
a1e31878 PP |
1050 | _ThisCommonField {val} |
1051 | { | |
1052 | } | |
1053 | ||
1054 | template <typename OtherLibObjT> | |
100fa861 | 1055 | CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept |
a1e31878 PP |
1056 | { |
1057 | _ThisCommonField::operator=(val); | |
1058 | return *this; | |
1059 | } | |
1060 | ||
dcb8ae9b | 1061 | Class cls() const noexcept |
a1e31878 | 1062 | { |
341a67c4 | 1063 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1064 | } |
1065 | ||
1066 | std::uint64_t size() const noexcept | |
1067 | { | |
1068 | return this->cls().size(); | |
1069 | } | |
1070 | ||
dcb8ae9b | 1071 | CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept |
a1e31878 | 1072 | { |
341a67c4 | 1073 | return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)}; |
a1e31878 PP |
1074 | } |
1075 | ||
dcb8ae9b | 1076 | nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept |
a1e31878 | 1077 | { |
341a67c4 | 1078 | const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name); |
a1e31878 PP |
1079 | |
1080 | if (libObjPtr) { | |
1081 | return CommonField<LibObjT> {libObjPtr}; | |
1082 | } | |
1083 | ||
1084 | return nonstd::nullopt; | |
1085 | } | |
1086 | ||
dcb8ae9b | 1087 | nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept |
a1e31878 PP |
1088 | { |
1089 | return (*this)[name.data()]; | |
1090 | } | |
1091 | }; | |
1092 | ||
1093 | using StructureField = CommonStructureField<bt_field>; | |
1094 | using ConstStructureField = CommonStructureField<const bt_field>; | |
1095 | ||
1096 | namespace internal { | |
1097 | ||
4927bae7 PP |
1098 | struct StructureFieldTypeDescr |
1099 | { | |
1100 | using Const = ConstStructureField; | |
1101 | using NonConst = StructureField; | |
1102 | }; | |
1103 | ||
1104 | template <> | |
1105 | struct TypeDescr<StructureField> : public StructureFieldTypeDescr | |
1106 | { | |
1107 | }; | |
1108 | ||
1109 | template <> | |
1110 | struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr | |
1111 | { | |
1112 | }; | |
1113 | ||
a1e31878 PP |
1114 | template <typename LibObjT> |
1115 | struct CommonArrayFieldSpec; | |
1116 | ||
b5f55e9f | 1117 | /* Functions specific to mutable array fields */ |
a1e31878 PP |
1118 | template <> |
1119 | struct CommonArrayFieldSpec<bt_field> final | |
1120 | { | |
1121 | static bt_field *elementFieldByIndex(bt_field * const libObjPtr, | |
1122 | const std::uint64_t index) noexcept | |
1123 | { | |
1124 | return bt_field_array_borrow_element_field_by_index(libObjPtr, index); | |
1125 | } | |
1126 | }; | |
1127 | ||
b5f55e9f | 1128 | /* Functions specific to constant array fields */ |
a1e31878 PP |
1129 | template <> |
1130 | struct CommonArrayFieldSpec<const bt_field> final | |
1131 | { | |
1132 | static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr, | |
1133 | const std::uint64_t index) noexcept | |
1134 | { | |
1135 | return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index); | |
1136 | } | |
1137 | }; | |
1138 | ||
b5f55e9f | 1139 | } /* namespace internal */ |
a1e31878 PP |
1140 | |
1141 | template <typename LibObjT> | |
1142 | class CommonArrayField : public CommonField<LibObjT> | |
1143 | { | |
1144 | private: | |
1145 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1146 | using _Spec = internal::CommonArrayFieldSpec<LibObjT>; | |
1147 | ||
1148 | protected: | |
1149 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1150 | using _ThisCommonArrayField = CommonArrayField<LibObjT>; | |
1151 | ||
1152 | public: | |
1153 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass, | |
1154 | ArrayFieldClass>::type; | |
1155 | ||
1156 | explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
1157 | { | |
1158 | BT_ASSERT_DBG(this->isArray()); | |
1159 | } | |
1160 | ||
1161 | template <typename OtherLibObjT> | |
100fa861 | 1162 | CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val} |
a1e31878 PP |
1163 | { |
1164 | } | |
1165 | ||
1166 | template <typename OtherLibObjT> | |
100fa861 | 1167 | _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept |
a1e31878 PP |
1168 | { |
1169 | _ThisCommonField::operator=(val); | |
1170 | return *this; | |
1171 | } | |
1172 | ||
dcb8ae9b | 1173 | Class cls() const noexcept |
a1e31878 | 1174 | { |
341a67c4 | 1175 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1176 | } |
1177 | ||
1178 | std::uint64_t length() const noexcept | |
1179 | { | |
341a67c4 | 1180 | return bt_field_array_get_length(this->libObjPtr()); |
a1e31878 PP |
1181 | } |
1182 | ||
dcb8ae9b | 1183 | CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept |
a1e31878 | 1184 | { |
341a67c4 | 1185 | return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)}; |
a1e31878 PP |
1186 | } |
1187 | }; | |
1188 | ||
1189 | using ArrayField = CommonArrayField<bt_field>; | |
1190 | using ConstArrayField = CommonArrayField<const bt_field>; | |
1191 | ||
4927bae7 PP |
1192 | namespace internal { |
1193 | ||
1194 | struct ArrayFieldTypeDescr | |
1195 | { | |
1196 | using Const = ConstArrayField; | |
1197 | using NonConst = ArrayField; | |
1198 | }; | |
1199 | ||
1200 | template <> | |
1201 | struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr | |
1202 | { | |
1203 | }; | |
1204 | ||
1205 | template <> | |
1206 | struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr | |
1207 | { | |
1208 | }; | |
1209 | ||
1210 | } /* namespace internal */ | |
1211 | ||
a1e31878 PP |
1212 | template <typename LibObjT> |
1213 | class CommonDynamicArrayField : public CommonArrayField<LibObjT> | |
1214 | { | |
1215 | private: | |
1216 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1217 | using typename CommonArrayField<LibObjT>::_ThisCommonArrayField; | |
1218 | ||
1219 | public: | |
1220 | explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept : | |
1221 | _ThisCommonArrayField {libObjPtr} | |
1222 | { | |
1223 | BT_ASSERT_DBG(this->isDynamicArray()); | |
1224 | } | |
1225 | ||
1226 | template <typename OtherLibObjT> | |
100fa861 | 1227 | CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept : |
a1e31878 PP |
1228 | _ThisCommonArrayField {val} |
1229 | { | |
1230 | } | |
1231 | ||
1232 | template <typename OtherLibObjT> | |
1233 | CommonDynamicArrayField<LibObjT>& | |
100fa861 | 1234 | operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept |
a1e31878 PP |
1235 | { |
1236 | _ThisCommonArrayField::operator=(val); | |
1237 | return *this; | |
1238 | } | |
1239 | ||
1240 | std::uint64_t length() const noexcept | |
1241 | { | |
1242 | return _ThisCommonArrayField::length(); | |
1243 | } | |
1244 | ||
dcb8ae9b | 1245 | void length(const std::uint64_t length) const |
a1e31878 PP |
1246 | { |
1247 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1248 | ||
341a67c4 | 1249 | const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length); |
a1e31878 PP |
1250 | |
1251 | if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) { | |
39278ebc | 1252 | throw MemoryError {}; |
a1e31878 PP |
1253 | } |
1254 | } | |
1255 | }; | |
1256 | ||
1257 | using DynamicArrayField = CommonDynamicArrayField<bt_field>; | |
1258 | using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>; | |
1259 | ||
1260 | namespace internal { | |
1261 | ||
4927bae7 PP |
1262 | struct DynamicArrayFieldTypeDescr |
1263 | { | |
1264 | using Const = ConstDynamicArrayField; | |
1265 | using NonConst = DynamicArrayField; | |
1266 | }; | |
1267 | ||
1268 | template <> | |
1269 | struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr | |
1270 | { | |
1271 | }; | |
1272 | ||
1273 | template <> | |
1274 | struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr | |
1275 | { | |
1276 | }; | |
1277 | ||
a1e31878 PP |
1278 | template <typename LibObjT> |
1279 | struct CommonOptionFieldSpec; | |
1280 | ||
b5f55e9f | 1281 | /* Functions specific to mutable option fields */ |
a1e31878 PP |
1282 | template <> |
1283 | struct CommonOptionFieldSpec<bt_field> final | |
1284 | { | |
1285 | static bt_field *field(bt_field * const libObjPtr) noexcept | |
1286 | { | |
1287 | return bt_field_option_borrow_field(libObjPtr); | |
1288 | } | |
1289 | }; | |
1290 | ||
b5f55e9f | 1291 | /* Functions specific to constant option fields */ |
a1e31878 PP |
1292 | template <> |
1293 | struct CommonOptionFieldSpec<const bt_field> final | |
1294 | { | |
1295 | static const bt_field *field(const bt_field * const libObjPtr) noexcept | |
1296 | { | |
1297 | return bt_field_option_borrow_field_const(libObjPtr); | |
1298 | } | |
1299 | }; | |
1300 | ||
b5f55e9f | 1301 | } /* namespace internal */ |
a1e31878 PP |
1302 | |
1303 | template <typename LibObjT> | |
1304 | class CommonOptionField : public CommonField<LibObjT> | |
1305 | { | |
1306 | private: | |
1307 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1308 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1309 | using _Spec = internal::CommonOptionFieldSpec<LibObjT>; | |
1310 | ||
1311 | public: | |
1312 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass, | |
1313 | OptionFieldClass>::type; | |
1314 | ||
1315 | explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
1316 | { | |
1317 | BT_ASSERT_DBG(this->isOption()); | |
1318 | } | |
1319 | ||
1320 | template <typename OtherLibObjT> | |
100fa861 | 1321 | CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val} |
a1e31878 PP |
1322 | { |
1323 | } | |
1324 | ||
1325 | template <typename OtherLibObjT> | |
100fa861 | 1326 | CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept |
a1e31878 PP |
1327 | { |
1328 | _ThisCommonField::operator=(val); | |
1329 | return *this; | |
1330 | } | |
1331 | ||
dcb8ae9b | 1332 | Class cls() const noexcept |
a1e31878 | 1333 | { |
341a67c4 | 1334 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1335 | } |
1336 | ||
dcb8ae9b | 1337 | void hasField(const bool hasField) const noexcept |
a1e31878 PP |
1338 | { |
1339 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1340 | ||
341a67c4 | 1341 | bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField)); |
a1e31878 PP |
1342 | } |
1343 | ||
1344 | bool hasField() const noexcept | |
1345 | { | |
a5edf52d | 1346 | return this->field().has_value(); |
a1e31878 PP |
1347 | } |
1348 | ||
dcb8ae9b | 1349 | nonstd::optional<CommonField<LibObjT>> field() const noexcept |
a1e31878 | 1350 | { |
341a67c4 | 1351 | const auto libObjPtr = _Spec::field(this->libObjPtr()); |
a1e31878 PP |
1352 | |
1353 | if (libObjPtr) { | |
1354 | return CommonField<LibObjT> {libObjPtr}; | |
1355 | } | |
1356 | ||
1357 | return nonstd::nullopt; | |
1358 | } | |
1359 | }; | |
1360 | ||
1361 | using OptionField = CommonOptionField<bt_field>; | |
1362 | using ConstOptionField = CommonOptionField<const bt_field>; | |
1363 | ||
1364 | namespace internal { | |
1365 | ||
4927bae7 PP |
1366 | struct OptionFieldTypeDescr |
1367 | { | |
1368 | using Const = ConstOptionField; | |
1369 | using NonConst = OptionField; | |
1370 | }; | |
1371 | ||
1372 | template <> | |
1373 | struct TypeDescr<OptionField> : public OptionFieldTypeDescr | |
1374 | { | |
1375 | }; | |
1376 | ||
1377 | template <> | |
1378 | struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr | |
1379 | { | |
1380 | }; | |
1381 | ||
a1e31878 PP |
1382 | template <typename LibObjT> |
1383 | struct CommonVariantFieldSpec; | |
1384 | ||
b5f55e9f | 1385 | /* Functions specific to mutable variant fields */ |
a1e31878 PP |
1386 | template <> |
1387 | struct CommonVariantFieldSpec<bt_field> final | |
1388 | { | |
1389 | static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept | |
1390 | { | |
1391 | return bt_field_variant_borrow_selected_option_field(libObjPtr); | |
1392 | } | |
1393 | }; | |
1394 | ||
b5f55e9f | 1395 | /* Functions specific to constant variant fields */ |
a1e31878 PP |
1396 | template <> |
1397 | struct CommonVariantFieldSpec<const bt_field> final | |
1398 | { | |
1399 | static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept | |
1400 | { | |
1401 | return bt_field_variant_borrow_selected_option_field_const(libObjPtr); | |
1402 | } | |
1403 | }; | |
1404 | ||
b5f55e9f | 1405 | } /* namespace internal */ |
a1e31878 PP |
1406 | |
1407 | template <typename LibObjT> | |
1408 | class CommonVariantField : public CommonField<LibObjT> | |
1409 | { | |
1410 | private: | |
1411 | using typename CommonField<LibObjT>::_LibObjPtr; | |
1412 | using typename CommonField<LibObjT>::_ThisCommonField; | |
1413 | using _Spec = internal::CommonVariantFieldSpec<LibObjT>; | |
1414 | ||
1415 | public: | |
1416 | using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass, | |
1417 | VariantFieldClass>::type; | |
1418 | ||
1419 | explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} | |
1420 | { | |
1421 | BT_ASSERT_DBG(this->isVariant()); | |
1422 | } | |
1423 | ||
1424 | template <typename OtherLibObjT> | |
100fa861 | 1425 | CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val} |
a1e31878 PP |
1426 | { |
1427 | } | |
1428 | ||
1429 | template <typename OtherLibObjT> | |
100fa861 | 1430 | CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept |
a1e31878 PP |
1431 | { |
1432 | _ThisCommonField::operator=(val); | |
1433 | return *this; | |
1434 | } | |
1435 | ||
dcb8ae9b | 1436 | Class cls() const noexcept |
a1e31878 | 1437 | { |
341a67c4 | 1438 | return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())}; |
a1e31878 PP |
1439 | } |
1440 | ||
dcb8ae9b | 1441 | void selectOption(const std::uint64_t index) const noexcept |
a1e31878 PP |
1442 | { |
1443 | static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`."); | |
1444 | ||
341a67c4 | 1445 | static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index)); |
a1e31878 PP |
1446 | } |
1447 | ||
dcb8ae9b | 1448 | CommonField<LibObjT> selectedOptionField() const noexcept |
a1e31878 | 1449 | { |
341a67c4 | 1450 | return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())}; |
a1e31878 PP |
1451 | } |
1452 | ||
1453 | std::uint64_t selectedOptionIndex() const noexcept | |
1454 | { | |
341a67c4 | 1455 | return bt_field_variant_get_selected_option_index(this->libObjPtr()); |
a1e31878 PP |
1456 | } |
1457 | }; | |
1458 | ||
1459 | using VariantField = CommonVariantField<bt_field>; | |
1460 | using ConstVariantField = CommonVariantField<const bt_field>; | |
1461 | ||
4927bae7 PP |
1462 | namespace internal { |
1463 | ||
1464 | struct VariantFieldTypeDescr | |
1465 | { | |
1466 | using Const = ConstVariantField; | |
1467 | using NonConst = VariantField; | |
1468 | }; | |
1469 | ||
1470 | template <> | |
1471 | struct TypeDescr<VariantField> : public VariantFieldTypeDescr | |
1472 | { | |
1473 | }; | |
1474 | ||
1475 | template <> | |
1476 | struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr | |
1477 | { | |
1478 | }; | |
1479 | ||
1480 | } /* namespace internal */ | |
1481 | ||
a1e31878 PP |
1482 | template <typename LibObjT> |
1483 | CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept | |
1484 | { | |
1485 | BT_ASSERT_DBG(this->isBool()); | |
341a67c4 | 1486 | return CommonBoolField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1487 | } |
1488 | ||
1489 | template <typename LibObjT> | |
1490 | CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept | |
1491 | { | |
1492 | BT_ASSERT_DBG(this->isBitArray()); | |
341a67c4 | 1493 | return CommonBitArrayField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1494 | } |
1495 | ||
1496 | template <typename LibObjT> | |
1497 | CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept | |
1498 | { | |
1499 | BT_ASSERT_DBG(this->isUnsignedInteger()); | |
341a67c4 | 1500 | return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1501 | } |
1502 | ||
1503 | template <typename LibObjT> | |
1504 | CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept | |
1505 | { | |
1506 | BT_ASSERT_DBG(this->isSignedInteger()); | |
341a67c4 | 1507 | return CommonSignedIntegerField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1508 | } |
1509 | ||
1510 | template <typename LibObjT> | |
1511 | CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept | |
1512 | { | |
1513 | BT_ASSERT_DBG(this->isUnsignedEnumeration()); | |
341a67c4 | 1514 | return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1515 | } |
1516 | ||
1517 | template <typename LibObjT> | |
1518 | CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept | |
1519 | { | |
1520 | BT_ASSERT_DBG(this->isSignedEnumeration()); | |
341a67c4 | 1521 | return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1522 | } |
1523 | ||
1524 | template <typename LibObjT> | |
1525 | CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept | |
1526 | { | |
1527 | BT_ASSERT_DBG(this->isSinglePrecisionReal()); | |
341a67c4 | 1528 | return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1529 | } |
1530 | ||
1531 | template <typename LibObjT> | |
1532 | CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept | |
1533 | { | |
1534 | BT_ASSERT_DBG(this->isDoublePrecisionReal()); | |
341a67c4 | 1535 | return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1536 | } |
1537 | ||
1538 | template <typename LibObjT> | |
1539 | CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept | |
1540 | { | |
1541 | BT_ASSERT_DBG(this->isString()); | |
341a67c4 | 1542 | return CommonStringField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1543 | } |
1544 | ||
1545 | template <typename LibObjT> | |
1546 | CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept | |
1547 | { | |
1548 | BT_ASSERT_DBG(this->isStructure()); | |
341a67c4 | 1549 | return CommonStructureField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1550 | } |
1551 | ||
1552 | template <typename LibObjT> | |
1553 | CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept | |
1554 | { | |
1555 | BT_ASSERT_DBG(this->isArray()); | |
341a67c4 | 1556 | return CommonArrayField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1557 | } |
1558 | ||
1559 | template <typename LibObjT> | |
1560 | CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept | |
1561 | { | |
1562 | BT_ASSERT_DBG(this->isDynamicArray()); | |
341a67c4 | 1563 | return CommonDynamicArrayField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1564 | } |
1565 | ||
1566 | template <typename LibObjT> | |
1567 | CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept | |
1568 | { | |
1569 | BT_ASSERT_DBG(this->isOption()); | |
341a67c4 | 1570 | return CommonOptionField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1571 | } |
1572 | ||
1573 | template <typename LibObjT> | |
1574 | CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept | |
1575 | { | |
1576 | BT_ASSERT_DBG(this->isVariant()); | |
341a67c4 | 1577 | return CommonVariantField<LibObjT> {this->libObjPtr()}; |
a1e31878 PP |
1578 | } |
1579 | ||
b5f55e9f | 1580 | } /* namespace bt2 */ |
a1e31878 | 1581 | |
b5f55e9f | 1582 | #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */ |