cpp-common/bt2: return `bt2c::CStringView` instead of `const char *`
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 11 Dec 2023 21:10:01 +0000 (16:10 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
This patch makes any method in `cpp-common/bt2` return
`bt2c::CStringView` instead of `const char *` when possible to improve
the usability of the wrapping API.

In `bt2::CommonStringField` and `bt2::CommonStringValue`, I didn't
bother having a different raw value type to set vs. get, therefore the
value() getter returns `bt2c::CStringView` while the value() setter has
overloads for `bt2c::CStringView`, `const char *`, and
`const std::string&`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iab7c7ea9a97035b9bea018f918a5d52516ab676c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11486
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2/clock-class.hpp
src/cpp-common/bt2/component-port.hpp
src/cpp-common/bt2/field-class.hpp
src/cpp-common/bt2/field.hpp
src/cpp-common/bt2/plugin-dev.hpp
src/cpp-common/bt2/raw-value-proxy.hpp
src/cpp-common/bt2/self-component-class.hpp
src/cpp-common/bt2/self-component-port.hpp
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2/value.hpp

index fd2683bd38d8362091288230c76685e5e3b2eea5..6c8c66380588cff939459947c95cec9895ba1259 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <babeltrace2/babeltrace.h>
 
+#include "cpp-common/bt2c/c-string-view.hpp"
 #include "cpp-common/bt2c/uuid.hpp"
 #include "cpp-common/bt2s/optional.hpp"
 
@@ -188,7 +189,7 @@ public:
         this->name(name.data());
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_clock_class_get_name(this->libObjPtr());
     }
@@ -209,7 +210,7 @@ public:
         this->description(description.data());
     }
 
-    const char *description() const noexcept
+    bt2c::CStringView description() const noexcept
     {
         return bt_clock_class_get_description(this->libObjPtr());
     }
index b82398481cfa352c7513ffddbc814125c7483436..1ab7a850af271a84440387fb11c63e34f1a04be6 100644 (file)
@@ -14,6 +14,8 @@
 
 #include "logging.hpp"
 
+#include "cpp-common/bt2c/c-string-view.hpp"
+
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
 #include "shared-object.hpp"
@@ -93,7 +95,7 @@ public:
         return static_cast<bool>(bt_component_is_sink(this->libObjPtr()));
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_component_get_name(this->libObjPtr());
     }
@@ -122,7 +124,7 @@ protected:
     }
 
 public:
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return this->_constComponent().name();
     }
@@ -467,7 +469,7 @@ public:
     {
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_port_get_name(this->_libConstPortPtr());
     }
index fe4cea469ffd33da534558f649fcbf725267f0a2..435cf46ccb3100e29372057a91540b4c9f3c5379 100644 (file)
@@ -13,6 +13,7 @@
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
+#include "cpp-common/bt2c/c-string-view.hpp"
 #include "cpp-common/bt2s/optional.hpp"
 
 #include "borrowed-object-iterator.hpp"
@@ -696,7 +697,7 @@ public:
             internal::ConstEnumerationFieldClassMappingSpec<LibObjT>::ranges(this->libObjPtr())};
     }
 
-    const char *label() const noexcept
+    bt2c::CStringView label() const noexcept
     {
         return internal::ConstEnumerationFieldClassMappingSpec<LibObjT>::label(this->libObjPtr());
     }
@@ -1016,7 +1017,7 @@ public:
         return CommonStructureFieldClassMember<const bt_field_class_structure_member> {*this};
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_field_class_structure_member_get_name(this->libObjPtr());
     }
@@ -1906,7 +1907,7 @@ public:
         return CommonVariantFieldClassOption<const bt_field_class_variant_option> {*this};
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_field_class_variant_option_get_name(this->libObjPtr());
     }
@@ -2048,7 +2049,7 @@ public:
         return ConstVariantFieldClassOption {_Spec::asBaseOption(this->libObjPtr())};
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return this->asBaseOption().name();
     }
index 2c40ae3307a46a3420faa29e20c184606eb32e0f..40fe3f02f400080ed7d3ab9303ac305fee8ef1ef 100644 (file)
@@ -13,6 +13,7 @@
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
+#include "cpp-common/bt2c/c-string-view.hpp"
 
 #include "borrowed-object.hpp"
 #include "field-class.hpp"
@@ -597,7 +598,7 @@ public:
         return _mLen;
     }
 
-    const char *operator[](const std::uint64_t index) const noexcept
+    bt2c::CStringView operator[](const std::uint64_t index) const noexcept
     {
         return _mLabels[index];
     }
@@ -932,7 +933,7 @@ private:
 
 public:
     using typename CommonField<LibObjT>::LibObjPtr;
-    using Value = const char *;
+    using Value = bt2c::CStringView;
 
     explicit CommonStringField(const LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
@@ -961,21 +962,26 @@ public:
         return RawStringValueProxy<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 char * const val) const
+    {
+        this->value(bt2c::CStringView {val});
+    }
+
     void value(const std::string& val) const
     {
-        this->value(val.data());
+        this->value(bt2c::CStringView {val.data()});
     }
 
     void append(const char * const begin, const std::uint64_t len) const
@@ -990,6 +996,11 @@ public:
         }
     }
 
+    void append(const char * const val) const
+    {
+        this->append(val, std::strlen(val));
+    }
+
     void append(const std::string& val) const
     {
         this->append(val.data(), val.size());
@@ -1003,7 +1014,7 @@ public:
         bt_field_string_clear(this->libObjPtr());
     }
 
-    const char *value() const noexcept
+    Value value() const noexcept
     {
         return bt_field_string_get_value(this->libObjPtr());
     }
index 220d1061cc0bda624738bc96379d052ee7ba3cdf..2f6e2fff526e3ba05028f25673ca09b11491828b 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <babeltrace2/babeltrace.h>
 
+#include "cpp-common/bt2c/c-string-view.hpp"
 #include "cpp-common/bt2c/logging.hpp"
 #include "cpp-common/vendor/fmt/core.h"
 
@@ -471,7 +472,7 @@ protected:
     }
 
 protected:
-    const char *_name() const noexcept
+    bt2c::CStringView _name() const noexcept
     {
         return _mSelfComp.name();
     }
index 2153856ffbb498a993c570ddac2a92e353a2e9f0..2bb5ba8553284bb4027fc23877f279bca346791b 100644 (file)
@@ -9,6 +9,8 @@
 
 #include <string>
 
+#include "cpp-common/bt2c/c-string-view.hpp"
+
 namespace bt2 {
 
 template <typename ObjT>
@@ -45,9 +47,15 @@ public:
     {
     }
 
+    RawStringValueProxy& operator=(const char * const rawVal)
+    {
+        RawValueProxy<ObjT>::operator=(bt2c::CStringView {rawVal});
+        return *this;
+    }
+
     RawStringValueProxy& operator=(const std::string& rawVal)
     {
-        RawValueProxy<ObjT>::operator=(rawVal.data());
+        RawValueProxy<ObjT>::operator=(bt2c::CStringView {rawVal.data()});
         return *this;
     }
 };
index 4d597f79883969578e76f5e7ae18eaad156e1e37..9a0275fd3d5e4249d90d21b8e1eecb22ca993e8e 100644 (file)
@@ -9,6 +9,8 @@
 
 #include <babeltrace2/babeltrace.h>
 
+#include "cpp-common/bt2c/c-string-view.hpp"
+
 #include "borrowed-object.hpp"
 
 namespace bt2 {
@@ -36,17 +38,17 @@ public:
     {
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_component_class_get_name(this->_libCompClsPtr());
     }
 
-    const char *description() const noexcept
+    bt2c::CStringView description() const noexcept
     {
         return bt_component_class_get_description(this->_libCompClsPtr());
     }
 
-    const char *help() const noexcept
+    bt2c::CStringView help() const noexcept
     {
         return bt_component_class_get_help(this->_libCompClsPtr());
     }
index 82201008676bb2553c69d622de4bc853fcd9a276..fff4aea76a37c7eaeacd871b4d1aef055e4c7379 100644 (file)
@@ -15,6 +15,7 @@
 #include "logging.hpp"
 
 #include "common/assert.h"
+#include "cpp-common/bt2c/c-string-view.hpp"
 
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
@@ -78,7 +79,7 @@ public:
         return this->asConstComponent().isSink();
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return this->asConstComponent().name();
     }
@@ -141,7 +142,7 @@ protected:
     }
 
 public:
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return this->_selfComponent().name();
     }
