Skip to content

Commit

Permalink
Add Bytes serialization support for std::span (#273)
Browse files Browse the repository at this point in the history
* Add Bytes serialization support for std::span

* Remove #if __cplusplus >= 202002L from includes

* clang-format
  • Loading branch information
JafarAbdi authored Nov 14, 2024
1 parent 730f993 commit 4ad359d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion examples/universal/z_bytes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<double>>(payload);
assert(std::equal(std::begin(input), std::end(input), std::begin(output)));
}
#endif

// Serializer, deserializer (for struct or tuple serialization)
{
// serialization
Expand Down Expand Up @@ -171,4 +181,4 @@ int main(int argc, char** argv) {
} catch (ZException e) {
std::cout << "Received an error :" << e.what() << "\n";
}
}
}
10 changes: 10 additions & 0 deletions include/zenoh/api/ext/serialization.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#pragma once

#if __cplusplus >= 202002L
#include <span>
#endif
#include <array>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -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 <class T, std::size_t Extent>
bool __zenoh_serialize_with_serializer(zenoh::ext::Serializer& serializer, std::span<T, Extent> value, ZResult* err) {
return __serialize_sequence_with_serializer(serializer, value.begin(), value.end(), value.size(), err);
}
#endif

template <class T>
bool serialize_with_serializer(zenoh::ext::Serializer& serializer, const T& t, ZResult* err) {
return __zenoh_serialize_with_serializer(serializer, t, err);
Expand Down

0 comments on commit 4ad359d

Please sign in to comment.