Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bytes serialization support for std::span #273

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/universal/z_bytes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "entity.pb.h"
#endif

#if __cplusplus >= 202002L
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not necessary (especially given that we do not include other related stl headers) , since the corresponding header is already included by serialization.hxx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I removed it from the include directives

#include <span>
#endif

#include "zenoh.hxx"
using namespace zenoh;

Expand Down Expand Up @@ -91,6 +95,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
12 changes: 12 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,15 @@ 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
Loading