Skip to content

Commit

Permalink
[#490] Add trait to determine payload value type of any payload
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Nov 6, 2024
1 parent 45f4781 commit 0cf2c2b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
31 changes: 31 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/payload_info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#ifndef IOX2_PAYLOAD_INFO_HPP
#define IOX2_PAYLOAD_INFO_HPP

#include "iox/slice.hpp"

namespace iox2 {

template <typename T>
struct PayloadInfo {
using TYPE = T;
};

template <typename T>
struct PayloadInfo<iox::Slice<T>> {
using TYPE = typename iox::Slice<T>::ValueType;
};

} // namespace iox2
#endif
16 changes: 5 additions & 11 deletions iceoryx2-ffi/cxx/include/iox2/port_factory_publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "iox/builder_addendum.hpp"
#include "iox/expected.hpp"
#include "iox2/internal/iceoryx2.hpp"
#include "iox2/payload_info.hpp"
#include "iox2/publisher.hpp"
#include "iox2/service_type.hpp"
#include "iox2/unable_to_deliver_strategy.hpp"
Expand Down Expand Up @@ -76,20 +77,13 @@ PortFactoryPublisher<S, Payload, UserHeader>::create() && -> iox::expected<Publi
// The payload type used by the C API is always a [u8].
// Thus need to convert from N to N * sizeof(payload).
// TODO: Consider alignment... not aligning each element properly will impact performance
if constexpr (iox::IsSlice<Payload>::VALUE) {
iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle,
value * sizeof(typename Payload::ValueType));
} else {
iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, value * sizeof(Payload));
}
iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle,
value * sizeof(typename PayloadInfo<Payload>::TYPE));
})
.or_else([&]() {
// Assume only one element if not otherwise specified
if constexpr (iox::IsSlice<Payload>::VALUE) {
iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, sizeof(typename Payload::ValueType));
} else {
iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, sizeof(Payload));
}
iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle,
sizeof(typename PayloadInfo<Payload>::TYPE));
});
m_max_loaned_samples.and_then(
[&](auto value) { iox2_port_factory_publisher_builder_set_max_loaned_samples(&m_handle, value); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "iox2/attribute_specifier.hpp"
#include "iox2/attribute_verifier.hpp"
#include "iox2/internal/iceoryx2.hpp"
#include "iox2/payload_info.hpp"
#include "iox2/port_factory_publish_subscribe.hpp"
#include "iox2/service_builder_publish_subscribe_error.hpp"
#include "iox2/service_type.hpp"
Expand All @@ -27,16 +28,6 @@

namespace iox2 {

template <typename T>
struct PayloadValueType {
using TYPE = T;
};

template <typename T>
struct PayloadValueType<iox::Slice<T>> {
using TYPE = typename iox::Slice<T>::ValueType;
};

/// Builder to create new [`MessagingPattern::PublishSubscribe`] based [`Service`]s
template <typename Payload, typename UserHeader, ServiceType S>
class ServiceBuilderPublishSubscribe {
Expand Down Expand Up @@ -149,7 +140,7 @@ inline void ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::set_paramete
[&](auto value) { iox2_service_builder_pub_sub_set_payload_alignment(&m_handle, value); });
m_max_nodes.and_then([&](auto value) { iox2_service_builder_pub_sub_set_max_nodes(&m_handle, value); });

using ValueType = typename PayloadValueType<Payload>::TYPE;
using ValueType = typename PayloadInfo<Payload>::TYPE;
auto type_variant = iox::IsSlice<Payload>::VALUE ? iox2_type_variant_e_DYNAMIC : iox2_type_variant_e_FIXED_SIZE;

// payload type details
Expand Down

0 comments on commit 0cf2c2b

Please sign in to comment.