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