@@ -516,7 +517,7 @@ public:
             internal::SelfComponentPortSpec<LibSelfCompPortT>::asConstPort(this->libObjPtr())};
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return this->asConstPort().name();
     }
index a043f3cc5c38523feefcb456ed2cf2537a9ec21c..74ad36790ae93ce05741c62d71262b1260f2729b 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <babeltrace2/babeltrace.h>
 
+#include "cpp-common/bt2c/c-string-view.hpp"
 #include "cpp-common/bt2s/optional.hpp"
 
 #include "borrowed-object.hpp"
@@ -460,7 +461,7 @@ public:
         this->name(name.data());
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_stream_get_name(this->libObjPtr());
     }
@@ -609,7 +610,7 @@ public:
 
     struct ConstEnvironmentEntry
     {
-        const char *name;
+        bt2c::CStringView name;
         ConstValue value;
     };
 
@@ -652,7 +653,7 @@ public:
         this->name(name.data());
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_trace_get_name(this->libObjPtr());
     }
@@ -957,7 +958,7 @@ public:
         this->name(name.data());
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_event_class_get_name(this->libObjPtr());
     }
@@ -997,7 +998,7 @@ public:
         this->emfUri(emfUri.data());
     }
 
-    const char *emfUri() const noexcept
+    bt2c::CStringView emfUri() const noexcept
     {
         return bt_event_class_get_emf_uri(this->libObjPtr());
     }
@@ -1301,7 +1302,7 @@ public:
         this->name(name.data());
     }
 
-    const char *name() const noexcept
+    bt2c::CStringView name() const noexcept
     {
         return bt_stream_class_get_name(this->libObjPtr());
     }
index 4dc4019a8677ffab3d36f35043217331eddbcc05..9b12211c6221ca31da58da6f6e24efe612883be3 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "common/assert.h"
 #include "common/common.h"
+#include "cpp-common/bt2c/c-string-view.hpp"
 
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
@@ -100,6 +101,7 @@ public:
     operator std::uint64_t() const noexcept;
     operator double() const noexcept;
     operator const char *() const noexcept;
+    operator bt2c::CStringView() const noexcept;
 
 private:
     ValueObjT _mObj;
@@ -337,7 +339,8 @@ template <typename ValueObjT>
 CommonValueRawValueProxy<ValueObjT>&
 CommonValueRawValueProxy<ValueObjT>::operator=(const std::string& rawVal)
 {
-    return *this = rawVal.data();
+    _mObj.asString().value(rawVal);
+    return *this;
 }
 
 template <typename ValueObjT>
@@ -370,6 +373,12 @@ CommonValueRawValueProxy<ValueObjT>::operator const char *() const noexcept
     return _mObj.asString().value();
 }
 
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>::operator bt2c::CStringView() const noexcept
+{
+    return _mObj.asString().value();
+}
+
 namespace internal {
 
 struct ValueTypeDescr
@@ -814,7 +823,7 @@ private:
 public:
     using typename CommonValue<LibObjT>::LibObjPtr;
     using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
-    using Value = const char *;
+    using Value = bt2c::CStringView;
 
     explicit CommonStringValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
     {
@@ -861,19 +870,24 @@ public:
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstStringValue`.");
 
-        const auto status = bt_value_string_set(this->libObjPtr(), val);
+        const auto status = bt_value_string_set(this->libObjPtr(), *val);
 
         if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
             throw MemoryError {};
         }
     }
 
+    void value(const char * const val) const
+    {
+        this->value(bt2c::CStringView {val});
+    }
+
     void value(const std::string& val) const
     {
-        this->value(val.data());
+        this->value(bt2c::CStringView {val.data()});
     }
 
-    const char *value() const noexcept
+    Value value() const noexcept
     {
         return bt_value_string_get(this->libObjPtr());
     }
@@ -1138,7 +1152,7 @@ struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
  * First argument is the entry's key, second is its value.
  */
 template <typename ObjT>
-using CommonMapValueForEachUserFunc = std::function<void(const char *, ObjT)>;
+using CommonMapValueForEachUserFunc = std::function<void(bt2c::CStringView, ObjT)>;
 
 /*
  * Template of a function to be passed to bt_value_map_foreach_entry()
This page took 0.034629 seconds and 4 git commands to generate.