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/bt2c/c-string-view.hpp"
18 #include "borrowed-object.hpp"
19 #include "field-class.hpp"
20 #include "internal/utils.hpp"
21 #include "optional-borrowed-object.hpp"
22 #include "raw-value-proxy.hpp"
26 template <typename LibObjT>
27 class CommonBoolField;
29 template <typename LibObjT>
30 class CommonBitArrayField;
32 template <typename LibObjT>
33 class CommonUnsignedIntegerField;
35 template <typename LibObjT>
36 class CommonSignedIntegerField;
38 template <typename LibObjT>
39 class CommonUnsignedEnumerationField;
41 template <typename LibObjT>
42 class CommonSignedEnumerationField;
44 template <typename LibObjT>
45 class CommonSinglePrecisionRealField;
47 template <typename LibObjT>
48 class CommonDoublePrecisionRealField;
50 template <typename LibObjT>
51 class CommonStringField;
53 template <typename LibObjT>
54 class CommonStructureField;
56 template <typename LibObjT>
57 class CommonArrayField;
59 template <typename LibObjT>
60 class CommonDynamicArrayField;
62 template <typename LibObjT>
63 class CommonOptionField;
65 template <typename LibObjT>
66 class CommonVariantField;
70 template <typename LibObjT>
71 struct CommonFieldSpec;
73 /* Functions specific to mutable fields */
75 struct CommonFieldSpec<bt_field> final
77 static bt_field_class *cls(bt_field * const libObjPtr) noexcept
79 return bt_field_borrow_class(libObjPtr);
83 /* Functions specific to constant fields */
85 struct CommonFieldSpec<const bt_field> final
87 static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept
89 return bt_field_borrow_class_const(libObjPtr);
93 } /* namespace internal */
95 template <typename LibObjT>
96 class CommonField : public BorrowedObject<LibObjT>
99 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
102 using _ThisCommonField = CommonField<LibObjT>;
105 using typename BorrowedObject<LibObjT>::LibObjPtr;
106 using Class = internal::DepFc<LibObjT>;
108 explicit CommonField(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
112 template <typename OtherLibObjT>
113 CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
117 template <typename OtherLibObjT>
118 _ThisCommonField operator=(const CommonField<OtherLibObjT> val) noexcept
120 _ThisBorrowedObject::operator=(val);
124 CommonField<const bt_field> asConst() const noexcept
126 return CommonField<const bt_field> {*this};
129 FieldClassType classType() const noexcept
131 return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
134 Class cls() const noexcept
136 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
139 bool isBool() const noexcept
141 return this->cls().isBool();
144 bool isBitArray() const noexcept
146 return this->cls().isBitArray();
149 bool isUnsignedInteger() const noexcept
151 return this->cls().isUnsignedInteger();
154 bool isSignedInteger() const noexcept
156 return this->cls().isSignedInteger();
159 bool isUnsignedEnumeration() const noexcept
161 return this->cls().isUnsignedEnumeration();
164 bool isSignedEnumeration() const noexcept
166 return this->cls().isSignedEnumeration();
169 bool isSinglePrecisionReal() const noexcept
171 return this->cls().isSinglePrecisionReal();
174 bool isDoublePrecisionReal() const noexcept
176 return this->cls().isDoublePrecisionReal();
179 bool isString() const noexcept
181 return this->cls().isString();
184 bool isStructure() const noexcept
186 return this->cls().isStructure();
189 bool isArray() const noexcept
191 return this->cls().isArray();
194 bool isDynamicArray() const noexcept
196 return this->cls().isDynamicArray();
199 bool isOption() const noexcept
201 return this->cls().isOption();
204 bool isVariant() const noexcept
206 return this->cls().isVariant();
209 template <typename FieldT>
210 FieldT as() const noexcept
212 return FieldT {this->libObjPtr()};
215 CommonBoolField<LibObjT> asBool() const noexcept;
216 CommonBitArrayField<LibObjT> asBitArray() const noexcept;
217 CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept;
218 CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept;
219 CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept;
220 CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept;
221 CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept;
222 CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept;
223 CommonStringField<LibObjT> asString() const noexcept;
224 CommonStructureField<LibObjT> asStructure() const noexcept;
225 CommonArrayField<LibObjT> asArray() const noexcept;
226 CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept;
227 CommonOptionField<LibObjT> asOption() const noexcept;
228 CommonVariantField<LibObjT> asVariant() const noexcept;
231 using Field = CommonField<bt_field>;
232 using ConstField = CommonField<const bt_field>;
236 struct FieldTypeDescr
238 using Const = ConstField;
239 using NonConst = Field;
243 struct TypeDescr<Field> : public FieldTypeDescr
248 struct TypeDescr<ConstField> : public FieldTypeDescr
252 } /* namespace internal */
254 template <typename LibObjT>
255 class CommonBoolField final : public CommonField<LibObjT>
258 using typename CommonField<LibObjT>::_ThisCommonField;
261 using typename CommonField<LibObjT>::LibObjPtr;
264 explicit CommonBoolField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
266 BT_ASSERT_DBG(this->isBool());
269 template <typename OtherLibObjT>
270 CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
274 template <typename OtherLibObjT>
275 CommonBoolField<LibObjT> operator=(const CommonBoolField<OtherLibObjT> val) noexcept
277 _ThisCommonField::operator=(val);
281 CommonBoolField<const bt_field> asConst() const noexcept
283 return CommonBoolField<const bt_field> {*this};
286 RawValueProxy<CommonBoolField> operator*() const noexcept
288 return RawValueProxy<CommonBoolField> {*this};
291 void value(const Value val) const noexcept
293 static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolField`.");
295 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
298 Value value() const noexcept
300 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
304 using BoolField = CommonBoolField<bt_field>;
305 using ConstBoolField = CommonBoolField<const bt_field>;
309 struct BoolFieldTypeDescr
311 using Const = ConstBoolField;
312 using NonConst = BoolField;
316 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
321 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
325 } /* namespace internal */
327 template <typename LibObjT>
328 class CommonBitArrayField final : public CommonField<LibObjT>
331 using typename CommonField<LibObjT>::_ThisCommonField;
334 using typename CommonField<LibObjT>::LibObjPtr;
335 using Class = internal::DepType<LibObjT, BitArrayFieldClass, ConstBitArrayFieldClass>;
337 explicit CommonBitArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
339 BT_ASSERT_DBG(this->isBitArray());
342 template <typename OtherLibObjT>
343 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
344 _ThisCommonField {val}
348 template <typename OtherLibObjT>
349 CommonBitArrayField<LibObjT> operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
351 _ThisCommonField::operator=(val);
355 CommonBitArrayField<const bt_field> asConst() const noexcept
357 return CommonBitArrayField<const bt_field> {*this};
360 ConstBitArrayFieldClass cls() const noexcept
362 return ConstBitArrayFieldClass {
363 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
368 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
371 void valueAsInteger(const std::uint64_t bits) const noexcept
373 static_assert(!std::is_const<LibObjT>::value,
374 "Not available with `bt2::ConstBitArrayField`.");
376 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
379 std::uint64_t valueAsInteger() const noexcept
381 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
384 bool bitValue(const std::uint64_t index) const noexcept
386 BT_ASSERT_DBG(index < this->cls().length());
387 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
391 using BitArrayField = CommonBitArrayField<bt_field>;
392 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
396 struct BitArrayFieldTypeDescr
398 using Const = ConstBitArrayField;
399 using NonConst = BitArrayField;
403 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
408 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
412 } /* namespace internal */
414 template <typename LibObjT>
415 class CommonUnsignedIntegerField : public CommonField<LibObjT>
418 using typename CommonField<LibObjT>::_ThisCommonField;
421 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
424 using typename CommonField<LibObjT>::LibObjPtr;
425 using Value = std::uint64_t;
426 using Class = internal::DepType<LibObjT, IntegerFieldClass, ConstIntegerFieldClass>;
428 explicit CommonUnsignedIntegerField(const LibObjPtr libObjPtr) noexcept :
429 _ThisCommonField {libObjPtr}
431 BT_ASSERT_DBG(this->isUnsignedInteger());
434 template <typename OtherLibObjT>
435 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
436 _ThisCommonField {val}
440 template <typename OtherLibObjT>
441 _ThisCommonUnsignedIntegerField
442 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
444 _ThisCommonField::operator=(val);
448 CommonUnsignedIntegerField<const bt_field> asConst() const noexcept
450 return CommonUnsignedIntegerField<const bt_field> {*this};
453 Class cls() const noexcept
455 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
458 RawValueProxy<CommonUnsignedIntegerField> operator*() const noexcept
460 return RawValueProxy<CommonUnsignedIntegerField> {*this};
463 void value(const Value val) const noexcept
465 static_assert(!std::is_const<LibObjT>::value,
466 "Not available with `bt2::ConstUnsignedIntegerField`.");
468 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
471 Value value() const noexcept
473 return bt_field_integer_unsigned_get_value(this->libObjPtr());
477 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
478 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
482 struct UnsignedIntegerFieldTypeDescr
484 using Const = ConstUnsignedIntegerField;
485 using NonConst = UnsignedIntegerField;
489 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
494 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
498 } /* namespace internal */
500 template <typename LibObjT>
501 class CommonSignedIntegerField : public CommonField<LibObjT>
504 using typename CommonField<LibObjT>::_ThisCommonField;
507 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
510 using typename CommonField<LibObjT>::LibObjPtr;
511 using Value = std::int64_t;
512 using Class = internal::DepType<LibObjT, IntegerFieldClass, ConstIntegerFieldClass>;
514 explicit CommonSignedIntegerField(const LibObjPtr libObjPtr) noexcept :
515 _ThisCommonField {libObjPtr}
517 BT_ASSERT_DBG(this->isSignedInteger());
520 template <typename OtherLibObjT>
521 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
522 _ThisCommonField {val}
526 template <typename OtherLibObjT>
527 _ThisCommonSignedIntegerField
528 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
530 _ThisCommonField::operator=(val);
534 CommonSignedIntegerField<const bt_field> asConst() const noexcept
536 return CommonSignedIntegerField<const bt_field> {*this};
539 Class cls() const noexcept
541 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
544 RawValueProxy<CommonSignedIntegerField> operator*() const noexcept
546 return RawValueProxy<CommonSignedIntegerField> {*this};
549 void value(const Value val) const noexcept
551 static_assert(!std::is_const<LibObjT>::value,
552 "Not available with `bt2::ConstSignedIntegerField`.");
554 bt_field_integer_signed_set_value(this->libObjPtr(), val);
557 Value value() const noexcept
559 return bt_field_integer_signed_get_value(this->libObjPtr());
563 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
564 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
568 struct SignedIntegerFieldTypeDescr
570 using Const = ConstSignedIntegerField;
571 using NonConst = SignedIntegerField;
575 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
580 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
584 } /* namespace internal */
586 class EnumerationFieldClassMappingLabels
589 explicit EnumerationFieldClassMappingLabels(
590 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
596 std::uint64_t length() const noexcept
601 bt2c::CStringView operator[](const std::uint64_t index) const noexcept
603 return _mLabels[index];
607 bt_field_class_enumeration_mapping_label_array _mLabels;
611 template <typename LibObjT>
612 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
615 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
618 using typename CommonField<LibObjT>::LibObjPtr;
620 using Class = internal::DepType<LibObjT, UnsignedEnumerationFieldClass,
621 ConstUnsignedEnumerationFieldClass>;
623 explicit CommonUnsignedEnumerationField(const LibObjPtr libObjPtr) noexcept :
624 _ThisCommonUnsignedIntegerField {libObjPtr}
626 BT_ASSERT_DBG(this->isUnsignedEnumeration());
629 template <typename OtherLibObjT>
630 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
632 _ThisCommonUnsignedIntegerField {val}
636 template <typename OtherLibObjT>
637 CommonUnsignedEnumerationField<LibObjT>
638 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
640 _ThisCommonUnsignedIntegerField::operator=(val);
644 CommonUnsignedEnumerationField<const bt_field> asConst() const noexcept
646 return CommonUnsignedEnumerationField<const bt_field> {*this};
649 Class cls() const noexcept
651 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
654 EnumerationFieldClassMappingLabels labels() const
656 bt_field_class_enumeration_mapping_label_array labelArray;
658 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
659 &labelArray, &count);
661 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
662 throw MemoryError {};
665 return EnumerationFieldClassMappingLabels {labelArray, count};
669 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
670 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
674 struct UnsignedEnumerationFieldTypeDescr
676 using Const = ConstUnsignedEnumerationField;
677 using NonConst = UnsignedEnumerationField;
681 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
686 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
690 } /* namespace internal */
692 template <typename LibObjT>
693 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
696 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
699 using typename CommonField<LibObjT>::LibObjPtr;
702 internal::DepType<LibObjT, SignedEnumerationFieldClass, ConstSignedEnumerationFieldClass>;
704 explicit CommonSignedEnumerationField(const LibObjPtr libObjPtr) noexcept :
705 _ThisCommonSignedIntegerField {libObjPtr}
707 BT_ASSERT_DBG(this->isSignedEnumeration());
710 template <typename OtherLibObjT>
711 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
712 _ThisCommonSignedIntegerField {val}
716 template <typename OtherLibObjT>
717 CommonSignedEnumerationField<LibObjT>
718 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
720 _ThisCommonSignedIntegerField::operator=(val);
724 CommonSignedEnumerationField<const bt_field> asConst() const noexcept
726 return CommonSignedEnumerationField<const bt_field> {*this};
729 Class cls() const noexcept
731 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
734 EnumerationFieldClassMappingLabels labels() const
736 bt_field_class_enumeration_mapping_label_array labelArray;
739 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
741 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
742 throw MemoryError {};
745 return EnumerationFieldClassMappingLabels {labelArray, count};
749 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
750 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
754 struct SignedEnumerationFieldTypeDescr
756 using Const = ConstSignedEnumerationField;
757 using NonConst = SignedEnumerationField;
761 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
766 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
770 } /* namespace internal */
772 template <typename LibObjT>
773 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
776 using typename CommonField<LibObjT>::_ThisCommonField;
779 using typename CommonField<LibObjT>::LibObjPtr;
782 explicit CommonSinglePrecisionRealField(const LibObjPtr libObjPtr) noexcept :
783 _ThisCommonField {libObjPtr}
785 BT_ASSERT_DBG(this->isSinglePrecisionReal());
788 template <typename OtherLibObjT>
789 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
791 _ThisCommonField {val}
795 template <typename OtherLibObjT>
796 CommonSinglePrecisionRealField<LibObjT>
797 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
799 _ThisCommonField::operator=(val);
803 CommonSinglePrecisionRealField<const bt_field> asConst() const noexcept
805 return CommonSinglePrecisionRealField<const bt_field> {*this};
808 RawValueProxy<CommonSinglePrecisionRealField> operator*() const noexcept
810 return RawValueProxy<CommonSinglePrecisionRealField> {*this};
813 void value(const Value val) const noexcept
815 static_assert(!std::is_const<LibObjT>::value,
816 "Not available with `bt2::ConstSinglePrecisionRealField`.");
818 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());
827 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
828 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
832 struct SinglePrecisionRealFieldTypeDescr
834 using Const = ConstSinglePrecisionRealField;
835 using NonConst = SinglePrecisionRealField;
839 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
844 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
848 } /* namespace internal */
850 template <typename LibObjT>
851 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
854 using typename CommonField<LibObjT>::_ThisCommonField;
857 using typename CommonField<LibObjT>::LibObjPtr;
858 using Value = double;
860 explicit CommonDoublePrecisionRealField(const LibObjPtr libObjPtr) noexcept :
861 _ThisCommonField {libObjPtr}
863 BT_ASSERT_DBG(this->isDoublePrecisionReal());
866 template <typename OtherLibObjT>
867 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
869 _ThisCommonField {val}
873 template <typename OtherLibObjT>
874 CommonDoublePrecisionRealField<LibObjT>
875 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
877 _ThisCommonField::operator=(val);
881 CommonDoublePrecisionRealField<const bt_field> asConst() const noexcept
883 return CommonDoublePrecisionRealField<const bt_field> {*this};
886 RawValueProxy<CommonDoublePrecisionRealField> operator*() const noexcept
888 return RawValueProxy<CommonDoublePrecisionRealField> {*this};
891 void value(const Value val) const noexcept
893 static_assert(!std::is_const<LibObjT>::value,
894 "Not available with `bt2::ConstDoublePrecisionRealField`.");
896 bt_field_real_double_precision_set_value(this->libObjPtr(), val);
899 Value value() const noexcept
901 return bt_field_real_double_precision_get_value(this->libObjPtr());
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>::_ThisCommonField;
935 using typename CommonField<LibObjT>::LibObjPtr;
936 using Value = bt2c::CStringView;
938 explicit CommonStringField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
940 BT_ASSERT_DBG(this->isString());
943 template <typename OtherLibObjT>
944 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
948 template <typename OtherLibObjT>
949 CommonStringField<LibObjT> operator=(const CommonStringField<OtherLibObjT> val) noexcept
951 _ThisCommonField::operator=(val);
955 CommonStringField<const bt_field> asConst() const noexcept
957 return CommonStringField<const bt_field> {*this};
960 RawValueProxy<CommonStringField> operator*() const noexcept
962 return RawValueProxy<CommonStringField> {*this};
965 void value(const Value val) const
967 static_assert(!std::is_const<LibObjT>::value,
968 "Not available with `bt2::ConstStringField`.");
970 const auto status = bt_field_string_set_value(this->libObjPtr(), *val);
972 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
973 throw MemoryError {};
977 void append(const bt2c::CStringView begin, const std::uint64_t len) const
979 static_assert(!std::is_const<LibObjT>::value,
980 "Not available with `bt2::ConstStringField`.");
982 const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len);
984 if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) {
985 throw MemoryError {};
989 void append(const bt2c::CStringView val) const
991 this->append(val, std::strlen(val));
994 void append(const std::string& val) const
996 this->append(val.data(), val.size());
999 void clear() const noexcept
1001 static_assert(!std::is_const<LibObjT>::value,
1002 "Not available with `bt2::ConstStringField`.");
1004 bt_field_string_clear(this->libObjPtr());
1007 Value value() const noexcept
1009 return bt_field_string_get_value(this->libObjPtr());
1012 std::uint64_t length() const noexcept
1014 return bt_field_string_get_length(this->libObjPtr());
1018 using StringField = CommonStringField<bt_field>;
1019 using ConstStringField = CommonStringField<const bt_field>;
1021 namespace internal {
1023 struct StringFieldTypeDescr
1025 using Const = ConstStringField;
1026 using NonConst = StringField;
1030 struct TypeDescr<StringField> : public StringFieldTypeDescr
1035 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
1039 template <typename LibObjT>
1040 struct CommonStructureFieldSpec;
1042 /* Functions specific to mutable structure fields */
1044 struct CommonStructureFieldSpec<bt_field> final
1046 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1047 const std::uint64_t index) noexcept
1049 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1052 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1054 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1058 /* Functions specific to constant structure fields */
1060 struct CommonStructureFieldSpec<const bt_field> final
1062 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1063 const std::uint64_t index) noexcept
1065 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1068 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1069 const char * const name) noexcept
1071 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1075 } /* namespace internal */
1077 template <typename LibObjT>
1078 class CommonStructureField final : public CommonField<LibObjT>
1081 using typename CommonField<LibObjT>::_ThisCommonField;
1082 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1085 using typename CommonField<LibObjT>::LibObjPtr;
1086 using Class = internal::DepType<LibObjT, StructureFieldClass, ConstStructureFieldClass>;
1088 explicit CommonStructureField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1090 BT_ASSERT_DBG(this->isStructure());
1093 template <typename OtherLibObjT>
1094 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1095 _ThisCommonField {val}
1099 template <typename OtherLibObjT>
1100 CommonStructureField<LibObjT> operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1102 _ThisCommonField::operator=(val);
1106 CommonStructureField<const bt_field> asConst() const noexcept
1108 return CommonStructureField<const bt_field> {*this};
1111 Class cls() const noexcept
1113 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1116 std::uint64_t length() const noexcept
1118 return this->cls().length();
1121 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1123 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1126 OptionalBorrowedObject<CommonField<LibObjT>>
1127 operator[](const bt2c::CStringView name) const noexcept
1129 return _Spec::memberFieldByName(this->libObjPtr(), name);
1133 using StructureField = CommonStructureField<bt_field>;
1134 using ConstStructureField = CommonStructureField<const bt_field>;
1136 namespace internal {
1138 struct StructureFieldTypeDescr
1140 using Const = ConstStructureField;
1141 using NonConst = StructureField;
1145 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1150 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1154 template <typename LibObjT>
1155 struct CommonArrayFieldSpec;
1157 /* Functions specific to mutable array fields */
1159 struct CommonArrayFieldSpec<bt_field> final
1161 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1162 const std::uint64_t index) noexcept
1164 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1168 /* Functions specific to constant array fields */
1170 struct CommonArrayFieldSpec<const bt_field> final
1172 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1173 const std::uint64_t index) noexcept
1175 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1179 } /* namespace internal */
1181 template <typename LibObjT>
1182 class CommonArrayField : public CommonField<LibObjT>
1185 using typename CommonField<LibObjT>::_ThisCommonField;
1186 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1189 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1192 using typename CommonField<LibObjT>::LibObjPtr;
1193 using Class = internal::DepType<LibObjT, ArrayFieldClass, ConstArrayFieldClass>;
1195 explicit CommonArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1197 BT_ASSERT_DBG(this->isArray());
1200 template <typename OtherLibObjT>
1201 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1205 template <typename OtherLibObjT>
1206 _ThisCommonArrayField operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1208 _ThisCommonField::operator=(val);
1212 CommonArrayField<const bt_field> asConst() const noexcept
1214 return CommonArrayField<const bt_field> {*this};
1217 Class cls() const noexcept
1219 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1222 std::uint64_t length() const noexcept
1224 return bt_field_array_get_length(this->libObjPtr());
1227 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1229 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1233 using ArrayField = CommonArrayField<bt_field>;
1234 using ConstArrayField = CommonArrayField<const bt_field>;
1236 namespace internal {
1238 struct ArrayFieldTypeDescr
1240 using Const = ConstArrayField;
1241 using NonConst = ArrayField;
1245 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1250 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1254 } /* namespace internal */
1256 template <typename LibObjT>
1257 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1260 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1263 using typename CommonField<LibObjT>::LibObjPtr;
1265 explicit CommonDynamicArrayField(const LibObjPtr libObjPtr) noexcept :
1266 _ThisCommonArrayField {libObjPtr}
1268 BT_ASSERT_DBG(this->isDynamicArray());
1271 template <typename OtherLibObjT>
1272 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1273 _ThisCommonArrayField {val}
1277 template <typename OtherLibObjT>
1278 CommonDynamicArrayField<LibObjT>
1279 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1281 _ThisCommonArrayField::operator=(val);
1285 CommonDynamicArrayField<const bt_field> asConst() const noexcept
1287 return CommonDynamicArrayField<const bt_field> {*this};
1290 std::uint64_t length() const noexcept
1292 return _ThisCommonArrayField::length();
1295 void length(const std::uint64_t length) const
1297 static_assert(!std::is_const<LibObjT>::value,
1298 "Not available with `bt2::ConstDynamicArrayField`.");
1300 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1302 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1303 throw MemoryError {};
1308 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1309 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1311 namespace internal {
1313 struct DynamicArrayFieldTypeDescr
1315 using Const = ConstDynamicArrayField;
1316 using NonConst = DynamicArrayField;
1320 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1325 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1329 template <typename LibObjT>
1330 struct CommonOptionFieldSpec;
1332 /* Functions specific to mutable option fields */
1334 struct CommonOptionFieldSpec<bt_field> final
1336 static bt_field *field(bt_field * const libObjPtr) noexcept
1338 return bt_field_option_borrow_field(libObjPtr);
1342 /* Functions specific to constant option fields */
1344 struct CommonOptionFieldSpec<const bt_field> final
1346 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1348 return bt_field_option_borrow_field_const(libObjPtr);
1352 } /* namespace internal */
1354 template <typename LibObjT>
1355 class CommonOptionField : public CommonField<LibObjT>
1358 using typename CommonField<LibObjT>::_ThisCommonField;
1359 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1362 using typename CommonField<LibObjT>::LibObjPtr;
1363 using Class = internal::DepType<LibObjT, OptionFieldClass, ConstOptionFieldClass>;
1365 explicit CommonOptionField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1367 BT_ASSERT_DBG(this->isOption());
1370 template <typename OtherLibObjT>
1371 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1375 template <typename OtherLibObjT>
1376 CommonOptionField<LibObjT> operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1378 _ThisCommonField::operator=(val);
1382 CommonOptionField<const bt_field> asConst() const noexcept
1384 return CommonOptionField<const bt_field> {*this};
1387 Class cls() const noexcept
1389 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1392 void hasField(const bool hasField) const noexcept
1394 static_assert(!std::is_const<LibObjT>::value,
1395 "Not available with `bt2::ConstOptionField`.");
1397 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1400 bool hasField() const noexcept
1402 return this->field();
1405 OptionalBorrowedObject<CommonField<LibObjT>> field() const noexcept
1407 return _Spec::field(this->libObjPtr());
1411 using OptionField = CommonOptionField<bt_field>;
1412 using ConstOptionField = CommonOptionField<const bt_field>;
1414 namespace internal {
1416 struct OptionFieldTypeDescr
1418 using Const = ConstOptionField;
1419 using NonConst = OptionField;
1423 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1428 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1432 template <typename LibObjT>
1433 struct CommonVariantFieldSpec;
1435 /* Functions specific to mutable variant fields */
1437 struct CommonVariantFieldSpec<bt_field> final
1439 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1441 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1445 /* Functions specific to constant variant fields */
1447 struct CommonVariantFieldSpec<const bt_field> final
1449 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1451 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1455 } /* namespace internal */
1457 template <typename LibObjT>
1458 class CommonVariantField : public CommonField<LibObjT>
1461 using typename CommonField<LibObjT>::_ThisCommonField;
1462 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1465 using typename CommonField<LibObjT>::LibObjPtr;
1466 using Class = internal::DepType<LibObjT, VariantFieldClass, ConstVariantFieldClass>;
1468 explicit CommonVariantField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1470 BT_ASSERT_DBG(this->isVariant());
1473 template <typename OtherLibObjT>
1474 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1478 template <typename OtherLibObjT>
1479 CommonVariantField<LibObjT> operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1481 _ThisCommonField::operator=(val);
1485 CommonVariantField<const bt_field> asConst() const noexcept
1487 return CommonVariantField<const bt_field> {*this};
1490 Class cls() const noexcept
1492 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1495 void selectOption(const std::uint64_t index) const noexcept
1497 static_assert(!std::is_const<LibObjT>::value,
1498 "Not available with `bt2::ConstVariantField`.");
1500 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1503 CommonField<LibObjT> selectedOptionField() const noexcept
1505 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1508 std::uint64_t selectedOptionIndex() const noexcept
1510 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1514 using VariantField = CommonVariantField<bt_field>;
1515 using ConstVariantField = CommonVariantField<const bt_field>;
1517 namespace internal {
1519 struct VariantFieldTypeDescr
1521 using Const = ConstVariantField;
1522 using NonConst = VariantField;
1526 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1531 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1535 } /* namespace internal */
1537 template <typename LibObjT>
1538 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1540 return CommonBoolField<LibObjT> {this->libObjPtr()};
1543 template <typename LibObjT>
1544 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1546 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1549 template <typename LibObjT>
1550 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1552 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1555 template <typename LibObjT>
1556 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1558 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1561 template <typename LibObjT>
1562 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1564 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1567 template <typename LibObjT>
1568 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1570 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1573 template <typename LibObjT>
1574 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1576 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1579 template <typename LibObjT>
1580 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1582 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1585 template <typename LibObjT>
1586 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1588 return CommonStringField<LibObjT> {this->libObjPtr()};
1591 template <typename LibObjT>
1592 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1594 return CommonStructureField<LibObjT> {this->libObjPtr()};
1597 template <typename LibObjT>
1598 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1600 return CommonArrayField<LibObjT> {this->libObjPtr()};
1603 template <typename LibObjT>
1604 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1606 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1609 template <typename LibObjT>
1610 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1612 return CommonOptionField<LibObjT> {this->libObjPtr()};
1615 template <typename LibObjT>
1616 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1618 return CommonVariantField<LibObjT> {this->libObjPtr()};
1621 } /* namespace bt2 */
1623 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */