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 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;
223 using Field = CommonField<bt_field>;
224 using ConstField = CommonField<const bt_field>;
228 struct FieldTypeDescr
230 using Const = ConstField;
231 using NonConst = Field;
235 struct TypeDescr<Field> : public FieldTypeDescr
240 struct TypeDescr<ConstField> : public FieldTypeDescr
244 } /* namespace internal */
246 template <typename LibObjT>
247 class CommonBoolField final : public CommonField<LibObjT>
250 using typename CommonField<LibObjT>::_LibObjPtr;
251 using typename CommonField<LibObjT>::_ThisCommonField;
256 explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
258 BT_ASSERT_DBG(this->isBool());
261 template <typename OtherLibObjT>
262 CommonBoolField(const CommonBoolField<OtherLibObjT>& val) noexcept : _ThisCommonField {val}
266 template <typename OtherLibObjT>
267 CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT>& val) noexcept
269 _ThisCommonField::operator=(val);
273 CommonBoolField<LibObjT>& operator=(const Value val) noexcept
275 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
277 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
281 Value value() const noexcept
283 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
286 operator Value() const noexcept
288 return this->value();
292 using BoolField = CommonBoolField<bt_field>;
293 using ConstBoolField = CommonBoolField<const bt_field>;
297 struct BoolFieldTypeDescr
299 using Const = ConstBoolField;
300 using NonConst = BoolField;
304 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
309 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
313 } /* namespace internal */
315 template <typename LibObjT>
316 class CommonBitArrayField final : public CommonField<LibObjT>
319 using typename CommonField<LibObjT>::_LibObjPtr;
320 using typename CommonField<LibObjT>::_ThisCommonField;
323 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
324 BitArrayFieldClass>::type;
326 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
328 BT_ASSERT_DBG(this->isBitArray());
331 template <typename OtherLibObjT>
332 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT>& val) noexcept :
333 _ThisCommonField {val}
337 template <typename OtherLibObjT>
338 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT>& val) noexcept
340 _ThisCommonField::operator=(val);
344 ConstBitArrayFieldClass cls() const noexcept
346 return ConstBitArrayFieldClass {
347 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
352 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
355 CommonBitArrayField<LibObjT>& operator=(const std::uint64_t bits) noexcept
357 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
359 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
363 std::uint64_t valueAsInteger() const noexcept
365 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
368 bool bitValue(const std::uint64_t index) const noexcept
370 BT_ASSERT_DBG(index < this->cls().length());
371 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
375 using BitArrayField = CommonBitArrayField<bt_field>;
376 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
380 struct BitArrayFieldTypeDescr
382 using Const = ConstBitArrayField;
383 using NonConst = BitArrayField;
387 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
392 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
396 } /* namespace internal */
398 template <typename LibObjT>
399 class CommonUnsignedIntegerField final : public CommonField<LibObjT>
402 using typename CommonField<LibObjT>::_ThisCommonField;
405 using typename CommonField<LibObjT>::_LibObjPtr;
406 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
409 using Value = std::uint64_t;
411 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
412 IntegerFieldClass>::type;
414 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
415 _ThisCommonField {libObjPtr}
417 BT_ASSERT_DBG(this->isUnsignedInteger());
420 template <typename OtherLibObjT>
421 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT>& val) noexcept :
422 _ThisCommonField {val}
426 template <typename OtherLibObjT>
427 _ThisCommonUnsignedIntegerField&
428 operator=(const CommonUnsignedIntegerField<OtherLibObjT>& val) noexcept
430 _ThisCommonField::operator=(val);
434 ConstIntegerFieldClass cls() const noexcept
436 return ConstIntegerFieldClass {
437 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
442 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
445 CommonUnsignedIntegerField<LibObjT>& operator=(const Value val) noexcept
447 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
449 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
453 Value value() const noexcept
455 return bt_field_integer_unsigned_get_value(this->libObjPtr());
458 operator Value() const noexcept
460 return this->value();
464 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
465 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
469 struct UnsignedIntegerFieldTypeDescr
471 using Const = ConstUnsignedIntegerField;
472 using NonConst = UnsignedIntegerField;
476 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
481 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
485 } /* namespace internal */
487 template <typename LibObjT>
488 class CommonSignedIntegerField final : public CommonField<LibObjT>
491 using typename CommonField<LibObjT>::_ThisCommonField;
494 using typename CommonField<LibObjT>::_LibObjPtr;
495 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
498 using Value = std::uint64_t;
500 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
501 IntegerFieldClass>::type;
503 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
504 _ThisCommonField {libObjPtr}
506 BT_ASSERT_DBG(this->isSignedInteger());
509 template <typename OtherLibObjT>
510 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT>& val) noexcept :
511 _ThisCommonField {val}
515 template <typename OtherLibObjT>
516 _ThisCommonSignedIntegerField&
517 operator=(const CommonSignedIntegerField<OtherLibObjT>& val) noexcept
519 _ThisCommonField::operator=(val);
523 ConstIntegerFieldClass cls() const noexcept
525 return ConstIntegerFieldClass {
526 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
531 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
534 CommonSignedIntegerField<LibObjT>& operator=(const Value val) noexcept
536 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
538 bt_field_integer_signed_set_value(this->libObjPtr(), val);
542 Value value() const noexcept
544 return bt_field_integer_signed_get_value(this->libObjPtr());
547 operator Value() const noexcept
549 return this->value();
553 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
554 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
558 struct SignedIntegerFieldTypeDescr
560 using Const = ConstSignedIntegerField;
561 using NonConst = SignedIntegerField;
565 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
570 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
574 } /* namespace internal */
576 class EnumerationFieldClassMappingLabels
579 explicit EnumerationFieldClassMappingLabels(
580 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
586 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
589 EnumerationFieldClassMappingLabels&
590 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
592 std::uint64_t size() const noexcept
597 bpstd::string_view operator[](const std::uint64_t index) const noexcept
599 return _mLabels[index];
603 bt_field_class_enumeration_mapping_label_array _mLabels;
604 std::uint64_t _mSize;
607 template <typename LibObjT>
608 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
611 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
612 using typename CommonField<LibObjT>::_LibObjPtr;
616 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
617 UnsignedEnumerationFieldClass>::type;
619 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
620 _ThisCommonUnsignedIntegerField {libObjPtr}
622 BT_ASSERT_DBG(this->isUnsignedEnumeration());
625 template <typename OtherLibObjT>
626 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT>& val) noexcept
628 _ThisCommonUnsignedIntegerField {val}
632 template <typename OtherLibObjT>
633 CommonUnsignedEnumerationField<LibObjT>&
634 operator=(const CommonUnsignedEnumerationField<OtherLibObjT>& val) noexcept
636 _ThisCommonUnsignedIntegerField::operator=(val);
640 ConstUnsignedEnumerationFieldClass cls() const noexcept
642 return ConstUnsignedEnumerationFieldClass {
643 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
648 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
651 EnumerationFieldClassMappingLabels labels() const
653 bt_field_class_enumeration_mapping_label_array labelArray;
655 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
656 &labelArray, &count);
658 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
659 throw LibMemoryError {};
662 return EnumerationFieldClassMappingLabels {labelArray, count};
666 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
667 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
671 struct UnsignedEnumerationFieldTypeDescr
673 using Const = ConstUnsignedEnumerationField;
674 using NonConst = UnsignedEnumerationField;
678 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
683 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
687 } /* namespace internal */
689 template <typename LibObjT>
690 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
693 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
694 using typename CommonField<LibObjT>::_LibObjPtr;
698 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
699 SignedEnumerationFieldClass>::type;
701 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
702 _ThisCommonSignedIntegerField {libObjPtr}
704 BT_ASSERT_DBG(this->isSignedEnumeration());
707 template <typename OtherLibObjT>
708 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT>& val) noexcept :
709 _ThisCommonSignedIntegerField {val}
713 template <typename OtherLibObjT>
714 CommonSignedEnumerationField<LibObjT>&
715 operator=(const CommonSignedEnumerationField<OtherLibObjT>& val) noexcept
717 _ThisCommonSignedIntegerField::operator=(val);
721 ConstSignedEnumerationFieldClass cls() const noexcept
723 return ConstSignedEnumerationFieldClass {
724 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
729 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
732 EnumerationFieldClassMappingLabels labels() const
734 bt_field_class_enumeration_mapping_label_array labelArray;
737 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
739 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
740 throw LibMemoryError {};
743 return EnumerationFieldClassMappingLabels {labelArray, count};
747 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
748 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
752 struct SignedEnumerationFieldTypeDescr
754 using Const = ConstSignedEnumerationField;
755 using NonConst = SignedEnumerationField;
759 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
764 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
768 } /* namespace internal */
770 template <typename LibObjT>
771 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
774 using typename CommonField<LibObjT>::_LibObjPtr;
775 using typename CommonField<LibObjT>::_ThisCommonField;
780 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
781 _ThisCommonField {libObjPtr}
783 BT_ASSERT_DBG(this->isSinglePrecisionReal());
786 template <typename OtherLibObjT>
787 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT>& val) noexcept
789 _ThisCommonField {val}
793 template <typename OtherLibObjT>
794 CommonSinglePrecisionRealField<LibObjT>&
795 operator=(const CommonSinglePrecisionRealField<OtherLibObjT>& val) noexcept
797 _ThisCommonField::operator=(val);
801 CommonSinglePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
803 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
805 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
809 Value value() const noexcept
811 return bt_field_real_single_precision_get_value(this->libObjPtr());
814 operator Value() const noexcept
816 return this->value();
820 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
821 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
825 struct SinglePrecisionRealFieldTypeDescr
827 using Const = ConstSinglePrecisionRealField;
828 using NonConst = SinglePrecisionRealField;
832 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
837 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
841 } /* namespace internal */
843 template <typename LibObjT>
844 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
847 using typename CommonField<LibObjT>::_LibObjPtr;
848 using typename CommonField<LibObjT>::_ThisCommonField;
851 using Value = double;
853 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
854 _ThisCommonField {libObjPtr}
856 BT_ASSERT_DBG(this->isDoublePrecisionReal());
859 template <typename OtherLibObjT>
860 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT>& val) noexcept
862 _ThisCommonField {val}
866 template <typename OtherLibObjT>
867 CommonDoublePrecisionRealField<LibObjT>&
868 operator=(const CommonDoublePrecisionRealField<OtherLibObjT>& val) noexcept
870 _ThisCommonField::operator=(val);
874 CommonDoublePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
876 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
878 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
882 Value value() const noexcept
884 return bt_field_real_single_precision_get_value(this->libObjPtr());
887 operator Value() const noexcept
889 return this->value();
893 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
894 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
898 struct DoublePrecisionRealFieldTypeDescr
900 using Const = ConstDoublePrecisionRealField;
901 using NonConst = DoublePrecisionRealField;
905 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
910 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
914 } /* namespace internal */
916 template <typename LibObjT>
917 class CommonStringField final : public CommonField<LibObjT>
920 using typename CommonField<LibObjT>::_LibObjPtr;
921 using typename CommonField<LibObjT>::_ThisCommonField;
924 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
926 BT_ASSERT_DBG(this->isString());
929 template <typename OtherLibObjT>
930 CommonStringField(const CommonStringField<OtherLibObjT>& val) noexcept : _ThisCommonField {val}
934 template <typename OtherLibObjT>
935 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT>& val) noexcept
937 _ThisCommonField::operator=(val);
941 CommonStringField<LibObjT>& operator=(const char * const val) noexcept
943 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
945 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
947 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
948 throw LibMemoryError {};
954 CommonStringField<LibObjT>& operator=(const std::string& val) noexcept
956 return *this = val.data();
959 void clear() noexcept
961 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
963 bt_field_string_clear(this->libObjPtr());
966 bpstd::string_view value() const noexcept
968 return bt_field_string_get_value(this->libObjPtr());
972 using StringField = CommonStringField<bt_field>;
973 using ConstStringField = CommonStringField<const bt_field>;
977 struct StringFieldTypeDescr
979 using Const = ConstStringField;
980 using NonConst = StringField;
984 struct TypeDescr<StringField> : public StringFieldTypeDescr
989 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
993 template <typename LibObjT>
994 struct CommonStructureFieldSpec;
996 /* Functions specific to mutable structure fields */
998 struct CommonStructureFieldSpec<bt_field> final
1000 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1001 const std::uint64_t index) noexcept
1003 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1006 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1008 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1012 /* Functions specific to constant structure fields */
1014 struct CommonStructureFieldSpec<const bt_field> final
1016 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1017 const std::uint64_t index) noexcept
1019 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1022 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1023 const char * const name) noexcept
1025 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1029 } /* namespace internal */
1031 template <typename LibObjT>
1032 class CommonStructureField final : public CommonField<LibObjT>
1035 using typename CommonField<LibObjT>::_LibObjPtr;
1036 using typename CommonField<LibObjT>::_ThisCommonField;
1037 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1040 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1041 StructureFieldClass>::type;
1043 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1044 _ThisCommonField {libObjPtr}
1046 BT_ASSERT_DBG(this->isStructure());
1049 template <typename OtherLibObjT>
1050 CommonStructureField(const CommonStructureField<OtherLibObjT>& val) noexcept :
1051 _ThisCommonField {val}
1055 template <typename OtherLibObjT>
1056 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT>& val) noexcept
1058 _ThisCommonField::operator=(val);
1062 ConstStructureFieldClass cls() const noexcept
1064 return ConstStructureFieldClass {
1065 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1068 Class cls() noexcept
1070 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1073 std::uint64_t size() const noexcept
1075 return this->cls().size();
1078 ConstField operator[](const std::uint64_t index) const noexcept
1080 return ConstField {internal::CommonStructureFieldSpec<const bt_field>::memberFieldByIndex(
1081 this->libObjPtr(), index)};
1084 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1086 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1089 nonstd::optional<ConstField> operator[](const char * const name) const noexcept
1091 const auto libObjPtr =
1092 internal::CommonStructureFieldSpec<const bt_field>::memberFieldByName(this->libObjPtr(),
1096 return ConstField {libObjPtr};
1099 return nonstd::nullopt;
1102 nonstd::optional<ConstField> operator[](const std::string& name) const noexcept
1104 return (*this)[name.data()];
1107 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) noexcept
1109 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1112 return CommonField<LibObjT> {libObjPtr};
1115 return nonstd::nullopt;
1118 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) noexcept
1120 return (*this)[name.data()];
1124 using StructureField = CommonStructureField<bt_field>;
1125 using ConstStructureField = CommonStructureField<const bt_field>;
1127 namespace internal {
1129 struct StructureFieldTypeDescr
1131 using Const = ConstStructureField;
1132 using NonConst = StructureField;
1136 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1141 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1145 template <typename LibObjT>
1146 struct CommonArrayFieldSpec;
1148 /* Functions specific to mutable array fields */
1150 struct CommonArrayFieldSpec<bt_field> final
1152 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1153 const std::uint64_t index) noexcept
1155 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1159 /* Functions specific to constant array fields */
1161 struct CommonArrayFieldSpec<const bt_field> final
1163 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1164 const std::uint64_t index) noexcept
1166 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1170 } /* namespace internal */
1172 template <typename LibObjT>
1173 class CommonArrayField : public CommonField<LibObjT>
1176 using typename CommonField<LibObjT>::_ThisCommonField;
1177 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1180 using typename CommonField<LibObjT>::_LibObjPtr;
1181 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1184 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1185 ArrayFieldClass>::type;
1187 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1189 BT_ASSERT_DBG(this->isArray());
1192 template <typename OtherLibObjT>
1193 CommonArrayField(const CommonArrayField<OtherLibObjT>& val) noexcept : _ThisCommonField {val}
1197 template <typename OtherLibObjT>
1198 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT>& val) noexcept
1200 _ThisCommonField::operator=(val);
1204 ConstArrayFieldClass cls() const noexcept
1206 return ConstArrayFieldClass {
1207 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1210 Class cls() noexcept
1212 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1215 std::uint64_t length() const noexcept
1217 return bt_field_array_get_length(this->libObjPtr());
1220 ConstField operator[](const std::uint64_t index) const noexcept
1222 return ConstField {internal::CommonArrayFieldSpec<const bt_field>::elementFieldByIndex(
1223 this->libObjPtr(), index)};
1226 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1228 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1232 using ArrayField = CommonArrayField<bt_field>;
1233 using ConstArrayField = CommonArrayField<const bt_field>;
1235 namespace internal {
1237 struct ArrayFieldTypeDescr
1239 using Const = ConstArrayField;
1240 using NonConst = ArrayField;
1244 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1249 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1253 } /* namespace internal */
1255 template <typename LibObjT>
1256 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1259 using typename CommonField<LibObjT>::_LibObjPtr;
1260 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1263 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1264 _ThisCommonArrayField {libObjPtr}
1266 BT_ASSERT_DBG(this->isDynamicArray());
1269 template <typename OtherLibObjT>
1270 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT>& val) noexcept :
1271 _ThisCommonArrayField {val}
1275 template <typename OtherLibObjT>
1276 CommonDynamicArrayField<LibObjT>&
1277 operator=(const CommonDynamicArrayField<OtherLibObjT>& val) noexcept
1279 _ThisCommonArrayField::operator=(val);
1283 std::uint64_t length() const noexcept
1285 return _ThisCommonArrayField::length();
1288 void length(const std::uint64_t length)
1290 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1292 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1294 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1295 throw LibMemoryError {};
1300 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1301 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1303 namespace internal {
1305 struct DynamicArrayFieldTypeDescr
1307 using Const = ConstDynamicArrayField;
1308 using NonConst = DynamicArrayField;
1312 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1317 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1321 template <typename LibObjT>
1322 struct CommonOptionFieldSpec;
1324 /* Functions specific to mutable option fields */
1326 struct CommonOptionFieldSpec<bt_field> final
1328 static bt_field *field(bt_field * const libObjPtr) noexcept
1330 return bt_field_option_borrow_field(libObjPtr);
1334 /* Functions specific to constant option fields */
1336 struct CommonOptionFieldSpec<const bt_field> final
1338 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1340 return bt_field_option_borrow_field_const(libObjPtr);
1344 } /* namespace internal */
1346 template <typename LibObjT>
1347 class CommonOptionField : public CommonField<LibObjT>
1350 using typename CommonField<LibObjT>::_LibObjPtr;
1351 using typename CommonField<LibObjT>::_ThisCommonField;
1352 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1355 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1356 OptionFieldClass>::type;
1358 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1360 BT_ASSERT_DBG(this->isOption());
1363 template <typename OtherLibObjT>
1364 CommonOptionField(const CommonOptionField<OtherLibObjT>& val) noexcept : _ThisCommonField {val}
1368 template <typename OtherLibObjT>
1369 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT>& val) noexcept
1371 _ThisCommonField::operator=(val);
1375 ConstOptionFieldClass cls() const noexcept
1377 return ConstOptionFieldClass {
1378 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1381 Class cls() noexcept
1383 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1386 void hasField(const bool hasField) noexcept
1388 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1390 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1393 bool hasField() const noexcept
1395 return this->field();
1398 nonstd::optional<ConstField> field() const noexcept
1400 const auto libObjPtr =
1401 internal::CommonOptionFieldSpec<const bt_field>::field(this->libObjPtr());
1404 return ConstField {libObjPtr};
1407 return nonstd::nullopt;
1410 nonstd::optional<CommonField<LibObjT>> field() noexcept
1412 const auto libObjPtr = _Spec::field(this->libObjPtr());
1415 return CommonField<LibObjT> {libObjPtr};
1418 return nonstd::nullopt;
1422 using OptionField = CommonOptionField<bt_field>;
1423 using ConstOptionField = CommonOptionField<const bt_field>;
1425 namespace internal {
1427 struct OptionFieldTypeDescr
1429 using Const = ConstOptionField;
1430 using NonConst = OptionField;
1434 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1439 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1443 template <typename LibObjT>
1444 struct CommonVariantFieldSpec;
1446 /* Functions specific to mutable variant fields */
1448 struct CommonVariantFieldSpec<bt_field> final
1450 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1452 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1456 /* Functions specific to constant variant fields */
1458 struct CommonVariantFieldSpec<const bt_field> final
1460 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1462 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1466 } /* namespace internal */
1468 template <typename LibObjT>
1469 class CommonVariantField : public CommonField<LibObjT>
1472 using typename CommonField<LibObjT>::_LibObjPtr;
1473 using typename CommonField<LibObjT>::_ThisCommonField;
1474 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1477 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1478 VariantFieldClass>::type;
1480 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1482 BT_ASSERT_DBG(this->isVariant());
1485 template <typename OtherLibObjT>
1486 CommonVariantField(const CommonVariantField<OtherLibObjT>& val) noexcept :
1487 _ThisCommonField {val}
1491 template <typename OtherLibObjT>
1492 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT>& val) noexcept
1494 _ThisCommonField::operator=(val);
1498 ConstVariantFieldClass cls() const noexcept
1500 return ConstVariantFieldClass {
1501 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1504 Class cls() noexcept
1506 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1509 void selectOption(const std::uint64_t index) noexcept
1511 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1513 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1516 ConstField selectedOptionField() const noexcept
1518 return ConstField {internal::CommonVariantFieldSpec<const bt_field>::selectedOptionField(
1519 this->libObjPtr())};
1522 CommonField<LibObjT> selectedOptionField() noexcept
1524 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1527 std::uint64_t selectedOptionIndex() const noexcept
1529 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1533 using VariantField = CommonVariantField<bt_field>;
1534 using ConstVariantField = CommonVariantField<const bt_field>;
1536 namespace internal {
1538 struct VariantFieldTypeDescr
1540 using Const = ConstVariantField;
1541 using NonConst = VariantField;
1545 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1550 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1554 } /* namespace internal */
1556 template <typename LibObjT>
1557 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1559 BT_ASSERT_DBG(this->isBool());
1560 return CommonBoolField<LibObjT> {this->libObjPtr()};
1563 template <typename LibObjT>
1564 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1566 BT_ASSERT_DBG(this->isBitArray());
1567 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1570 template <typename LibObjT>
1571 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1573 BT_ASSERT_DBG(this->isUnsignedInteger());
1574 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1577 template <typename LibObjT>
1578 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1580 BT_ASSERT_DBG(this->isSignedInteger());
1581 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1584 template <typename LibObjT>
1585 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1587 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1588 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1591 template <typename LibObjT>
1592 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1594 BT_ASSERT_DBG(this->isSignedEnumeration());
1595 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1598 template <typename LibObjT>
1599 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1601 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1602 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1605 template <typename LibObjT>
1606 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1608 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1609 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1612 template <typename LibObjT>
1613 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1615 BT_ASSERT_DBG(this->isString());
1616 return CommonStringField<LibObjT> {this->libObjPtr()};
1619 template <typename LibObjT>
1620 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1622 BT_ASSERT_DBG(this->isStructure());
1623 return CommonStructureField<LibObjT> {this->libObjPtr()};
1626 template <typename LibObjT>
1627 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1629 BT_ASSERT_DBG(this->isArray());
1630 return CommonArrayField<LibObjT> {this->libObjPtr()};
1633 template <typename LibObjT>
1634 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1636 BT_ASSERT_DBG(this->isDynamicArray());
1637 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1640 template <typename LibObjT>
1641 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1643 BT_ASSERT_DBG(this->isOption());
1644 return CommonOptionField<LibObjT> {this->libObjPtr()};
1647 template <typename LibObjT>
1648 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1650 BT_ASSERT_DBG(this->isVariant());
1651 return CommonVariantField<LibObjT> {this->libObjPtr()};
1654 } /* namespace bt2 */
1656 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */