diff --git a/rmw_zenoh_cpp/src/detail/payload.hpp b/rmw_zenoh_cpp/src/detail/payload.hpp deleted file mode 100644 index 7743b43c..00000000 --- a/rmw_zenoh_cpp/src/detail/payload.hpp +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2024 Open Source Robotics Foundation, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef DETAIL__PAYLOAD_HPP_ -#define DETAIL__PAYLOAD_HPP_ - -#include -#include -#include -#include -#include - -#include - -namespace rmw_zenoh_cpp -{ -///============================================================================= -class Payload -{ -public: - explicit Payload(const zenoh::Bytes & bytes) - { - zenoh::Bytes::SliceIterator slices = bytes.slice_iter(); - std::optional slice = slices.next(); - if (!slice.has_value()) { - bytes_ = nullptr; - } else { - if (!slices.next().has_value()) { - bytes_ = Contiguous {slice.value(), bytes.clone()}; - } else { - bytes_ = bytes.as_vector(); - } - } - } - - ~Payload() = default; - - const uint8_t * data() - { - if (std::holds_alternative(bytes_)) { - return nullptr; - } else if (std::holds_alternative(bytes_)) { - return std::get(bytes_).data(); - } else { - return std::get(bytes_).slice.data; - } - } - - size_t size() - { - if (std::holds_alternative(bytes_)) { - return 0; - } else if (std::holds_alternative(bytes_)) { - return std::get(bytes_).size(); - } else { - return std::get(bytes_).slice.len; - } - } - - bool empty() - { - return std::holds_alternative(bytes_); - } - -private: - struct Contiguous - { - zenoh::Slice slice; - zenoh::Bytes bytes; - }; - using NonContiguous = std::vector; - using Empty = std::nullptr_t; - // Is `std::vector` in case of a non-contiguous payload - // and `zenoh::Slice` plus a `zenoh::Bytes` otherwise. - std::variant bytes_; -}; -} // namespace rmw_zenoh_cpp - -#endif // DETAIL__PAYLOAD_HPP_ diff --git a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp index bb033255..37ab0dba 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp @@ -35,7 +35,6 @@ #include "attachment_helpers.hpp" #include "type_support_common.hpp" #include "zenoh_utils.hpp" -#include "payload.hpp" #include "rcutils/allocator.h" diff --git a/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp b/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp index 35173656..408343bb 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp @@ -96,4 +96,48 @@ std::chrono::nanoseconds::rep ZenohReply::get_received_timestamp() const { return received_timestamp_; } + +///============================================================================= +Payload::Payload(const zenoh::Bytes & bytes) +{ + zenoh::Bytes::SliceIterator slices = bytes.slice_iter(); + std::optional slice = slices.next(); + if (!slice.has_value()) { + bytes_ = nullptr; + } else { + if (!slices.next().has_value()) { + bytes_ = Contiguous {slice.value(), bytes.clone()}; + } else { + bytes_ = bytes.as_vector(); + } + } +} + +const uint8_t * Payload::data() +{ + if (std::holds_alternative(bytes_)) { + return nullptr; + } else if (std::holds_alternative(bytes_)) { + return std::get(bytes_).data(); + } else { + return std::get(bytes_).slice.data; + } +} + +size_t Payload::size() +{ + if (std::holds_alternative(bytes_)) { + return 0; + } else if (std::holds_alternative(bytes_)) { + return std::get(bytes_).size(); + } else { + return std::get(bytes_).slice.len; + } +} + +bool Payload::empty() +{ + return std::holds_alternative(bytes_); +} + } // namespace rmw_zenoh_cpp diff --git a/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp b/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp index 1b1efc87..f4305491 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp @@ -21,6 +21,9 @@ #include #include #include +#include +#include +#include #include "rmw/types.h" @@ -80,6 +83,32 @@ class ZenohQuery final zenoh::Query query_; std::chrono::nanoseconds::rep received_timestamp_; }; + +class Payload +{ +public: + explicit Payload(const zenoh::Bytes & bytes); + + ~Payload() = default; + + const uint8_t * data() const; + + size_t size() const; + + bool empty() const; + +private: + struct Contiguous + { + zenoh::Slice slice; + zenoh::Bytes bytes; + }; + using NonContiguous = std::vector; + using Empty = std::nullptr_t; + // Is `std::vector` in case of a non-contiguous payload + // and `zenoh::Slice` plus a `zenoh::Bytes` otherwise. + std::variant bytes_; +} } // namespace rmw_zenoh_cpp #endif // DETAIL__ZENOH_UTILS_HPP_