cpp-common/bt2: add `CommonStringField::length` method
[babeltrace.git] / src / cpp-common / bt2 / field.hpp
index cb9359e78c6bf114b12f71328b76db3fd1824947..e051a806e4ab2ca1104bcbf42e2d24e93db78e2b 100644 (file)
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
-#include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
+#include "cpp-common/bt2c/c-string-view.hpp"
 
 #include "borrowed-object.hpp"
 #include "field-class.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "raw-value-proxy.hpp"
 
 namespace bt2 {
@@ -99,13 +99,13 @@ private:
     using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
 
 protected:
-    using typename BorrowedObject<LibObjT>::_LibObjPtr;
     using _ThisCommonField = CommonField<LibObjT>;
 
 public:
+    using typename BorrowedObject<LibObjT>::LibObjPtr;
     using Class = internal::DepFc<LibObjT>;
 
-    explicit CommonField(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
+    explicit CommonField(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
     {
     }
 
@@ -255,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());
     }
@@ -328,13 +328,13 @@ template <typename LibObjT>
 class CommonBitArrayField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
+    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());
     }
@@ -418,14 +418,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>;
 
-    explicit CommonUnsignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonUnsignedIntegerField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isUnsignedInteger());
@@ -504,14 +504,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>;
 
-    explicit CommonSignedIntegerField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonSignedIntegerField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isSignedInteger());
@@ -598,7 +598,7 @@ public:
         return _mLen;
     }
 
-    bpstd::string_view operator[](const std::uint64_t index) const noexcept
+    bt2c::CStringView operator[](const std::uint64_t index) const noexcept
     {
         return _mLabels[index];
     }
@@ -613,13 +613,14 @@ class CommonUnsignedEnumerationField final : public CommonUnsignedIntegerField<L
 {
 private:
     using typename CommonUnsignedIntegerField<LibObjT>::_ThisCommonUnsignedIntegerField;
-    using typename CommonField<LibObjT>::_LibObjPtr;
 
 public:
+    using typename CommonField<LibObjT>::LibObjPtr;
+
     using Class = internal::DepType<LibObjT, UnsignedEnumerationFieldClass,
                                     ConstUnsignedEnumerationFieldClass>;
 
-    explicit CommonUnsignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonUnsignedEnumerationField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonUnsignedIntegerField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isUnsignedEnumeration());
@@ -693,13 +694,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 =
         internal::DepType<LibObjT, SignedEnumerationFieldClass, ConstSignedEnumerationFieldClass>;
 
-    explicit CommonSignedEnumerationField(const _LibObjPtr libObjPtr) noexcept :
+    explicit CommonSignedEnumerationField(const LibObjPtr libObjPtr) noexcept :
         _ThisCommonSignedIntegerField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isSignedEnumeration());
@@ -771,13 +773,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());
@@ -849,13 +851,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());
@@ -927,13 +929,13 @@ template <typename LibObjT>
 class CommonStringField final : public CommonField<LibObjT>
 {
 private:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
-    using Value = const char *;
+    using typename CommonField<LibObjT>::LibObjPtr;
+    using Value = bt2c::CStringView;
 
-    explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
+    explicit CommonStringField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isString());
     }
@@ -955,29 +957,24 @@ public:
         return CommonStringField<const bt_field> {*this};
     }
 
-    RawStringValueProxy<CommonStringField> operator*() const noexcept
+    RawValueProxy<CommonStringField> operator*() const noexcept
     {
-        return RawStringValueProxy<CommonStringField> {*this};
+        return RawValueProxy<CommonStringField> {*this};
     }
 
-    void value(const char * const val) const
+    void 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);
+        const auto status = bt_field_string_set_value(this->libObjPtr(), *val);
 
         if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
             throw MemoryError {};
         }
     }
 
-    void value(const std::string& val) const
-    {
-        this->value(val.data());
-    }
-
-    void append(const char * const begin, const std::uint64_t len) const
+    void append(const bt2c::CStringView begin, const std::uint64_t len) const
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstStringField`.");
@@ -989,6 +986,11 @@ public:
         }
     }
 
+    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());
@@ -1002,10 +1004,15 @@ public:
         bt_field_string_clear(this->libObjPtr());
     }
 
-    const char *value() const noexcept
+    Value value() const noexcept
     {
         return bt_field_string_get_value(this->libObjPtr());
     }
+
+    std::uint64_t length() const noexcept
+    {
+        return bt_field_string_get_length(this->libObjPtr());
+    }
 };
 
 using StringField = CommonStringField<bt_field>;
@@ -1071,15 +1078,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 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());
     }
@@ -1117,20 +1123,10 @@ public:
         return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
     }
 
-    nonstd::optional<CommonField<LibObjT>> operator[](const char * const 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) const noexcept
+    OptionalBorrowedObject<CommonField<LibObjT>>
+    operator[](const bt2c::CStringView name) const noexcept
     {
-        return (*this)[name.data()];
+        return _Spec::memberFieldByName(this->libObjPtr(), name);
     }
 };
 
@@ -1190,13 +1186,13 @@ private:
     using _Spec = internal::CommonArrayFieldSpec<LibObjT>;
 
 protected:
-    using typename CommonField<LibObjT>::_LibObjPtr;
     using _ThisCommonArrayField = CommonArrayField<LibObjT>;
 
 public:
+    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());
     }
@@ -1261,11 +1257,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());
@@ -1358,14 +1355,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 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());
     }
@@ -1405,15 +1402,9 @@ public:
         return this->field().has_value();
     }
 
-    nonstd::optional<CommonField<LibObjT>> field() const 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());
     }
 };
 
@@ -1467,14 +1458,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 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());
     }
This page took 0.028388 seconds and 4 git commands to generate.