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 FieldClassType classType() const noexcept
126 return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
129 ConstFieldClass cls() const noexcept
131 return ConstFieldClass {internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
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<LibObjT>& operator=(const Value val) noexcept
283 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
285 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
289 Value value() const noexcept
291 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
294 operator Value() const noexcept
296 return this->value();
300 using BoolField = CommonBoolField<bt_field>;
301 using ConstBoolField = CommonBoolField<const bt_field>;
305 struct BoolFieldTypeDescr
307 using Const = ConstBoolField;
308 using NonConst = BoolField;
312 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
317 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
321 } /* namespace internal */
323 template <typename LibObjT>
324 class CommonBitArrayField final : public CommonField<LibObjT>
327 using typename CommonField<LibObjT>::_LibObjPtr;
328 using typename CommonField<LibObjT>::_ThisCommonField;
331 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
332 BitArrayFieldClass>::type;
334 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
336 BT_ASSERT_DBG(this->isBitArray());
339 template <typename OtherLibObjT>
340 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
341 _ThisCommonField {val}
345 template <typename OtherLibObjT>
346 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
348 _ThisCommonField::operator=(val);
352 ConstBitArrayFieldClass cls() const noexcept
354 return ConstBitArrayFieldClass {
355 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
360 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
363 CommonBitArrayField<LibObjT>& operator=(const std::uint64_t bits) noexcept
365 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
367 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
371 std::uint64_t valueAsInteger() const noexcept
373 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
376 bool bitValue(const std::uint64_t index) const noexcept
378 BT_ASSERT_DBG(index < this->cls().length());
379 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
383 using BitArrayField = CommonBitArrayField<bt_field>;
384 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
388 struct BitArrayFieldTypeDescr
390 using Const = ConstBitArrayField;
391 using NonConst = BitArrayField;
395 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
400 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
404 } /* namespace internal */
406 template <typename LibObjT>
407 class CommonUnsignedIntegerField : public CommonField<LibObjT>
410 using typename CommonField<LibObjT>::_ThisCommonField;
413 using typename CommonField<LibObjT>::_LibObjPtr;
414 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
417 using Value = std::uint64_t;
419 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
420 IntegerFieldClass>::type;
422 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
423 _ThisCommonField {libObjPtr}
425 BT_ASSERT_DBG(this->isUnsignedInteger());
428 template <typename OtherLibObjT>
429 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
430 _ThisCommonField {val}
434 template <typename OtherLibObjT>
435 _ThisCommonUnsignedIntegerField&
436 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
438 _ThisCommonField::operator=(val);
442 ConstIntegerFieldClass cls() const noexcept
444 return ConstIntegerFieldClass {
445 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
450 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
453 CommonUnsignedIntegerField<LibObjT>& operator=(const Value val) noexcept
455 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
457 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
461 Value value() const noexcept
463 return bt_field_integer_unsigned_get_value(this->libObjPtr());
466 operator Value() const noexcept
468 return this->value();
472 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
473 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
477 struct UnsignedIntegerFieldTypeDescr
479 using Const = ConstUnsignedIntegerField;
480 using NonConst = UnsignedIntegerField;
484 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
489 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
493 } /* namespace internal */
495 template <typename LibObjT>
496 class CommonSignedIntegerField : public CommonField<LibObjT>
499 using typename CommonField<LibObjT>::_ThisCommonField;
502 using typename CommonField<LibObjT>::_LibObjPtr;
503 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
506 using Value = std::int64_t;
508 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
509 IntegerFieldClass>::type;
511 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
512 _ThisCommonField {libObjPtr}
514 BT_ASSERT_DBG(this->isSignedInteger());
517 template <typename OtherLibObjT>
518 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
519 _ThisCommonField {val}
523 template <typename OtherLibObjT>
524 _ThisCommonSignedIntegerField&
525 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
527 _ThisCommonField::operator=(val);
531 ConstIntegerFieldClass cls() const noexcept
533 return ConstIntegerFieldClass {
534 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
539 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
542 CommonSignedIntegerField<LibObjT>& operator=(const Value val) noexcept
544 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
546 bt_field_integer_signed_set_value(this->libObjPtr(), val);
550 Value value() const noexcept
552 return bt_field_integer_signed_get_value(this->libObjPtr());
555 operator Value() const noexcept
557 return this->value();
561 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
562 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
566 struct SignedIntegerFieldTypeDescr
568 using Const = ConstSignedIntegerField;
569 using NonConst = SignedIntegerField;
573 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
578 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
582 } /* namespace internal */
584 class EnumerationFieldClassMappingLabels
587 explicit EnumerationFieldClassMappingLabels(
588 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
594 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
597 EnumerationFieldClassMappingLabels&
598 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
600 std::uint64_t size() const noexcept
605 bpstd::string_view operator[](const std::uint64_t index) const noexcept
607 return _mLabels[index];
611 bt_field_class_enumeration_mapping_label_array _mLabels;
612 std::uint64_t _mSize;
615 template <typename LibObjT>
616 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
619 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
620 using typename CommonField<LibObjT>::_LibObjPtr;
624 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
625 UnsignedEnumerationFieldClass>::type;
627 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
628 _ThisCommonUnsignedIntegerField {libObjPtr}
630 BT_ASSERT_DBG(this->isUnsignedEnumeration());
633 template <typename OtherLibObjT>
634 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
636 _ThisCommonUnsignedIntegerField {val}
640 template <typename OtherLibObjT>
641 CommonUnsignedEnumerationField<LibObjT>&
642 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
644 _ThisCommonUnsignedIntegerField::operator=(val);
648 ConstUnsignedEnumerationFieldClass cls() const noexcept
650 return ConstUnsignedEnumerationFieldClass {
651 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
654 using CommonUnsignedIntegerField<LibObjT>::operator=;
658 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
661 EnumerationFieldClassMappingLabels labels() const
663 bt_field_class_enumeration_mapping_label_array labelArray;
665 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
666 &labelArray, &count);
668 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
669 throw MemoryError {};
672 return EnumerationFieldClassMappingLabels {labelArray, count};
676 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
677 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
681 struct UnsignedEnumerationFieldTypeDescr
683 using Const = ConstUnsignedEnumerationField;
684 using NonConst = UnsignedEnumerationField;
688 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
693 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
697 } /* namespace internal */
699 template <typename LibObjT>
700 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
703 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
704 using typename CommonField<LibObjT>::_LibObjPtr;
708 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
709 SignedEnumerationFieldClass>::type;
711 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
712 _ThisCommonSignedIntegerField {libObjPtr}
714 BT_ASSERT_DBG(this->isSignedEnumeration());
717 template <typename OtherLibObjT>
718 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
719 _ThisCommonSignedIntegerField {val}
723 template <typename OtherLibObjT>
724 CommonSignedEnumerationField<LibObjT>&
725 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
727 _ThisCommonSignedIntegerField::operator=(val);
731 using CommonSignedIntegerField<LibObjT>::operator=;
733 ConstSignedEnumerationFieldClass cls() const noexcept
735 return ConstSignedEnumerationFieldClass {
736 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
741 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
744 EnumerationFieldClassMappingLabels labels() const
746 bt_field_class_enumeration_mapping_label_array labelArray;
749 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
751 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
752 throw MemoryError {};
755 return EnumerationFieldClassMappingLabels {labelArray, count};
759 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
760 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
764 struct SignedEnumerationFieldTypeDescr
766 using Const = ConstSignedEnumerationField;
767 using NonConst = SignedEnumerationField;
771 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
776 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
780 } /* namespace internal */
782 template <typename LibObjT>
783 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
786 using typename CommonField<LibObjT>::_LibObjPtr;
787 using typename CommonField<LibObjT>::_ThisCommonField;
792 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
793 _ThisCommonField {libObjPtr}
795 BT_ASSERT_DBG(this->isSinglePrecisionReal());
798 template <typename OtherLibObjT>
799 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
801 _ThisCommonField {val}
805 template <typename OtherLibObjT>
806 CommonSinglePrecisionRealField<LibObjT>&
807 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
809 _ThisCommonField::operator=(val);
813 CommonSinglePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
815 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
817 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
821 Value value() const noexcept
823 return bt_field_real_single_precision_get_value(this->libObjPtr());
826 operator Value() const noexcept
828 return this->value();
832 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
833 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
837 struct SinglePrecisionRealFieldTypeDescr
839 using Const = ConstSinglePrecisionRealField;
840 using NonConst = SinglePrecisionRealField;
844 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
849 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
853 } /* namespace internal */
855 template <typename LibObjT>
856 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
859 using typename CommonField<LibObjT>::_LibObjPtr;
860 using typename CommonField<LibObjT>::_ThisCommonField;
863 using Value = double;
865 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
866 _ThisCommonField {libObjPtr}
868 BT_ASSERT_DBG(this->isDoublePrecisionReal());
871 template <typename OtherLibObjT>
872 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
874 _ThisCommonField {val}
878 template <typename OtherLibObjT>
879 CommonDoublePrecisionRealField<LibObjT>&
880 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
882 _ThisCommonField::operator=(val);
886 CommonDoublePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
888 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
890 bt_field_real_double_precision_set_value(this->libObjPtr(), val);
894 Value value() const noexcept
896 return bt_field_real_double_precision_get_value(this->libObjPtr());
899 operator Value() const noexcept
901 return this->value();
905 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
906 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
910 struct DoublePrecisionRealFieldTypeDescr
912 using Const = ConstDoublePrecisionRealField;
913 using NonConst = DoublePrecisionRealField;
917 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
922 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
926 } /* namespace internal */
928 template <typename LibObjT>
929 class CommonStringField final : public CommonField<LibObjT>
932 using typename CommonField<LibObjT>::_LibObjPtr;
933 using typename CommonField<LibObjT>::_ThisCommonField;
936 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
938 BT_ASSERT_DBG(this->isString());
941 template <typename OtherLibObjT>
942 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
946 template <typename OtherLibObjT>
947 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
949 _ThisCommonField::operator=(val);
953 CommonStringField<LibObjT>& operator=(const char * const val)
955 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
957 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
959 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
960 throw MemoryError {};
966 CommonStringField<LibObjT>& operator=(const std::string& val)
968 return *this = val.data();
971 void append(const char * const begin, const std::uint64_t len)
973 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
975 const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len);
977 if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) {
978 throw MemoryError {};
982 void append(const std::string& val)
984 this->append(val.data(), val.size());
987 void clear() noexcept
989 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
991 bt_field_string_clear(this->libObjPtr());
994 bpstd::string_view value() const noexcept
996 return bt_field_string_get_value(this->libObjPtr());
1000 using StringField = CommonStringField<bt_field>;
1001 using ConstStringField = CommonStringField<const bt_field>;
1003 namespace internal {
1005 struct StringFieldTypeDescr
1007 using Const = ConstStringField;
1008 using NonConst = StringField;
1012 struct TypeDescr<StringField> : public StringFieldTypeDescr
1017 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
1021 template <typename LibObjT>
1022 struct CommonStructureFieldSpec;
1024 /* Functions specific to mutable structure fields */
1026 struct CommonStructureFieldSpec<bt_field> final
1028 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1029 const std::uint64_t index) noexcept
1031 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1034 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1036 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1040 /* Functions specific to constant structure fields */
1042 struct CommonStructureFieldSpec<const bt_field> final
1044 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1045 const std::uint64_t index) noexcept
1047 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1050 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1051 const char * const name) noexcept
1053 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1057 } /* namespace internal */
1059 template <typename LibObjT>
1060 class CommonStructureField final : public CommonField<LibObjT>
1063 using typename CommonField<LibObjT>::_LibObjPtr;
1064 using typename CommonField<LibObjT>::_ThisCommonField;
1065 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1068 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1069 StructureFieldClass>::type;
1071 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1072 _ThisCommonField {libObjPtr}
1074 BT_ASSERT_DBG(this->isStructure());
1077 template <typename OtherLibObjT>
1078 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1079 _ThisCommonField {val}
1083 template <typename OtherLibObjT>
1084 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1086 _ThisCommonField::operator=(val);
1090 ConstStructureFieldClass cls() const noexcept
1092 return ConstStructureFieldClass {
1093 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1096 Class cls() noexcept
1098 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1101 std::uint64_t size() const noexcept
1103 return this->cls().size();
1106 ConstField operator[](const std::uint64_t index) const noexcept
1108 return ConstField {internal::CommonStructureFieldSpec<const bt_field>::memberFieldByIndex(
1109 this->libObjPtr(), index)};
1112 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1114 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1117 nonstd::optional<ConstField> operator[](const char * const name) const noexcept
1119 const auto libObjPtr =
1120 internal::CommonStructureFieldSpec<const bt_field>::memberFieldByName(this->libObjPtr(),
1124 return ConstField {libObjPtr};
1127 return nonstd::nullopt;
1130 nonstd::optional<ConstField> operator[](const std::string& name) const noexcept
1132 return (*this)[name.data()];
1135 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) noexcept
1137 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1140 return CommonField<LibObjT> {libObjPtr};
1143 return nonstd::nullopt;
1146 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) noexcept
1148 return (*this)[name.data()];
1152 using StructureField = CommonStructureField<bt_field>;
1153 using ConstStructureField = CommonStructureField<const bt_field>;
1155 namespace internal {
1157 struct StructureFieldTypeDescr
1159 using Const = ConstStructureField;
1160 using NonConst = StructureField;
1164 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1169 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1173 template <typename LibObjT>
1174 struct CommonArrayFieldSpec;
1176 /* Functions specific to mutable array fields */
1178 struct CommonArrayFieldSpec<bt_field> final
1180 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1181 const std::uint64_t index) noexcept
1183 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1187 /* Functions specific to constant array fields */
1189 struct CommonArrayFieldSpec<const bt_field> final
1191 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1192 const std::uint64_t index) noexcept
1194 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1198 } /* namespace internal */
1200 template <typename LibObjT>
1201 class CommonArrayField : public CommonField<LibObjT>
1204 using typename CommonField<LibObjT>::_ThisCommonField;
1205 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1208 using typename CommonField<LibObjT>::_LibObjPtr;
1209 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1212 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1213 ArrayFieldClass>::type;
1215 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1217 BT_ASSERT_DBG(this->isArray());
1220 template <typename OtherLibObjT>
1221 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1225 template <typename OtherLibObjT>
1226 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1228 _ThisCommonField::operator=(val);
1232 ConstArrayFieldClass cls() const noexcept
1234 return ConstArrayFieldClass {
1235 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1238 Class cls() noexcept
1240 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1243 std::uint64_t length() const noexcept
1245 return bt_field_array_get_length(this->libObjPtr());
1248 ConstField operator[](const std::uint64_t index) const noexcept
1250 return ConstField {internal::CommonArrayFieldSpec<const bt_field>::elementFieldByIndex(
1251 this->libObjPtr(), index)};
1254 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1256 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1260 using ArrayField = CommonArrayField<bt_field>;
1261 using ConstArrayField = CommonArrayField<const bt_field>;
1263 namespace internal {
1265 struct ArrayFieldTypeDescr
1267 using Const = ConstArrayField;
1268 using NonConst = ArrayField;
1272 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1277 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1281 } /* namespace internal */
1283 template <typename LibObjT>
1284 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1287 using typename CommonField<LibObjT>::_LibObjPtr;
1288 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1291 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1292 _ThisCommonArrayField {libObjPtr}
1294 BT_ASSERT_DBG(this->isDynamicArray());
1297 template <typename OtherLibObjT>
1298 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1299 _ThisCommonArrayField {val}
1303 template <typename OtherLibObjT>
1304 CommonDynamicArrayField<LibObjT>&
1305 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1307 _ThisCommonArrayField::operator=(val);
1311 std::uint64_t length() const noexcept
1313 return _ThisCommonArrayField::length();
1316 void length(const std::uint64_t length)
1318 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1320 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1322 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1323 throw MemoryError {};
1328 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1329 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1331 namespace internal {
1333 struct DynamicArrayFieldTypeDescr
1335 using Const = ConstDynamicArrayField;
1336 using NonConst = DynamicArrayField;
1340 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1345 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1349 template <typename LibObjT>
1350 struct CommonOptionFieldSpec;
1352 /* Functions specific to mutable option fields */
1354 struct CommonOptionFieldSpec<bt_field> final
1356 static bt_field *field(bt_field * const libObjPtr) noexcept
1358 return bt_field_option_borrow_field(libObjPtr);
1362 /* Functions specific to constant option fields */
1364 struct CommonOptionFieldSpec<const bt_field> final
1366 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1368 return bt_field_option_borrow_field_const(libObjPtr);
1372 } /* namespace internal */
1374 template <typename LibObjT>
1375 class CommonOptionField : public CommonField<LibObjT>
1378 using typename CommonField<LibObjT>::_LibObjPtr;
1379 using typename CommonField<LibObjT>::_ThisCommonField;
1380 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1383 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1384 OptionFieldClass>::type;
1386 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1388 BT_ASSERT_DBG(this->isOption());
1391 template <typename OtherLibObjT>
1392 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1396 template <typename OtherLibObjT>
1397 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1399 _ThisCommonField::operator=(val);
1403 ConstOptionFieldClass cls() const noexcept
1405 return ConstOptionFieldClass {
1406 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1409 Class cls() noexcept
1411 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1414 void hasField(const bool hasField) noexcept
1416 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1418 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1421 bool hasField() const noexcept
1423 return this->field().has_value();
1426 nonstd::optional<ConstField> field() const noexcept
1428 const auto libObjPtr =
1429 internal::CommonOptionFieldSpec<const bt_field>::field(this->libObjPtr());
1432 return ConstField {libObjPtr};
1435 return nonstd::nullopt;
1438 nonstd::optional<CommonField<LibObjT>> field() noexcept
1440 const auto libObjPtr = _Spec::field(this->libObjPtr());
1443 return CommonField<LibObjT> {libObjPtr};
1446 return nonstd::nullopt;
1450 using OptionField = CommonOptionField<bt_field>;
1451 using ConstOptionField = CommonOptionField<const bt_field>;
1453 namespace internal {
1455 struct OptionFieldTypeDescr
1457 using Const = ConstOptionField;
1458 using NonConst = OptionField;
1462 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1467 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1471 template <typename LibObjT>
1472 struct CommonVariantFieldSpec;
1474 /* Functions specific to mutable variant fields */
1476 struct CommonVariantFieldSpec<bt_field> final
1478 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1480 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1484 /* Functions specific to constant variant fields */
1486 struct CommonVariantFieldSpec<const bt_field> final
1488 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1490 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1494 } /* namespace internal */
1496 template <typename LibObjT>
1497 class CommonVariantField : public CommonField<LibObjT>
1500 using typename CommonField<LibObjT>::_LibObjPtr;
1501 using typename CommonField<LibObjT>::_ThisCommonField;
1502 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1505 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1506 VariantFieldClass>::type;
1508 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1510 BT_ASSERT_DBG(this->isVariant());
1513 template <typename OtherLibObjT>
1514 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1518 template <typename OtherLibObjT>
1519 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1521 _ThisCommonField::operator=(val);
1525 ConstVariantFieldClass cls() const noexcept
1527 return ConstVariantFieldClass {
1528 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1531 Class cls() noexcept
1533 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1536 void selectOption(const std::uint64_t index) noexcept
1538 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1540 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1543 ConstField selectedOptionField() const noexcept
1545 return ConstField {internal::CommonVariantFieldSpec<const bt_field>::selectedOptionField(
1546 this->libObjPtr())};
1549 CommonField<LibObjT> selectedOptionField() noexcept
1551 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1554 std::uint64_t selectedOptionIndex() const noexcept
1556 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1560 using VariantField = CommonVariantField<bt_field>;
1561 using ConstVariantField = CommonVariantField<const bt_field>;
1563 namespace internal {
1565 struct VariantFieldTypeDescr
1567 using Const = ConstVariantField;
1568 using NonConst = VariantField;
1572 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1577 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1581 } /* namespace internal */
1583 template <typename LibObjT>
1584 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1586 BT_ASSERT_DBG(this->isBool());
1587 return CommonBoolField<LibObjT> {this->libObjPtr()};
1590 template <typename LibObjT>
1591 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1593 BT_ASSERT_DBG(this->isBitArray());
1594 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1597 template <typename LibObjT>
1598 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1600 BT_ASSERT_DBG(this->isUnsignedInteger());
1601 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1604 template <typename LibObjT>
1605 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1607 BT_ASSERT_DBG(this->isSignedInteger());
1608 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1611 template <typename LibObjT>
1612 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1614 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1615 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1618 template <typename LibObjT>
1619 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1621 BT_ASSERT_DBG(this->isSignedEnumeration());
1622 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1625 template <typename LibObjT>
1626 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1628 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1629 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1632 template <typename LibObjT>
1633 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1635 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1636 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1639 template <typename LibObjT>
1640 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1642 BT_ASSERT_DBG(this->isString());
1643 return CommonStringField<LibObjT> {this->libObjPtr()};
1646 template <typename LibObjT>
1647 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1649 BT_ASSERT_DBG(this->isStructure());
1650 return CommonStructureField<LibObjT> {this->libObjPtr()};
1653 template <typename LibObjT>
1654 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1656 BT_ASSERT_DBG(this->isArray());
1657 return CommonArrayField<LibObjT> {this->libObjPtr()};
1660 template <typename LibObjT>
1661 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1663 BT_ASSERT_DBG(this->isDynamicArray());
1664 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1667 template <typename LibObjT>
1668 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1670 BT_ASSERT_DBG(this->isOption());
1671 return CommonOptionField<LibObjT> {this->libObjPtr()};
1674 template <typename LibObjT>
1675 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1677 BT_ASSERT_DBG(this->isVariant());
1678 return CommonVariantField<LibObjT> {this->libObjPtr()};
1681 } /* namespace bt2 */
1683 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */