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