2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
10 #include <type_traits>
12 #include <babeltrace2/babeltrace.h>
14 #include "common/assert.h"
15 #include "internal/borrowed-obj.hpp"
16 #include "internal/utils.hpp"
17 #include "cpp-common/optional.hpp"
18 #include "cpp-common/string_view.hpp"
19 #include "field-class.hpp"
23 template <typename LibObjT>
24 class CommonBoolField;
26 template <typename LibObjT>
27 class CommonBitArrayField;
29 template <typename LibObjT>
30 class CommonUnsignedIntegerField;
32 template <typename LibObjT>
33 class CommonSignedIntegerField;
35 template <typename LibObjT>
36 class CommonUnsignedEnumerationField;
38 template <typename LibObjT>
39 class CommonSignedEnumerationField;
41 template <typename LibObjT>
42 class CommonSinglePrecisionRealField;
44 template <typename LibObjT>
45 class CommonDoublePrecisionRealField;
47 template <typename LibObjT>
48 class CommonStringField;
50 template <typename LibObjT>
51 class CommonStructureField;
53 template <typename LibObjT>
54 class CommonArrayField;
56 template <typename LibObjT>
57 class CommonDynamicArrayField;
59 template <typename LibObjT>
60 class CommonOptionField;
62 template <typename LibObjT>
63 class CommonVariantField;
67 template <typename LibObjT>
68 struct CommonFieldSpec;
70 /* Functions specific to mutable fields */
72 struct CommonFieldSpec<bt_field> final
74 static bt_field_class *cls(bt_field * const libObjPtr) noexcept
76 return bt_field_borrow_class(libObjPtr);
80 /* Functions specific to constant fields */
82 struct CommonFieldSpec<const bt_field> final
84 static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept
86 return bt_field_borrow_class_const(libObjPtr);
90 } /* namespace internal */
92 template <typename LibObjT>
93 class CommonField : public internal::BorrowedObj<LibObjT>
96 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
99 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
100 using _ThisCommonField = CommonField<LibObjT>;
104 typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type;
106 explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
110 template <typename OtherLibObjT>
111 CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObj {val}
115 template <typename OtherLibObjT>
116 _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept
118 _ThisBorrowedObj::operator=(val);
122 FieldClassType classType() const noexcept
124 return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
127 ConstFieldClass cls() const noexcept
129 return ConstFieldClass {internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
134 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
137 bool isBool() const noexcept
139 return this->cls().isBool();
142 bool isBitArray() const noexcept
144 return this->cls().isBitArray();
147 bool isUnsignedInteger() const noexcept
149 return this->cls().isUnsignedInteger();
152 bool isSignedInteger() const noexcept
154 return this->cls().isSignedInteger();
157 bool isUnsignedEnumeration() const noexcept
159 return this->cls().isUnsignedEnumeration();
162 bool isSignedEnumeration() const noexcept
164 return this->cls().isSignedEnumeration();
167 bool isSinglePrecisionReal() const noexcept
169 return this->cls().isSinglePrecisionReal();
172 bool isDoublePrecisionReal() const noexcept
174 return this->cls().isDoublePrecisionReal();
177 bool isString() const noexcept
179 return this->cls().isString();
182 bool isStructure() const noexcept
184 return this->cls().isStructure();
187 bool isArray() const noexcept
189 return this->cls().isArray();
192 bool isDynamicArray() const noexcept
194 return this->cls().isDynamicArray();
197 bool isOption() const noexcept
199 return this->cls().isOption();
202 bool isVariant() const noexcept
204 return this->cls().isVariant();
207 template <typename FieldT>
208 FieldT as() const noexcept
210 return FieldT {this->libObjPtr()};
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;
229 using Field = CommonField<bt_field>;
230 using ConstField = CommonField<const bt_field>;
234 struct FieldTypeDescr
236 using Const = ConstField;
237 using NonConst = Field;
241 struct TypeDescr<Field> : public FieldTypeDescr
246 struct TypeDescr<ConstField> : public FieldTypeDescr
250 } /* namespace internal */
252 template <typename LibObjT>
253 class CommonBoolField final : public CommonField<LibObjT>
256 using typename CommonField<LibObjT>::_LibObjPtr;
257 using typename CommonField<LibObjT>::_ThisCommonField;
262 explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
264 BT_ASSERT_DBG(this->isBool());
267 template <typename OtherLibObjT>
268 CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
272 template <typename OtherLibObjT>
273 CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
275 _ThisCommonField::operator=(val);
279 CommonBoolField<LibObjT>& operator=(const Value val) noexcept
281 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
283 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
287 Value value() const noexcept
289 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
292 operator Value() const noexcept
294 return this->value();
298 using BoolField = CommonBoolField<bt_field>;
299 using ConstBoolField = CommonBoolField<const bt_field>;
303 struct BoolFieldTypeDescr
305 using Const = ConstBoolField;
306 using NonConst = BoolField;
310 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
315 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
319 } /* namespace internal */
321 template <typename LibObjT>
322 class CommonBitArrayField final : public CommonField<LibObjT>
325 using typename CommonField<LibObjT>::_LibObjPtr;
326 using typename CommonField<LibObjT>::_ThisCommonField;
329 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
330 BitArrayFieldClass>::type;
332 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
334 BT_ASSERT_DBG(this->isBitArray());
337 template <typename OtherLibObjT>
338 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
339 _ThisCommonField {val}
343 template <typename OtherLibObjT>
344 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
346 _ThisCommonField::operator=(val);
350 ConstBitArrayFieldClass cls() const noexcept
352 return ConstBitArrayFieldClass {
353 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
358 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
361 CommonBitArrayField<LibObjT>& operator=(const std::uint64_t bits) noexcept
363 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
365 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
369 std::uint64_t valueAsInteger() const noexcept
371 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
374 bool bitValue(const std::uint64_t index) const noexcept
376 BT_ASSERT_DBG(index < this->cls().length());
377 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
381 using BitArrayField = CommonBitArrayField<bt_field>;
382 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
386 struct BitArrayFieldTypeDescr
388 using Const = ConstBitArrayField;
389 using NonConst = BitArrayField;
393 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
398 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
402 } /* namespace internal */
404 template <typename LibObjT>
405 class CommonUnsignedIntegerField final : public CommonField<LibObjT>
408 using typename CommonField<LibObjT>::_ThisCommonField;
411 using typename CommonField<LibObjT>::_LibObjPtr;
412 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
415 using Value = std::uint64_t;
417 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
418 IntegerFieldClass>::type;
420 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
421 _ThisCommonField {libObjPtr}
423 BT_ASSERT_DBG(this->isUnsignedInteger());
426 template <typename OtherLibObjT>
427 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
428 _ThisCommonField {val}
432 template <typename OtherLibObjT>
433 _ThisCommonUnsignedIntegerField&
434 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
436 _ThisCommonField::operator=(val);
440 ConstIntegerFieldClass cls() const noexcept
442 return ConstIntegerFieldClass {
443 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
448 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
451 CommonUnsignedIntegerField<LibObjT>& operator=(const Value val) noexcept
453 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
455 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
459 Value value() const noexcept
461 return bt_field_integer_unsigned_get_value(this->libObjPtr());
464 operator Value() const noexcept
466 return this->value();
470 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
471 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
475 struct UnsignedIntegerFieldTypeDescr
477 using Const = ConstUnsignedIntegerField;
478 using NonConst = UnsignedIntegerField;
482 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
487 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
491 } /* namespace internal */
493 template <typename LibObjT>
494 class CommonSignedIntegerField final : public CommonField<LibObjT>
497 using typename CommonField<LibObjT>::_ThisCommonField;
500 using typename CommonField<LibObjT>::_LibObjPtr;
501 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
504 using Value = std::uint64_t;
506 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
507 IntegerFieldClass>::type;
509 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
510 _ThisCommonField {libObjPtr}
512 BT_ASSERT_DBG(this->isSignedInteger());
515 template <typename OtherLibObjT>
516 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
517 _ThisCommonField {val}
521 template <typename OtherLibObjT>
522 _ThisCommonSignedIntegerField&
523 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
525 _ThisCommonField::operator=(val);
529 ConstIntegerFieldClass cls() const noexcept
531 return ConstIntegerFieldClass {
532 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
537 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
540 CommonSignedIntegerField<LibObjT>& operator=(const Value val) noexcept
542 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
544 bt_field_integer_signed_set_value(this->libObjPtr(), val);
548 Value value() const noexcept
550 return bt_field_integer_signed_get_value(this->libObjPtr());
553 operator Value() const noexcept
555 return this->value();
559 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
560 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
564 struct SignedIntegerFieldTypeDescr
566 using Const = ConstSignedIntegerField;
567 using NonConst = SignedIntegerField;
571 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
576 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
580 } /* namespace internal */
582 class EnumerationFieldClassMappingLabels
585 explicit EnumerationFieldClassMappingLabels(
586 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
592 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
595 EnumerationFieldClassMappingLabels&
596 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
598 std::uint64_t size() const noexcept
603 bpstd::string_view operator[](const std::uint64_t index) const noexcept
605 return _mLabels[index];
609 bt_field_class_enumeration_mapping_label_array _mLabels;
610 std::uint64_t _mSize;
613 template <typename LibObjT>
614 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
617 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
618 using typename CommonField<LibObjT>::_LibObjPtr;
622 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
623 UnsignedEnumerationFieldClass>::type;
625 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
626 _ThisCommonUnsignedIntegerField {libObjPtr}
628 BT_ASSERT_DBG(this->isUnsignedEnumeration());
631 template <typename OtherLibObjT>
632 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
634 _ThisCommonUnsignedIntegerField {val}
638 template <typename OtherLibObjT>
639 CommonUnsignedEnumerationField<LibObjT>&
640 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
642 _ThisCommonUnsignedIntegerField::operator=(val);
646 ConstUnsignedEnumerationFieldClass cls() const noexcept
648 return ConstUnsignedEnumerationFieldClass {
649 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
654 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
657 EnumerationFieldClassMappingLabels labels() const
659 bt_field_class_enumeration_mapping_label_array labelArray;
661 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
662 &labelArray, &count);
664 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
665 throw MemoryError {};
668 return EnumerationFieldClassMappingLabels {labelArray, count};
672 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
673 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
677 struct UnsignedEnumerationFieldTypeDescr
679 using Const = ConstUnsignedEnumerationField;
680 using NonConst = UnsignedEnumerationField;
684 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
689 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
693 } /* namespace internal */
695 template <typename LibObjT>
696 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
699 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
700 using typename CommonField<LibObjT>::_LibObjPtr;
704 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
705 SignedEnumerationFieldClass>::type;
707 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
708 _ThisCommonSignedIntegerField {libObjPtr}
710 BT_ASSERT_DBG(this->isSignedEnumeration());
713 template <typename OtherLibObjT>
714 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
715 _ThisCommonSignedIntegerField {val}
719 template <typename OtherLibObjT>
720 CommonSignedEnumerationField<LibObjT>&
721 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
723 _ThisCommonSignedIntegerField::operator=(val);
727 ConstSignedEnumerationFieldClass cls() const noexcept
729 return ConstSignedEnumerationFieldClass {
730 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
735 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
738 EnumerationFieldClassMappingLabels labels() const
740 bt_field_class_enumeration_mapping_label_array labelArray;
743 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
745 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
746 throw MemoryError {};
749 return EnumerationFieldClassMappingLabels {labelArray, count};
753 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
754 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
758 struct SignedEnumerationFieldTypeDescr
760 using Const = ConstSignedEnumerationField;
761 using NonConst = SignedEnumerationField;
765 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
770 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
774 } /* namespace internal */
776 template <typename LibObjT>
777 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
780 using typename CommonField<LibObjT>::_LibObjPtr;
781 using typename CommonField<LibObjT>::_ThisCommonField;
786 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
787 _ThisCommonField {libObjPtr}
789 BT_ASSERT_DBG(this->isSinglePrecisionReal());
792 template <typename OtherLibObjT>
793 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
795 _ThisCommonField {val}
799 template <typename OtherLibObjT>
800 CommonSinglePrecisionRealField<LibObjT>&
801 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
803 _ThisCommonField::operator=(val);
807 CommonSinglePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
809 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
811 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
815 Value value() const noexcept
817 return bt_field_real_single_precision_get_value(this->libObjPtr());
820 operator Value() const noexcept
822 return this->value();
826 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
827 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
831 struct SinglePrecisionRealFieldTypeDescr
833 using Const = ConstSinglePrecisionRealField;
834 using NonConst = SinglePrecisionRealField;
838 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
843 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
847 } /* namespace internal */
849 template <typename LibObjT>
850 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
853 using typename CommonField<LibObjT>::_LibObjPtr;
854 using typename CommonField<LibObjT>::_ThisCommonField;
857 using Value = double;
859 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
860 _ThisCommonField {libObjPtr}
862 BT_ASSERT_DBG(this->isDoublePrecisionReal());
865 template <typename OtherLibObjT>
866 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
868 _ThisCommonField {val}
872 template <typename OtherLibObjT>
873 CommonDoublePrecisionRealField<LibObjT>&
874 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
876 _ThisCommonField::operator=(val);
880 CommonDoublePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
882 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
884 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
888 Value value() const noexcept
890 return bt_field_real_single_precision_get_value(this->libObjPtr());
893 operator Value() const noexcept
895 return this->value();
899 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
900 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
904 struct DoublePrecisionRealFieldTypeDescr
906 using Const = ConstDoublePrecisionRealField;
907 using NonConst = DoublePrecisionRealField;
911 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
916 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
920 } /* namespace internal */
922 template <typename LibObjT>
923 class CommonStringField final : public CommonField<LibObjT>
926 using typename CommonField<LibObjT>::_LibObjPtr;
927 using typename CommonField<LibObjT>::_ThisCommonField;
930 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
932 BT_ASSERT_DBG(this->isString());
935 template <typename OtherLibObjT>
936 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
940 template <typename OtherLibObjT>
941 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
943 _ThisCommonField::operator=(val);
947 CommonStringField<LibObjT>& operator=(const char * const val) noexcept
949 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
951 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
953 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
954 throw MemoryError {};
960 CommonStringField<LibObjT>& operator=(const std::string& val) noexcept
962 return *this = val.data();
965 void clear() noexcept
967 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
969 bt_field_string_clear(this->libObjPtr());
972 bpstd::string_view value() const noexcept
974 return bt_field_string_get_value(this->libObjPtr());
978 using StringField = CommonStringField<bt_field>;
979 using ConstStringField = CommonStringField<const bt_field>;
983 struct StringFieldTypeDescr
985 using Const = ConstStringField;
986 using NonConst = StringField;
990 struct TypeDescr<StringField> : public StringFieldTypeDescr
995 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
999 template <typename LibObjT>
1000 struct CommonStructureFieldSpec;
1002 /* Functions specific to mutable structure fields */
1004 struct CommonStructureFieldSpec<bt_field> final
1006 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1007 const std::uint64_t index) noexcept
1009 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1012 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1014 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1018 /* Functions specific to constant structure fields */
1020 struct CommonStructureFieldSpec<const bt_field> final
1022 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1023 const std::uint64_t index) noexcept
1025 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1028 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1029 const char * const name) noexcept
1031 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1035 } /* namespace internal */
1037 template <typename LibObjT>
1038 class CommonStructureField final : public CommonField<LibObjT>
1041 using typename CommonField<LibObjT>::_LibObjPtr;
1042 using typename CommonField<LibObjT>::_ThisCommonField;
1043 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1046 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1047 StructureFieldClass>::type;
1049 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1050 _ThisCommonField {libObjPtr}
1052 BT_ASSERT_DBG(this->isStructure());
1055 template <typename OtherLibObjT>
1056 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1057 _ThisCommonField {val}
1061 template <typename OtherLibObjT>
1062 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1064 _ThisCommonField::operator=(val);
1068 ConstStructureFieldClass cls() const noexcept
1070 return ConstStructureFieldClass {
1071 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1074 Class cls() noexcept
1076 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1079 std::uint64_t size() const noexcept
1081 return this->cls().size();
1084 ConstField operator[](const std::uint64_t index) const noexcept
1086 return ConstField {internal::CommonStructureFieldSpec<const bt_field>::memberFieldByIndex(
1087 this->libObjPtr(), index)};
1090 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1092 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1095 nonstd::optional<ConstField> operator[](const char * const name) const noexcept
1097 const auto libObjPtr =
1098 internal::CommonStructureFieldSpec<const bt_field>::memberFieldByName(this->libObjPtr(),
1102 return ConstField {libObjPtr};
1105 return nonstd::nullopt;
1108 nonstd::optional<ConstField> operator[](const std::string& name) const noexcept
1110 return (*this)[name.data()];
1113 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) noexcept
1115 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1118 return CommonField<LibObjT> {libObjPtr};
1121 return nonstd::nullopt;
1124 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) noexcept
1126 return (*this)[name.data()];
1130 using StructureField = CommonStructureField<bt_field>;
1131 using ConstStructureField = CommonStructureField<const bt_field>;
1133 namespace internal {
1135 struct StructureFieldTypeDescr
1137 using Const = ConstStructureField;
1138 using NonConst = StructureField;
1142 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1147 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1151 template <typename LibObjT>
1152 struct CommonArrayFieldSpec;
1154 /* Functions specific to mutable array fields */
1156 struct CommonArrayFieldSpec<bt_field> final
1158 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1159 const std::uint64_t index) noexcept
1161 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1165 /* Functions specific to constant array fields */
1167 struct CommonArrayFieldSpec<const bt_field> final
1169 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1170 const std::uint64_t index) noexcept
1172 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1176 } /* namespace internal */
1178 template <typename LibObjT>
1179 class CommonArrayField : public CommonField<LibObjT>
1182 using typename CommonField<LibObjT>::_ThisCommonField;
1183 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1186 using typename CommonField<LibObjT>::_LibObjPtr;
1187 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1190 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1191 ArrayFieldClass>::type;
1193 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1195 BT_ASSERT_DBG(this->isArray());
1198 template <typename OtherLibObjT>
1199 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1203 template <typename OtherLibObjT>
1204 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1206 _ThisCommonField::operator=(val);
1210 ConstArrayFieldClass cls() const noexcept
1212 return ConstArrayFieldClass {
1213 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1216 Class cls() noexcept
1218 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1221 std::uint64_t length() const noexcept
1223 return bt_field_array_get_length(this->libObjPtr());
1226 ConstField operator[](const std::uint64_t index) const noexcept
1228 return ConstField {internal::CommonArrayFieldSpec<const bt_field>::elementFieldByIndex(
1229 this->libObjPtr(), index)};
1232 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1234 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1238 using ArrayField = CommonArrayField<bt_field>;
1239 using ConstArrayField = CommonArrayField<const bt_field>;
1241 namespace internal {
1243 struct ArrayFieldTypeDescr
1245 using Const = ConstArrayField;
1246 using NonConst = ArrayField;
1250 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1255 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1259 } /* namespace internal */
1261 template <typename LibObjT>
1262 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1265 using typename CommonField<LibObjT>::_LibObjPtr;
1266 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1269 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1270 _ThisCommonArrayField {libObjPtr}
1272 BT_ASSERT_DBG(this->isDynamicArray());
1275 template <typename OtherLibObjT>
1276 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1277 _ThisCommonArrayField {val}
1281 template <typename OtherLibObjT>
1282 CommonDynamicArrayField<LibObjT>&
1283 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1285 _ThisCommonArrayField::operator=(val);
1289 std::uint64_t length() const noexcept
1291 return _ThisCommonArrayField::length();
1294 void length(const std::uint64_t length)
1296 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1298 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1300 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1301 throw MemoryError {};
1306 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1307 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1309 namespace internal {
1311 struct DynamicArrayFieldTypeDescr
1313 using Const = ConstDynamicArrayField;
1314 using NonConst = DynamicArrayField;
1318 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1323 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1327 template <typename LibObjT>
1328 struct CommonOptionFieldSpec;
1330 /* Functions specific to mutable option fields */
1332 struct CommonOptionFieldSpec<bt_field> final
1334 static bt_field *field(bt_field * const libObjPtr) noexcept
1336 return bt_field_option_borrow_field(libObjPtr);
1340 /* Functions specific to constant option fields */
1342 struct CommonOptionFieldSpec<const bt_field> final
1344 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1346 return bt_field_option_borrow_field_const(libObjPtr);
1350 } /* namespace internal */
1352 template <typename LibObjT>
1353 class CommonOptionField : public CommonField<LibObjT>
1356 using typename CommonField<LibObjT>::_LibObjPtr;
1357 using typename CommonField<LibObjT>::_ThisCommonField;
1358 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1361 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1362 OptionFieldClass>::type;
1364 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1366 BT_ASSERT_DBG(this->isOption());
1369 template <typename OtherLibObjT>
1370 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1374 template <typename OtherLibObjT>
1375 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1377 _ThisCommonField::operator=(val);
1381 ConstOptionFieldClass cls() const noexcept
1383 return ConstOptionFieldClass {
1384 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1387 Class cls() noexcept
1389 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1392 void hasField(const bool hasField) noexcept
1394 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1396 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1399 bool hasField() const noexcept
1401 return this->field();
1404 nonstd::optional<ConstField> field() const noexcept
1406 const auto libObjPtr =
1407 internal::CommonOptionFieldSpec<const bt_field>::field(this->libObjPtr());
1410 return ConstField {libObjPtr};
1413 return nonstd::nullopt;
1416 nonstd::optional<CommonField<LibObjT>> field() noexcept
1418 const auto libObjPtr = _Spec::field(this->libObjPtr());
1421 return CommonField<LibObjT> {libObjPtr};
1424 return nonstd::nullopt;
1428 using OptionField = CommonOptionField<bt_field>;
1429 using ConstOptionField = CommonOptionField<const bt_field>;
1431 namespace internal {
1433 struct OptionFieldTypeDescr
1435 using Const = ConstOptionField;
1436 using NonConst = OptionField;
1440 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1445 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1449 template <typename LibObjT>
1450 struct CommonVariantFieldSpec;
1452 /* Functions specific to mutable variant fields */
1454 struct CommonVariantFieldSpec<bt_field> final
1456 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1458 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1462 /* Functions specific to constant variant fields */
1464 struct CommonVariantFieldSpec<const bt_field> final
1466 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1468 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1472 } /* namespace internal */
1474 template <typename LibObjT>
1475 class CommonVariantField : public CommonField<LibObjT>
1478 using typename CommonField<LibObjT>::_LibObjPtr;
1479 using typename CommonField<LibObjT>::_ThisCommonField;
1480 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1483 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1484 VariantFieldClass>::type;
1486 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1488 BT_ASSERT_DBG(this->isVariant());
1491 template <typename OtherLibObjT>
1492 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1496 template <typename OtherLibObjT>
1497 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1499 _ThisCommonField::operator=(val);
1503 ConstVariantFieldClass cls() const noexcept
1505 return ConstVariantFieldClass {
1506 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1509 Class cls() noexcept
1511 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1514 void selectOption(const std::uint64_t index) noexcept
1516 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1518 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1521 ConstField selectedOptionField() const noexcept
1523 return ConstField {internal::CommonVariantFieldSpec<const bt_field>::selectedOptionField(
1524 this->libObjPtr())};
1527 CommonField<LibObjT> selectedOptionField() noexcept
1529 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1532 std::uint64_t selectedOptionIndex() const noexcept
1534 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1538 using VariantField = CommonVariantField<bt_field>;
1539 using ConstVariantField = CommonVariantField<const bt_field>;
1541 namespace internal {
1543 struct VariantFieldTypeDescr
1545 using Const = ConstVariantField;
1546 using NonConst = VariantField;
1550 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1555 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1559 } /* namespace internal */
1561 template <typename LibObjT>
1562 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1564 BT_ASSERT_DBG(this->isBool());
1565 return CommonBoolField<LibObjT> {this->libObjPtr()};
1568 template <typename LibObjT>
1569 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1571 BT_ASSERT_DBG(this->isBitArray());
1572 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1575 template <typename LibObjT>
1576 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1578 BT_ASSERT_DBG(this->isUnsignedInteger());
1579 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1582 template <typename LibObjT>
1583 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1585 BT_ASSERT_DBG(this->isSignedInteger());
1586 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1589 template <typename LibObjT>
1590 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1592 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1593 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1596 template <typename LibObjT>
1597 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1599 BT_ASSERT_DBG(this->isSignedEnumeration());
1600 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1603 template <typename LibObjT>
1604 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1606 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1607 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1610 template <typename LibObjT>
1611 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1613 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1614 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1617 template <typename LibObjT>
1618 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1620 BT_ASSERT_DBG(this->isString());
1621 return CommonStringField<LibObjT> {this->libObjPtr()};
1624 template <typename LibObjT>
1625 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1627 BT_ASSERT_DBG(this->isStructure());
1628 return CommonStructureField<LibObjT> {this->libObjPtr()};
1631 template <typename LibObjT>
1632 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1634 BT_ASSERT_DBG(this->isArray());
1635 return CommonArrayField<LibObjT> {this->libObjPtr()};
1638 template <typename LibObjT>
1639 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1641 BT_ASSERT_DBG(this->isDynamicArray());
1642 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1645 template <typename LibObjT>
1646 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1648 BT_ASSERT_DBG(this->isOption());
1649 return CommonOptionField<LibObjT> {this->libObjPtr()};
1652 template <typename LibObjT>
1653 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1655 BT_ASSERT_DBG(this->isVariant());
1656 return CommonVariantField<LibObjT> {this->libObjPtr()};
1659 } /* namespace bt2 */
1661 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */