cpp-common/bt2: make setters return `*this`
[babeltrace.git] / src / cpp-common / bt2 / field.hpp
index a59ac9fdf686305060b96f26327dd133bb23978b..05e1c24422125ee4364c35d9e07ca2699dbc1052 100644 (file)
@@ -7,16 +7,19 @@
 #ifndef BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
 #define BABELTRACE_CPP_COMMON_BT2_FIELD_HPP
 
-#include <type_traits>
 #include <cstdint>
+#include <type_traits>
+
 #include <babeltrace2/babeltrace.h>
 
 #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<const bt_field> final
 } /* namespace internal */
 
 template <typename LibObjT>
-class CommonField : public internal::BorrowedObj<LibObjT>
+class CommonField : public BorrowedObject<LibObjT>
 {
 private:
-    using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
+    using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
 
 protected:
-    using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
     using _ThisCommonField = CommonField<LibObjT>;
 
 public:
-    using Class =
-        typename std::conditional<std::is_const<LibObjT>::value, ConstFieldClass, FieldClass>::type;
+    using typename BorrowedObject<LibObjT>::LibObjPtr;
+    using Class = internal::DepFc<LibObjT>;
 
-    explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
+    explicit CommonField(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObj {val}
+    CommonField(const CommonField<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
     {
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept
+    _ThisCommonField operator=(const CommonField<OtherLibObjT> val) noexcept
     {
-        _ThisBorrowedObj::operator=(val);
+        _ThisBorrowedObject::operator=(val);
         return *this;
     }
 
-    FieldClassType classType() const noexcept
+    CommonField<const bt_field> asConst() const noexcept
     {
-        return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
+        return CommonField<const bt_field> {*this};
     }
 
-    ConstFieldClass cls() const noexcept
+    FieldClassType classType() const noexcept
     {
-        return ConstFieldClass {internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
@@ -253,13 +255,13 @@ template <typename LibObjT>
 class CommonBoolField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
+    using typename CommonField<LibObjT>::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,15 +272,25 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
+    CommonBoolField<LibObjT> operator=(const CommonBoolField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    CommonBoolField<LibObjT>& operator=(const Value val) noexcept
+    CommonBoolField<const bt_field> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonBoolField<const bt_field> {*this};
+    }
+
+    RawValueProxy<CommonBoolField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonBoolField> {*this};
+    }
+
+    CommonBoolField value(const Value val) const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolField`.");
 
         bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
         return *this;
@@ -288,11 +300,6 @@ public:
     {
         return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using BoolField = CommonBoolField<bt_field>;
@@ -322,14 +329,13 @@ template <typename LibObjT>
 class CommonBitArrayField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstBitArrayFieldClass,
-                                            BitArrayFieldClass>::type;
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Class = internal::DepType<LibObjT, BitArrayFieldClass, ConstBitArrayFieldClass>;
 
-    explicit CommonBitArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
+    explicit CommonBitArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isBitArray());
     }
@@ -341,12 +347,17 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
+    CommonBitArrayField<LibObjT> operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
+    CommonBitArrayField<const bt_field> asConst() const noexcept
+    {
+        return CommonBitArrayField<const bt_field> {*this};
+    }
+
     ConstBitArrayFieldClass cls() const noexcept
     {
         return ConstBitArrayFieldClass {
@@ -358,9 +369,10 @@ public:
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    CommonBitArrayField<LibObjT>& operator=(const std::uint64_t bits) noexcept
+    CommonBitArrayField valueAsInteger(const std::uint64_t bits) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstBitArrayField`.");
 
         bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
         return *this;
@@ -408,16 +420,14 @@ private:
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 protected:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using _ThisCommonUnsignedIntegerField = CommonUnsignedIntegerField<LibObjT>;
 
 public:
+    using typename CommonField<LibObjT>::LibObjPtr;
     using Value = std::uint64_t;
+    using Class = internal::DepType<LibObjT, IntegerFieldClass, ConstIntegerFieldClass>;
 
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
-                                            IntegerFieldClass>::type;
-
-    explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonUnsignedIntegerField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isUnsignedInteger());
@@ -430,27 +440,32 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonUnsignedIntegerField&
+    _ThisCommonUnsignedIntegerField
     operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    ConstIntegerFieldClass cls() const noexcept
+    CommonUnsignedIntegerField<const bt_field> asConst() const noexcept
     {
-        return ConstIntegerFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonUnsignedIntegerField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    CommonUnsignedIntegerField<LibObjT>& operator=(const Value val) noexcept
+    RawValueProxy<CommonUnsignedIntegerField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonUnsignedIntegerField> {*this};
+    }
+
+    CommonUnsignedIntegerField value(const Value val) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstUnsignedIntegerField`.");
 
         bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
         return *this;
@@ -460,11 +475,6 @@ public:
     {
         return bt_field_integer_unsigned_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
@@ -497,16 +507,14 @@ private:
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 protected:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using _ThisCommonSignedIntegerField = CommonSignedIntegerField<LibObjT>;
 
 public:
+    using typename CommonField<LibObjT>::LibObjPtr;
     using Value = std::int64_t;
+    using Class = internal::DepType<LibObjT, IntegerFieldClass, ConstIntegerFieldClass>;
 
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstIntegerFieldClass,
-                                            IntegerFieldClass>::type;
-
-    explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonSignedIntegerField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isSignedInteger());
@@ -519,27 +527,32 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonSignedIntegerField&
+    _ThisCommonSignedIntegerField
     operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    ConstIntegerFieldClass cls() const noexcept
+    CommonSignedIntegerField<const bt_field> asConst() const noexcept
     {
-        return ConstIntegerFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonSignedIntegerField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    CommonSignedIntegerField<LibObjT>& operator=(const Value val) noexcept
+    RawValueProxy<CommonSignedIntegerField> operator*() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return RawValueProxy<CommonSignedIntegerField> {*this};
+    }
+
+    CommonSignedIntegerField value(const Value val) const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstSignedIntegerField`.");
 
         bt_field_integer_signed_set_value(this->libObjPtr(), val);
         return *this;
@@ -549,11 +562,6 @@ public:
     {
         return bt_field_integer_signed_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
@@ -585,29 +593,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 <typename LibObjT>
@@ -615,14 +617,14 @@ class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<L
 {
 private:
     using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
-    using typename CommonField<LibObjT>::_LibObjPtr;
 
 public:
-    using Class =
-        typename std::conditional<std::is_const<LibObjT>::value, ConstUnsignedEnumerationFieldClass,
-                                  UnsignedEnumerationFieldClass>::type;
+    using typename CommonField<LibObjT>::LibObjPtr;
 
-    explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
+    using Class = internal::DepType<LibObjT, UnsignedEnumerationFieldClass,
+                                    ConstUnsignedEnumerationFieldClass>;
+
+    explicit CommonUnsignedEnumerationField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonUnsignedIntegerField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isUnsignedEnumeration());
@@ -636,20 +638,19 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonUnsignedEnumerationField<LibObjT>&
+    CommonUnsignedEnumerationField<LibObjT>
     operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
     {
         _ThisCommonUnsignedIntegerField::operator=(val);
         return *this;
     }
 
-    ConstUnsignedEnumerationFieldClass cls() const noexcept
+    CommonUnsignedEnumerationField<const bt_field> asConst() const noexcept
     {
-        return ConstUnsignedEnumerationFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonUnsignedEnumerationField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
@@ -697,14 +698,14 @@ class CommonSignedEnumerationField final : public CommonSignedIntegerField<LibOb
 {
 private:
     using typename CommonSignedIntegerField<LibObjT>::_ThisCommonSignedIntegerField;
-    using typename CommonField<LibObjT>::_LibObjPtr;
 
 public:
+    using typename CommonField<LibObjT>::LibObjPtr;
+
     using Class =
-        typename std::conditional<std::is_const<LibObjT>::value, ConstSignedEnumerationFieldClass,
-                                  SignedEnumerationFieldClass>::type;
+        internal::DepType<LibObjT, SignedEnumerationFieldClass, ConstSignedEnumerationFieldClass>;
 
-    explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonSignedEnumerationField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonSignedIntegerField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isSignedEnumeration());
@@ -717,20 +718,19 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonSignedEnumerationField<LibObjT>&
+    CommonSignedEnumerationField<LibObjT>
     operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
     {
         _ThisCommonSignedIntegerField::operator=(val);
         return *this;
     }
 
-    ConstSignedEnumerationFieldClass cls() const noexcept
+    CommonSignedEnumerationField<const bt_field> asConst() const noexcept
     {
-        return ConstSignedEnumerationFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonSignedEnumerationField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
@@ -777,13 +777,13 @@ template <typename LibObjT>
 class CommonSinglePrecisionRealField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
+    using typename CommonField<LibObjT>::LibObjPtr;
     using Value = float;
 
-    explicit CommonSinglePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonSinglePrecisionRealField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isSinglePrecisionReal());
@@ -797,16 +797,27 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonSinglePrecisionRealField<LibObjT>&
+    CommonSinglePrecisionRealField<LibObjT>
     operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    CommonSinglePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
+    CommonSinglePrecisionRealField<const bt_field> asConst() const noexcept
+    {
+        return CommonSinglePrecisionRealField<const bt_field> {*this};
+    }
+
+    RawValueProxy<CommonSinglePrecisionRealField> operator*() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return RawValueProxy<CommonSinglePrecisionRealField> {*this};
+    }
+
+    CommonSinglePrecisionRealField value(const Value val) const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstSinglePrecisionRealField`.");
 
         bt_field_real_single_precision_set_value(this->libObjPtr(), val);
         return *this;
@@ -816,11 +827,6 @@ public:
     {
         return bt_field_real_single_precision_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
@@ -850,13 +856,13 @@ template <typename LibObjT>
 class CommonDoublePrecisionRealField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
+    using typename CommonField<LibObjT>::LibObjPtr;
     using Value = double;
 
-    explicit CommonDoublePrecisionRealField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonDoublePrecisionRealField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isDoublePrecisionReal());
@@ -870,16 +876,27 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonDoublePrecisionRealField<LibObjT>&
+    CommonDoublePrecisionRealField<LibObjT>
     operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    CommonDoublePrecisionRealField<LibObjT>& operator=(const Value val) noexcept
+    CommonDoublePrecisionRealField<const bt_field> asConst() const noexcept
+    {
+        return CommonDoublePrecisionRealField<const bt_field> {*this};
+    }
+
+    RawValueProxy<CommonDoublePrecisionRealField> operator*() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return RawValueProxy<CommonDoublePrecisionRealField> {*this};
+    }
+
+    CommonDoublePrecisionRealField value(const Value val) const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstDoublePrecisionRealField`.");
 
         bt_field_real_double_precision_set_value(this->libObjPtr(), val);
         return *this;
@@ -889,11 +906,6 @@ public:
     {
         return bt_field_real_double_precision_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
@@ -923,11 +935,13 @@ template <typename LibObjT>
 class CommonStringField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
-    explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Value = bt2c::CStringView;
+
+    explicit CommonStringField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isString());
     }
@@ -938,17 +952,28 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
+    CommonStringField<LibObjT> operator=(const CommonStringField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    CommonStringField<LibObjT>& operator=(const char * const val)
+    CommonStringField<const bt_field> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonStringField<const bt_field> {*this};
+    }
 
-        const auto status = bt_field_string_set_value(this->libObjPtr(), val);
+    RawValueProxy<CommonStringField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonStringField> {*this};
+    }
+
+    CommonStringField value(const Value val) const
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstStringField`.");
+
+        const auto status = bt_field_string_set_value(this->libObjPtr(), *val);
 
         if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
             throw MemoryError {};
@@ -957,22 +982,48 @@ public:
         return *this;
     }
 
-    CommonStringField<LibObjT>& operator=(const std::string& val)
+    CommonStringField append(const bt2c::CStringView begin, const std::uint64_t len) const
+    {
+        static_assert(!std::is_const<LibObjT>::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 {};
+        }
+
+        return *this;
+    }
+
+    CommonStringField append(const bt2c::CStringView val) const
     {
-        return *this = val.data();
+        return this->append(val, std::strlen(val));
     }
 
-    void clear() noexcept
+    CommonStringField append(const std::string& val) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return this->append(val.data(), val.size());
+    }
+
+    CommonStringField clear() const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstStringField`.");
 
         bt_field_string_clear(this->libObjPtr());
+        return *this;
     }
 
-    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<bt_field>;
@@ -1038,16 +1089,14 @@ template <typename LibObjT>
 class CommonStructureField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
     using _Spec = internal::CommonStructureFieldSpec<LibObjT>;
 
 public:
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstStructureFieldClass,
-                                            StructureFieldClass>::type;
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Class = internal::DepType<LibObjT, StructureFieldClass, ConstStructureFieldClass>;
 
-    explicit CommonStructureField(const _LibObjPtr libObjPtr) noexcept :
-        _ThisCommonField {libObjPtr}
+    explicit CommonStructureField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isStructure());
     }
@@ -1059,71 +1108,36 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
+    CommonStructureField<LibObjT> operator=(const CommonStructureField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    ConstStructureFieldClass cls() const noexcept
+    CommonStructureField<const bt_field> asConst() const noexcept
     {
-        return ConstStructureFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonStructureField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::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<const bt_field>::memberFieldByIndex(
-            this->libObjPtr(), index)};
+        return this->cls().length();
     }
 
-    CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
+    CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
     {
         return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
     }
 
-    nonstd::optional<ConstField> operator[](const char * const name) const noexcept
-    {
-        const auto libObjPtr =
-            internal::CommonStructureFieldSpec<const bt_field>::memberFieldByName(this->libObjPtr(),
-                                                                                  name);
-
-        if (libObjPtr) {
-            return ConstField {libObjPtr};
-        }
-
-        return nonstd::nullopt;
-    }
-
-    nonstd::optional<ConstField> operator[](const std::string& name) const noexcept
-    {
-        return (*this)[name.data()];
-    }
-
-    nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) noexcept
+    OptionalBorrowedObject<CommonField<LibObjT>>
+    operator[](const bt2c::CStringView name) const noexcept
     {
-        const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
-
-        if (libObjPtr) {
-            return CommonField<LibObjT> {libObjPtr};
-        }
-
-        return nonstd::nullopt;
-    }
-
-    nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) noexcept
-    {
-        return (*this)[name.data()];
+        return _Spec::memberFieldByName(this->libObjPtr(), name);
     }
 };
 
@@ -1183,14 +1197,13 @@ private:
     using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
 
 protected:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using _ThisCommonArrayField = CommonArrayField<LibObjT>;
 
 public:
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstArrayFieldClass,
-                                            ArrayFieldClass>::type;
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Class = internal::DepType<LibObjT, ArrayFieldClass, ConstArrayFieldClass>;
 
-    explicit CommonArrayField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
+    explicit CommonArrayField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isArray());
     }
@@ -1201,19 +1214,18 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
+    _ThisCommonArrayField operator=(const CommonArrayField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    ConstArrayFieldClass cls() const noexcept
+    CommonArrayField<const bt_field> asConst() const noexcept
     {
-        return ConstArrayFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonArrayField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
@@ -1223,13 +1235,7 @@ public:
         return bt_field_array_get_length(this->libObjPtr());
     }
 
-    ConstField operator[](const std::uint64_t index) const noexcept
-    {
-        return ConstField {internal::CommonArrayFieldSpec<const bt_field>::elementFieldByIndex(
-            this->libObjPtr(), index)};
-    }
-
-    CommonField<LibObjT> operator[](const std::uint64_t index) noexcept
+    CommonField<LibObjT> operator[](const std::uint64_t index) const noexcept
     {
         return CommonField<LibObjT> {_Spec::elementFieldByIndex(this->libObjPtr(), index)};
     }
@@ -1262,11 +1268,12 @@ template <typename LibObjT>
 class CommonDynamicArrayField : public CommonArrayField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonArrayField<LibObjT>::_ThisCommonArrayField;
 
 public:
-    explicit CommonDynamicArrayField(const _LibObjPtr libObjPtr) noexcept :
+    using typename CommonField<LibObjT>::LibObjPtr;
+
+    explicit CommonDynamicArrayField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonArrayField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isDynamicArray());
@@ -1279,27 +1286,35 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonDynamicArrayField<LibObjT>&
+    CommonDynamicArrayField<LibObjT>
     operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
     {
         _ThisCommonArrayField::operator=(val);
         return *this;
     }
 
+    CommonDynamicArrayField<const bt_field> asConst() const noexcept
+    {
+        return CommonDynamicArrayField<const bt_field> {*this};
+    }
+
     std::uint64_t length() const noexcept
     {
         return _ThisCommonArrayField::length();
     }
 
-    void length(const std::uint64_t length)
+    CommonDynamicArrayField length(const std::uint64_t length) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstDynamicArrayField`.");
 
         const auto status = bt_field_array_dynamic_set_length(this->libObjPtr(), length);
 
         if (status == BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_MEMORY_ERROR) {
             throw MemoryError {};
         }
+
+        return *this;
     }
 };
 
@@ -1353,15 +1368,14 @@ template <typename LibObjT>
 class CommonOptionField : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
     using _Spec = internal::CommonOptionFieldSpec<LibObjT>;
 
 public:
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstOptionFieldClass,
-                                            OptionFieldClass>::type;
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Class = internal::DepType<LibObjT, OptionFieldClass, ConstOptionFieldClass>;
 
-    explicit CommonOptionField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
+    explicit CommonOptionField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isOption());
     }
@@ -1372,28 +1386,29 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
+    CommonOptionField<LibObjT> operator=(const CommonOptionField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    ConstOptionFieldClass cls() const noexcept
+    CommonOptionField<const bt_field> asConst() const noexcept
     {
-        return ConstOptionFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonOptionField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    void hasField(const bool hasField) noexcept
+    CommonOptionField hasField(const bool hasField) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstOptionField`.");
 
         bt_field_option_set_has_field(this->libObjPtr(), static_cast<bt_bool>(hasField));
+        return *this;
     }
 
     bool hasField() const noexcept
@@ -1401,27 +1416,9 @@ public:
         return this->field();
     }
 
-    nonstd::optional<ConstField> field() const noexcept
-    {
-        const auto libObjPtr =
-            internal::CommonOptionFieldSpec<const bt_field>::field(this->libObjPtr());
-
-        if (libObjPtr) {
-            return ConstField {libObjPtr};
-        }
-
-        return nonstd::nullopt;
-    }
-
-    nonstd::optional<CommonField<LibObjT>> field() noexcept
+    OptionalBorrowedObject<CommonField<LibObjT>> field() const noexcept
     {
-        const auto libObjPtr = _Spec::field(this->libObjPtr());
-
-        if (libObjPtr) {
-            return CommonField<LibObjT> {libObjPtr};
-        }
-
-        return nonstd::nullopt;
+        return _Spec::field(this->libObjPtr());
     }
 };
 
@@ -1475,15 +1472,14 @@ template <typename LibObjT>
 class CommonVariantField : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
     using _Spec = internal::CommonVariantFieldSpec<LibObjT>;
 
 public:
-    using Class = typename std::conditional<std::is_const<LibObjT>::value, ConstVariantFieldClass,
-                                            VariantFieldClass>::type;
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Class = internal::DepType<LibObjT, VariantFieldClass, ConstVariantFieldClass>;
 
-    explicit CommonVariantField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
+    explicit CommonVariantField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isVariant());
     }
@@ -1494,37 +1490,34 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
+    CommonVariantField<LibObjT> operator=(const CommonVariantField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
     }
 
-    ConstVariantFieldClass cls() const noexcept
+    CommonVariantField<const bt_field> asConst() const noexcept
     {
-        return ConstVariantFieldClass {
-            internal::CommonFieldSpec<const bt_field>::cls(this->libObjPtr())};
+        return CommonVariantField<const bt_field> {*this};
     }
 
-    Class cls() noexcept
+    Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    void selectOption(const std::uint64_t index) noexcept
+    CommonVariantField selectOption(const std::uint64_t index) const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstVariantField`.");
 
-        static_cast<void>(bt_field_variant_select_option_by_index(this->libObjPtr(), index));
-    }
+        const auto status = bt_field_variant_select_option_by_index(this->libObjPtr(), index);
 
-    ConstField selectedOptionField() const noexcept
-    {
-        return ConstField {internal::CommonVariantFieldSpec<const bt_field>::selectedOptionField(
-            this->libObjPtr())};
+        BT_ASSERT_DBG(status == BT_FIELD_VARIANT_SELECT_OPTION_STATUS_OK);
+        return *this;
     }
 
-    CommonField<LibObjT> selectedOptionField() noexcept
+    CommonField<LibObjT> selectedOptionField() const noexcept
     {
         return CommonField<LibObjT> {_Spec::selectedOptionField(this->libObjPtr())};
     }
@@ -1561,98 +1554,84 @@ struct TypeDescr<ConstVariantField> : public VariantFieldTypeDescr
 template <typename LibObjT>
 CommonBoolField<LibObjT> CommonField<LibObjT>::asBool() const noexcept
 {
-    BT_ASSERT_DBG(this->isBool());
     return CommonBoolField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonBitArrayField<LibObjT> CommonField<LibObjT>::asBitArray() const noexcept
 {
-    BT_ASSERT_DBG(this->isBitArray());
     return CommonBitArrayField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonUnsignedIntegerField<LibObjT> CommonField<LibObjT>::asUnsignedInteger() const noexcept
 {
-    BT_ASSERT_DBG(this->isUnsignedInteger());
     return CommonUnsignedIntegerField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonSignedIntegerField<LibObjT> CommonField<LibObjT>::asSignedInteger() const noexcept
 {
-    BT_ASSERT_DBG(this->isSignedInteger());
     return CommonSignedIntegerField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonUnsignedEnumerationField<LibObjT> CommonField<LibObjT>::asUnsignedEnumeration() const noexcept
 {
-    BT_ASSERT_DBG(this->isUnsignedEnumeration());
     return CommonUnsignedEnumerationField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonSignedEnumerationField<LibObjT> CommonField<LibObjT>::asSignedEnumeration() const noexcept
 {
-    BT_ASSERT_DBG(this->isSignedEnumeration());
     return CommonSignedEnumerationField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonSinglePrecisionRealField<LibObjT> CommonField<LibObjT>::asSinglePrecisionReal() const noexcept
 {
-    BT_ASSERT_DBG(this->isSinglePrecisionReal());
     return CommonSinglePrecisionRealField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonDoublePrecisionRealField<LibObjT> CommonField<LibObjT>::asDoublePrecisionReal() const noexcept
 {
-    BT_ASSERT_DBG(this->isDoublePrecisionReal());
     return CommonDoublePrecisionRealField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonStringField<LibObjT> CommonField<LibObjT>::asString() const noexcept
 {
-    BT_ASSERT_DBG(this->isString());
     return CommonStringField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonStructureField<LibObjT> CommonField<LibObjT>::asStructure() const noexcept
 {
-    BT_ASSERT_DBG(this->isStructure());
     return CommonStructureField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonArrayField<LibObjT> CommonField<LibObjT>::asArray() const noexcept
 {
-    BT_ASSERT_DBG(this->isArray());
     return CommonArrayField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonDynamicArrayField<LibObjT> CommonField<LibObjT>::asDynamicArray() const noexcept
 {
-    BT_ASSERT_DBG(this->isDynamicArray());
     return CommonDynamicArrayField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonOptionField<LibObjT> CommonField<LibObjT>::asOption() const noexcept
 {
-    BT_ASSERT_DBG(this->isOption());
     return CommonOptionField<LibObjT> {this->libObjPtr()};
 }
 
 template <typename LibObjT>
 CommonVariantField<LibObjT> CommonField<LibObjT>::asVariant() const noexcept
 {
-    BT_ASSERT_DBG(this->isVariant());
     return CommonVariantField<LibObjT> {this->libObjPtr()};
 }
 
This page took 0.037356 seconds and 4 git commands to generate.