cpp-common/bt2: do not use `bpstd::string_view`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 13 Nov 2023 20:43:31 +0000 (15:43 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
Using `bpstd::string_view` in those contexts was a mistake: building a
`bpstd::string_view` object from a null-terminated string requires to
get its length (think strlen()), and an optional string view requires to
return `nonstd::optional<bpstd::string_view>` which means checking the
library pointer first.

All this means it's not a zero cost, and zero cost is what those C++
bindings aim to be.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib51fb9e326c4b4c450a381631b9db69f6cd0530b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11370
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/cpp-common/bt2/clock-class.hpp
src/cpp-common/bt2/field-class.hpp
src/cpp-common/bt2/field.hpp
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2/value.hpp

index bd127dfa673a0e58cde6f43597111884520d13a6..72064f844f49bf6ce36f4e81f2a57682ab078b49 100644 (file)
@@ -14,7 +14,6 @@
 #include <babeltrace2/babeltrace.h>
 
 #include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
 #include "cpp-common/uuid-view.hpp"
 
 #include "borrowed-object.hpp"
@@ -189,15 +188,9 @@ public:
         this->name(name.data());
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
-        const auto name = bt_clock_class_get_name(this->libObjPtr());
-
-        if (name) {
-            return name;
-        }
-
-        return nonstd::nullopt;
+        return bt_clock_class_get_name(this->libObjPtr());
     }
 
     void description(const char * const description) const
@@ -216,15 +209,9 @@ public:
         this->description(description.data());
     }
 
-    nonstd::optional<bpstd::string_view> description() const noexcept
+    const char *description() const noexcept
     {
-        const auto description = bt_clock_class_get_description(this->libObjPtr());
-
-        if (description) {
-            return description;
-        }
-
-        return nonstd::nullopt;
+        return bt_clock_class_get_description(this->libObjPtr());
     }
 
     void uuid(const std::uint8_t * const uuid) const noexcept
index 3890bc2204b2d49728347b6c78547c3440ee17db..f299690edce680bd5685e3a4d908dfd34cbac769 100644 (file)
@@ -14,7 +14,6 @@
 
 #include "common/assert.h"
 #include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
 
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
@@ -695,7 +694,7 @@ public:
             internal::ConstEnumerationFieldClassMappingSpec<LibObjT>::ranges(this->libObjPtr())};
     }
 
-    bpstd::string_view label() const noexcept
+    const char *label() const noexcept
     {
         return internal::ConstEnumerationFieldClassMappingSpec<LibObjT>::label(this->libObjPtr());
     }
@@ -1021,7 +1020,7 @@ public:
         return CommonStructureFieldClassMember<const bt_field_class_structure_member> {*this};
     }
 
-    bpstd::string_view name() const noexcept
+    const char *name() const noexcept
     {
         return bt_field_class_structure_member_get_name(this->libObjPtr());
     }
@@ -1919,15 +1918,9 @@ public:
         return CommonVariantFieldClassOption<const bt_field_class_variant_option> {*this};
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
-        const auto name = bt_field_class_variant_option_get_name(this->libObjPtr());
-
-        if (name) {
-            return name;
-        }
-
-        return nonstd::nullopt;
+        return bt_field_class_variant_option_get_name(this->libObjPtr());
     }
 
     _FieldClass fieldClass() const noexcept
@@ -2066,7 +2059,7 @@ public:
         return ConstVariantFieldClassOption {_Spec::asBaseOption(this->libObjPtr())};
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
         return this->asBaseOption().name();
     }
index cb9359e78c6bf114b12f71328b76db3fd1824947..32ea17c777d520c8862dbc589aa448a8234b8aff 100644 (file)
@@ -14,7 +14,6 @@
 
 #include "common/assert.h"
 #include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
 
 #include "borrowed-object.hpp"
 #include "field-class.hpp"
@@ -598,7 +597,7 @@ public:
         return _mLen;
     }
 
-    bpstd::string_view operator[](const std::uint64_t index) const noexcept
+    const char *operator[](const std::uint64_t index) const noexcept
     {
         return _mLabels[index];
     }
index 14863e184b0e122791bb8277642ed18525e0ea8a..b6f349be9a6f7545292078460e7f6c2bdd3c94ff 100644 (file)
@@ -13,7 +13,6 @@
 #include <babeltrace2/babeltrace.h>
 
 #include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
 
 #include "borrowed-object.hpp"
 #include "clock-class.hpp"
@@ -489,15 +488,9 @@ public:
         this->name(name.data());
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
-        const auto name = bt_stream_get_name(this->libObjPtr());
-
-        if (name) {
-            return name;
-        }
-
-        return nonstd::nullopt;
+        return bt_stream_get_name(this->libObjPtr());
     }
 
     template <typename LibValT>
@@ -644,7 +637,7 @@ public:
 
     struct ConstEnvironmentEntry
     {
-        bpstd::string_view name;
+        const char *name;
         ConstValue value;
     };
 
@@ -687,15 +680,9 @@ public:
         this->name(name.data());
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
-        const auto name = bt_trace_get_name(this->libObjPtr());
-
-        if (name) {
-            return name;
-        }
-
-        return nonstd::nullopt;
+        return bt_trace_get_name(this->libObjPtr());
     }
 
     void uuid(const bt2_common::UuidView& uuid) const noexcept
@@ -1011,15 +998,9 @@ public:
         this->name(name.data());
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
-        const auto name = bt_event_class_get_name(this->libObjPtr());
-
-        if (name) {
-            return name;
-        }
-
-        return nonstd::nullopt;
+        return bt_event_class_get_name(this->libObjPtr());
     }
 
     void logLevel(const LogLevel logLevel) const noexcept
@@ -1058,15 +1039,9 @@ public:
         this->emfUri(emfUri.data());
     }
 
-    nonstd::optional<bpstd::string_view> emfUri() const noexcept
+    const char *emfUri() const noexcept
     {
-        const auto emfUri = bt_event_class_get_emf_uri(this->libObjPtr());
-
-        if (emfUri) {
-            return emfUri;
-        }
-
-        return nonstd::nullopt;
+        return bt_event_class_get_emf_uri(this->libObjPtr());
     }
 
     void payloadFieldClass(const StructureFieldClass fc) const
@@ -1381,15 +1356,9 @@ public:
         this->name(name.data());
     }
 
-    nonstd::optional<bpstd::string_view> name() const noexcept
+    const char *name() const noexcept
     {
-        const auto name = bt_stream_class_get_name(this->libObjPtr());
-
-        if (name) {
-            return name;
-        }
-
-        return nonstd::nullopt;
+        return bt_stream_class_get_name(this->libObjPtr());
     }
 
     void assignsAutomaticEventClassId(const bool val) const noexcept
index cfbc92b52c4d1904c125ac56a4a9e2b60a6ac136..54d33bcc5c3e0ba2991cf0f01d4a0f1f7064f6e3 100644 (file)
@@ -16,7 +16,6 @@
 #include "common/assert.h"
 #include "common/common.h"
 #include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
 
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
@@ -1139,7 +1138,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 bpstd::string_view&, ObjT)>;
+using CommonMapValueForEachUserFunc = std::function<void(const char *, ObjT)>;
 
 /*
  * Template of a function to be passed to bt_value_map_foreach_entry()
This page took 0.029391 seconds and 4 git commands to generate.