X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Ffield.hpp;h=6853510eb0cf55ddfc4536d93a0500540a01b1e6;hb=bd6e66fd0c3012ad6bcb03cdb3fb687767fb6ccf;hp=7b6777182fd7fca144cf371811f60753a943aedc;hpb=100fa861dbb7e463175e63326e8871446f0f35f0;p=babeltrace.git diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index 7b677718..6853510e 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -7,16 +7,19 @@ #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_HPP #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP -#include #include +#include + #include #include "common/assert.h" -#include "internal/borrowed-obj.hpp" -#include "internal/utils.hpp" -#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 { @@ -90,46 +93,45 @@ struct CommonFieldSpec final } /* namespace internal */ template -class CommonField : public internal::BorrowedObj +class CommonField : public BorrowedObject { private: - using typename internal::BorrowedObj::_ThisBorrowedObj; + using typename BorrowedObject::_ThisBorrowedObject; protected: - using typename internal::BorrowedObj::_LibObjPtr; using _ThisCommonField = CommonField; public: - using Class = - typename std::conditional::value, ConstFieldClass, FieldClass>::type; + using typename BorrowedObject::LibObjPtr; + using Class = internal::DepFc; - explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} + explicit CommonField(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { } template - CommonField(const CommonField val) noexcept : _ThisBorrowedObj {val} + CommonField(const CommonField val) noexcept : _ThisBorrowedObject {val} { } template - _ThisCommonField& operator=(const CommonField val) noexcept + _ThisCommonField operator=(const CommonField val) noexcept { - _ThisBorrowedObj::operator=(val); + _ThisBorrowedObject::operator=(val); return *this; } - FieldClassType classType() const noexcept + CommonField asConst() const noexcept { - return static_cast(bt_field_get_class_type(this->libObjPtr())); + return CommonField {*this}; } - ConstFieldClass cls() const noexcept + FieldClassType classType() const noexcept { - return ConstFieldClass {internal::CommonFieldSpec::cls(this->libObjPtr())}; + return static_cast(bt_field_get_class_type(this->libObjPtr())); } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } @@ -253,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()); } @@ -270,28 +272,32 @@ public: } template - CommonBoolField& operator=(const CommonBoolField val) noexcept + CommonBoolField operator=(const CommonBoolField val) noexcept { _ThisCommonField::operator=(val); return *this; } - CommonBoolField& operator=(const Value val) noexcept + CommonBoolField asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonBoolField {*this}; + } - bt_field_bool_set_value(this->libObjPtr(), static_cast(val)); - return *this; + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; } - Value value() const noexcept + void value(const Value val) const noexcept { - return static_cast(bt_field_bool_get_value(this->libObjPtr())); + static_assert(!std::is_const::value, "Not available with `bt2::ConstBoolField`."); + + bt_field_bool_set_value(this->libObjPtr(), static_cast(val)); } - operator Value() const noexcept + Value value() const noexcept { - return this->value(); + return static_cast(bt_field_bool_get_value(this->libObjPtr())); } }; @@ -322,14 +328,13 @@ template class CommonBitArrayField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: - using Class = typename std::conditional::value, ConstBitArrayFieldClass, - BitArrayFieldClass>::type; + 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()); } @@ -341,12 +346,17 @@ public: } template - CommonBitArrayField& operator=(const CommonBitArrayField val) noexcept + CommonBitArrayField operator=(const CommonBitArrayField val) noexcept { _ThisCommonField::operator=(val); return *this; } + CommonBitArrayField asConst() const noexcept + { + return CommonBitArrayField {*this}; + } + ConstBitArrayFieldClass cls() const noexcept { return ConstBitArrayFieldClass { @@ -358,12 +368,12 @@ public: return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - CommonBitArrayField& operator=(const std::uint64_t bits) noexcept + void valueAsInteger(const std::uint64_t bits) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + 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 @@ -402,22 +412,20 @@ struct TypeDescr : public BitArrayFieldTypeDescr } /* namespace internal */ template -class CommonUnsignedIntegerField final : public CommonField +class CommonUnsignedIntegerField : public CommonField { 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; - using Class = typename std::conditional::value, ConstIntegerFieldClass, - IntegerFieldClass>::type; - - explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonUnsignedIntegerField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isUnsignedInteger()); @@ -430,41 +438,40 @@ public: } template - _ThisCommonUnsignedIntegerField& + _ThisCommonUnsignedIntegerField operator=(const CommonUnsignedIntegerField val) noexcept { _ThisCommonField::operator=(val); return *this; } - ConstIntegerFieldClass cls() const noexcept + CommonUnsignedIntegerField asConst() const noexcept { - return ConstIntegerFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonUnsignedIntegerField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - CommonUnsignedIntegerField& operator=(const Value val) noexcept + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; + } + + void value(const Value val) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + 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 { return bt_field_integer_unsigned_get_value(this->libObjPtr()); } - - operator Value() const noexcept - { - return this->value(); - } }; using UnsignedIntegerField = CommonUnsignedIntegerField; @@ -491,22 +498,20 @@ struct TypeDescr : public UnsignedIntegerFieldTypeDes } /* namespace internal */ template -class CommonSignedIntegerField final : public CommonField +class CommonSignedIntegerField : public CommonField { private: using typename CommonField::_ThisCommonField; protected: - using typename CommonField::_LibObjPtr; using _ThisCommonSignedIntegerField = CommonSignedIntegerField; public: - using Value = std::uint64_t; - - using Class = typename std::conditional::value, ConstIntegerFieldClass, - IntegerFieldClass>::type; + 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()); @@ -519,41 +524,40 @@ public: } template - _ThisCommonSignedIntegerField& + _ThisCommonSignedIntegerField operator=(const CommonSignedIntegerField val) noexcept { _ThisCommonField::operator=(val); return *this; } - ConstIntegerFieldClass cls() const noexcept + CommonSignedIntegerField asConst() const noexcept { - return ConstIntegerFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonSignedIntegerField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - CommonSignedIntegerField& operator=(const Value val) noexcept + RawValueProxy operator*() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return RawValueProxy {*this}; + } + + void 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 { return bt_field_integer_signed_get_value(this->libObjPtr()); } - - operator Value() const noexcept - { - return this->value(); - } }; using SignedIntegerField = CommonSignedIntegerField; @@ -585,29 +589,23 @@ public: explicit EnumerationFieldClassMappingLabels( const bt_field_class_enumeration_mapping_label_array labels, const std::uint64_t size) : _mLabels {labels}, - _mSize {size} + _mLen {size} { } - EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept = - default; - - EnumerationFieldClassMappingLabels& - operator=(const EnumerationFieldClassMappingLabels&) noexcept = default; - - std::uint64_t size() const noexcept + std::uint64_t length() const noexcept { - return _mSize; + 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]; } private: bt_field_class_enumeration_mapping_label_array _mLabels; - std::uint64_t _mSize; + std::uint64_t _mLen; }; template @@ -615,14 +613,14 @@ class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField::_ThisCommonUnsignedIntegerField; - using typename CommonField::_LibObjPtr; public: - using Class = - typename std::conditional::value, ConstUnsignedEnumerationFieldClass, - UnsignedEnumerationFieldClass>::type; + 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()); @@ -636,20 +634,19 @@ public: } template - CommonUnsignedEnumerationField& + CommonUnsignedEnumerationField operator=(const CommonUnsignedEnumerationField val) noexcept { _ThisCommonUnsignedIntegerField::operator=(val); return *this; } - ConstUnsignedEnumerationFieldClass cls() const noexcept + CommonUnsignedEnumerationField asConst() const noexcept { - return ConstUnsignedEnumerationFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonUnsignedEnumerationField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } @@ -697,14 +694,14 @@ class CommonSignedEnumerationField final : public CommonSignedIntegerField::_ThisCommonSignedIntegerField; - using typename CommonField::_LibObjPtr; public: + using typename CommonField::LibObjPtr; + using Class = - typename std::conditional::value, ConstSignedEnumerationFieldClass, - SignedEnumerationFieldClass>::type; + internal::DepType; - explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonSignedEnumerationField(const LibObjPtr libObjPtr) noexcept : _ThisCommonSignedIntegerField {libObjPtr} { BT_ASSERT_DBG(this->isSignedEnumeration()); @@ -717,20 +714,19 @@ public: } template - CommonSignedEnumerationField& + CommonSignedEnumerationField operator=(const CommonSignedEnumerationField val) noexcept { _ThisCommonSignedIntegerField::operator=(val); return *this; } - ConstSignedEnumerationFieldClass cls() const noexcept + CommonSignedEnumerationField asConst() const noexcept { - return ConstSignedEnumerationFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonSignedEnumerationField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } @@ -777,13 +773,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()); @@ -797,29 +793,34 @@ public: } template - CommonSinglePrecisionRealField& + CommonSinglePrecisionRealField operator=(const CommonSinglePrecisionRealField val) noexcept { _ThisCommonField::operator=(val); return *this; } - CommonSinglePrecisionRealField& operator=(const Value val) noexcept + CommonSinglePrecisionRealField asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonSinglePrecisionRealField {*this}; + } - bt_field_real_single_precision_set_value(this->libObjPtr(), val); - return *this; + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; } - Value value() const noexcept + void value(const Value val) const noexcept { - return bt_field_real_single_precision_get_value(this->libObjPtr()); + static_assert(!std::is_const::value, + "Not available with `bt2::ConstSinglePrecisionRealField`."); + + bt_field_real_single_precision_set_value(this->libObjPtr(), val); } - operator Value() const noexcept + Value value() const noexcept { - return this->value(); + return bt_field_real_single_precision_get_value(this->libObjPtr()); } }; @@ -850,13 +851,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()); @@ -870,29 +871,34 @@ public: } template - CommonDoublePrecisionRealField& + CommonDoublePrecisionRealField operator=(const CommonDoublePrecisionRealField val) noexcept { _ThisCommonField::operator=(val); return *this; } - CommonDoublePrecisionRealField& operator=(const Value val) noexcept + CommonDoublePrecisionRealField asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonDoublePrecisionRealField {*this}; + } - bt_field_real_single_precision_set_value(this->libObjPtr(), val); - return *this; + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; } - Value value() const noexcept + void value(const Value val) const noexcept { - return bt_field_real_single_precision_get_value(this->libObjPtr()); + static_assert(!std::is_const::value, + "Not available with `bt2::ConstDoublePrecisionRealField`."); + + bt_field_real_double_precision_set_value(this->libObjPtr(), val); } - operator Value() const noexcept + Value value() const noexcept { - return this->value(); + return bt_field_real_double_precision_get_value(this->libObjPtr()); } }; @@ -923,11 +929,13 @@ template class CommonStringField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; public: - explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} + using typename CommonField::LibObjPtr; + using Value = bt2c::CStringView; + + explicit CommonStringField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isString()); } @@ -938,41 +946,73 @@ public: } template - CommonStringField& operator=(const CommonStringField val) noexcept + CommonStringField operator=(const CommonStringField val) noexcept { _ThisCommonField::operator=(val); return *this; } - CommonStringField& operator=(const char * const val) noexcept + CommonStringField asConst() const noexcept + { + return CommonStringField {*this}; + } + + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; + } + + void value(const Value val) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `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 {}; } + } - return *this; + void append(const bt2c::CStringView begin, const std::uint64_t len) const + { + static_assert(!std::is_const::value, + "Not available with `bt2::ConstStringField`."); + + const auto status = bt_field_string_append_with_length(this->libObjPtr(), begin, len); + + if (status == BT_FIELD_STRING_APPEND_STATUS_MEMORY_ERROR) { + throw MemoryError {}; + } + } + + void append(const bt2c::CStringView val) const + { + this->append(val, std::strlen(val)); } - CommonStringField& operator=(const std::string& val) noexcept + void append(const std::string& val) const { - return *this = val.data(); + this->append(val.data(), val.size()); } - void clear() noexcept + void clear() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, + "Not available with `bt2::ConstStringField`."); bt_field_string_clear(this->libObjPtr()); } - bpstd::string_view 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; @@ -1038,16 +1078,14 @@ template class CommonStructureField final : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; using _Spec = internal::CommonStructureFieldSpec; public: - using Class = typename std::conditional::value, ConstStructureFieldClass, - StructureFieldClass>::type; + 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()); } @@ -1059,71 +1097,36 @@ public: } template - CommonStructureField& operator=(const CommonStructureField val) noexcept + CommonStructureField operator=(const CommonStructureField val) noexcept { _ThisCommonField::operator=(val); return *this; } - ConstStructureFieldClass cls() const noexcept + CommonStructureField asConst() const noexcept { - return ConstStructureFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonStructureField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - std::uint64_t size() const noexcept - { - return this->cls().size(); - } - - ConstField operator[](const std::uint64_t index) const noexcept + std::uint64_t length() const noexcept { - return ConstField {internal::CommonStructureFieldSpec::memberFieldByIndex( - this->libObjPtr(), index)}; + return this->cls().length(); } - CommonField operator[](const std::uint64_t index) noexcept + CommonField operator[](const std::uint64_t index) const noexcept { return CommonField {_Spec::memberFieldByIndex(this->libObjPtr(), index)}; } - nonstd::optional operator[](const char * const name) const noexcept - { - const auto libObjPtr = - internal::CommonStructureFieldSpec::memberFieldByName(this->libObjPtr(), - name); - - if (libObjPtr) { - return ConstField {libObjPtr}; - } - - return nonstd::nullopt; - } - - nonstd::optional operator[](const std::string& name) const noexcept - { - return (*this)[name.data()]; - } - - nonstd::optional> operator[](const char * const name) noexcept - { - const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name); - - if (libObjPtr) { - return CommonField {libObjPtr}; - } - - return nonstd::nullopt; - } - - nonstd::optional> operator[](const std::string& name) noexcept + OptionalBorrowedObject> + operator[](const bt2c::CStringView name) const noexcept { - return (*this)[name.data()]; + return _Spec::memberFieldByName(this->libObjPtr(), name); } }; @@ -1183,14 +1186,13 @@ private: using _Spec = internal::CommonArrayFieldSpec; protected: - using typename CommonField::_LibObjPtr; using _ThisCommonArrayField = CommonArrayField; public: - using Class = typename std::conditional::value, ConstArrayFieldClass, - ArrayFieldClass>::type; + 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()); } @@ -1201,19 +1203,18 @@ public: } template - _ThisCommonArrayField& operator=(const CommonArrayField val) noexcept + _ThisCommonArrayField operator=(const CommonArrayField val) noexcept { _ThisCommonField::operator=(val); return *this; } - ConstArrayFieldClass cls() const noexcept + CommonArrayField asConst() const noexcept { - return ConstArrayFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonArrayField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } @@ -1223,13 +1224,7 @@ public: return bt_field_array_get_length(this->libObjPtr()); } - ConstField operator[](const std::uint64_t index) const noexcept - { - return ConstField {internal::CommonArrayFieldSpec::elementFieldByIndex( - this->libObjPtr(), index)}; - } - - CommonField operator[](const std::uint64_t index) noexcept + CommonField operator[](const std::uint64_t index) const noexcept { return CommonField {_Spec::elementFieldByIndex(this->libObjPtr(), index)}; } @@ -1262,11 +1257,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()); @@ -1279,21 +1275,27 @@ public: } template - CommonDynamicArrayField& + CommonDynamicArrayField operator=(const CommonDynamicArrayField val) noexcept { _ThisCommonArrayField::operator=(val); return *this; } + CommonDynamicArrayField asConst() const noexcept + { + return CommonDynamicArrayField {*this}; + } + std::uint64_t length() const noexcept { return _ThisCommonArrayField::length(); } - void length(const std::uint64_t length) + void length(const std::uint64_t length) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, + "Not available with `bt2::ConstDynamicArrayField`."); const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length); @@ -1353,15 +1355,14 @@ template class CommonOptionField : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; using _Spec = internal::CommonOptionFieldSpec; public: - using Class = typename std::conditional::value, ConstOptionFieldClass, - OptionFieldClass>::type; + 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()); } @@ -1372,26 +1373,26 @@ public: } template - CommonOptionField& operator=(const CommonOptionField val) noexcept + CommonOptionField operator=(const CommonOptionField val) noexcept { _ThisCommonField::operator=(val); return *this; } - ConstOptionFieldClass cls() const noexcept + CommonOptionField asConst() const noexcept { - return ConstOptionFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonOptionField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - void hasField(const bool hasField) noexcept + void hasField(const bool hasField) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, + "Not available with `bt2::ConstOptionField`."); bt_field_option_set_has_field(this->libObjPtr(), static_cast(hasField)); } @@ -1401,27 +1402,9 @@ public: return this->field(); } - nonstd::optional field() const noexcept - { - const auto libObjPtr = - internal::CommonOptionFieldSpec::field(this->libObjPtr()); - - if (libObjPtr) { - return ConstField {libObjPtr}; - } - - return nonstd::nullopt; - } - - nonstd::optional> field() 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()); } }; @@ -1475,15 +1458,14 @@ template class CommonVariantField : public CommonField { private: - using typename CommonField::_LibObjPtr; using typename CommonField::_ThisCommonField; using _Spec = internal::CommonVariantFieldSpec; public: - using Class = typename std::conditional::value, ConstVariantFieldClass, - VariantFieldClass>::type; + 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()); } @@ -1494,37 +1476,31 @@ public: } template - CommonVariantField& operator=(const CommonVariantField val) noexcept + CommonVariantField operator=(const CommonVariantField val) noexcept { _ThisCommonField::operator=(val); return *this; } - ConstVariantFieldClass cls() const noexcept + CommonVariantField asConst() const noexcept { - return ConstVariantFieldClass { - internal::CommonFieldSpec::cls(this->libObjPtr())}; + return CommonVariantField {*this}; } - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } - void selectOption(const std::uint64_t index) noexcept + void selectOption(const std::uint64_t index) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, + "Not available with `bt2::ConstVariantField`."); static_cast(bt_field_variant_select_option_by_index(this->libObjPtr(), index)); } - ConstField selectedOptionField() const noexcept - { - return ConstField {internal::CommonVariantFieldSpec::selectedOptionField( - this->libObjPtr())}; - } - - CommonField selectedOptionField() noexcept + CommonField selectedOptionField() const noexcept { return CommonField {_Spec::selectedOptionField(this->libObjPtr())}; } @@ -1561,98 +1537,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()}; }