Add support for appending std::string_view
std::string_view is used more in the project now that c++17 is
available. It should be allowed as a base type in serialization of dbus
interfaces.
To avoid an extra string copy, this function makes use of
sd_bus_message_append_string_iovec, which allows appending in string
pieces that might not be null terminated. This function is piped
through the test framework, and interfaces to match.
Change-Id: Iee3e2cea9759fa9759cec98ab30c12c822aa3b73
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/test/message/append.cpp b/test/message/append.cpp
index 59e83b4..b0ef54e 100644
--- a/test/message/append.cpp
+++ b/test/message/append.cpp
@@ -27,6 +27,12 @@
using testing::SafeMatcherCast;
using testing::StrEq;
+MATCHER_P(iovec_equal, match_string, "")
+{
+ const char* start = std::bit_cast<char*>(arg->iov_base);
+ return std::string(start, arg->iov_len) == match_string;
+}
+
class AppendTest : public testing::Test
{
protected:
@@ -64,6 +70,13 @@
SafeMatcherCast<const char*>(StrEq(str)))))
.WillOnce(Return(0));
}
+ void expect_basic_string_iovec(const char* str, size_t size)
+ {
+ std::string tmp = {str, size};
+ EXPECT_CALL(mock, sd_bus_message_append_string_iovec(
+ nullptr, iovec_equal(tmp), 1))
+ .WillOnce(Return(0));
+ }
void expect_open_container(char type, const char* contents)
{
@@ -187,6 +200,20 @@
new_message().append(std::move(s));
}
+TEST_F(AppendTest, LValueStringView)
+{
+ std::string_view s{"asdf"};
+ expect_basic_string_iovec(s.data(), s.size());
+ new_message().append(s);
+}
+
+TEST_F(AppendTest, RValueStringView)
+{
+ std::string_view s{"asdf"};
+ expect_basic_string_iovec(s.data(), s.size());
+ new_message().append(std::string_view{"asdf"});
+}
+
TEST_F(AppendTest, ObjectPath)
{
sdbusplus::message::object_path o{"/asdf"};