Skip to content

Commit 662530c

Browse files
committed
Move Payload into zenoh_utils
1 parent 904dcad commit 662530c

File tree

4 files changed

+73
-91
lines changed

4 files changed

+73
-91
lines changed

rmw_zenoh_cpp/src/detail/payload.hpp

-90
This file was deleted.

rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "attachment_helpers.hpp"
3636
#include "type_support_common.hpp"
3737
#include "zenoh_utils.hpp"
38-
#include "payload.hpp"
3938

4039
#include "rcutils/allocator.h"
4140

rmw_zenoh_cpp/src/detail/zenoh_utils.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,48 @@ std::chrono::nanoseconds::rep ZenohReply::get_received_timestamp() const
9696
{
9797
return received_timestamp_;
9898
}
99+
100+
///=============================================================================
101+
Payload::Payload(const zenoh::Bytes & bytes)
102+
{
103+
zenoh::Bytes::SliceIterator slices = bytes.slice_iter();
104+
std::optional<zenoh::Slice> slice = slices.next();
105+
if (!slice.has_value()) {
106+
bytes_ = nullptr;
107+
} else {
108+
if (!slices.next().has_value()) {
109+
bytes_ = Contiguous {slice.value(), bytes.clone()};
110+
} else {
111+
bytes_ = bytes.as_vector();
112+
}
113+
}
114+
}
115+
116+
const uint8_t * Payload::data()
117+
{
118+
if (std::holds_alternative<Empty>(bytes_)) {
119+
return nullptr;
120+
} else if (std::holds_alternative<NonContiguous>(bytes_)) {
121+
return std::get<NonContiguous>(bytes_).data();
122+
} else {
123+
return std::get<Contiguous>(bytes_).slice.data;
124+
}
125+
}
126+
127+
size_t Payload::size()
128+
{
129+
if (std::holds_alternative<Empty>(bytes_)) {
130+
return 0;
131+
} else if (std::holds_alternative<NonContiguous>(bytes_)) {
132+
return std::get<NonContiguous>(bytes_).size();
133+
} else {
134+
return std::get<Contiguous>(bytes_).slice.len;
135+
}
136+
}
137+
138+
bool Payload::empty()
139+
{
140+
return std::holds_alternative<Empty>(bytes_);
141+
}
142+
99143
} // namespace rmw_zenoh_cpp

rmw_zenoh_cpp/src/detail/zenoh_utils.hpp

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <chrono>
2222
#include <functional>
2323
#include <optional>
24+
#include <utility>
25+
#include <variant>
26+
#include <vector>
2427

2528
#include "rmw/types.h"
2629

@@ -80,6 +83,32 @@ class ZenohQuery final
8083
zenoh::Query query_;
8184
std::chrono::nanoseconds::rep received_timestamp_;
8285
};
86+
87+
class Payload
88+
{
89+
public:
90+
explicit Payload(const zenoh::Bytes & bytes);
91+
92+
~Payload() = default;
93+
94+
const uint8_t * data() const;
95+
96+
size_t size() const;
97+
98+
bool empty() const;
99+
100+
private:
101+
struct Contiguous
102+
{
103+
zenoh::Slice slice;
104+
zenoh::Bytes bytes;
105+
};
106+
using NonContiguous = std::vector<uint8_t>;
107+
using Empty = std::nullptr_t;
108+
// Is `std::vector<uint8_t>` in case of a non-contiguous payload
109+
// and `zenoh::Slice` plus a `zenoh::Bytes` otherwise.
110+
std::variant<NonContiguous, Contiguous, Empty> bytes_;
111+
}
83112
} // namespace rmw_zenoh_cpp
84113

85114
#endif // DETAIL__ZENOH_UTILS_HPP_

0 commit comments

Comments
 (0)