2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
11 #include <type_traits>
13 #include <babeltrace2/babeltrace.h>
15 #include "common/assert.h"
16 #include "cpp-common/optional.hpp"
17 #include "cpp-common/string_view.hpp"
19 #include "borrowed-object.hpp"
20 #include "field-class.hpp"
21 #include "internal/utils.hpp"
25 template <typename LibObjT>
26 class CommonBoolField;
28 template <typename LibObjT>
29 class CommonBitArrayField;
31 template <typename LibObjT>
32 class CommonUnsignedIntegerField;
34 template <typename LibObjT>
35 class CommonSignedIntegerField;
37 template <typename LibObjT>
38 class CommonUnsignedEnumerationField;
40 template <typename LibObjT>
41 class CommonSignedEnumerationField;
43 template <typename LibObjT>
44 class CommonSinglePrecisionRealField;
46 template <typename LibObjT>
47 class CommonDoublePrecisionRealField;
49 template <typename LibObjT>
50 class CommonStringField;
52 template <typename LibObjT>
53 class CommonStructureField;
55 template <typename LibObjT>
56 class CommonArrayField;
58 template <typename LibObjT>
59 class CommonDynamicArrayField;
61 template <typename LibObjT>
62 class CommonOptionField;
64 template <typename LibObjT>
65 class CommonVariantField;
69 template <typename LibObjT>
70 struct CommonFieldSpec;
72 /* Functions specific to mutable fields */
74 struct CommonFieldSpec<bt_field> final
76 static bt_field_class *cls(bt_field * const libObjPtr) noexcept
78 return bt_field_borrow_class(libObjPtr);
82 /* Functions specific to constant fields */
84 struct CommonFieldSpec<const bt_field> final
86 static const bt_field_class *cls(const bt_field * const libObjPtr) noexcept
88 return bt_field_borrow_class_const(libObjPtr);
92 } /* namespace internal */
94 template <typename LibObjT>
95 class CommonField : public BorrowedObject<LibObjT>
98 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
101 using typename BorrowedObject<LibObjT>::_LibObjPtr;
102 using _ThisCommonField = CommonField<LibObjT>;
106 typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type;
108 explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
112 template <typename OtherLibObjT>
113 CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
117 template <typename OtherLibObjT>
118 _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept
120 _ThisBorrowedObject::operator=(val);
124 CommonField<const bt_field> asConst() const noexcept
126 return CommonField<const bt_field> {*this};
129 FieldClassType classType() const noexcept
131 return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
134 Class cls() const noexcept
136 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
139 bool isBool() const noexcept
141 return this->cls().isBool();
144 bool isBitArray() const noexcept
146 return this->cls().isBitArray();
149 bool isUnsignedInteger() const noexcept
151 return this->cls().isUnsignedInteger();
154 bool isSignedInteger() const noexcept
156 return this->cls().isSignedInteger();
159 bool isUnsignedEnumeration() const noexcept
161 return this->cls().isUnsignedEnumeration();
164 bool isSignedEnumeration() const noexcept
166 return this->cls().isSignedEnumeration();
169 bool isSinglePrecisionReal() const noexcept
171 return this->cls().isSinglePrecisionReal();
174 bool isDoublePrecisionReal() const noexcept
176 return this->cls().isDoublePrecisionReal();
179 bool isString() const noexcept
181 return this->cls().isString();
184 bool isStructure() const noexcept
186 return this->cls().isStructure();
189 bool isArray() const noexcept
191 return this->cls().isArray();
194 bool isDynamicArray() const noexcept
196 return this->cls().isDynamicArray();
199 bool isOption() const noexcept
201 return this->cls().isOption();
204 bool isVariant() const noexcept
206 return this->cls().isVariant();
209 template <typename FieldT>
210 FieldT as() const noexcept
212 return FieldT {this->libObjPtr()};
215 CommonBoolField<LibObjT> asBool() const noexcept;
216 CommonBitArrayField<LibObjT> asBitArray() const noexcept;
217 CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept;
218 CommonSignedIntegerField<LibObjT> asSignedInteger() const noexcept;
219 CommonUnsignedEnumerationField<LibObjT> asUnsignedEnumeration() const noexcept;
220 CommonSignedEnumerationField<LibObjT> asSignedEnumeration() const noexcept;
221 CommonSinglePrecisionRealField<LibObjT> asSinglePrecisionReal() const noexcept;
222 CommonDoublePrecisionRealField<LibObjT> asDoublePrecisionReal() const noexcept;
223 CommonStringField<LibObjT> asString() const noexcept;
224 CommonStructureField<LibObjT> asStructure() const noexcept;
225 CommonArrayField<LibObjT> asArray() const noexcept;
226 CommonDynamicArrayField<LibObjT> asDynamicArray() const noexcept;
227 CommonOptionField<LibObjT> asOption() const noexcept;
228 CommonVariantField<LibObjT> asVariant() const noexcept;
231 using Field = CommonField<bt_field>;
232 using ConstField = CommonField<const bt_field>;
236 struct FieldTypeDescr
238 using Const = ConstField;
239 using NonConst = Field;
243 struct TypeDescr<Field> : public FieldTypeDescr
248 struct TypeDescr<ConstField> : public FieldTypeDescr
252 } /* namespace internal */
254 template <typename LibObjT>
255 class CommonBoolField final : public CommonField<LibObjT>
258 using typename CommonField<LibObjT>::_LibObjPtr;
259 using typename CommonField<LibObjT>::_ThisCommonField;
264 explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
266 BT_ASSERT_DBG(this->isBool());
269 template <typename OtherLibObjT>
270 CommonBoolField(const CommonBoolField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
274 template <typename OtherLibObjT>
275 CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
277 _ThisCommonField::operator=(val);
281 CommonBoolField<const bt_field> asConst() const noexcept
283 return CommonBoolField<const bt_field> {*this};
286 CommonBoolField<LibObjT> operator=(const Value val) const noexcept
288 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
290 bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
294 Value value() const noexcept
296 return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
299 operator Value() const noexcept
301 return this->value();
305 using BoolField = CommonBoolField<bt_field>;
306 using ConstBoolField = CommonBoolField<const bt_field>;
310 struct BoolFieldTypeDescr
312 using Const = ConstBoolField;
313 using NonConst = BoolField;
317 struct TypeDescr<BoolField> : public BoolFieldTypeDescr
322 struct TypeDescr<ConstBoolField> : public BoolFieldTypeDescr
326 } /* namespace internal */
328 template <typename LibObjT>
329 class CommonBitArrayField final : public CommonField<LibObjT>
332 using typename CommonField<LibObjT>::_LibObjPtr;
333 using typename CommonField<LibObjT>::_ThisCommonField;
336 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
337 BitArrayFieldClass>::type;
339 explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
341 BT_ASSERT_DBG(this->isBitArray());
344 template <typename OtherLibObjT>
345 CommonBitArrayField(const CommonBitArrayField<OtherLibObjT> val) noexcept :
346 _ThisCommonField {val}
350 template <typename OtherLibObjT>
351 CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
353 _ThisCommonField::operator=(val);
357 CommonBitArrayField<const bt_field> asConst() const noexcept
359 return CommonBitArrayField<const bt_field> {*this};
362 ConstBitArrayFieldClass cls() const noexcept
364 return ConstBitArrayFieldClass {
365 internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
370 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
373 CommonBitArrayField<LibObjT> operator=(const std::uint64_t bits) const noexcept
375 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
377 bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
381 std::uint64_t valueAsInteger() const noexcept
383 return bt_field_bit_array_get_value_as_integer(this->libObjPtr());
386 bool bitValue(const std::uint64_t index) const noexcept
388 BT_ASSERT_DBG(index < this->cls().length());
389 return static_cast<bool>(this->valueAsInteger() & (1ULL << index));
393 using BitArrayField = CommonBitArrayField<bt_field>;
394 using ConstBitArrayField = CommonBitArrayField<const bt_field>;
398 struct BitArrayFieldTypeDescr
400 using Const = ConstBitArrayField;
401 using NonConst = BitArrayField;
405 struct TypeDescr<BitArrayField> : public BitArrayFieldTypeDescr
410 struct TypeDescr<ConstBitArrayField> : public BitArrayFieldTypeDescr
414 } /* namespace internal */
416 template <typename LibObjT>
417 class CommonUnsignedIntegerField : public CommonField<LibObjT>
420 using typename CommonField<LibObjT>::_ThisCommonField;
423 using typename CommonField<LibObjT>::_LibObjPtr;
424 using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
427 using Value = std::uint64_t;
429 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
430 IntegerFieldClass>::type;
432 explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
433 _ThisCommonField {libObjPtr}
435 BT_ASSERT_DBG(this->isUnsignedInteger());
438 template <typename OtherLibObjT>
439 CommonUnsignedIntegerField(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept :
440 _ThisCommonField {val}
444 template <typename OtherLibObjT>
445 _ThisCommonUnsignedIntegerField&
446 operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
448 _ThisCommonField::operator=(val);
452 CommonUnsignedIntegerField<const bt_field> asConst() const noexcept
454 return CommonUnsignedIntegerField<const bt_field> {*this};
457 Class cls() const noexcept
459 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
462 CommonUnsignedIntegerField<LibObjT> operator=(const Value val) const noexcept
464 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
466 bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
470 Value value() const noexcept
472 return bt_field_integer_unsigned_get_value(this->libObjPtr());
475 operator Value() const noexcept
477 return this->value();
481 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
482 using ConstUnsignedIntegerField = CommonUnsignedIntegerField<const bt_field>;
486 struct UnsignedIntegerFieldTypeDescr
488 using Const = ConstUnsignedIntegerField;
489 using NonConst = UnsignedIntegerField;
493 struct TypeDescr<UnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
498 struct TypeDescr<ConstUnsignedIntegerField> : public UnsignedIntegerFieldTypeDescr
502 } /* namespace internal */
504 template <typename LibObjT>
505 class CommonSignedIntegerField : public CommonField<LibObjT>
508 using typename CommonField<LibObjT>::_ThisCommonField;
511 using typename CommonField<LibObjT>::_LibObjPtr;
512 using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
515 using Value = std::int64_t;
517 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
518 IntegerFieldClass>::type;
520 explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
521 _ThisCommonField {libObjPtr}
523 BT_ASSERT_DBG(this->isSignedInteger());
526 template <typename OtherLibObjT>
527 CommonSignedIntegerField(const CommonSignedIntegerField<OtherLibObjT> val) noexcept :
528 _ThisCommonField {val}
532 template <typename OtherLibObjT>
533 _ThisCommonSignedIntegerField&
534 operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
536 _ThisCommonField::operator=(val);
540 CommonSignedIntegerField<const bt_field> asConst() const noexcept
542 return CommonSignedIntegerField<const bt_field> {*this};
545 Class cls() const noexcept
547 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
550 CommonSignedIntegerField<LibObjT> operator=(const Value val) const noexcept
552 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
554 bt_field_integer_signed_set_value(this->libObjPtr(), val);
558 Value value() const noexcept
560 return bt_field_integer_signed_get_value(this->libObjPtr());
563 operator Value() const noexcept
565 return this->value();
569 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
570 using ConstSignedIntegerField = CommonSignedIntegerField<const bt_field>;
574 struct SignedIntegerFieldTypeDescr
576 using Const = ConstSignedIntegerField;
577 using NonConst = SignedIntegerField;
581 struct TypeDescr<SignedIntegerField> : public SignedIntegerFieldTypeDescr
586 struct TypeDescr<ConstSignedIntegerField> : public SignedIntegerFieldTypeDescr
590 } /* namespace internal */
592 class EnumerationFieldClassMappingLabels
595 explicit EnumerationFieldClassMappingLabels(
596 const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) :
602 EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
605 EnumerationFieldClassMappingLabels&
606 operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
608 std::uint64_t length() const noexcept
613 bpstd::string_view operator[](const std::uint64_t index) const noexcept
615 return _mLabels[index];
619 bt_field_class_enumeration_mapping_label_array _mLabels;
623 template <typename LibObjT>
624 class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<LibObjT>
627 using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
628 using typename CommonField<LibObjT>::_LibObjPtr;
632 typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
633 UnsignedEnumerationFieldClass>::type;
635 explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
636 _ThisCommonUnsignedIntegerField {libObjPtr}
638 BT_ASSERT_DBG(this->isUnsignedEnumeration());
641 template <typename OtherLibObjT>
642 CommonUnsignedEnumerationField(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
644 _ThisCommonUnsignedIntegerField {val}
648 template <typename OtherLibObjT>
649 CommonUnsignedEnumerationField<LibObjT>&
650 operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
652 _ThisCommonUnsignedIntegerField::operator=(val);
656 CommonUnsignedEnumerationField<const bt_field> asConst() const noexcept
658 return CommonUnsignedEnumerationField<const bt_field> {*this};
661 using CommonUnsignedIntegerField<LibObjT>::operator=;
663 Class cls() const noexcept
665 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
668 EnumerationFieldClassMappingLabels labels() const
670 bt_field_class_enumeration_mapping_label_array labelArray;
672 const auto status = bt_field_enumeration_unsigned_get_mapping_labels(this->libObjPtr(),
673 &labelArray, &count);
675 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
676 throw MemoryError {};
679 return EnumerationFieldClassMappingLabels {labelArray, count};
683 using UnsignedEnumerationField = CommonUnsignedEnumerationField<bt_field>;
684 using ConstUnsignedEnumerationField = CommonUnsignedEnumerationField<const bt_field>;
688 struct UnsignedEnumerationFieldTypeDescr
690 using Const = ConstUnsignedEnumerationField;
691 using NonConst = UnsignedEnumerationField;
695 struct TypeDescr<UnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
700 struct TypeDescr<ConstUnsignedEnumerationField> : public UnsignedEnumerationFieldTypeDescr
704 } /* namespace internal */
706 template <typename LibObjT>
707 class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibObjT>
710 using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
711 using typename CommonField<LibObjT>::_LibObjPtr;
715 typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
716 SignedEnumerationFieldClass>::type;
718 explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
719 _ThisCommonSignedIntegerField {libObjPtr}
721 BT_ASSERT_DBG(this->isSignedEnumeration());
724 template <typename OtherLibObjT>
725 CommonSignedEnumerationField(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept :
726 _ThisCommonSignedIntegerField {val}
730 template <typename OtherLibObjT>
731 CommonSignedEnumerationField<LibObjT>&
732 operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
734 _ThisCommonSignedIntegerField::operator=(val);
738 CommonSignedEnumerationField<const bt_field> asConst() const noexcept
740 return CommonSignedEnumerationField<const bt_field> {*this};
743 using CommonSignedIntegerField<LibObjT>::operator=;
745 Class cls() const noexcept
747 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
750 EnumerationFieldClassMappingLabels labels() const
752 bt_field_class_enumeration_mapping_label_array labelArray;
755 bt_field_enumeration_signed_get_mapping_labels(this->libObjPtr(), &labelArray, &count);
757 if (status == BT_FIELD_ENUMERATION_GET_MAPPING_LABELS_STATUS_MEMORY_ERROR) {
758 throw MemoryError {};
761 return EnumerationFieldClassMappingLabels {labelArray, count};
765 using SignedEnumerationField = CommonSignedEnumerationField<bt_field>;
766 using ConstSignedEnumerationField = CommonSignedEnumerationField<const bt_field>;
770 struct SignedEnumerationFieldTypeDescr
772 using Const = ConstSignedEnumerationField;
773 using NonConst = SignedEnumerationField;
777 struct TypeDescr<SignedEnumerationField> : public SignedEnumerationFieldTypeDescr
782 struct TypeDescr<ConstSignedEnumerationField> : public SignedEnumerationFieldTypeDescr
786 } /* namespace internal */
788 template <typename LibObjT>
789 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
792 using typename CommonField<LibObjT>::_LibObjPtr;
793 using typename CommonField<LibObjT>::_ThisCommonField;
798 explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
799 _ThisCommonField {libObjPtr}
801 BT_ASSERT_DBG(this->isSinglePrecisionReal());
804 template <typename OtherLibObjT>
805 CommonSinglePrecisionRealField(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
807 _ThisCommonField {val}
811 template <typename OtherLibObjT>
812 CommonSinglePrecisionRealField<LibObjT>&
813 operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
815 _ThisCommonField::operator=(val);
819 CommonSinglePrecisionRealField<const bt_field> asConst() const noexcept
821 return CommonSinglePrecisionRealField<const bt_field> {*this};
824 CommonSinglePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
826 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
828 bt_field_real_single_precision_set_value(this->libObjPtr(), val);
832 Value value() const noexcept
834 return bt_field_real_single_precision_get_value(this->libObjPtr());
837 operator Value() const noexcept
839 return this->value();
843 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
844 using ConstSinglePrecisionRealField = CommonSinglePrecisionRealField<const bt_field>;
848 struct SinglePrecisionRealFieldTypeDescr
850 using Const = ConstSinglePrecisionRealField;
851 using NonConst = SinglePrecisionRealField;
855 struct TypeDescr<SinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
860 struct TypeDescr<ConstSinglePrecisionRealField> : public SinglePrecisionRealFieldTypeDescr
864 } /* namespace internal */
866 template <typename LibObjT>
867 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
870 using typename CommonField<LibObjT>::_LibObjPtr;
871 using typename CommonField<LibObjT>::_ThisCommonField;
874 using Value = double;
876 explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
877 _ThisCommonField {libObjPtr}
879 BT_ASSERT_DBG(this->isDoublePrecisionReal());
882 template <typename OtherLibObjT>
883 CommonDoublePrecisionRealField(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
885 _ThisCommonField {val}
889 template <typename OtherLibObjT>
890 CommonDoublePrecisionRealField<LibObjT>&
891 operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
893 _ThisCommonField::operator=(val);
897 CommonDoublePrecisionRealField<const bt_field> asConst() const noexcept
899 return CommonDoublePrecisionRealField<const bt_field> {*this};
902 CommonDoublePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
904 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
906 bt_field_real_double_precision_set_value(this->libObjPtr(), val);
910 Value value() const noexcept
912 return bt_field_real_double_precision_get_value(this->libObjPtr());
915 operator Value() const noexcept
917 return this->value();
921 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
922 using ConstDoublePrecisionRealField = CommonDoublePrecisionRealField<const bt_field>;
926 struct DoublePrecisionRealFieldTypeDescr
928 using Const = ConstDoublePrecisionRealField;
929 using NonConst = DoublePrecisionRealField;
933 struct TypeDescr<DoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
938 struct TypeDescr<ConstDoublePrecisionRealField> : public DoublePrecisionRealFieldTypeDescr
942 } /* namespace internal */
944 template <typename LibObjT>
945 class CommonStringField final : public CommonField<LibObjT>
948 using typename CommonField<LibObjT>::_LibObjPtr;
949 using typename CommonField<LibObjT>::_ThisCommonField;
952 explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
954 BT_ASSERT_DBG(this->isString());
957 template <typename OtherLibObjT>
958 CommonStringField(const CommonStringField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
962 template <typename OtherLibObjT>
963 CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
965 _ThisCommonField::operator=(val);
969 CommonStringField<const bt_field> asConst() const noexcept
971 return CommonStringField<const bt_field> {*this};
974 CommonStringField<LibObjT> operator=(const char * const val) const
976 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
978 const auto status = bt_field_string_set_value(this->libObjPtr(), val);
980 if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
981 throw MemoryError {};
987 CommonStringField<LibObjT> operator=(const std::string& val) const
989 return *this = val.data();
992 void append(const char * const begin, const std::uint64_t len) const
994 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
996 const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len);
998 if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) {
999 throw MemoryError {};
1003 void append(const std::string& val) const
1005 this->append(val.data(), val.size());
1008 void clear() const noexcept
1010 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1012 bt_field_string_clear(this->libObjPtr());
1015 bpstd::string_view value() const noexcept
1017 return bt_field_string_get_value(this->libObjPtr());
1021 using StringField = CommonStringField<bt_field>;
1022 using ConstStringField = CommonStringField<const bt_field>;
1024 namespace internal {
1026 struct StringFieldTypeDescr
1028 using Const = ConstStringField;
1029 using NonConst = StringField;
1033 struct TypeDescr<StringField> : public StringFieldTypeDescr
1038 struct TypeDescr<ConstStringField> : public StringFieldTypeDescr
1042 template <typename LibObjT>
1043 struct CommonStructureFieldSpec;
1045 /* Functions specific to mutable structure fields */
1047 struct CommonStructureFieldSpec<bt_field> final
1049 static bt_field *memberFieldByIndex(bt_field * const libObjPtr,
1050 const std::uint64_t index) noexcept
1052 return bt_field_structure_borrow_member_field_by_index(libObjPtr, index);
1055 static bt_field *memberFieldByName(bt_field * const libObjPtr, const char * const name) noexcept
1057 return bt_field_structure_borrow_member_field_by_name(libObjPtr, name);
1061 /* Functions specific to constant structure fields */
1063 struct CommonStructureFieldSpec<const bt_field> final
1065 static const bt_field *memberFieldByIndex(const bt_field * const libObjPtr,
1066 const std::uint64_t index) noexcept
1068 return bt_field_structure_borrow_member_field_by_index_const(libObjPtr, index);
1071 static const bt_field *memberFieldByName(const bt_field * const libObjPtr,
1072 const char * const name) noexcept
1074 return bt_field_structure_borrow_member_field_by_name_const(libObjPtr, name);
1078 } /* namespace internal */
1080 template <typename LibObjT>
1081 class CommonStructureField final : public CommonField<LibObjT>
1084 using typename CommonField<LibObjT>::_LibObjPtr;
1085 using typename CommonField<LibObjT>::_ThisCommonField;
1086 using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
1089 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
1090 StructureFieldClass>::type;
1092 explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
1093 _ThisCommonField {libObjPtr}
1095 BT_ASSERT_DBG(this->isStructure());
1098 template <typename OtherLibObjT>
1099 CommonStructureField(const CommonStructureField<OtherLibObjT> val) noexcept :
1100 _ThisCommonField {val}
1104 template <typename OtherLibObjT>
1105 CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
1107 _ThisCommonField::operator=(val);
1111 CommonStructureField<const bt_field> asConst() const noexcept
1113 return CommonStructureField<const bt_field> {*this};
1116 Class cls() const noexcept
1118 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1121 std::uint64_t length() const noexcept
1123 return this->cls().length();
1126 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1128 return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
1131 nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept
1133 const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
1136 return CommonField<LibObjT> {libObjPtr};
1139 return nonstd::nullopt;
1142 nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
1144 return (*this)[name.data()];
1148 using StructureField = CommonStructureField<bt_field>;
1149 using ConstStructureField = CommonStructureField<const bt_field>;
1151 namespace internal {
1153 struct StructureFieldTypeDescr
1155 using Const = ConstStructureField;
1156 using NonConst = StructureField;
1160 struct TypeDescr<StructureField> : public StructureFieldTypeDescr
1165 struct TypeDescr<ConstStructureField> : public StructureFieldTypeDescr
1169 template <typename LibObjT>
1170 struct CommonArrayFieldSpec;
1172 /* Functions specific to mutable array fields */
1174 struct CommonArrayFieldSpec<bt_field> final
1176 static bt_field *elementFieldByIndex(bt_field * const libObjPtr,
1177 const std::uint64_t index) noexcept
1179 return bt_field_array_borrow_element_field_by_index(libObjPtr, index);
1183 /* Functions specific to constant array fields */
1185 struct CommonArrayFieldSpec<const bt_field> final
1187 static const bt_field *elementFieldByIndex(const bt_field * const libObjPtr,
1188 const std::uint64_t index) noexcept
1190 return bt_field_array_borrow_element_field_by_index_const(libObjPtr, index);
1194 } /* namespace internal */
1196 template <typename LibObjT>
1197 class CommonArrayField : public CommonField<LibObjT>
1200 using typename CommonField<LibObjT>::_ThisCommonField;
1201 using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
1204 using typename CommonField<LibObjT>::_LibObjPtr;
1205 using _ThisCommonArrayField = CommonArrayField<LibObjT>;
1208 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
1209 ArrayFieldClass>::type;
1211 explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1213 BT_ASSERT_DBG(this->isArray());
1216 template <typename OtherLibObjT>
1217 CommonArrayField(const CommonArrayField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1221 template <typename OtherLibObjT>
1222 _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
1224 _ThisCommonField::operator=(val);
1228 CommonArrayField<const bt_field> asConst() const noexcept
1230 return CommonArrayField<const bt_field> {*this};
1233 Class cls() const noexcept
1235 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1238 std::uint64_t length() const noexcept
1240 return bt_field_array_get_length(this->libObjPtr());
1243 CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
1245 return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
1249 using ArrayField = CommonArrayField<bt_field>;
1250 using ConstArrayField = CommonArrayField<const bt_field>;
1252 namespace internal {
1254 struct ArrayFieldTypeDescr
1256 using Const = ConstArrayField;
1257 using NonConst = ArrayField;
1261 struct TypeDescr<ArrayField> : public ArrayFieldTypeDescr
1266 struct TypeDescr<ConstArrayField> : public ArrayFieldTypeDescr
1270 } /* namespace internal */
1272 template <typename LibObjT>
1273 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
1276 using typename CommonField<LibObjT>::_LibObjPtr;
1277 using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
1280 explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
1281 _ThisCommonArrayField {libObjPtr}
1283 BT_ASSERT_DBG(this->isDynamicArray());
1286 template <typename OtherLibObjT>
1287 CommonDynamicArrayField(const CommonDynamicArrayField<OtherLibObjT> val) noexcept :
1288 _ThisCommonArrayField {val}
1292 template <typename OtherLibObjT>
1293 CommonDynamicArrayField<LibObjT>&
1294 operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
1296 _ThisCommonArrayField::operator=(val);
1300 CommonDynamicArrayField<const bt_field> asConst() const noexcept
1302 return CommonDynamicArrayField<const bt_field> {*this};
1305 std::uint64_t length() const noexcept
1307 return _ThisCommonArrayField::length();
1310 void length(const std::uint64_t length) const
1312 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1314 const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
1316 if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
1317 throw MemoryError {};
1322 using DynamicArrayField = CommonDynamicArrayField<bt_field>;
1323 using ConstDynamicArrayField = CommonDynamicArrayField<const bt_field>;
1325 namespace internal {
1327 struct DynamicArrayFieldTypeDescr
1329 using Const = ConstDynamicArrayField;
1330 using NonConst = DynamicArrayField;
1334 struct TypeDescr<DynamicArrayField> : public DynamicArrayFieldTypeDescr
1339 struct TypeDescr<ConstDynamicArrayField> : public DynamicArrayFieldTypeDescr
1343 template <typename LibObjT>
1344 struct CommonOptionFieldSpec;
1346 /* Functions specific to mutable option fields */
1348 struct CommonOptionFieldSpec<bt_field> final
1350 static bt_field *field(bt_field * const libObjPtr) noexcept
1352 return bt_field_option_borrow_field(libObjPtr);
1356 /* Functions specific to constant option fields */
1358 struct CommonOptionFieldSpec<const bt_field> final
1360 static const bt_field *field(const bt_field * const libObjPtr) noexcept
1362 return bt_field_option_borrow_field_const(libObjPtr);
1366 } /* namespace internal */
1368 template <typename LibObjT>
1369 class CommonOptionField : public CommonField<LibObjT>
1372 using typename CommonField<LibObjT>::_LibObjPtr;
1373 using typename CommonField<LibObjT>::_ThisCommonField;
1374 using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
1377 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
1378 OptionFieldClass>::type;
1380 explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1382 BT_ASSERT_DBG(this->isOption());
1385 template <typename OtherLibObjT>
1386 CommonOptionField(const CommonOptionField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1390 template <typename OtherLibObjT>
1391 CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
1393 _ThisCommonField::operator=(val);
1397 CommonOptionField<const bt_field> asConst() const noexcept
1399 return CommonOptionField<const bt_field> {*this};
1402 Class cls() const noexcept
1404 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1407 void hasField(const bool hasField) const noexcept
1409 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1411 bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
1414 bool hasField() const noexcept
1416 return this->field().has_value();
1419 nonstd::optional<CommonField<LibObjT>> field() const noexcept
1421 const auto libObjPtr = _Spec::field(this->libObjPtr());
1424 return CommonField<LibObjT> {libObjPtr};
1427 return nonstd::nullopt;
1431 using OptionField = CommonOptionField<bt_field>;
1432 using ConstOptionField = CommonOptionField<const bt_field>;
1434 namespace internal {
1436 struct OptionFieldTypeDescr
1438 using Const = ConstOptionField;
1439 using NonConst = OptionField;
1443 struct TypeDescr<OptionField> : public OptionFieldTypeDescr
1448 struct TypeDescr<ConstOptionField> : public OptionFieldTypeDescr
1452 template <typename LibObjT>
1453 struct CommonVariantFieldSpec;
1455 /* Functions specific to mutable variant fields */
1457 struct CommonVariantFieldSpec<bt_field> final
1459 static bt_field *selectedOptionField(bt_field * const libObjPtr) noexcept
1461 return bt_field_variant_borrow_selected_option_field(libObjPtr);
1465 /* Functions specific to constant variant fields */
1467 struct CommonVariantFieldSpec<const bt_field> final
1469 static const bt_field *selectedOptionField(const bt_field * const libObjPtr) noexcept
1471 return bt_field_variant_borrow_selected_option_field_const(libObjPtr);
1475 } /* namespace internal */
1477 template <typename LibObjT>
1478 class CommonVariantField : public CommonField<LibObjT>
1481 using typename CommonField<LibObjT>::_LibObjPtr;
1482 using typename CommonField<LibObjT>::_ThisCommonField;
1483 using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
1486 using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
1487 VariantFieldClass>::type;
1489 explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
1491 BT_ASSERT_DBG(this->isVariant());
1494 template <typename OtherLibObjT>
1495 CommonVariantField(const CommonVariantField<OtherLibObjT> val) noexcept : _ThisCommonField {val}
1499 template <typename OtherLibObjT>
1500 CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
1502 _ThisCommonField::operator=(val);
1506 CommonVariantField<const bt_field> asConst() const noexcept
1508 return CommonVariantField<const bt_field> {*this};
1511 Class cls() const noexcept
1513 return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
1516 void selectOption(const std::uint64_t index) const noexcept
1518 static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
1520 static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
1523 CommonField<LibObjT> selectedOptionField() const noexcept
1525 return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
1528 std::uint64_t selectedOptionIndex() const noexcept
1530 return bt_field_variant_get_selected_option_index(this->libObjPtr());
1534 using VariantField = CommonVariantField<bt_field>;
1535 using ConstVariantField = CommonVariantField<const bt_field>;
1537 namespace internal {
1539 struct VariantFieldTypeDescr
1541 using Const = ConstVariantField;
1542 using NonConst = VariantField;
1546 struct TypeDescr<VariantField> : public VariantFieldTypeDescr
1551 struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
1555 } /* namespace internal */
1557 template <typename LibObjT>
1558 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
1560 BT_ASSERT_DBG(this->isBool());
1561 return CommonBoolField<LibObjT> {this->libObjPtr()};
1564 template <typename LibObjT>
1565 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
1567 BT_ASSERT_DBG(this->isBitArray());
1568 return CommonBitArrayField<LibObjT> {this->libObjPtr()};
1571 template <typename LibObjT>
1572 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
1574 BT_ASSERT_DBG(this->isUnsignedInteger());
1575 return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
1578 template <typename LibObjT>
1579 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
1581 BT_ASSERT_DBG(this->isSignedInteger());
1582 return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
1585 template <typename LibObjT>
1586 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
1588 BT_ASSERT_DBG(this->isUnsignedEnumeration());
1589 return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
1592 template <typename LibObjT>
1593 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
1595 BT_ASSERT_DBG(this->isSignedEnumeration());
1596 return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
1599 template <typename LibObjT>
1600 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
1602 BT_ASSERT_DBG(this->isSinglePrecisionReal());
1603 return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
1606 template <typename LibObjT>
1607 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
1609 BT_ASSERT_DBG(this->isDoublePrecisionReal());
1610 return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
1613 template <typename LibObjT>
1614 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
1616 BT_ASSERT_DBG(this->isString());
1617 return CommonStringField<LibObjT> {this->libObjPtr()};
1620 template <typename LibObjT>
1621 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
1623 BT_ASSERT_DBG(this->isStructure());
1624 return CommonStructureField<LibObjT> {this->libObjPtr()};
1627 template <typename LibObjT>
1628 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
1630 BT_ASSERT_DBG(this->isArray());
1631 return CommonArrayField<LibObjT> {this->libObjPtr()};
1634 template <typename LibObjT>
1635 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
1637 BT_ASSERT_DBG(this->isDynamicArray());
1638 return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
1641 template <typename LibObjT>
1642 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
1644 BT_ASSERT_DBG(this->isOption());
1645 return CommonOptionField<LibObjT> {this->libObjPtr()};
1648 template <typename LibObjT>
1649 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
1651 BT_ASSERT_DBG(this->isVariant());
1652 return CommonVariantField<LibObjT> {this->libObjPtr()};
1655 } /* namespace bt2 */
1657 #endif /* BABELTRACE_CPP_COMMON_BT2_FIELD_HPP */