[systemd-devel] Extend sd_bus_message_append_basic to take length of a string

Stanislav Angelovič stanislav.angelovic at protonmail.com
Mon Mar 20 16:01:17 UTC 2023


Hi folks,

Does sd-bus API provide a way to append a char string of a basic type (be it SD_BUS_TYPE_STRING​, SD_BUS_TYPE_OBJECT_PATH​, or SD_BUS_TYPE_SIGNATURE​ for that matter) to a D-Bus message while allowing the caller to explicitly specify the length of that char string?

sd_bus_message_append_basic()​ function takes a pointer only, and calls strlen()​ on that pointer.

If there is no such API, could we add a new function with string size as an additional parameter. It would make the API more usable and general and, as a specific use case, help C++ code using sd-bus API to append char strings of type std::string_view​ to the D-Bus message without having to convert a std::string_view​ to a std::string​ just to get null-terminated string which can then be passed safely to sd_bus_message_append_basic()​. Right now we have to do that conversion which defeats the fundamental purpose of std::string_view​ type in C++ standard library.

For example something along these lines:

int sd_bus_message_append_basic_string_n(sd_bus_message *m, char type, const void *p, size_t len);​

Which would work only for the above-mentioned three D-Bus string types.

And in C++ code (sdbus-c++ wrapper, for example), we could call it simply like this:

std::string_view view{...};
sd_bus_message_append_basic_string_n(msg, SD_BUS_TYPE_STRING, view.data(), view.length());

What do you think of this proposal? Could we extend sd-bus API along these lines? Would you do that or shall I create a PR?

Kind regards,

Stanislav.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20230320/0929a4cf/attachment.htm>


More information about the systemd-devel mailing list