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 Class cls() const noexcept
131 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
134 bool isBool() const noexcept
136 return this->cls().isBool();
139 bool isBitArray() const noexcept
141 return this->cls().isBitArray();
144 bool isUnsignedInteger() const noexcept
146 return this->cls().isUnsignedInteger();
149 bool isSignedInteger() const noexcept
151 return this->cls().isSignedInteger();
154 bool isUnsignedEnumeration() const noexcept
156 return this->cls().isUnsignedEnumeration();
159 bool isSignedEnumeration() const noexcept
161 return this->cls().isSignedEnumeration();
164 bool isSinglePrecisionReal() const noexcept
166 return this->cls().isSinglePrecisionReal();
169 bool isDoublePrecisionReal() const noexcept
171 return this->cls().isDoublePrecisionReal();
174 bool isString() const noexcept
176 return this->cls().isString();
179 bool isStructure() const noexcept
181 return this->cls().isStructure();
184 bool isArray() const noexcept
186 return this->cls().isArray();
189 bool isDynamicArray() const noexcept
191 return this->cls().isDynamicArray();
194 bool isOption() const noexcept
196 return this->cls().isOption();
199 bool isVariant() const noexcept
201 return this->cls().isVariant();
204 template <typename FieldT>
205 FieldT as() const noexcept
207 return FieldT {this->libObjPtr()};
210 CommonBoolField<LibObjT> asBool() const noexcept;
211 CommonBitArrayField<LibObjT> asBitArray() const noexcept;
212 CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept;
213 CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept;
214 CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept;
215 CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept;
216 CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept;
217 CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept;
218 CommonStringField<LibObjT> asString() const noexcept;
219 CommonStructureField<LibObjT> asStructure() const noexcept;
220 CommonArrayField<LibObjT> asArray() const noexcept;
221 CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept;
222 CommonOptionField<LibObjT> asOption() const noexcept;
223 CommonVariantField<LibObjT> asVariant() const noexcept;
226 using Field = CommonField<bt_field>;
227 using ConstField = CommonField<const bt_field>;
231 struct FieldTypeDescr
233 using Const = ConstField;
234 using NonConst = Field;
238 struct TypeDescr<Field> : public FieldTypeDescr
243 struct TypeDescr<ConstField> : public FieldTypeDescr
247 } /* namespace internal */
249 template <typename LibObjT>
250 class CommonBoolField final : public CommonField<LibObjT>
253 using typename CommonField<LibObjT>::_LibObjPtr;
254 using typename CommonField<LibObjT>::_ThisCommonField;
259 explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
261 BT_ASSERT_DBG(this->isBool());
264 template <typename OtherLibObjT>
265 CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
269 template <typename OtherLibObjT>
270 CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
272 _ThisCommonField::operator=(val);
276 CommonBoolField<LibObjT> operator=(const Value val) const noexcept
278 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
280 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
284 Value value() const noexcept
286 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
289 operator Value() const noexcept
291 return this->value();
295 using BoolField = CommonBoolField<bt_field>;
296 using ConstBoolField = CommonBoolField<const bt_field>;
300 struct BoolFieldTypeDescr
302 using Const = ConstBoolField;
303 using NonConst = BoolField;
307 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
312 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
316 } /* namespace internal */
318 template <typename LibObjT>
319 class CommonBitArrayField final : public CommonField<LibObjT>
322 using typename CommonField<LibObjT>::_LibObjPtr;
323 using typename CommonField<LibObjT>::_ThisCommonField;
326 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
327 BitArrayFieldClass>::type;
329 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
331 BT_ASSERT_DBG(this->isBitArray());
334 template <typename OtherLibObjT>
335 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
336 _ThisCommonField {val}
340 template <typename OtherLibObjT>
341 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
343 _ThisCommonField::operator=(val);
347 ConstBitArrayFieldClass cls() const noexcept
349 return ConstBitArrayFieldClass {
350 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
355 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
358 CommonBitArrayField<LibObjT> operator=(const std::uint64_t bits) const noexcept
360 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
362 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
366 std::uint64_t valueAsInteger() const noexcept
368 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
371 bool bitValue(const std::uint64_t index) const noexcept
373 BT_ASSERT_DBG(index < this->cls().length());
374 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
378 using BitArrayField = CommonBitArrayField<bt_field>;
379 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
383 struct BitArrayFieldTypeDescr
385 using Const = ConstBitArrayField;
386 using NonConst = BitArrayField;
390 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
395 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
399 } /* namespace internal */
401 template <typename LibObjT>
402 class CommonUnsignedIntegerField : public CommonField<LibObjT>
405 using typename CommonField<LibObjT>::_ThisCommonField;
408 using typename CommonField<LibObjT>::_LibObjPtr;
409 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
412 using Value = std::uint64_t;
414 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
415 IntegerFieldClass>::type;
417 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
418 _ThisCommonField {libObjPtr}
420 BT_ASSERT_DBG(this->isUnsignedInteger());
423 template <typename OtherLibObjT>
424 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
425 _ThisCommonField {val}
429 template <typename OtherLibObjT>
430 _ThisCommonUnsignedIntegerField&
431 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
433 _ThisCommonField::operator=(val);
437 Class cls() const noexcept
439 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
442 CommonUnsignedIntegerField<LibObjT> operator=(const Value val) const noexcept
444 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
446 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
450 Value value() const noexcept
452 return bt_field_integer_unsigned_get_value(this->libObjPtr());
455 operator Value() const noexcept
457 return this->value();
461 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
462 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
466 struct UnsignedIntegerFieldTypeDescr
468 using Const = ConstUnsignedIntegerField;
469 using NonConst = UnsignedIntegerField;
473 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
478 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
482 } /* namespace internal */
484 template <typename LibObjT>
485 class CommonSignedIntegerField : public CommonField<LibObjT>
488 using typename CommonField<LibObjT>::_ThisCommonField;
491 using typename CommonField<LibObjT>::_LibObjPtr;
492 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
495 using Value = std::int64_t;
497 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
498 IntegerFieldClass>::type;
500 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
501 _ThisCommonField {libObjPtr}
503 BT_ASSERT_DBG(this->isSignedInteger());
506 template <typename OtherLibObjT>
507 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
508 _ThisCommonField {val}
512 template <typename OtherLibObjT>
513 _ThisCommonSignedIntegerField&
514 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
516 _ThisCommonField::operator=(val);
520 Class cls() const noexcept
522 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
525 CommonSignedIntegerField<LibObjT> operator=(const Value val) const noexcept
527 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
529 bt_field_integer_signed_set_value(this->libObjPtr(), val);
533 Value value() const noexcept
535 return bt_field_integer_signed_get_value(this->libObjPtr());
538 operator Value() const noexcept
540 return this->value();
544 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
545 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
549 struct SignedIntegerFieldTypeDescr
551 using Const = ConstSignedIntegerField;
552 using NonConst = SignedIntegerField;
556 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
561 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
565 } /* namespace internal */
567 class EnumerationFieldClassMappingLabels
570 explicit EnumerationFieldClassMappingLabels(
571 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
577 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
580 EnumerationFieldClassMappingLabels&
581 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
583 std::uint64_t size() const noexcept
588 bpstd::string_view operator[](const std::uint64_t index) const noexcept
590 return _mLabels[index];
594 bt_field_class_enumeration_mapping_label_array _mLabels;
595 std::uint64_t _mSize;
598 template <typename LibObjT>
599 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
602 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
603 using typename CommonField<LibObjT>::_LibObjPtr;
607 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
608 UnsignedEnumerationFieldClass>::type;
610 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
611 _ThisCommonUnsignedIntegerField {libObjPtr}
613 BT_ASSERT_DBG(this->isUnsignedEnumeration());
616 template <typename OtherLibObjT>
617 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
619 _ThisCommonUnsignedIntegerField {val}
623 template <typename OtherLibObjT>
624 CommonUnsignedEnumerationField<LibObjT>&
625 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
627 _ThisCommonUnsignedIntegerField::operator=(val);
631 using CommonUnsignedIntegerField<LibObjT>::operator=;
633 Class cls() const noexcept
635 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
638 EnumerationFieldClassMappingLabels labels() const
640 bt_field_class_enumeration_mapping_label_array labelArray;
642 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
643 &labelArray, &count);
645 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
646 throw MemoryError {};
649 return EnumerationFieldClassMappingLabels {labelArray, count};
653 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
654 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
658 struct UnsignedEnumerationFieldTypeDescr
660 using Const = ConstUnsignedEnumerationField;
661 using NonConst = UnsignedEnumerationField;
665 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
670 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
674 } /* namespace internal */
676 template <typename LibObjT>
677 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
680 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
681 using typename CommonField<LibObjT>::_LibObjPtr;
685 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
686 SignedEnumerationFieldClass>::type;
688 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
689 _ThisCommonSignedIntegerField {libObjPtr}
691 BT_ASSERT_DBG(this->isSignedEnumeration());
694 template <typename OtherLibObjT>
695 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
696 _ThisCommonSignedIntegerField {val}
700 template <typename OtherLibObjT>
701 CommonSignedEnumerationField<LibObjT>&
702 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
704 _ThisCommonSignedIntegerField::operator=(val);
708 using CommonSignedIntegerField<LibObjT>::operator=;
710 Class cls() const noexcept
712 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
715 EnumerationFieldClassMappingLabels labels() const
717 bt_field_class_enumeration_mapping_label_array labelArray;
720 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
722 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
723 throw MemoryError {};
726 return EnumerationFieldClassMappingLabels {labelArray, count};
730 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
731 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
735 struct SignedEnumerationFieldTypeDescr
737 using Const = ConstSignedEnumerationField;
738 using NonConst = SignedEnumerationField;
742 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
747 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
751 } /* namespace internal */
753 template <typename LibObjT>
754 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
757 using typename CommonField<LibObjT>::_LibObjPtr;
758 using typename CommonField<LibObjT>::_ThisCommonField;
763 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
764 _ThisCommonField {libObjPtr}
766 BT_ASSERT_DBG(this->isSinglePrecisionReal());
769 template <typename OtherLibObjT>
770 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
772 _ThisCommonField {val}
776 template <typename OtherLibObjT>
777 CommonSinglePrecisionRealField<LibObjT>&
778 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
780 _ThisCommonField::operator=(val);
784 CommonSinglePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
786 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
788 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
792 Value value() const noexcept
794 return bt_field_real_single_precision_get_value(this->libObjPtr());
797 operator Value() const noexcept
799 return this->value();
803 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
804 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
808 struct SinglePrecisionRealFieldTypeDescr
810 using Const = ConstSinglePrecisionRealField;
811 using NonConst = SinglePrecisionRealField;
815 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
820 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
824 } /* namespace internal */
826 template <typename LibObjT>
827 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
830 using typename CommonField<LibObjT>::_LibObjPtr;
831 using typename CommonField<LibObjT>::_ThisCommonField;
834 using Value = double;
836 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
837 _ThisCommonField {libObjPtr}
839 BT_ASSERT_DBG(this->isDoublePrecisionReal());
842 template <typename OtherLibObjT>
843 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
845 _ThisCommonField {val}
849 template <typename OtherLibObjT>
850 CommonDoublePrecisionRealField<LibObjT>&
851 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
853 _ThisCommonField::operator=(val);
857 CommonDoublePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
859 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
861 bt_field_real_double_precision_set_value(this->libObjPtr(), val);
865 Value value() const noexcept
867 return bt_field_real_double_precision_get_value(this->libObjPtr());
870 operator Value() const noexcept
872 return this->value();
876 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
877 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
881 struct DoublePrecisionRealFieldTypeDescr
883 using Const = ConstDoublePrecisionRealField;
884 using NonConst = DoublePrecisionRealField;
888 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
893 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
897 } /* namespace internal */
899 template <typename LibObjT>
900 class CommonStringField final : public CommonField<LibObjT>
903 using typename CommonField<LibObjT>::_LibObjPtr;
904 using typename CommonField<LibObjT>::_ThisCommonField;
907 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
909 BT_ASSERT_DBG(this->isString());
912 template <typename OtherLibObjT>
913 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
917 template <typename OtherLibObjT>
918 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
920 _ThisCommonField::operator=(val);
924 CommonStringField<LibObjT> operator=(const char * const val) const
926 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
928 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
930 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
931 throw MemoryError {};
937 CommonStringField<LibObjT> operator=(const std::string& val) const
939 return *this = val.data();
942 void append(const char * const begin, const std::uint64_t len) const
944 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
946 const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len);
948 if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) {
949 throw MemoryError {};
953 void append(const std::string& val) const
955 this->append(val.data(), val.size());
958 void clear() const noexcept
960 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
962 bt_field_string_clear(this->libObjPtr());
965 bpstd::string_view value() const noexcept
967 return bt_field_string_get_value(this->libObjPtr());
971 using StringField = CommonStringField<bt_field>;
972 using ConstStringField = CommonStringField<const bt_field>;
976 struct StringFieldTypeDescr
978 using Const = ConstStringField;
979 using NonConst = StringField;
983 struct TypeDescr<StringField> : public StringFieldTypeDescr
988 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
992 template <typename LibObjT>
993 struct CommonStructureFieldSpec;
995 /* Functions specific to mutable structure fields */
997 struct CommonStructureFieldSpec<bt_field> final
999 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1000 const std::uint64_t index) noexcept
1002 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1005 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1007 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1011 /* Functions specific to constant structure fields */
1013 struct CommonStructureFieldSpec<const bt_field> final
1015 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1016 const std::uint64_t index) noexcept
1018 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1021 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1022 const char * const name) noexcept
1024 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1028 } /* namespace internal */
1030 template <typename LibObjT>
1031 class CommonStructureField final : public CommonField<LibObjT>
1034 using typename CommonField<LibObjT>::_LibObjPtr;
1035 using typename CommonField<LibObjT>::_ThisCommonField;
1036 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1039 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1040 StructureFieldClass>::type;
1042 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1043 _ThisCommonField {libObjPtr}
1045 BT_ASSERT_DBG(this->isStructure());
1048 template <typename OtherLibObjT>
1049 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1050 _ThisCommonField {val}
1054 template <typename OtherLibObjT>
1055 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1057 _ThisCommonField::operator=(val);
1061 Class cls() const noexcept
1063 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1066 std::uint64_t size() const noexcept
1068 return this->cls().size();
1071 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1073 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1076 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept
1078 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1081 return CommonField<LibObjT> {libObjPtr};
1084 return nonstd::nullopt;
1087 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
1089 return (*this)[name.data()];
1093 using StructureField = CommonStructureField<bt_field>;
1094 using ConstStructureField = CommonStructureField<const bt_field>;
1096 namespace internal {
1098 struct StructureFieldTypeDescr
1100 using Const = ConstStructureField;
1101 using NonConst = StructureField;
1105 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1110 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1114 template <typename LibObjT>
1115 struct CommonArrayFieldSpec;
1117 /* Functions specific to mutable array fields */
1119 struct CommonArrayFieldSpec<bt_field> final
1121 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1122 const std::uint64_t index) noexcept
1124 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1128 /* Functions specific to constant array fields */
1130 struct CommonArrayFieldSpec<const bt_field> final
1132 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1133 const std::uint64_t index) noexcept
1135 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1139 } /* namespace internal */
1141 template <typename LibObjT>
1142 class CommonArrayField : public CommonField<LibObjT>
1145 using typename CommonField<LibObjT>::_ThisCommonField;
1146 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1149 using typename CommonField<LibObjT>::_LibObjPtr;
1150 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1153 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1154 ArrayFieldClass>::type;
1156 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1158 BT_ASSERT_DBG(this->isArray());
1161 template <typename OtherLibObjT>
1162 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1166 template <typename OtherLibObjT>
1167 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1169 _ThisCommonField::operator=(val);
1173 Class cls() const noexcept
1175 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1178 std::uint64_t length() const noexcept
1180 return bt_field_array_get_length(this->libObjPtr());
1183 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1185 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1189 using ArrayField = CommonArrayField<bt_field>;
1190 using ConstArrayField = CommonArrayField<const bt_field>;
1192 namespace internal {
1194 struct ArrayFieldTypeDescr
1196 using Const = ConstArrayField;
1197 using NonConst = ArrayField;
1201 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1206 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1210 } /* namespace internal */
1212 template <typename LibObjT>
1213 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1216 using typename CommonField<LibObjT>::_LibObjPtr;
1217 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1220 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1221 _ThisCommonArrayField {libObjPtr}
1223 BT_ASSERT_DBG(this->isDynamicArray());
1226 template <typename OtherLibObjT>
1227 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1228 _ThisCommonArrayField {val}
1232 template <typename OtherLibObjT>
1233 CommonDynamicArrayField<LibObjT>&
1234 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1236 _ThisCommonArrayField::operator=(val);
1240 std::uint64_t length() const noexcept
1242 return _ThisCommonArrayField::length();
1245 void length(const std::uint64_t length) const
1247 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1249 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1251 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1252 throw MemoryError {};
1257 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1258 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1260 namespace internal {
1262 struct DynamicArrayFieldTypeDescr
1264 using Const = ConstDynamicArrayField;
1265 using NonConst = DynamicArrayField;
1269 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1274 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1278 template <typename LibObjT>
1279 struct CommonOptionFieldSpec;
1281 /* Functions specific to mutable option fields */
1283 struct CommonOptionFieldSpec<bt_field> final
1285 static bt_field *field(bt_field * const libObjPtr) noexcept
1287 return bt_field_option_borrow_field(libObjPtr);
1291 /* Functions specific to constant option fields */
1293 struct CommonOptionFieldSpec<const bt_field> final
1295 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1297 return bt_field_option_borrow_field_const(libObjPtr);
1301 } /* namespace internal */
1303 template <typename LibObjT>
1304 class CommonOptionField : public CommonField<LibObjT>
1307 using typename CommonField<LibObjT>::_LibObjPtr;
1308 using typename CommonField<LibObjT>::_ThisCommonField;
1309 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1312 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1313 OptionFieldClass>::type;
1315 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1317 BT_ASSERT_DBG(this->isOption());
1320 template <typename OtherLibObjT>
1321 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1325 template <typename OtherLibObjT>
1326 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1328 _ThisCommonField::operator=(val);
1332 Class cls() const noexcept
1334 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1337 void hasField(const bool hasField) const noexcept
1339 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1341 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1344 bool hasField() const noexcept
1346 return this->field().has_value();
1349 nonstd::optional<CommonField<LibObjT>> field() const noexcept
1351 const auto libObjPtr = _Spec::field(this->libObjPtr());
1354 return CommonField<LibObjT> {libObjPtr};
1357 return nonstd::nullopt;
1361 using OptionField = CommonOptionField<bt_field>;
1362 using ConstOptionField = CommonOptionField<const bt_field>;
1364 namespace internal {
1366 struct OptionFieldTypeDescr
1368 using Const = ConstOptionField;
1369 using NonConst = OptionField;
1373 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1378 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1382 template <typename LibObjT>
1383 struct CommonVariantFieldSpec;
1385 /* Functions specific to mutable variant fields */
1387 struct CommonVariantFieldSpec<bt_field> final
1389 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1391 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1395 /* Functions specific to constant variant fields */
1397 struct CommonVariantFieldSpec<const bt_field> final
1399 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1401 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1405 } /* namespace internal */
1407 template <typename LibObjT>
1408 class CommonVariantField : public CommonField<LibObjT>
1411 using typename CommonField<LibObjT>::_LibObjPtr;
1412 using typename CommonField<LibObjT>::_ThisCommonField;
1413 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1416 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1417 VariantFieldClass>::type;
1419 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1421 BT_ASSERT_DBG(this->isVariant());
1424 template <typename OtherLibObjT>
1425 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1429 template <typename OtherLibObjT>
1430 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1432 _ThisCommonField::operator=(val);
1436 Class cls() const noexcept
1438 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1441 void selectOption(const std::uint64_t index) const noexcept
1443 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1445 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1448 CommonField<LibObjT> selectedOptionField() const noexcept
1450 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1453 std::uint64_t selectedOptionIndex() const noexcept
1455 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1459 using VariantField = CommonVariantField<bt_field>;
1460 using ConstVariantField = CommonVariantField<const bt_field>;
1462 namespace internal {
1464 struct VariantFieldTypeDescr
1466 using Const = ConstVariantField;
1467 using NonConst = VariantField;
1471 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1476 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1480 } /* namespace internal */
1482 template <typename LibObjT>
1483 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1485 BT_ASSERT_DBG(this->isBool());
1486 return CommonBoolField<LibObjT> {this->libObjPtr()};
1489 template <typename LibObjT>
1490 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1492 BT_ASSERT_DBG(this->isBitArray());
1493 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1496 template <typename LibObjT>
1497 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1499 BT_ASSERT_DBG(this->isUnsignedInteger());
1500 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1503 template <typename LibObjT>
1504 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1506 BT_ASSERT_DBG(this->isSignedInteger());
1507 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1510 template <typename LibObjT>
1511 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1513 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1514 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1517 template <typename LibObjT>
1518 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1520 BT_ASSERT_DBG(this->isSignedEnumeration());
1521 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1524 template <typename LibObjT>
1525 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1527 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1528 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1531 template <typename LibObjT>
1532 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1534 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1535 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1538 template <typename LibObjT>
1539 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1541 BT_ASSERT_DBG(this->isString());
1542 return CommonStringField<LibObjT> {this->libObjPtr()};
1545 template <typename LibObjT>
1546 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1548 BT_ASSERT_DBG(this->isStructure());
1549 return CommonStructureField<LibObjT> {this->libObjPtr()};
1552 template <typename LibObjT>
1553 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1555 BT_ASSERT_DBG(this->isArray());
1556 return CommonArrayField<LibObjT> {this->libObjPtr()};
1559 template <typename LibObjT>
1560 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1562 BT_ASSERT_DBG(this->isDynamicArray());
1563 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1566 template <typename LibObjT>
1567 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1569 BT_ASSERT_DBG(this->isOption());
1570 return CommonOptionField<LibObjT> {this->libObjPtr()};
1573 template <typename LibObjT>
1574 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1576 BT_ASSERT_DBG(this->isVariant());
1577 return CommonVariantField<LibObjT> {this->libObjPtr()};
1580 } /* namespace bt2 */
1582 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */