Skip to content

Commit

Permalink
[#264] Enable slice methods only for slice Payload; remove reinterpre…
Browse files Browse the repository at this point in the history
…t cast
  • Loading branch information
elfenpiff committed Jul 31, 2024
1 parent ca12b62 commit c2194fe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 5 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "iox2/unique_port_id.hpp"

#include <cstdint>
#include <type_traits>

namespace iox2 {
/// Sending endpoint of a publish-subscriber based communication.
Expand Down Expand Up @@ -64,13 +65,15 @@ class Publisher {
/// [`Payload`].
///
/// On failure it returns [`PublisherLoanError`] describing the failure.
template <typename T = Payload, typename = std::enable_if<iox::IsSlice<T>::VALUE, T>>
auto
loan_slice(uint64_t number_of_elements) -> iox::expected<SampleMut<S, Payload, UserHeader>, PublisherLoanError>;

/// Loans/allocates a [`SampleMut`] from the underlying data segment of the [`Publisher`].
/// The user has to initialize the payload before it can be sent.
///
/// On failure it returns [`PublisherLoanError`] describing the failure.
template <typename T = Payload, typename = std::enable_if_t<iox::IsSlice<T>::VALUE, T>>
auto loan_slice_uninit(uint64_t number_of_elements)
-> iox::expected<SampleMut<S, Payload, UserHeader>, PublisherLoanError>;

Expand Down Expand Up @@ -171,12 +174,14 @@ Publisher<S, Payload, UserHeader>::loan() -> iox::expected<SampleMut<S, Payload,
}

template <ServiceType S, typename Payload, typename UserHeader>
template <typename, typename>
inline auto Publisher<S, Payload, UserHeader>::loan_slice(const uint64_t number_of_elements)
-> iox::expected<SampleMut<S, Payload, UserHeader>, PublisherLoanError> {
IOX_TODO();
}

template <ServiceType S, typename Payload, typename UserHeader>
template <typename, typename>
inline auto Publisher<S, Payload, UserHeader>::loan_slice_uninit(const uint64_t number_of_elements)
-> iox::expected<SampleMut<S, Payload, UserHeader>, PublisherLoanError> {
IOX_TODO();
Expand Down
8 changes: 3 additions & 5 deletions iceoryx2-ffi/cxx/include/iox2/sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@ inline auto Sample<S, Payload, UserHeader>::operator->() const -> const Payload*
template <ServiceType S, typename Payload, typename UserHeader>
inline auto Sample<S, Payload, UserHeader>::payload() const -> const Payload& {
auto* ref_handle = iox2_cast_sample_ref_h(m_handle);
const Payload* payload_ptr = nullptr;
const void* payload_ptr = nullptr;
size_t payload_len = 0;

// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast), no other way for type erasure
iox2_sample_payload(ref_handle, reinterpret_cast<const void**>(&payload_ptr), &payload_len);
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
iox2_sample_payload(ref_handle, &payload_ptr, &payload_len);
IOX_ASSERT(sizeof(Payload) <= payload_len, "");

return *payload_ptr;
return *static_cast<const Payload*>(payload_ptr);
}

template <ServiceType S, typename Payload, typename UserHeader>
Expand Down

0 comments on commit c2194fe

Please sign in to comment.