2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
10 #include <type_traits>
12 #include <babeltrace2/babeltrace.h>
14 #include "common/assert.h"
15 #include "internal/borrowed-obj.hpp"
16 #include "internal/utils.hpp"
17 #include "cpp-common/optional.hpp"
18 #include "cpp-common/string_view.hpp"
19 #include "field-class.hpp"
23 template <typename LibObjT>
24 class CommonBoolField;
26 template <typename LibObjT>
27 class CommonBitArrayField;
29 template <typename LibObjT>
30 class CommonUnsignedIntegerField;
32 template <typename LibObjT>
33 class CommonSignedIntegerField;
35 template <typename LibObjT>
36 class CommonUnsignedEnumerationField;
38 template <typename LibObjT>
39 class CommonSignedEnumerationField;
41 template <typename LibObjT>
42 class CommonSinglePrecisionRealField;
44 template <typename LibObjT>
45 class CommonDoublePrecisionRealField;
47 template <typename LibObjT>
48 class CommonStringField;
50 template <typename LibObjT>
51 class CommonStructureField;
53 template <typename LibObjT>
54 class CommonArrayField;
56 template <typename LibObjT>
57 class CommonDynamicArrayField;
59 template <typename LibObjT>
60 class CommonOptionField;
62 template <typename LibObjT>
63 class CommonVariantField;
67 template <typename LibObjT>
68 struct CommonFieldSpec;
70 /* Functions specific to mutable fields */
72 struct CommonFieldSpec<bt_field> final
74 static bt_field_class *cls(bt_field * const libObjPtr) noexcept
76 return bt_field_borrow_class(libObjPtr);
80 /* Functions specific to constant fields */
82 struct CommonFieldSpec<const bt_field> final
84 static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept
86 return bt_field_borrow_class_const(libObjPtr);
90 } /* namespace internal */
92 template <typename LibObjT>
93 class CommonField : public internal::BorrowedObj<LibObjT>
96 using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
99 using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
100 using _ThisCommonField = CommonField<LibObjT>;
104 typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type;
106 explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
110 template <typename OtherLibObjT>
111 CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObj {val}
115 template <typename OtherLibObjT>
116 _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept
118 _ThisBorrowedObj::operator=(val);
122 FieldClassType classType() const noexcept
124 return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
127 ConstFieldClass cls() const noexcept
129 return ConstFieldClass {internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
134 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
137 bool isBool() const noexcept
139 return this->cls().isBool();
142 bool isBitArray() const noexcept
144 return this->cls().isBitArray();
147 bool isUnsignedInteger() const noexcept
149 return this->cls().isUnsignedInteger();
152 bool isSignedInteger() const noexcept
154 return this->cls().isSignedInteger();
157 bool isUnsignedEnumeration() const noexcept
159 return this->cls().isUnsignedEnumeration();
162 bool isSignedEnumeration() const noexcept
164 return this->cls().isSignedEnumeration();
167 bool isSinglePrecisionReal() const noexcept
169 return this->cls().isSinglePrecisionReal();
172 bool isDoublePrecisionReal() const noexcept
174 return this->cls().isDoublePrecisionReal();
177 bool isString() const noexcept
179 return this->cls().isString();
182 bool isStructure() const noexcept
184 return this->cls().isStructure();
187 bool isArray() const noexcept
189 return this->cls().isArray();
192 bool isDynamicArray() const noexcept
194 return this->cls().isDynamicArray();
197 bool isOption() const noexcept
199 return this->cls().isOption();
202 bool isVariant() const noexcept
204 return this->cls().isVariant();
207 template <typename FieldT>
208 FieldT as() const noexcept
210 return FieldT {this->libObjPtr()};
213 CommonBoolField<LibObjT> asBool() const noexcept;
214 CommonBitArrayField<LibObjT> asBitArray() const noexcept;
215 CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept;
216 CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept;
217 CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept;
218 CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept;
219 CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept;
220 CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept;
221 CommonStringField<LibObjT> asString() const noexcept;
222 CommonStructureField<LibObjT> asStructure() const noexcept;
223 CommonArrayField<LibObjT> asArray() const noexcept;
224 CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept;
225 CommonOptionField<LibObjT> asOption() const noexcept;
226 CommonVariantField<LibObjT> asVariant() const noexcept;
229 using Field = CommonField<bt_field>;
230 using ConstField = CommonField<const bt_field>;
234 struct FieldTypeDescr
236 using Const = ConstField;
237 using NonConst = Field;
241 struct TypeDescr<Field> : public FieldTypeDescr
246 struct TypeDescr<ConstField> : public FieldTypeDescr
250 } /* namespace internal */
252 template <typename LibObjT>
253 class CommonBoolField final : public CommonField<LibObjT>
256 using typename CommonField<LibObjT>::_LibObjPtr;
257 using typename CommonField<LibObjT>::_ThisCommonField;
262 explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
264 BT_ASSERT_DBG(this->isBool());
267 template <typename OtherLibObjT>
268 CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
272 template <typename OtherLibObjT>
273 CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
275 _ThisCommonField::operator=(val);
279 CommonBoolField<LibObjT>& operator=(const Value val) noexcept
281 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
283 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
287 Value value() const noexcept
289 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
292 operator Value() const noexcept
294 return this->value();
298 using BoolField = CommonBoolField<bt_field>;
299 using ConstBoolField = CommonBoolField<const bt_field>;
303 struct BoolFieldTypeDescr
305 using Const = ConstBoolField;
306 using NonConst = BoolField;
310 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
315 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
319 } /* namespace internal */
321 template <typename LibObjT>
322 class CommonBitArrayField final : public CommonField<LibObjT>
325 using typename CommonField<LibObjT>::_LibObjPtr;
326 using typename CommonField<LibObjT>::_ThisCommonField;
329 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
330 BitArrayFieldClass>::type;
332 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
334 BT_ASSERT_DBG(this->isBitArray());
337 template <typename OtherLibObjT>
338 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
339 _ThisCommonField {val}
343 template <typename OtherLibObjT>
344 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
346 _ThisCommonField::operator=(val);
350 ConstBitArrayFieldClass cls() const noexcept
352 return ConstBitArrayFieldClass {
353 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
358 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
361 CommonBitArrayField<LibObjT>& operator=(const std::uint64_t bits) noexcept
363 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
365 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
369 std::uint64_t valueAsInteger() const noexcept
371 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
374 bool bitValue(const std::uint64_t index) const noexcept
376 BT_ASSERT_DBG(index < this->cls().length());
377 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
381 using BitArrayField = CommonBitArrayField<bt_field>;
382 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
386 struct BitArrayFieldTypeDescr
388 using Const = ConstBitArrayField;
389 using NonConst = BitArrayField;
393 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
398 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
402 } /* namespace internal */
404 template <typename LibObjT>
405 class CommonUnsignedIntegerField : public CommonField<LibObjT>
408 using typename CommonField<LibObjT>::_ThisCommonField;
411 using typename CommonField<LibObjT>::_LibObjPtr;
412 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
415 using Value = std::uint64_t;
417 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
418 IntegerFieldClass>::type;
420 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
421 _ThisCommonField {libObjPtr}
423 BT_ASSERT_DBG(this->isUnsignedInteger());
426 template <typename OtherLibObjT>
427 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
428 _ThisCommonField {val}
432 template <typename OtherLibObjT>
433 _ThisCommonUnsignedIntegerField&
434 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
436 _ThisCommonField::operator=(val);
440 ConstIntegerFieldClass cls() const noexcept
442 return ConstIntegerFieldClass {
443 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
448 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
451 CommonUnsignedIntegerField<LibObjT>& operator=(const Value val) noexcept
453 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
455 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
459 Value value() const noexcept
461 return bt_field_integer_unsigned_get_value(this->libObjPtr());
464 operator Value() const noexcept
466 return this->value();
470 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
471 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
475 struct UnsignedIntegerFieldTypeDescr
477 using Const = ConstUnsignedIntegerField;
478 using NonConst = UnsignedIntegerField;
482 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
487 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
491 } /* namespace internal */
493 template <typename LibObjT>
494 class CommonSignedIntegerField : public CommonField<LibObjT>
497 using typename CommonField<LibObjT>::_ThisCommonField;
500 using typename CommonField<LibObjT>::_LibObjPtr;
501 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
504 using Value = std::int64_t;
506 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
507 IntegerFieldClass>::type;
509 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
510 _ThisCommonField {libObjPtr}
512 BT_ASSERT_DBG(this->isSignedInteger());
515 template <typename OtherLibObjT>
516 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
517 _ThisCommonField {val}
521 template <typename OtherLibObjT>
522 _ThisCommonSignedIntegerField&
523 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
525 _ThisCommonField::operator=(val);
529 ConstIntegerFieldClass cls() const noexcept
531 return ConstIntegerFieldClass {
532 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
537 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
540 CommonSignedIntegerField<LibObjT>& operator=(const Value val) noexcept
542 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
544 bt_field_integer_signed_set_value(this->libObjPtr(), val);
548 Value value() const noexcept
550 return bt_field_integer_signed_get_value(this->libObjPtr());
553 operator Value() const noexcept
555 return this->value();
559 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
560 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
564 struct SignedIntegerFieldTypeDescr
566 using Const = ConstSignedIntegerField;
567 using NonConst = SignedIntegerField;
571 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
576 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
580 } /* namespace internal */
582 class EnumerationFieldClassMappingLabels
585 explicit EnumerationFieldClassMappingLabels(
586 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
592 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
595 EnumerationFieldClassMappingLabels&
596 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
598 std::uint64_t size() const noexcept
603 bpstd::string_view operator[](const std::uint64_t index) const noexcept
605 return _mLabels[index];
609 bt_field_class_enumeration_mapping_label_array _mLabels;
610 std::uint64_t _mSize;
613 template <typename LibObjT>
614 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
617 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
618 using typename CommonField<LibObjT>::_LibObjPtr;
622 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
623 UnsignedEnumerationFieldClass>::type;
625 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
626 _ThisCommonUnsignedIntegerField {libObjPtr}
628 BT_ASSERT_DBG(this->isUnsignedEnumeration());
631 template <typename OtherLibObjT>
632 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
634 _ThisCommonUnsignedIntegerField {val}
638 template <typename OtherLibObjT>
639 CommonUnsignedEnumerationField<LibObjT>&
640 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
642 _ThisCommonUnsignedIntegerField::operator=(val);
646 ConstUnsignedEnumerationFieldClass cls() const noexcept
648 return ConstUnsignedEnumerationFieldClass {
649 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
652 using CommonUnsignedIntegerField<LibObjT>::operator=;
656 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
659 EnumerationFieldClassMappingLabels labels() const
661 bt_field_class_enumeration_mapping_label_array labelArray;
663 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
664 &labelArray, &count);
666 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
667 throw MemoryError {};
670 return EnumerationFieldClassMappingLabels {labelArray, count};
674 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
675 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
679 struct UnsignedEnumerationFieldTypeDescr
681 using Const = ConstUnsignedEnumerationField;
682 using NonConst = UnsignedEnumerationField;
686 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
691 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
695 } /* namespace internal */
697 template <typename LibObjT>
698 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
701 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
702 using typename CommonField<LibObjT>::_LibObjPtr;
706 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
707 SignedEnumerationFieldClass>::type;
709 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
710 _ThisCommonSignedIntegerField {libObjPtr}
712 BT_ASSERT_DBG(this->isSignedEnumeration());
715 template <typename OtherLibObjT>
716 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
717 _ThisCommonSignedIntegerField {val}
721 template <typename OtherLibObjT>
722 CommonSignedEnumerationField<LibObjT>&
723 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
725 _ThisCommonSignedIntegerField::operator=(val);
729 using CommonSignedIntegerField<LibObjT>::operator=;
731 ConstSignedEnumerationFieldClass cls() const noexcept
733 return ConstSignedEnumerationFieldClass {
734 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
739 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
742 EnumerationFieldClassMappingLabels labels() const
744 bt_field_class_enumeration_mapping_label_array labelArray;
747 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
749 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
750 throw MemoryError {};
753 return EnumerationFieldClassMappingLabels {labelArray, count};
757 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
758 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
762 struct SignedEnumerationFieldTypeDescr
764 using Const = ConstSignedEnumerationField;
765 using NonConst = SignedEnumerationField;
769 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
774 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
778 } /* namespace internal */
780 template <typename LibObjT>
781 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
784 using typename CommonField<LibObjT>::_LibObjPtr;
785 using typename CommonField<LibObjT>::_ThisCommonField;
790 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
791 _ThisCommonField {libObjPtr}
793 BT_ASSERT_DBG(this->isSinglePrecisionReal());
796 template <typename OtherLibObjT>
797 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
799 _ThisCommonField {val}
803 template <typename OtherLibObjT>
804 CommonSinglePrecisionRealField<LibObjT>&
805 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
807 _ThisCommonField::operator=(val);
811 CommonSinglePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
813 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
815 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
819 Value value() const noexcept
821 return bt_field_real_single_precision_get_value(this->libObjPtr());
824 operator Value() const noexcept
826 return this->value();
830 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
831 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
835 struct SinglePrecisionRealFieldTypeDescr
837 using Const = ConstSinglePrecisionRealField;
838 using NonConst = SinglePrecisionRealField;
842 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
847 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
851 } /* namespace internal */
853 template <typename LibObjT>
854 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
857 using typename CommonField<LibObjT>::_LibObjPtr;
858 using typename CommonField<LibObjT>::_ThisCommonField;
861 using Value = double;
863 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
864 _ThisCommonField {libObjPtr}
866 BT_ASSERT_DBG(this->isDoublePrecisionReal());
869 template <typename OtherLibObjT>
870 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
872 _ThisCommonField {val}
876 template <typename OtherLibObjT>
877 CommonDoublePrecisionRealField<LibObjT>&
878 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
880 _ThisCommonField::operator=(val);
884 CommonDoublePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
886 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
888 bt_field_real_double_precision_set_value(this->libObjPtr(), val);
892 Value value() const noexcept
894 return bt_field_real_double_precision_get_value(this->libObjPtr());
897 operator Value() const noexcept
899 return this->value();
903 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
904 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
908 struct DoublePrecisionRealFieldTypeDescr
910 using Const = ConstDoublePrecisionRealField;
911 using NonConst = DoublePrecisionRealField;
915 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
920 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
924 } /* namespace internal */
926 template <typename LibObjT>
927 class CommonStringField final : public CommonField<LibObjT>
930 using typename CommonField<LibObjT>::_LibObjPtr;
931 using typename CommonField<LibObjT>::_ThisCommonField;
934 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
936 BT_ASSERT_DBG(this->isString());
939 template <typename OtherLibObjT>
940 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
944 template <typename OtherLibObjT>
945 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
947 _ThisCommonField::operator=(val);
951 CommonStringField<LibObjT>& operator=(const char * const val)
953 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
955 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
957 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
958 throw MemoryError {};
964 CommonStringField<LibObjT>& operator=(const std::string& val)
966 return *this = val.data();
969 void append(const char * const begin, const std::uint64_t len)
971 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
973 const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len);
975 if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) {
976 throw MemoryError {};
980 void append(const std::string& val)
982 this->append(val.data(), val.size());
985 void clear() noexcept
987 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
989 bt_field_string_clear(this->libObjPtr());
992 bpstd::string_view value() const noexcept
994 return bt_field_string_get_value(this->libObjPtr());
998 using StringField = CommonStringField<bt_field>;
999 using ConstStringField = CommonStringField<const bt_field>;
1001 namespace internal {
1003 struct StringFieldTypeDescr
1005 using Const = ConstStringField;
1006 using NonConst = StringField;
1010 struct TypeDescr<StringField> : public StringFieldTypeDescr
1015 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
1019 template <typename LibObjT>
1020 struct CommonStructureFieldSpec;
1022 /* Functions specific to mutable structure fields */
1024 struct CommonStructureFieldSpec<bt_field> final
1026 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1027 const std::uint64_t index) noexcept
1029 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1032 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1034 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1038 /* Functions specific to constant structure fields */
1040 struct CommonStructureFieldSpec<const bt_field> final
1042 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1043 const std::uint64_t index) noexcept
1045 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1048 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1049 const char * const name) noexcept
1051 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1055 } /* namespace internal */
1057 template <typename LibObjT>
1058 class CommonStructureField final : public CommonField<LibObjT>
1061 using typename CommonField<LibObjT>::_LibObjPtr;
1062 using typename CommonField<LibObjT>::_ThisCommonField;
1063 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1066 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1067 StructureFieldClass>::type;
1069 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1070 _ThisCommonField {libObjPtr}
1072 BT_ASSERT_DBG(this->isStructure());
1075 template <typename OtherLibObjT>
1076 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1077 _ThisCommonField {val}
1081 template <typename OtherLibObjT>
1082 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1084 _ThisCommonField::operator=(val);
1088 ConstStructureFieldClass cls() const noexcept
1090 return ConstStructureFieldClass {
1091 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1094 Class cls() noexcept
1096 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1099 std::uint64_t size() const noexcept
1101 return this->cls().size();
1104 ConstField operator[](const std::uint64_t index) const noexcept
1106 return ConstField {internal::CommonStructureFieldSpec<const bt_field>::memberFieldByIndex(
1107 this->libObjPtr(), index)};
1110 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1112 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1115 nonstd::optional<ConstField> operator[](const char * const name) const noexcept
1117 const auto libObjPtr =
1118 internal::CommonStructureFieldSpec<const bt_field>::memberFieldByName(this->libObjPtr(),
1122 return ConstField {libObjPtr};
1125 return nonstd::nullopt;
1128 nonstd::optional<ConstField> operator[](const std::string& name) const noexcept
1130 return (*this)[name.data()];
1133 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) noexcept
1135 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1138 return CommonField<LibObjT> {libObjPtr};
1141 return nonstd::nullopt;
1144 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) noexcept
1146 return (*this)[name.data()];
1150 using StructureField = CommonStructureField<bt_field>;
1151 using ConstStructureField = CommonStructureField<const bt_field>;
1153 namespace internal {
1155 struct StructureFieldTypeDescr
1157 using Const = ConstStructureField;
1158 using NonConst = StructureField;
1162 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1167 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1171 template <typename LibObjT>
1172 struct CommonArrayFieldSpec;
1174 /* Functions specific to mutable array fields */
1176 struct CommonArrayFieldSpec<bt_field> final
1178 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1179 const std::uint64_t index) noexcept
1181 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1185 /* Functions specific to constant array fields */
1187 struct CommonArrayFieldSpec<const bt_field> final
1189 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1190 const std::uint64_t index) noexcept
1192 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1196 } /* namespace internal */
1198 template <typename LibObjT>
1199 class CommonArrayField : public CommonField<LibObjT>
1202 using typename CommonField<LibObjT>::_ThisCommonField;
1203 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1206 using typename CommonField<LibObjT>::_LibObjPtr;
1207 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1210 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1211 ArrayFieldClass>::type;
1213 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1215 BT_ASSERT_DBG(this->isArray());
1218 template <typename OtherLibObjT>
1219 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1223 template <typename OtherLibObjT>
1224 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1226 _ThisCommonField::operator=(val);
1230 ConstArrayFieldClass cls() const noexcept
1232 return ConstArrayFieldClass {
1233 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1236 Class cls() noexcept
1238 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1241 std::uint64_t length() const noexcept
1243 return bt_field_array_get_length(this->libObjPtr());
1246 ConstField operator[](const std::uint64_t index) const noexcept
1248 return ConstField {internal::CommonArrayFieldSpec<const bt_field>::elementFieldByIndex(
1249 this->libObjPtr(), index)};
1252 CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
1254 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1258 using ArrayField = CommonArrayField<bt_field>;
1259 using ConstArrayField = CommonArrayField<const bt_field>;
1261 namespace internal {
1263 struct ArrayFieldTypeDescr
1265 using Const = ConstArrayField;
1266 using NonConst = ArrayField;
1270 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1275 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1279 } /* namespace internal */
1281 template <typename LibObjT>
1282 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1285 using typename CommonField<LibObjT>::_LibObjPtr;
1286 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1289 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1290 _ThisCommonArrayField {libObjPtr}
1292 BT_ASSERT_DBG(this->isDynamicArray());
1295 template <typename OtherLibObjT>
1296 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1297 _ThisCommonArrayField {val}
1301 template <typename OtherLibObjT>
1302 CommonDynamicArrayField<LibObjT>&
1303 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1305 _ThisCommonArrayField::operator=(val);
1309 std::uint64_t length() const noexcept
1311 return _ThisCommonArrayField::length();
1314 void length(const std::uint64_t length)
1316 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1318 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1320 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1321 throw MemoryError {};
1326 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1327 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1329 namespace internal {
1331 struct DynamicArrayFieldTypeDescr
1333 using Const = ConstDynamicArrayField;
1334 using NonConst = DynamicArrayField;
1338 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1343 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1347 template <typename LibObjT>
1348 struct CommonOptionFieldSpec;
1350 /* Functions specific to mutable option fields */
1352 struct CommonOptionFieldSpec<bt_field> final
1354 static bt_field *field(bt_field * const libObjPtr) noexcept
1356 return bt_field_option_borrow_field(libObjPtr);
1360 /* Functions specific to constant option fields */
1362 struct CommonOptionFieldSpec<const bt_field> final
1364 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1366 return bt_field_option_borrow_field_const(libObjPtr);
1370 } /* namespace internal */
1372 template <typename LibObjT>
1373 class CommonOptionField : public CommonField<LibObjT>
1376 using typename CommonField<LibObjT>::_LibObjPtr;
1377 using typename CommonField<LibObjT>::_ThisCommonField;
1378 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1381 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1382 OptionFieldClass>::type;
1384 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1386 BT_ASSERT_DBG(this->isOption());
1389 template <typename OtherLibObjT>
1390 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1394 template <typename OtherLibObjT>
1395 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1397 _ThisCommonField::operator=(val);
1401 ConstOptionFieldClass cls() const noexcept
1403 return ConstOptionFieldClass {
1404 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1407 Class cls() noexcept
1409 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1412 void hasField(const bool hasField) noexcept
1414 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1416 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1419 bool hasField() const noexcept
1421 return this->field().has_value();
1424 nonstd::optional<ConstField> field() const noexcept
1426 const auto libObjPtr =
1427 internal::CommonOptionFieldSpec<const bt_field>::field(this->libObjPtr());
1430 return ConstField {libObjPtr};
1433 return nonstd::nullopt;
1436 nonstd::optional<CommonField<LibObjT>> field() noexcept
1438 const auto libObjPtr = _Spec::field(this->libObjPtr());
1441 return CommonField<LibObjT> {libObjPtr};
1444 return nonstd::nullopt;
1448 using OptionField = CommonOptionField<bt_field>;
1449 using ConstOptionField = CommonOptionField<const bt_field>;
1451 namespace internal {
1453 struct OptionFieldTypeDescr
1455 using Const = ConstOptionField;
1456 using NonConst = OptionField;
1460 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1465 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1469 template <typename LibObjT>
1470 struct CommonVariantFieldSpec;
1472 /* Functions specific to mutable variant fields */
1474 struct CommonVariantFieldSpec<bt_field> final
1476 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1478 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1482 /* Functions specific to constant variant fields */
1484 struct CommonVariantFieldSpec<const bt_field> final
1486 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1488 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1492 } /* namespace internal */
1494 template <typename LibObjT>
1495 class CommonVariantField : public CommonField<LibObjT>
1498 using typename CommonField<LibObjT>::_LibObjPtr;
1499 using typename CommonField<LibObjT>::_ThisCommonField;
1500 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1503 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1504 VariantFieldClass>::type;
1506 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1508 BT_ASSERT_DBG(this->isVariant());
1511 template <typename OtherLibObjT>
1512 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1516 template <typename OtherLibObjT>
1517 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1519 _ThisCommonField::operator=(val);
1523 ConstVariantFieldClass cls() const noexcept
1525 return ConstVariantFieldClass {
1526 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
1529 Class cls() noexcept
1531 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1534 void selectOption(const std::uint64_t index) noexcept
1536 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1538 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1541 ConstField selectedOptionField() const noexcept
1543 return ConstField {internal::CommonVariantFieldSpec<const bt_field>::selectedOptionField(
1544 this->libObjPtr())};
1547 CommonField<LibObjT> selectedOptionField() noexcept
1549 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1552 std::uint64_t selectedOptionIndex() const noexcept
1554 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1558 using VariantField = CommonVariantField<bt_field>;
1559 using ConstVariantField = CommonVariantField<const bt_field>;
1561 namespace internal {
1563 struct VariantFieldTypeDescr
1565 using Const = ConstVariantField;
1566 using NonConst = VariantField;
1570 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1575 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1579 } /* namespace internal */
1581 template <typename LibObjT>
1582 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1584 BT_ASSERT_DBG(this->isBool());
1585 return CommonBoolField<LibObjT> {this->libObjPtr()};
1588 template <typename LibObjT>
1589 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1591 BT_ASSERT_DBG(this->isBitArray());
1592 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1595 template <typename LibObjT>
1596 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1598 BT_ASSERT_DBG(this->isUnsignedInteger());
1599 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1602 template <typename LibObjT>
1603 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1605 BT_ASSERT_DBG(this->isSignedInteger());
1606 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1609 template <typename LibObjT>
1610 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1612 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1613 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1616 template <typename LibObjT>
1617 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1619 BT_ASSERT_DBG(this->isSignedEnumeration());
1620 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1623 template <typename LibObjT>
1624 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1626 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1627 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1630 template <typename LibObjT>
1631 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1633 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1634 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1637 template <typename LibObjT>
1638 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1640 BT_ASSERT_DBG(this->isString());
1641 return CommonStringField<LibObjT> {this->libObjPtr()};
1644 template <typename LibObjT>
1645 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1647 BT_ASSERT_DBG(this->isStructure());
1648 return CommonStructureField<LibObjT> {this->libObjPtr()};
1651 template <typename LibObjT>
1652 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1654 BT_ASSERT_DBG(this->isArray());
1655 return CommonArrayField<LibObjT> {this->libObjPtr()};
1658 template <typename LibObjT>
1659 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1661 BT_ASSERT_DBG(this->isDynamicArray());
1662 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1665 template <typename LibObjT>
1666 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1668 BT_ASSERT_DBG(this->isOption());
1669 return CommonOptionField<LibObjT> {this->libObjPtr()};
1672 template <typename LibObjT>
1673 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1675 BT_ASSERT_DBG(this->isVariant());
1676 return CommonVariantField<LibObjT> {this->libObjPtr()};
1679 } /* namespace bt2 */
1681 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */