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