From 15030982ca99a8930d5c13b7fdff5dcba204a551 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 16 Jan 2024 13:31:06 -0500 Subject: [PATCH] cpp-common/bt2: use bt2c::CStringView in parameters and return values throughout From what I saw, at -O2 with gcc, passing and returning CStringViews generates the same code as passing and returning `const char *`. Change the `cpp-common/bt2` code to take a return C strings using CStringViews when possible, which is more convenient than `const char *`. Since std::string is implicitly convertible to CStringView, there is generaly no need to have separate overloads that take std::strings. I left the `CommonStringField::append(const std::string &)` overload, because this one has an advantage over the `CStringView` overload: it uses the size of the `std::string`, which is cheaper than calling `std::strlen`. There are some cases where leaving the `const char *` overload is necessary. For instance, when there is also a `bool` overload. In that case, passing a `const char *` to the function would prefer the `bool` overload over the `CStringView` overload, which is not what we want. Change-Id: I4c995c995da3edd2f0e9f7034053d52cb1541bb0 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/11690 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/cpp-common/bt2/clock-class.hpp | 14 +- src/cpp-common/bt2/component-port.hpp | 12 +- src/cpp-common/bt2/field-class.hpp | 53 +++----- src/cpp-common/bt2/field.hpp | 28 +--- src/cpp-common/bt2/plugin-dev.hpp | 85 +++--------- src/cpp-common/bt2/raw-value-proxy.hpp | 21 --- src/cpp-common/bt2/self-component-port.hpp | 119 +++------------- src/cpp-common/bt2/trace-ir.hpp | 66 ++------- src/cpp-common/bt2/value.hpp | 149 ++++----------------- 9 files changed, 106 insertions(+), 441 deletions(-) diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index 6c8c6638..9e66cbf4 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -173,7 +173,7 @@ public: return static_cast(bt_clock_class_origin_is_unix_epoch(this->libObjPtr())); } - void name(const char * const name) const + void name(const bt2c::CStringView name) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); @@ -184,17 +184,12 @@ public: } } - void name(const std::string& name) const - { - this->name(name.data()); - } - bt2c::CStringView name() const noexcept { return bt_clock_class_get_name(this->libObjPtr()); } - void description(const char * const description) const + void description(const bt2c::CStringView description) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); @@ -205,11 +200,6 @@ public: } } - void description(const std::string& description) const - { - this->description(description.data()); - } - bt2c::CStringView description() const noexcept { return bt_clock_class_get_description(this->libObjPtr()); diff --git a/src/cpp-common/bt2/component-port.hpp b/src/cpp-common/bt2/component-port.hpp index 1ab7a850..110c9b62 100644 --- a/src/cpp-common/bt2/component-port.hpp +++ b/src/cpp-common/bt2/component-port.hpp @@ -262,8 +262,7 @@ public: } Port operator[](std::uint64_t index) const noexcept; - Port operator[](const char *name) const noexcept; - Port operator[](const std::string& name) const noexcept; + Port operator[](bt2c::CStringView name) const noexcept; Iterator begin() const noexcept; Iterator end() const noexcept; }; @@ -505,18 +504,11 @@ ConstComponentPorts::operator[](const std::uint64_t index) c template typename ConstComponentPorts::Port -ConstComponentPorts::operator[](const char * const name) const noexcept +ConstComponentPorts::operator[](const bt2c::CStringView name) const noexcept { return Port {_Spec::portByName(this->libObjPtr(), name)}; } -template -typename ConstComponentPorts::Port -ConstComponentPorts::operator[](const std::string& name) const noexcept -{ - return (*this)[name.data()]; -} - template typename ConstComponentPorts::Iterator ConstComponentPorts::begin() const noexcept diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index 435cf46c..b0420c4f 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -853,18 +853,13 @@ public: this->libObjPtr(), index)}; } - OptionalBorrowedObject operator[](const char * const label) const noexcept + OptionalBorrowedObject operator[](const bt2c::CStringView label) const noexcept { return internal::CommonEnumerationFieldClassSpec::mappingByLabel( this->libObjPtr(), label); } - OptionalBorrowedObject operator[](const std::string& label) const noexcept - { - return (*this)[label.data()]; - } - - void addMapping(const char * const label, const typename Mapping::RangeSet ranges) const + void addMapping(const bt2c::CStringView label, const typename Mapping::RangeSet ranges) const { const auto status = internal::CommonEnumerationFieldClassSpec::addMapping( this->libObjPtr(), label, ranges.libObjPtr()); @@ -874,11 +869,6 @@ public: } } - void addMapping(const std::string& label, const typename Mapping::RangeSet ranges) const - { - this->addMapping(label.data(), ranges); - } - Iterator begin() const noexcept { return Iterator {*this, 0}; @@ -1146,7 +1136,7 @@ public: return CommonStructureFieldClass {*this}; } - void appendMember(const char * const name, const FieldClass fc) const + void appendMember(const bt2c::CStringView name, const FieldClass fc) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstStructureFieldClass`."); @@ -1159,11 +1149,6 @@ public: } } - void appendMember(const std::string& name, const FieldClass fc) const - { - this->appendMember(name.data(), fc); - } - std::uint64_t length() const noexcept { return bt_field_class_structure_get_member_count(this->libObjPtr()); @@ -1185,17 +1170,12 @@ public: this->libObjPtr(), index)}; } - OptionalBorrowedObject operator[](const char * const name) const noexcept + OptionalBorrowedObject operator[](const bt2c::CStringView name) const noexcept { return internal::CommonStructureFieldClassSpec::memberByName(this->libObjPtr(), name); } - OptionalBorrowedObject operator[](const std::string& name) const noexcept - { - return (*this)[name.data()]; - } - Shared shared() const noexcept { return Shared::createWithRef(*this); @@ -2176,17 +2156,12 @@ public: this->libObjPtr(), index)}; } - OptionalBorrowedObject