From 5b5b29dadb9aa657158fc8a2e2e790b95ae195e2 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Mon, 11 Dec 2023 12:14:13 -0500 Subject: [PATCH] cpp-common: Add Blob, StaticBlob, and DynamicBlob FC This commit copies the implementation of the static and dynamic array field class wrappers. The FieldClassType enum size is specified to 64 bits to accommodate for enumerator value larger than 32 bits. (e.g. BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD) Here is the clang++ error In file included from ../../../../src/cpp-common/bt2/trace-ir.hpp:19: ../../../../src/cpp-common/bt2/field-class.hpp:170:20: error: enumerator value evaluates to 2684354560, which cannot be narrowed to type 'int' [-Wc++11-narrowing] DynamicBlob = BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB, ^ ../../../../src/cpp-common/bt2/field-class.hpp:171:41: error: enumerator value evaluates to 6979321856, which cannot be narrowed to type 'int' [-Wc++11-narrowing] DynamicBlobWithoutLengthField = BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD, ^ ../../../../src/cpp-common/bt2/field-class.hpp:172:38: error: enumerator value evaluates to 11274289152, which cannot be narrowed to type 'int' [-Wc++11-narrowing] DynamicBlobWithLengthField = BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD, ^ Signed-off-by: Francis Deslauriers Change-Id: I0a8f14e811e80ef71edbe1e20e2cfcfac1df80b3 Reviewed-on: https://review.lttng.org/c/babeltrace/+/7620 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/12703 Tested-by: jenkins CI-Build: Simon Marchi --- src/cpp-common/bt2/field-class.hpp | 292 ++++++++++++++++++++++++++++- src/cpp-common/bt2/trace-ir.hpp | 33 ++++ 2 files changed, 324 insertions(+), 1 deletion(-) diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index 29d4a384..6ba39aa4 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -98,6 +98,15 @@ class CommonStaticArrayFieldClass; template class CommonDynamicArrayWithLengthFieldClass; +template +class CommonBlobFieldClass; + +template +class CommonStaticBlobFieldClass; + +template +class CommonDynamicBlobWithLengthFieldClass; + template class CommonOptionFieldClass; @@ -134,7 +143,7 @@ class CommonStreamClass; template class CommonTraceClass; -enum class FieldClassType +enum class FieldClassType : std::uint64_t { Bool = BT_FIELD_CLASS_TYPE_BOOL, BitArray = BT_FIELD_CLASS_TYPE_BIT_ARRAY, @@ -159,6 +168,10 @@ enum class FieldClassType BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD, VariantWithSignedIntegerSelector = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, + StaticBlob = BT_FIELD_CLASS_TYPE_STATIC_BLOB, + DynamicBlob = BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB, + DynamicBlobWithoutLengthField = BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD, + DynamicBlobWithLengthField = BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD, }; template @@ -356,6 +369,31 @@ public: return this->_libTypeIs(BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD); } + bool isBlob() const noexcept + { + return this->_libTypeIs(BT_FIELD_CLASS_TYPE_BLOB); + } + + bool isStaticBlob() const noexcept + { + return this->_libTypeIs(BT_FIELD_CLASS_TYPE_STATIC_BLOB); + } + + bool isDynamicBlob() const noexcept + { + return this->_libTypeIs(BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB); + } + + bool isDynamicBlobWithoutLength() const noexcept + { + return this->_libTypeIs(BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD); + } + + bool isDynamicBlobWithLength() const noexcept + { + return this->_libTypeIs(BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD); + } + template FieldClassT as() const noexcept { @@ -377,6 +415,9 @@ public: CommonArrayFieldClass asArray() const noexcept; CommonStaticArrayFieldClass asStaticArray() const noexcept; CommonDynamicArrayWithLengthFieldClass asDynamicArrayWithLength() const noexcept; + CommonBlobFieldClass asBlob() const noexcept; + CommonStaticBlobFieldClass asStaticBlob() const noexcept; + CommonDynamicBlobWithLengthFieldClass asDynamicBlobWithLength() const noexcept; CommonOptionFieldClass asOption() const noexcept; CommonOptionWithSelectorFieldClass asOptionWithSelector() const noexcept; CommonOptionWithBoolSelectorFieldClass asOptionWithBoolSelector() const noexcept; @@ -1464,6 +1505,233 @@ struct TypeDescr : { }; +} /* namespace internal */ + +template +class CommonBlobFieldClass : public CommonFieldClass +{ +private: + using typename CommonFieldClass::_ThisCommonFieldClass; + +protected: + using _ThisCommonBlobFieldClass = CommonBlobFieldClass; + +public: + using Shared = SharedFieldClass, LibObjT>; + using typename CommonFieldClass::LibObjPtr; + + explicit CommonBlobFieldClass(const LibObjPtr libObjPtr) noexcept : + _ThisCommonFieldClass {libObjPtr} + { + BT_ASSERT_DBG(this->isBlob()); + } + + template + CommonBlobFieldClass(const CommonBlobFieldClass fc) noexcept : + _ThisCommonFieldClass {fc} + { + } + + template + CommonBlobFieldClass operator=(const CommonBlobFieldClass fc) noexcept + { + _ThisCommonFieldClass::operator=(fc); + return *this; + } + + CommonBlobFieldClass asConst() const noexcept + { + return CommonBlobFieldClass {*this}; + } + + bt2c::CStringView mediaType() const noexcept + { + return bt_field_class_blob_get_media_type(this->libObjPtr()); + } + + CommonBlobFieldClass mediaType(const bt2c::CStringView mediaType) const + { + static_assert(!std::is_const::value, + "Not available with `bt2::ConstBlobFieldClass`."); + + if (bt_field_class_blob_set_media_type(this->libObjPtr(), mediaType) == + BT_FIELD_CLASS_BLOB_SET_MEDIA_TYPE_STATUS_MEMORY_ERROR) { + throw MemoryError {}; + } + + return *this; + } + + Shared shared() const noexcept + { + return Shared::createWithRef(*this); + } +}; + +using BlobFieldClass = CommonBlobFieldClass; +using ConstBlobFieldClass = CommonBlobFieldClass; + +namespace internal { + +struct BlobFieldClassTypeDescr +{ + using Const = ConstBlobFieldClass; + using NonConst = BlobFieldClass; +}; + +template <> +struct TypeDescr : public BlobFieldClassTypeDescr +{ +}; + +template <> +struct TypeDescr : public BlobFieldClassTypeDescr +{ +}; + +} /* namespace internal */ + +template +class CommonStaticBlobFieldClass final : public CommonBlobFieldClass +{ +private: + using typename CommonBlobFieldClass::_ThisCommonBlobFieldClass; + +public: + using Shared = SharedFieldClass, LibObjT>; + using typename CommonFieldClass::LibObjPtr; + + explicit CommonStaticBlobFieldClass(const LibObjPtr libObjPtr) noexcept : + _ThisCommonBlobFieldClass {libObjPtr} + { + BT_ASSERT_DBG(this->isStaticBlob()); + } + + template + CommonStaticBlobFieldClass(const CommonStaticBlobFieldClass fc) noexcept : + _ThisCommonBlobFieldClass {fc} + { + } + + template + CommonStaticBlobFieldClass operator=(const CommonStaticBlobFieldClass fc) noexcept + { + _ThisCommonBlobFieldClass::operator=(fc); + return *this; + } + + CommonStaticBlobFieldClass asConst() const noexcept + { + return CommonStaticBlobFieldClass {*this}; + } + + std::uint64_t length() const noexcept + { + return bt_field_class_blob_static_get_length(this->libObjPtr()); + } + + Shared shared() const noexcept + { + return Shared::createWithRef(*this); + } +}; + +using StaticBlobFieldClass = CommonStaticBlobFieldClass; +using ConstStaticBlobFieldClass = CommonStaticBlobFieldClass; + +namespace internal { + +struct StaticBlobFieldClassTypeDescr +{ + using Const = ConstStaticBlobFieldClass; + using NonConst = StaticBlobFieldClass; +}; + +template <> +struct TypeDescr : public StaticBlobFieldClassTypeDescr +{ +}; + +template <> +struct TypeDescr : public StaticBlobFieldClassTypeDescr +{ +}; + +} /* namespace internal */ + +template +class CommonDynamicBlobWithLengthFieldClass final : public CommonBlobFieldClass +{ +private: + using typename CommonBlobFieldClass::_ThisCommonBlobFieldClass; + +public: + using Shared = SharedFieldClass, LibObjT>; + using typename CommonFieldClass::LibObjPtr; + + explicit CommonDynamicBlobWithLengthFieldClass(const LibObjPtr libObjPtr) noexcept : + _ThisCommonBlobFieldClass {libObjPtr} + { + BT_ASSERT_DBG(this->isDynamicBlobWithLength()); + } + + template + CommonDynamicBlobWithLengthFieldClass( + const CommonDynamicBlobWithLengthFieldClass fc) noexcept : + _ThisCommonBlobFieldClass {fc} + { + } + + template + CommonDynamicBlobWithLengthFieldClass + operator=(const CommonDynamicBlobWithLengthFieldClass fc) noexcept + { + _ThisCommonBlobFieldClass::operator=(fc); + return *this; + } + + CommonDynamicBlobWithLengthFieldClass asConst() const noexcept + { + return CommonDynamicBlobWithLengthFieldClass {*this}; + } + + ConstFieldLocation lengthFieldLocation() const noexcept + { + return ConstFieldLocation { + bt_field_class_blob_dynamic_with_length_field_borrow_length_field_location_const( + this->libObjPtr())}; + } + + Shared shared() const noexcept + { + return Shared::createWithRef(*this); + } +}; + +using DynamicBlobWithLengthFieldClass = CommonDynamicBlobWithLengthFieldClass; + +using ConstDynamicBlobWithLengthFieldClass = + CommonDynamicBlobWithLengthFieldClass; + +namespace internal { + +struct DynamicBlobWithLengthFieldClassTypeDescr +{ + using Const = ConstDynamicBlobWithLengthFieldClass; + using NonConst = DynamicBlobWithLengthFieldClass; +}; + +template <> +struct TypeDescr : public DynamicBlobWithLengthFieldClassTypeDescr +{ +}; + +template <> +struct TypeDescr : + public DynamicBlobWithLengthFieldClassTypeDescr +{ +}; + template struct CommonOptionFieldClassSpec; @@ -2655,6 +2923,28 @@ CommonFieldClass::asDynamicArrayWithLength() const noexcept return CommonDynamicArrayWithLengthFieldClass {this->libObjPtr()}; } +template +CommonBlobFieldClass CommonFieldClass::asBlob() const noexcept +{ + BT_ASSERT_DBG(this->isBlob()); + return CommonBlobFieldClass {this->libObjPtr()}; +} + +template +CommonStaticBlobFieldClass CommonFieldClass::asStaticBlob() const noexcept +{ + BT_ASSERT_DBG(this->isStaticBlob()); + return CommonStaticBlobFieldClass {this->libObjPtr()}; +} + +template +CommonDynamicBlobWithLengthFieldClass +CommonFieldClass::asDynamicBlobWithLength() const noexcept +{ + BT_ASSERT_DBG(this->isDynamicBlobWithLength()); + return CommonDynamicBlobWithLengthFieldClass {this->libObjPtr()}; +} + template CommonOptionFieldClass CommonFieldClass::asOption() const noexcept { diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 5f0d87e1..462291c7 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -1876,6 +1876,39 @@ public: return DynamicArrayWithLengthFieldClass::Shared::createWithoutRef(libObjPtr); } + StaticBlobFieldClass::Shared createStaticBlobFieldClass(const std::uint64_t length) const + { + static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); + + const auto libObjPtr = bt_field_class_blob_static_create(this->libObjPtr(), length); + + internal::validateCreatedObjPtr(libObjPtr); + return StaticBlobFieldClass::Shared::createWithoutRef(libObjPtr); + } + + BlobFieldClass::Shared createDynamicBlobWithoutLengthFieldLocationFieldClass() const + { + static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); + + const auto libObjPtr = + bt_field_class_blob_dynamic_without_length_field_location_create(this->libObjPtr()); + + internal::validateCreatedObjPtr(libObjPtr); + return BlobFieldClass::Shared::createWithoutRef(libObjPtr); + } + + DynamicBlobWithLengthFieldClass::Shared createDynamicBlobWithLengthFieldLocationFieldClass( + const ConstFieldLocation lengthFieldLocation) const + { + static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); + + const auto libObjPtr = bt_field_class_blob_dynamic_with_length_field_location_create( + this->libObjPtr(), lengthFieldLocation.libObjPtr()); + + internal::validateCreatedObjPtr(libObjPtr); + return DynamicBlobWithLengthFieldClass::Shared::createWithoutRef(libObjPtr); + } + StructureFieldClass::Shared createStructureFieldClass() const { static_assert(!std::is_const::value, "Not available with `bt2::ConstTraceClass`."); -- 2.34.1