From 5cc5088c99cfef4584388872e51a92b8f2268ed3 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 13 Nov 2023 15:43:31 -0500 Subject: [PATCH] cpp-common/bt2: do not use `bpstd::string_view` 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` 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 Change-Id: Ib51fb9e326c4b4c450a381631b9db69f6cd0530b Reviewed-on: https://review.lttng.org/c/babeltrace/+/11370 Reviewed-by: Simon Marchi --- src/cpp-common/bt2/clock-class.hpp | 21 +++--------- src/cpp-common/bt2/field-class.hpp | 17 +++------- src/cpp-common/bt2/field.hpp | 3 +- src/cpp-common/bt2/trace-ir.hpp | 53 +++++++----------------------- src/cpp-common/bt2/value.hpp | 3 +- 5 files changed, 22 insertions(+), 75 deletions(-) diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index bd127dfa..72064f84 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -14,7 +14,6 @@ #include #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 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 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 diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index 3890bc22..f299690e 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -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::ranges(this->libObjPtr())}; } - bpstd::string_view label() const noexcept + const char *label() const noexcept { return internal::ConstEnumerationFieldClassMappingSpec::label(this->libObjPtr()); } @@ -1021,7 +1020,7 @@ public: return CommonStructureFieldClassMember {*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 {*this}; } - nonstd::optional 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 name() const noexcept + const char *name() const noexcept { return this->asBaseOption().name(); } diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index cb9359e7..32ea17c7 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -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]; } diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 14863e18..b6f349be 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -13,7 +13,6 @@ #include #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 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 @@ -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 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 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 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 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 diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index cfbc92b5..54d33bcc 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -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 : public ArrayValueTypeDescr * First argument is the entry's key, second is its value. */ template -using CommonMapValueForEachUserFunc = std::function; +using CommonMapValueForEachUserFunc = std::function; /* * Template of a function to be passed to bt_value_map_foreach_entry() -- 2.34.1