From 4ad359db556d7a6d192e61f086e67a15abe2c85e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jafar=20Uru=C3=A7?= Date: Thu, 14 Nov 2024 16:39:10 +0100 Subject: [PATCH] Add Bytes serialization support for std::span (#273) * Add Bytes serialization support for std::span * Remove #if __cplusplus >= 202002L from includes * clang-format --- examples/universal/z_bytes.cxx | 12 +++++++++++- include/zenoh/api/ext/serialization.hxx | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/examples/universal/z_bytes.cxx b/examples/universal/z_bytes.cxx index 9bbda18..cc8646f 100644 --- a/examples/universal/z_bytes.cxx +++ b/examples/universal/z_bytes.cxx @@ -91,6 +91,16 @@ int _main(int argc, char** argv) { assert(input == output); } +// Span +#if __cplusplus >= 202002L + { + double input[] = {1.0, 2.0, 3.0, 4.0}; + const auto payload = ext::serialize(std::span(input)); + const auto output = ext::deserialize>(payload); + assert(std::equal(std::begin(input), std::end(input), std::begin(output))); + } +#endif + // Serializer, deserializer (for struct or tuple serialization) { // serialization @@ -171,4 +181,4 @@ int main(int argc, char** argv) { } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; } -} \ No newline at end of file +} diff --git a/include/zenoh/api/ext/serialization.hxx b/include/zenoh/api/ext/serialization.hxx index 416aa47..b235462 100644 --- a/include/zenoh/api/ext/serialization.hxx +++ b/include/zenoh/api/ext/serialization.hxx @@ -13,6 +13,9 @@ #pragma once +#if __cplusplus >= 202002L +#include +#endif #include #include #include @@ -236,6 +239,13 @@ bool __zenoh_serialize_with_serializer(zenoh::ext::Serializer& serializer, const return __serialize_sequence_with_serializer(serializer, value.begin(), value.end(), value.size(), err); } +#if __cplusplus >= 202002L +template +bool __zenoh_serialize_with_serializer(zenoh::ext::Serializer& serializer, std::span value, ZResult* err) { + return __serialize_sequence_with_serializer(serializer, value.begin(), value.end(), value.size(), err); +} +#endif + template bool serialize_with_serializer(zenoh::ext::Serializer& serializer, const T& t, ZResult* err) { return __zenoh_serialize_with_serializer(serializer, t, err);