X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Ffield.hpp;h=05e1c24422125ee4364c35d9e07ca2699dbc1052;hb=2a24eba8b45ab4bd62dfb2cd95f31e4c2a7a91a8;hp=cb9359e78c6bf114b12f71328b76db3fd1824947;hpb=ac30a4709ff225c8c93337ee61709b33d3772d76;p=babeltrace.git diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index cb9359e7..05e1c244 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -13,12 +13,12 @@ #include #include "common/assert.h" -#include "cpp-common/optional.hpp" -#include "cpp-common/string_view.hpp" +#include "cpp-common/bt2c/c-string-view.hpp" #include "borrowed-object.hpp" #include "field-class.hpp" #include "internal/utils.hpp" +#include "optional-borrowed-object.hpp" #include "raw-value-proxy.hpp" namespace bt2 { @@ -99,13 +99,13 @@ private: using typename BorrowedObject::_ThisBorrowedObject; protected: - using typename BorrowedObject::_LibObjPtr; using _ThisCommonField = CommonField; public: + using typename BorrowedObject::LibObjPtr; using Class = internal::DepFc; - explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} + explicit CommonField(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { } @@ -255,13 +255,13 @@ template class CommonBoolField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: + using typename CommonField::LibObjPtr; using Value = bool; - explicit CommonBoolField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + explicit CommonBoolField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isBool()); } @@ -288,11 +288,12 @@ public: return RawValueProxy {*this}; } - void value(const Value val) const noexcept + CommonBoolField value(const Value val) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstBoolField`."); bt_field_bool_set_value(this->libObjPtr(), static_cast(val)); + return *this; } Value value() const noexcept @@ -328,13 +329,13 @@ template class CommonBitArrayField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: + using typename CommonField::LibObjPtr; using Class = internal::DepType; - explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + explicit CommonBitArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isBitArray()); } @@ -368,12 +369,13 @@ public: return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - void valueAsInteger(const std::uint64_t bits) const noexcept + CommonBitArrayField valueAsInteger(const std::uint64_t bits) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstBitArrayField`."); bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits); + return *this; } std::uint64_t valueAsInteger() const noexcept @@ -418,14 +420,14 @@ private: using typename CommonField::_ThisCommonField; protected: - using typename CommonField::_LibObjPtr; using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField; public: + using typename CommonField::LibObjPtr; using Value = std::uint64_t; using Class = internal::DepType; - explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonUnsignedIntegerField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isUnsignedInteger()); @@ -460,12 +462,13 @@ public: return RawValueProxy {*this}; } - void value(const Value val) const noexcept + CommonUnsignedIntegerField value(const Value val) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstUnsignedIntegerField`."); bt_field_integer_unsigned_set_value(this->libObjPtr(), val); + return *this; } Value value() const noexcept @@ -504,14 +507,14 @@ private: using typename CommonField::_ThisCommonField; protected: - using typename CommonField::_LibObjPtr; using _ThisCommonSignedIntegerField = CommonSignedIntegerField; public: + using typename CommonField::LibObjPtr; using Value = std::int64_t; using Class = internal::DepType; - explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonSignedIntegerField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isSignedInteger()); @@ -546,12 +549,13 @@ public: return RawValueProxy {*this}; } - void value(const Value val) const noexcept + CommonSignedIntegerField value(const Value val) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstSignedIntegerField`."); bt_field_integer_signed_set_value(this->libObjPtr(), val); + return *this; } Value value() const noexcept @@ -598,7 +602,7 @@ public: return _mLen; } - bpstd::string_view operator[](const std::uint64_t index) const noexcept + bt2c::CStringView operator[](const std::uint64_t index) const noexcept { return _mLabels[index]; } @@ -613,13 +617,14 @@ class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField::_ThisCommonUnsignedIntegerField; - using typename CommonField::_LibObjPtr; public: + using typename CommonField::LibObjPtr; + using Class = internal::DepType; - explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonUnsignedEnumerationField(const LibObjPtr libObjPtr) noexcept : _ThisCommonUnsignedIntegerField {libObjPtr} { BT_ASSERT_DBG(this->isUnsignedEnumeration()); @@ -693,13 +698,14 @@ class CommonSignedEnumerationField final : public CommonSignedIntegerField::_ThisCommonSignedIntegerField; - using typename CommonField::_LibObjPtr; public: + using typename CommonField::LibObjPtr; + using Class = internal::DepType; - explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonSignedEnumerationField(const LibObjPtr libObjPtr) noexcept : _ThisCommonSignedIntegerField {libObjPtr} { BT_ASSERT_DBG(this->isSignedEnumeration()); @@ -771,13 +777,13 @@ template class CommonSinglePrecisionRealField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: + using typename CommonField::LibObjPtr; using Value = float; - explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonSinglePrecisionRealField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isSinglePrecisionReal()); @@ -808,12 +814,13 @@ public: return RawValueProxy {*this}; } - void value(const Value val) const noexcept + CommonSinglePrecisionRealField value(const Value val) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstSinglePrecisionRealField`."); bt_field_real_single_precision_set_value(this->libObjPtr(), val); + return *this; } Value value() const noexcept @@ -849,13 +856,13 @@ template class CommonDoublePrecisionRealField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: + using typename CommonField::LibObjPtr; using Value = double; - explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonDoublePrecisionRealField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isDoublePrecisionReal()); @@ -886,12 +893,13 @@ public: return RawValueProxy {*this}; } - void value(const Value val) const noexcept + CommonDoublePrecisionRealField value(const Value val) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstDoublePrecisionRealField`."); bt_field_real_double_precision_set_value(this->libObjPtr(), val); + return *this; } Value value() const noexcept @@ -927,13 +935,13 @@ template class CommonStringField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: - using Value = const char *; + using typename CommonField::LibObjPtr; + using Value = bt2c::CStringView; - explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + explicit CommonStringField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isString()); } @@ -955,29 +963,26 @@ public: return CommonStringField {*this}; } - RawStringValueProxy operator*() const noexcept + RawValueProxy operator*() const noexcept { - return RawStringValueProxy {*this}; + return RawValueProxy {*this}; } - void value(const char * const val) const + CommonStringField value(const Value val) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstStringField`."); - const auto status = bt_field_string_set_value(this->libObjPtr(), val); + const auto status = bt_field_string_set_value(this->libObjPtr(), *val); if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) { throw MemoryError {}; } - } - void value(const std::string& val) const - { - this->value(val.data()); + return *this; } - void append(const char * const begin, const std::uint64_t len) const + CommonStringField append(const bt2c::CStringView begin, const std::uint64_t len) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstStringField`."); @@ -987,25 +992,38 @@ public: if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) { throw MemoryError {}; } + + return *this; } - void append(const std::string& val) const + CommonStringField append(const bt2c::CStringView val) const { - this->append(val.data(), val.size()); + return this->append(val, std::strlen(val)); } - void clear() const noexcept + CommonStringField append(const std::string& val) const + { + return this->append(val.data(), val.size()); + } + + CommonStringField clear() const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstStringField`."); bt_field_string_clear(this->libObjPtr()); + return *this; } - const char *value() const noexcept + Value value() const noexcept { return bt_field_string_get_value(this->libObjPtr()); } + + std::uint64_t length() const noexcept + { + return bt_field_string_get_length(this->libObjPtr()); + } }; using StringField = CommonStringField; @@ -1071,15 +1089,14 @@ template class CommonStructureField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; using _Spec = internal::CommonStructureFieldSpec; public: + using typename CommonField::LibObjPtr; using Class = internal::DepType; - explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept : - _ThisCommonField {libObjPtr} + explicit CommonStructureField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isStructure()); } @@ -1117,20 +1134,10 @@ public: return CommonField {_Spec::memberFieldByIndex(this->libObjPtr(), index)}; } - nonstd::optional> operator[](const char * const name) const noexcept - { - const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name); - - if (libObjPtr) { - return CommonField {libObjPtr}; - } - - return nonstd::nullopt; - } - - nonstd::optional> operator[](const std::string& name) const noexcept + OptionalBorrowedObject> + operator[](const bt2c::CStringView name) const noexcept { - return (*this)[name.data()]; + return _Spec::memberFieldByName(this->libObjPtr(), name); } }; @@ -1190,13 +1197,13 @@ private: using _Spec = internal::CommonArrayFieldSpec; protected: - using typename CommonField::_LibObjPtr; using _ThisCommonArrayField = CommonArrayField; public: + using typename CommonField::LibObjPtr; using Class = internal::DepType; - explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + explicit CommonArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isArray()); } @@ -1261,11 +1268,12 @@ template class CommonDynamicArrayField : public CommonArrayField { private: - using typename CommonField::_LibObjPtr; using typename CommonArrayField::_ThisCommonArrayField; public: - explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept : + using typename CommonField::LibObjPtr; + + explicit CommonDynamicArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonArrayField {libObjPtr} { BT_ASSERT_DBG(this->isDynamicArray()); @@ -1295,7 +1303,7 @@ public: return _ThisCommonArrayField::length(); } - void length(const std::uint64_t length) const + CommonDynamicArrayField length(const std::uint64_t length) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstDynamicArrayField`."); @@ -1305,6 +1313,8 @@ public: if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) { throw MemoryError {}; } + + return *this; } }; @@ -1358,14 +1368,14 @@ template class CommonOptionField : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; using _Spec = internal::CommonOptionFieldSpec; public: + using typename CommonField::LibObjPtr; using Class = internal::DepType; - explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + explicit CommonOptionField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isOption()); } @@ -1392,28 +1402,23 @@ public: return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - void hasField(const bool hasField) const noexcept + CommonOptionField hasField(const bool hasField) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstOptionField`."); bt_field_option_set_has_field(this->libObjPtr(), static_cast(hasField)); + return *this; } bool hasField() const noexcept { - return this->field().has_value(); + return this->field(); } - nonstd::optional> field() const noexcept + OptionalBorrowedObject> field() const noexcept { - const auto libObjPtr = _Spec::field(this->libObjPtr()); - - if (libObjPtr) { - return CommonField {libObjPtr}; - } - - return nonstd::nullopt; + return _Spec::field(this->libObjPtr()); } }; @@ -1467,14 +1472,14 @@ template class CommonVariantField : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; using _Spec = internal::CommonVariantFieldSpec; public: + using typename CommonField::LibObjPtr; using Class = internal::DepType; - explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + explicit CommonVariantField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isVariant()); } @@ -1501,12 +1506,15 @@ public: return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - void selectOption(const std::uint64_t index) const noexcept + CommonVariantField selectOption(const std::uint64_t index) const noexcept { static_assert(!std::is_const::value, "Not available with `bt2::ConstVariantField`."); - static_cast(bt_field_variant_select_option_by_index(this->libObjPtr(), index)); + const auto status = bt_field_variant_select_option_by_index(this->libObjPtr(), index); + + BT_ASSERT_DBG(status == BT_FIELD_VARIANT_SELECT_OPTION_STATUS_OK); + return *this; } CommonField selectedOptionField() const noexcept @@ -1546,98 +1554,84 @@ struct TypeDescr : public VariantFieldTypeDescr template CommonBoolField CommonField::asBool() const noexcept { - BT_ASSERT_DBG(this->isBool()); return CommonBoolField {this->libObjPtr()}; } template CommonBitArrayField CommonField::asBitArray() const noexcept { - BT_ASSERT_DBG(this->isBitArray()); return CommonBitArrayField {this->libObjPtr()}; } template CommonUnsignedIntegerField CommonField::asUnsignedInteger() const noexcept { - BT_ASSERT_DBG(this->isUnsignedInteger()); return CommonUnsignedIntegerField {this->libObjPtr()}; } template CommonSignedIntegerField CommonField::asSignedInteger() const noexcept { - BT_ASSERT_DBG(this->isSignedInteger()); return CommonSignedIntegerField {this->libObjPtr()}; } template CommonUnsignedEnumerationField CommonField::asUnsignedEnumeration() const noexcept { - BT_ASSERT_DBG(this->isUnsignedEnumeration()); return CommonUnsignedEnumerationField {this->libObjPtr()}; } template CommonSignedEnumerationField CommonField::asSignedEnumeration() const noexcept { - BT_ASSERT_DBG(this->isSignedEnumeration()); return CommonSignedEnumerationField {this->libObjPtr()}; } template CommonSinglePrecisionRealField CommonField::asSinglePrecisionReal() const noexcept { - BT_ASSERT_DBG(this->isSinglePrecisionReal()); return CommonSinglePrecisionRealField {this->libObjPtr()}; } template CommonDoublePrecisionRealField CommonField::asDoublePrecisionReal() const noexcept { - BT_ASSERT_DBG(this->isDoublePrecisionReal()); return CommonDoublePrecisionRealField {this->libObjPtr()}; } template CommonStringField CommonField::asString() const noexcept { - BT_ASSERT_DBG(this->isString()); return CommonStringField {this->libObjPtr()}; } template CommonStructureField CommonField::asStructure() const noexcept { - BT_ASSERT_DBG(this->isStructure()); return CommonStructureField {this->libObjPtr()}; } template CommonArrayField CommonField::asArray() const noexcept { - BT_ASSERT_DBG(this->isArray()); return CommonArrayField {this->libObjPtr()}; } template CommonDynamicArrayField CommonField::asDynamicArray() const noexcept { - BT_ASSERT_DBG(this->isDynamicArray()); return CommonDynamicArrayField {this->libObjPtr()}; } template CommonOptionField CommonField::asOption() const noexcept { - BT_ASSERT_DBG(this->isOption()); return CommonOptionField {this->libObjPtr()}; } template CommonVariantField CommonField::asVariant() const noexcept { - BT_ASSERT_DBG(this->isVariant()); return CommonVariantField {this->libObjPtr()}; }