X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Ffield.hpp;h=e051a806e4ab2ca1104bcbf42e2d24e93db78e2b;hb=7b3d2ca62ba9d0f2e619935bbd855646dd71bc37;hp=61cdd60a2b894bd8ba3c965405d44f96a5ebad3f;hpb=02f1b80317d17a9c72a82f0138b9f8143506c1ba;p=babeltrace.git diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index 61cdd60a..e051a806 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -13,12 +13,13 @@ #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-obj.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 { @@ -92,46 +93,45 @@ struct CommonFieldSpec final } /* namespace internal */ template -class CommonField : public BorrowedObj +class CommonField : public BorrowedObject { private: - using typename BorrowedObj::_ThisBorrowedObj; + using typename BorrowedObject::_ThisBorrowedObject; protected: - using typename 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())}; } @@ -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()); } @@ -272,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())); } }; @@ -324,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()); } @@ -343,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 { @@ -360,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 @@ -410,16 +418,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; - 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()); @@ -432,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 { - 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::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; @@ -499,16 +504,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; - using Class = typename std::conditional::value, ConstIntegerFieldClass, - IntegerFieldClass>::type; - - explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept : + explicit CommonSignedIntegerField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr} { BT_ASSERT_DBG(this->isSignedInteger()); @@ -521,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 + { + 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::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; @@ -587,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 @@ -617,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; - explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept : + using Class = internal::DepType; + + explicit CommonUnsignedEnumerationField(const LibObjPtr libObjPtr) noexcept : _ThisCommonUnsignedIntegerField {libObjPtr} { BT_ASSERT_DBG(this->isUnsignedEnumeration()); @@ -638,22 +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}; } - using CommonUnsignedIntegerField::operator=; - - Class cls() noexcept + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; } @@ -701,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()); @@ -721,22 +714,19 @@ public: } template - CommonSignedEnumerationField& + CommonSignedEnumerationField operator=(const CommonSignedEnumerationField val) noexcept { _ThisCommonSignedIntegerField::operator=(val); return *this; } - using CommonSignedIntegerField::operator=; - - 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())}; } @@ -783,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()); @@ -803,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()); } }; @@ -856,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()); @@ -876,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_double_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_double_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()); } }; @@ -929,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()); } @@ -944,33 +946,38 @@ 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) + 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; - } - - CommonStringField& operator=(const std::string& val) - { - return *this = val.data(); } - void append(const char * const begin, const std::uint64_t len) + void append(const bt2c::CStringView begin, const std::uint64_t len) 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_append_with_length(this->libObjPtr(), begin, len); @@ -979,22 +986,33 @@ public: } } - void append(const std::string& val) + void append(const bt2c::CStringView val) const + { + this->append(val, std::strlen(val)); + } + + void append(const std::string& val) const { 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; @@ -1060,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()); } @@ -1081,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 + OptionalBorrowedObject> + operator[](const bt2c::CStringView 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 - { - return (*this)[name.data()]; + return _Spec::memberFieldByName(this->libObjPtr(), name); } }; @@ -1205,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()); } @@ -1223,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())}; } @@ -1245,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)}; } @@ -1284,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()); @@ -1301,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); @@ -1375,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()); } @@ -1394,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)); } @@ -1423,27 +1402,9 @@ public: return this->field().has_value(); } - nonstd::optional field() const noexcept + OptionalBorrowedObject> field() const noexcept { - const auto libObjPtr = - internal::CommonOptionFieldSpec::field(this->libObjPtr()); - - if (libObjPtr) { - return ConstField {libObjPtr}; - } - - return nonstd::nullopt; - } - - nonstd::optional> field() noexcept - { - const auto libObjPtr = _Spec::field(this->libObjPtr()); - - if (libObjPtr) { - return CommonField {libObjPtr}; - } - - return nonstd::nullopt; + return _Spec::field(this->libObjPtr()); } }; @@ -1497,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()); } @@ -1516,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())}; }