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
11 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "common/assert.h"
16 #include "cpp-common/optional.hpp"
17 #include "cpp-common/string_view.hpp"
19 #include "borrowed-object.hpp"
20 #include "field-class.hpp"
21 #include "internal/utils.hpp"
25 template <typename LibObjT>
26 class CommonBoolField;
28 template <typename LibObjT>
29 class CommonBitArrayField;
31 template <typename LibObjT>
32 class CommonUnsignedIntegerField;
34 template <typename LibObjT>
35 class CommonSignedIntegerField;
37 template <typename LibObjT>
38 class CommonUnsignedEnumerationField;
40 template <typename LibObjT>
41 class CommonSignedEnumerationField;
43 template <typename LibObjT>
44 class CommonSinglePrecisionRealField;
46 template <typename LibObjT>
47 class CommonDoublePrecisionRealField;
49 template <typename LibObjT>
50 class CommonStringField;
52 template <typename LibObjT>
53 class CommonStructureField;
55 template <typename LibObjT>
56 class CommonArrayField;
58 template <typename LibObjT>
59 class CommonDynamicArrayField;
61 template <typename LibObjT>
62 class CommonOptionField;
64 template <typename LibObjT>
65 class CommonVariantField;
69 template <typename LibObjT>
70 struct CommonFieldSpec;
72 /* Functions specific to mutable fields */
74 struct CommonFieldSpec<bt_field> final
76 static bt_field_class *cls(bt_field * const libObjPtr) noexcept
78 return bt_field_borrow_class(libObjPtr);
82 /* Functions specific to constant fields */
84 struct CommonFieldSpec<const bt_field> final
86 static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept
88 return bt_field_borrow_class_const(libObjPtr);
92 } /* namespace internal */
94 template <typename LibObjT>
95 class CommonField : public BorrowedObject<LibObjT>
98 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
101 using typename BorrowedObject<LibObjT>::_LibObjPtr;
102 using _ThisCommonField = CommonField<LibObjT>;
106 typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type;
108 explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
112 template <typename OtherLibObjT>
113 CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
117 template <typename OtherLibObjT>
118 _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept
120 _ThisBorrowedObject::operator=(val);
124 CommonField<const bt_field> asConst() const noexcept
126 return CommonField<const bt_field> {*this};
129 FieldClassType classType() const noexcept
131 return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
134 Class cls() const noexcept
136 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
139 bool isBool() const noexcept
141 return this->cls().isBool();
144 bool isBitArray() const noexcept
146 return this->cls().isBitArray();
149 bool isUnsignedInteger() const noexcept
151 return this->cls().isUnsignedInteger();
154 bool isSignedInteger() const noexcept
156 return this->cls().isSignedInteger();
159 bool isUnsignedEnumeration() const noexcept
161 return this->cls().isUnsignedEnumeration();
164 bool isSignedEnumeration() const noexcept
166 return this->cls().isSignedEnumeration();
169 bool isSinglePrecisionReal() const noexcept
171 return this->cls().isSinglePrecisionReal();
174 bool isDoublePrecisionReal() const noexcept
176 return this->cls().isDoublePrecisionReal();
179 bool isString() const noexcept
181 return this->cls().isString();
184 bool isStructure() const noexcept
186 return this->cls().isStructure();
189 bool isArray() const noexcept
191 return this->cls().isArray();
194 bool isDynamicArray() const noexcept
196 return this->cls().isDynamicArray();
199 bool isOption() const noexcept
201 return this->cls().isOption();
204 bool isVariant() const noexcept
206 return this->cls().isVariant();
209 template <typename FieldT>
210 FieldT as() const noexcept
212 return FieldT {this->libObjPtr()};
215 CommonBoolField<LibObjT> asBool() const noexcept;
216 CommonBitArrayField<LibObjT> asBitArray() const noexcept;
217 CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept;
218 CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept;
219 CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept;
220 CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept;
221 CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept;
222 CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept;
223 CommonStringField<LibObjT> asString() const noexcept;
224 CommonStructureField<LibObjT> asStructure() const noexcept;
225 CommonArrayField<LibObjT> asArray() const noexcept;
226 CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept;
227 CommonOptionField<LibObjT> asOption() const noexcept;
228 CommonVariantField<LibObjT> asVariant() const noexcept;
231 using Field = CommonField<bt_field>;
232 using ConstField = CommonField<const bt_field>;
236 struct FieldTypeDescr
238 using Const = ConstField;
239 using NonConst = Field;
243 struct TypeDescr<Field> : public FieldTypeDescr
248 struct TypeDescr<ConstField> : public FieldTypeDescr
252 } /* namespace internal */
254 template <typename LibObjT>
255 class CommonBoolField final : public CommonField<LibObjT>
258 using typename CommonField<LibObjT>::_LibObjPtr;
259 using typename CommonField<LibObjT>::_ThisCommonField;
264 explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
266 BT_ASSERT_DBG(this->isBool());
269 template <typename OtherLibObjT>
270 CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
274 template <typename OtherLibObjT>
275 CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
277 _ThisCommonField::operator=(val);
281 CommonBoolField<const bt_field> asConst() const noexcept
283 return CommonBoolField<const bt_field> {*this};
286 CommonBoolField<LibObjT> operator=(const Value val) const noexcept
288 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolField`.");
290 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
294 Value value() const noexcept
296 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
299 operator Value() const noexcept
301 return this->value();
305 using BoolField = CommonBoolField<bt_field>;
306 using ConstBoolField = CommonBoolField<const bt_field>;
310 struct BoolFieldTypeDescr
312 using Const = ConstBoolField;
313 using NonConst = BoolField;
317 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
322 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
326 } /* namespace internal */
328 template <typename LibObjT>
329 class CommonBitArrayField final : public CommonField<LibObjT>
332 using typename CommonField<LibObjT>::_LibObjPtr;
333 using typename CommonField<LibObjT>::_ThisCommonField;
336 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
337 BitArrayFieldClass>::type;
339 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
341 BT_ASSERT_DBG(this->isBitArray());
344 template <typename OtherLibObjT>
345 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
346 _ThisCommonField {val}
350 template <typename OtherLibObjT>
351 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
353 _ThisCommonField::operator=(val);
357 CommonBitArrayField<const bt_field> asConst() const noexcept
359 return CommonBitArrayField<const bt_field> {*this};
362 ConstBitArrayFieldClass cls() const noexcept
364 return ConstBitArrayFieldClass {
365 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
370 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
373 CommonBitArrayField<LibObjT> operator=(const std::uint64_t bits) const noexcept
375 static_assert(!std::is_const<LibObjT>::value,
376 "Not available with `bt2::ConstBitArrayField`.");
378 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
382 std::uint64_t valueAsInteger() const noexcept
384 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
387 bool bitValue(const std::uint64_t index) const noexcept
389 BT_ASSERT_DBG(index < this->cls().length());
390 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
394 using BitArrayField = CommonBitArrayField<bt_field>;
395 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
399 struct BitArrayFieldTypeDescr
401 using Const = ConstBitArrayField;
402 using NonConst = BitArrayField;
406 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
411 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
415 } /* namespace internal */
417 template <typename LibObjT>
418 class CommonUnsignedIntegerField : public CommonField<LibObjT>
421 using typename CommonField<LibObjT>::_ThisCommonField;
424 using typename CommonField<LibObjT>::_LibObjPtr;
425 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
428 using Value = std::uint64_t;
430 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
431 IntegerFieldClass>::type;
433 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
434 _ThisCommonField {libObjPtr}
436 BT_ASSERT_DBG(this->isUnsignedInteger());
439 template <typename OtherLibObjT>
440 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
441 _ThisCommonField {val}
445 template <typename OtherLibObjT>
446 _ThisCommonUnsignedIntegerField&
447 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
449 _ThisCommonField::operator=(val);
453 CommonUnsignedIntegerField<const bt_field> asConst() const noexcept
455 return CommonUnsignedIntegerField<const bt_field> {*this};
458 Class cls() const noexcept
460 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
463 CommonUnsignedIntegerField<LibObjT> operator=(const Value val) const noexcept
465 static_assert(!std::is_const<LibObjT>::value,
466 "Not available with `bt2::ConstUnsignedIntegerField`.");
468 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
472 Value value() const noexcept
474 return bt_field_integer_unsigned_get_value(this->libObjPtr());
477 operator Value() const noexcept
479 return this->value();
483 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
484 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
488 struct UnsignedIntegerFieldTypeDescr
490 using Const = ConstUnsignedIntegerField;
491 using NonConst = UnsignedIntegerField;
495 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
500 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
504 } /* namespace internal */
506 template <typename LibObjT>
507 class CommonSignedIntegerField : public CommonField<LibObjT>
510 using typename CommonField<LibObjT>::_ThisCommonField;
513 using typename CommonField<LibObjT>::_LibObjPtr;
514 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
517 using Value = std::int64_t;
519 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
520 IntegerFieldClass>::type;
522 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
523 _ThisCommonField {libObjPtr}
525 BT_ASSERT_DBG(this->isSignedInteger());
528 template <typename OtherLibObjT>
529 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
530 _ThisCommonField {val}
534 template <typename OtherLibObjT>
535 _ThisCommonSignedIntegerField&
536 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
538 _ThisCommonField::operator=(val);
542 CommonSignedIntegerField<const bt_field> asConst() const noexcept
544 return CommonSignedIntegerField<const bt_field> {*this};
547 Class cls() const noexcept
549 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
552 CommonSignedIntegerField<LibObjT> operator=(const Value val) const noexcept
554 static_assert(!std::is_const<LibObjT>::value,
555 "Not available with `bt2::ConstSignedIntegerField`.");
557 bt_field_integer_signed_set_value(this->libObjPtr(), val);
561 Value value() const noexcept
563 return bt_field_integer_signed_get_value(this->libObjPtr());
566 operator Value() const noexcept
568 return this->value();
572 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
573 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
577 struct SignedIntegerFieldTypeDescr
579 using Const = ConstSignedIntegerField;
580 using NonConst = SignedIntegerField;
584 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
589 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
593 } /* namespace internal */
595 class EnumerationFieldClassMappingLabels
598 explicit EnumerationFieldClassMappingLabels(
599 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
605 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
608 EnumerationFieldClassMappingLabels&
609 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
611 std::uint64_t length() const noexcept
616 bpstd::string_view operator[](const std::uint64_t index) const noexcept
618 return _mLabels[index];
622 bt_field_class_enumeration_mapping_label_array _mLabels;
626 template <typename LibObjT>
627 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
630 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
631 using typename CommonField<LibObjT>::_LibObjPtr;
635 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
636 UnsignedEnumerationFieldClass>::type;
638 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
639 _ThisCommonUnsignedIntegerField {libObjPtr}
641 BT_ASSERT_DBG(this->isUnsignedEnumeration());
644 template <typename OtherLibObjT>
645 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
647 _ThisCommonUnsignedIntegerField {val}
651 template <typename OtherLibObjT>
652 CommonUnsignedEnumerationField<LibObjT>&
653 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
655 _ThisCommonUnsignedIntegerField::operator=(val);
659 CommonUnsignedEnumerationField<const bt_field> asConst() const noexcept
661 return CommonUnsignedEnumerationField<const bt_field> {*this};
664 using CommonUnsignedIntegerField<LibObjT>::operator=;
666 Class cls() const noexcept
668 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
671 EnumerationFieldClassMappingLabels labels() const
673 bt_field_class_enumeration_mapping_label_array labelArray;
675 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
676 &labelArray, &count);
678 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
679 throw MemoryError {};
682 return EnumerationFieldClassMappingLabels {labelArray, count};
686 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
687 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
691 struct UnsignedEnumerationFieldTypeDescr
693 using Const = ConstUnsignedEnumerationField;
694 using NonConst = UnsignedEnumerationField;
698 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
703 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
707 } /* namespace internal */
709 template <typename LibObjT>
710 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
713 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
714 using typename CommonField<LibObjT>::_LibObjPtr;
718 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
719 SignedEnumerationFieldClass>::type;
721 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
722 _ThisCommonSignedIntegerField {libObjPtr}
724 BT_ASSERT_DBG(this->isSignedEnumeration());
727 template <typename OtherLibObjT>
728 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
729 _ThisCommonSignedIntegerField {val}
733 template <typename OtherLibObjT>
734 CommonSignedEnumerationField<LibObjT>&
735 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
737 _ThisCommonSignedIntegerField::operator=(val);
741 CommonSignedEnumerationField<const bt_field> asConst() const noexcept
743 return CommonSignedEnumerationField<const bt_field> {*this};
746 using CommonSignedIntegerField<LibObjT>::operator=;
748 Class cls() const noexcept
750 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
753 EnumerationFieldClassMappingLabels labels() const
755 bt_field_class_enumeration_mapping_label_array labelArray;
758 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
760 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
761 throw MemoryError {};
764 return EnumerationFieldClassMappingLabels {labelArray, count};
768 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
769 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
773 struct SignedEnumerationFieldTypeDescr
775 using Const = ConstSignedEnumerationField;
776 using NonConst = SignedEnumerationField;
780 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
785 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
789 } /* namespace internal */
791 template <typename LibObjT>
792 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
795 using typename CommonField<LibObjT>::_LibObjPtr;
796 using typename CommonField<LibObjT>::_ThisCommonField;
801 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
802 _ThisCommonField {libObjPtr}
804 BT_ASSERT_DBG(this->isSinglePrecisionReal());
807 template <typename OtherLibObjT>
808 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
810 _ThisCommonField {val}
814 template <typename OtherLibObjT>
815 CommonSinglePrecisionRealField<LibObjT>&
816 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
818 _ThisCommonField::operator=(val);
822 CommonSinglePrecisionRealField<const bt_field> asConst() const noexcept
824 return CommonSinglePrecisionRealField<const bt_field> {*this};
827 CommonSinglePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
829 static_assert(!std::is_const<LibObjT>::value,
830 "Not available with `bt2::ConstSinglePrecisionRealField`.");
832 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
836 Value value() const noexcept
838 return bt_field_real_single_precision_get_value(this->libObjPtr());
841 operator Value() const noexcept
843 return this->value();
847 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
848 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
852 struct SinglePrecisionRealFieldTypeDescr
854 using Const = ConstSinglePrecisionRealField;
855 using NonConst = SinglePrecisionRealField;
859 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
864 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
868 } /* namespace internal */
870 template <typename LibObjT>
871 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
874 using typename CommonField<LibObjT>::_LibObjPtr;
875 using typename CommonField<LibObjT>::_ThisCommonField;
878 using Value = double;
880 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
881 _ThisCommonField {libObjPtr}
883 BT_ASSERT_DBG(this->isDoublePrecisionReal());
886 template <typename OtherLibObjT>
887 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
889 _ThisCommonField {val}
893 template <typename OtherLibObjT>
894 CommonDoublePrecisionRealField<LibObjT>&
895 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
897 _ThisCommonField::operator=(val);
901 CommonDoublePrecisionRealField<const bt_field> asConst() const noexcept
903 return CommonDoublePrecisionRealField<const bt_field> {*this};
906 CommonDoublePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
908 static_assert(!std::is_const<LibObjT>::value,
909 "Not available with `bt2::ConstDoublePrecisionRealField`.");
911 bt_field_real_double_precision_set_value(this->libObjPtr(), val);
915 Value value() const noexcept
917 return bt_field_real_double_precision_get_value(this->libObjPtr());
920 operator Value() const noexcept
922 return this->value();
926 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
927 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
931 struct DoublePrecisionRealFieldTypeDescr
933 using Const = ConstDoublePrecisionRealField;
934 using NonConst = DoublePrecisionRealField;
938 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
943 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
947 } /* namespace internal */
949 template <typename LibObjT>
950 class CommonStringField final : public CommonField<LibObjT>
953 using typename CommonField<LibObjT>::_LibObjPtr;
954 using typename CommonField<LibObjT>::_ThisCommonField;
957 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
959 BT_ASSERT_DBG(this->isString());
962 template <typename OtherLibObjT>
963 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
967 template <typename OtherLibObjT>
968 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
970 _ThisCommonField::operator=(val);
974 CommonStringField<const bt_field> asConst() const noexcept
976 return CommonStringField<const bt_field> {*this};
979 CommonStringField<LibObjT> operator=(const char * const val) const
981 static_assert(!std::is_const<LibObjT>::value,
982 "Not available with `bt2::ConstStringField`.");
984 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
986 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
987 throw MemoryError {};
993 CommonStringField<LibObjT> operator=(const std::string& val) const
995 return *this = val.data();
998 void append(const char * const begin, const std::uint64_t len) const
1000 static_assert(!std::is_const<LibObjT>::value,
1001 "Not available with `bt2::ConstStringField`.");
1003 const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len);
1005 if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) {
1006 throw MemoryError {};
1010 void append(const std::string& val) const
1012 this->append(val.data(), val.size());
1015 void clear() const noexcept
1017 static_assert(!std::is_const<LibObjT>::value,
1018 "Not available with `bt2::ConstStringField`.");
1020 bt_field_string_clear(this->libObjPtr());
1023 bpstd::string_view value() const noexcept
1025 return bt_field_string_get_value(this->libObjPtr());
1029 using StringField = CommonStringField<bt_field>;
1030 using ConstStringField = CommonStringField<const bt_field>;
1032 namespace internal {
1034 struct StringFieldTypeDescr
1036 using Const = ConstStringField;
1037 using NonConst = StringField;
1041 struct TypeDescr<StringField> : public StringFieldTypeDescr
1046 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
1050 template <typename LibObjT>
1051 struct CommonStructureFieldSpec;
1053 /* Functions specific to mutable structure fields */
1055 struct CommonStructureFieldSpec<bt_field> final
1057 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1058 const std::uint64_t index) noexcept
1060 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1063 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1065 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1069 /* Functions specific to constant structure fields */
1071 struct CommonStructureFieldSpec<const bt_field> final
1073 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1074 const std::uint64_t index) noexcept
1076 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1079 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1080 const char * const name) noexcept
1082 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1086 } /* namespace internal */
1088 template <typename LibObjT>
1089 class CommonStructureField final : public CommonField<LibObjT>
1092 using typename CommonField<LibObjT>::_LibObjPtr;
1093 using typename CommonField<LibObjT>::_ThisCommonField;
1094 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1097 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1098 StructureFieldClass>::type;
1100 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1101 _ThisCommonField {libObjPtr}
1103 BT_ASSERT_DBG(this->isStructure());
1106 template <typename OtherLibObjT>
1107 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1108 _ThisCommonField {val}
1112 template <typename OtherLibObjT>
1113 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1115 _ThisCommonField::operator=(val);
1119 CommonStructureField<const bt_field> asConst() const noexcept
1121 return CommonStructureField<const bt_field> {*this};
1124 Class cls() const noexcept
1126 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1129 std::uint64_t length() const noexcept
1131 return this->cls().length();
1134 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1136 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1139 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept
1141 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1144 return CommonField<LibObjT> {libObjPtr};
1147 return nonstd::nullopt;
1150 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
1152 return (*this)[name.data()];
1156 using StructureField = CommonStructureField<bt_field>;
1157 using ConstStructureField = CommonStructureField<const bt_field>;
1159 namespace internal {
1161 struct StructureFieldTypeDescr
1163 using Const = ConstStructureField;
1164 using NonConst = StructureField;
1168 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1173 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1177 template <typename LibObjT>
1178 struct CommonArrayFieldSpec;
1180 /* Functions specific to mutable array fields */
1182 struct CommonArrayFieldSpec<bt_field> final
1184 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1185 const std::uint64_t index) noexcept
1187 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1191 /* Functions specific to constant array fields */
1193 struct CommonArrayFieldSpec<const bt_field> final
1195 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1196 const std::uint64_t index) noexcept
1198 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1202 } /* namespace internal */
1204 template <typename LibObjT>
1205 class CommonArrayField : public CommonField<LibObjT>
1208 using typename CommonField<LibObjT>::_ThisCommonField;
1209 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1212 using typename CommonField<LibObjT>::_LibObjPtr;
1213 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1216 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1217 ArrayFieldClass>::type;
1219 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1221 BT_ASSERT_DBG(this->isArray());
1224 template <typename OtherLibObjT>
1225 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1229 template <typename OtherLibObjT>
1230 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1232 _ThisCommonField::operator=(val);
1236 CommonArrayField<const bt_field> asConst() const noexcept
1238 return CommonArrayField<const bt_field> {*this};
1241 Class cls() const noexcept
1243 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1246 std::uint64_t length() const noexcept
1248 return bt_field_array_get_length(this->libObjPtr());
1251 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1253 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1257 using ArrayField = CommonArrayField<bt_field>;
1258 using ConstArrayField = CommonArrayField<const bt_field>;
1260 namespace internal {
1262 struct ArrayFieldTypeDescr
1264 using Const = ConstArrayField;
1265 using NonConst = ArrayField;
1269 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1274 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1278 } /* namespace internal */
1280 template <typename LibObjT>
1281 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1284 using typename CommonField<LibObjT>::_LibObjPtr;
1285 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1288 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1289 _ThisCommonArrayField {libObjPtr}
1291 BT_ASSERT_DBG(this->isDynamicArray());
1294 template <typename OtherLibObjT>
1295 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1296 _ThisCommonArrayField {val}
1300 template <typename OtherLibObjT>
1301 CommonDynamicArrayField<LibObjT>&
1302 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1304 _ThisCommonArrayField::operator=(val);
1308 CommonDynamicArrayField<const bt_field> asConst() const noexcept
1310 return CommonDynamicArrayField<const bt_field> {*this};
1313 std::uint64_t length() const noexcept
1315 return _ThisCommonArrayField::length();
1318 void length(const std::uint64_t length) const
1320 static_assert(!std::is_const<LibObjT>::value,
1321 "Not available with `bt2::ConstDynamicArrayField`.");
1323 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1325 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1326 throw MemoryError {};
1331 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1332 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1334 namespace internal {
1336 struct DynamicArrayFieldTypeDescr
1338 using Const = ConstDynamicArrayField;
1339 using NonConst = DynamicArrayField;
1343 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1348 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1352 template <typename LibObjT>
1353 struct CommonOptionFieldSpec;
1355 /* Functions specific to mutable option fields */
1357 struct CommonOptionFieldSpec<bt_field> final
1359 static bt_field *field(bt_field * const libObjPtr) noexcept
1361 return bt_field_option_borrow_field(libObjPtr);
1365 /* Functions specific to constant option fields */
1367 struct CommonOptionFieldSpec<const bt_field> final
1369 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1371 return bt_field_option_borrow_field_const(libObjPtr);
1375 } /* namespace internal */
1377 template <typename LibObjT>
1378 class CommonOptionField : public CommonField<LibObjT>
1381 using typename CommonField<LibObjT>::_LibObjPtr;
1382 using typename CommonField<LibObjT>::_ThisCommonField;
1383 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1386 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1387 OptionFieldClass>::type;
1389 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1391 BT_ASSERT_DBG(this->isOption());
1394 template <typename OtherLibObjT>
1395 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1399 template <typename OtherLibObjT>
1400 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1402 _ThisCommonField::operator=(val);
1406 CommonOptionField<const bt_field> asConst() const noexcept
1408 return CommonOptionField<const bt_field> {*this};
1411 Class cls() const noexcept
1413 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1416 void hasField(const bool hasField) const noexcept
1418 static_assert(!std::is_const<LibObjT>::value,
1419 "Not available with `bt2::ConstOptionField`.");
1421 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1424 bool hasField() const noexcept
1426 return this->field().has_value();
1429 nonstd::optional<CommonField<LibObjT>> field() const noexcept
1431 const auto libObjPtr = _Spec::field(this->libObjPtr());
1434 return CommonField<LibObjT> {libObjPtr};
1437 return nonstd::nullopt;
1441 using OptionField = CommonOptionField<bt_field>;
1442 using ConstOptionField = CommonOptionField<const bt_field>;
1444 namespace internal {
1446 struct OptionFieldTypeDescr
1448 using Const = ConstOptionField;
1449 using NonConst = OptionField;
1453 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1458 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1462 template <typename LibObjT>
1463 struct CommonVariantFieldSpec;
1465 /* Functions specific to mutable variant fields */
1467 struct CommonVariantFieldSpec<bt_field> final
1469 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1471 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1475 /* Functions specific to constant variant fields */
1477 struct CommonVariantFieldSpec<const bt_field> final
1479 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1481 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1485 } /* namespace internal */
1487 template <typename LibObjT>
1488 class CommonVariantField : public CommonField<LibObjT>
1491 using typename CommonField<LibObjT>::_LibObjPtr;
1492 using typename CommonField<LibObjT>::_ThisCommonField;
1493 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1496 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1497 VariantFieldClass>::type;
1499 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1501 BT_ASSERT_DBG(this->isVariant());
1504 template <typename OtherLibObjT>
1505 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1509 template <typename OtherLibObjT>
1510 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1512 _ThisCommonField::operator=(val);
1516 CommonVariantField<const bt_field> asConst() const noexcept
1518 return CommonVariantField<const bt_field> {*this};
1521 Class cls() const noexcept
1523 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1526 void selectOption(const std::uint64_t index) const noexcept
1528 static_assert(!std::is_const<LibObjT>::value,
1529 "Not available with `bt2::ConstVariantField`.");
1531 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1534 CommonField<LibObjT> selectedOptionField() const noexcept
1536 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1539 std::uint64_t selectedOptionIndex() const noexcept
1541 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1545 using VariantField = CommonVariantField<bt_field>;
1546 using ConstVariantField = CommonVariantField<const bt_field>;
1548 namespace internal {
1550 struct VariantFieldTypeDescr
1552 using Const = ConstVariantField;
1553 using NonConst = VariantField;
1557 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1562 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1566 } /* namespace internal */
1568 template <typename LibObjT>
1569 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1571 BT_ASSERT_DBG(this->isBool());
1572 return CommonBoolField<LibObjT> {this->libObjPtr()};
1575 template <typename LibObjT>
1576 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1578 BT_ASSERT_DBG(this->isBitArray());
1579 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1582 template <typename LibObjT>
1583 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1585 BT_ASSERT_DBG(this->isUnsignedInteger());
1586 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1589 template <typename LibObjT>
1590 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1592 BT_ASSERT_DBG(this->isSignedInteger());
1593 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1596 template <typename LibObjT>
1597 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1599 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1600 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1603 template <typename LibObjT>
1604 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1606 BT_ASSERT_DBG(this->isSignedEnumeration());
1607 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1610 template <typename LibObjT>
1611 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1613 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1614 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1617 template <typename LibObjT>
1618 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1620 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1621 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1624 template <typename LibObjT>
1625 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1627 BT_ASSERT_DBG(this->isString());
1628 return CommonStringField<LibObjT> {this->libObjPtr()};
1631 template <typename LibObjT>
1632 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1634 BT_ASSERT_DBG(this->isStructure());
1635 return CommonStructureField<LibObjT> {this->libObjPtr()};
1638 template <typename LibObjT>
1639 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1641 BT_ASSERT_DBG(this->isArray());
1642 return CommonArrayField<LibObjT> {this->libObjPtr()};
1645 template <typename LibObjT>
1646 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1648 BT_ASSERT_DBG(this->isDynamicArray());
1649 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1652 template <typename LibObjT>
1653 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1655 BT_ASSERT_DBG(this->isOption());
1656 return CommonOptionField<LibObjT> {this->libObjPtr()};
1659 template <typename LibObjT>
1660 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1662 BT_ASSERT_DBG(this->isVariant());
1663 return CommonVariantField<LibObjT> {this->libObjPtr()};
1666 } /* namespace bt2 */
1668 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */