From 2d1b857404173270510aa30c3c9f59d290cd528e Mon Sep 17 00:00:00 2001 From: Jeff Ithier Date: Sat, 2 Nov 2024 13:35:55 +0100 Subject: [PATCH] [#500] Expose UniqueSubscriberId UniqueNotifierId UniqueListenerId bytes in CXX API --- .../cxx/include/iox2/unique_port_id.hpp | 6 ++++ iceoryx2-ffi/cxx/src/unique_port_id.cpp | 33 +++++++++++++++++-- .../cxx/tests/src/unique_port_id_tests.cpp | 21 +++++++++--- .../ffi/src/api/unique_listener_id.rs | 18 ++++++++++ .../ffi/src/api/unique_notifier_id.rs | 18 ++++++++++ .../ffi/src/api/unique_subscriber_id.rs | 19 +++++++++++ 6 files changed, 108 insertions(+), 7 deletions(-) diff --git a/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp b/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp index 5360069e1..2e9e03406 100644 --- a/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp @@ -55,6 +55,8 @@ class UniqueSubscriberId { auto operator=(UniqueSubscriberId&& rhs) noexcept -> UniqueSubscriberId&; ~UniqueSubscriberId(); + auto bytes() -> iox::optional>; + private: template friend class Subscriber; @@ -76,6 +78,8 @@ class UniqueNotifierId { auto operator=(UniqueNotifierId&& rhs) noexcept -> UniqueNotifierId&; ~UniqueNotifierId(); + auto bytes() -> iox::optional>; + private: template friend class Notifier; @@ -97,6 +101,8 @@ class UniqueListenerId { auto operator=(UniqueListenerId&& rhs) noexcept -> UniqueListenerId&; ~UniqueListenerId(); + auto bytes() -> iox::optional>; + private: template friend class Listener; diff --git a/iceoryx2-ffi/cxx/src/unique_port_id.cpp b/iceoryx2-ffi/cxx/src/unique_port_id.cpp index cf657eda6..13a76c686 100644 --- a/iceoryx2-ffi/cxx/src/unique_port_id.cpp +++ b/iceoryx2-ffi/cxx/src/unique_port_id.cpp @@ -45,9 +45,9 @@ UniquePublisherId::UniquePublisherId(iox2_unique_publisher_id_h handle) auto UniquePublisherId::bytes() -> iox::optional> { if (m_handle != nullptr) { - std::array unique_port_id {}; - iox2_unique_publisher_id_value(m_handle, unique_port_id.data()); - return unique_port_id; + std::array bytes {}; + iox2_unique_publisher_id_value(m_handle, bytes.data()); + return bytes; } return iox::nullopt; }; @@ -90,6 +90,15 @@ UniqueSubscriberId::UniqueSubscriberId(iox2_unique_subscriber_id_h handle) : m_handle { handle } { } +auto UniqueSubscriberId::bytes() -> iox::optional> { + if (m_handle != nullptr) { + std::array bytes {}; + iox2_unique_subscriber_id_value(m_handle, bytes.data()); + return bytes; + } + return iox::nullopt; +}; + void UniqueSubscriberId::drop() { if (m_handle != nullptr) { iox2_unique_subscriber_id_drop(m_handle); @@ -127,6 +136,15 @@ UniqueNotifierId::UniqueNotifierId(iox2_unique_notifier_id_h handle) : m_handle { handle } { } +auto UniqueNotifierId::bytes() -> iox::optional> { + if (m_handle != nullptr) { + std::array bytes {}; + iox2_unique_notifier_id_value(m_handle, bytes.data()); + return bytes; + } + return iox::nullopt; +}; + void UniqueNotifierId::drop() { if (m_handle != nullptr) { iox2_unique_notifier_id_drop(m_handle); @@ -164,6 +182,15 @@ UniqueListenerId::UniqueListenerId(iox2_unique_listener_id_h handle) : m_handle { handle } { } +auto UniqueListenerId::bytes() -> iox::optional> { + if (m_handle != nullptr) { + std::array bytes {}; + iox2_unique_listener_id_value(m_handle, bytes.data()); + return bytes; + } + return iox::nullopt; +}; + void UniqueListenerId::drop() { if (m_handle != nullptr) { iox2_unique_listener_id_drop(m_handle); diff --git a/iceoryx2-ffi/cxx/tests/src/unique_port_id_tests.cpp b/iceoryx2-ffi/cxx/tests/src/unique_port_id_tests.cpp index 57447b9c8..89e86cd4c 100644 --- a/iceoryx2-ffi/cxx/tests/src/unique_port_id_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/unique_port_id_tests.cpp @@ -65,11 +65,24 @@ struct UniquePortIdTest : public ::testing::Test { TYPED_TEST_SUITE(UniquePortIdTest, iox2_testing::ServiceTypes); -TYPED_TEST(UniquePortIdTest, unique_publisher_id_value) { +TYPED_TEST(UniquePortIdTest, unique_port_id_value) { auto null_id = std::array {}; - auto unique_port_id = this->publisher_1.id(); - ASSERT_TRUE(unique_port_id.bytes().has_value()); - ASSERT_NE(unique_port_id.bytes().value(), null_id); + + auto unique_publisher_id = this->publisher_1.id(); + ASSERT_TRUE(unique_publisher_id.bytes().has_value()); + ASSERT_NE(unique_publisher_id.bytes().value(), null_id); + + auto unique_subscriber_id = this->publisher_1.id(); + ASSERT_TRUE(unique_subscriber_id.bytes().has_value()); + ASSERT_NE(unique_subscriber_id.bytes().value(), null_id); + + auto unique_notifier_id = this->notifier_1.id(); + ASSERT_TRUE(unique_notifier_id.bytes().has_value()); + ASSERT_NE(unique_notifier_id.bytes().value(), null_id); + + auto unique_listener_id = this->listener_1.id(); + ASSERT_TRUE(unique_listener_id.bytes().has_value()); + ASSERT_NE(unique_listener_id.bytes().value(), null_id); } TYPED_TEST(UniquePortIdTest, unique_port_id_from_same_port_is_equal) { diff --git a/iceoryx2-ffi/ffi/src/api/unique_listener_id.rs b/iceoryx2-ffi/ffi/src/api/unique_listener_id.rs index b286894fa..89530262c 100644 --- a/iceoryx2-ffi/ffi/src/api/unique_listener_id.rs +++ b/iceoryx2-ffi/ffi/src/api/unique_listener_id.rs @@ -86,6 +86,24 @@ impl HandleToType for iox2_unique_listener_id_h_ref { // BEGIN C API +#[no_mangle] +unsafe extern "C" fn iox2_unique_listener_id_value( + handle: iox2_unique_listener_id_h, + id_ptr: *mut u8, +) { + handle.assert_non_null(); + + let h = &mut *handle.as_type(); + + if let Some(Some(id)) = (h.value.internal.as_ptr() as *const Option).as_ref() + { + let bytes = id.value().to_ne_bytes(); + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len()); + } + } +} + /// This function needs to be called to destroy the unique listener id! /// /// # Arguments diff --git a/iceoryx2-ffi/ffi/src/api/unique_notifier_id.rs b/iceoryx2-ffi/ffi/src/api/unique_notifier_id.rs index 8b289bd8a..22e0950e0 100644 --- a/iceoryx2-ffi/ffi/src/api/unique_notifier_id.rs +++ b/iceoryx2-ffi/ffi/src/api/unique_notifier_id.rs @@ -86,6 +86,24 @@ impl HandleToType for iox2_unique_notifier_id_h_ref { // BEGIN C API +#[no_mangle] +unsafe extern "C" fn iox2_unique_notifier_id_value( + handle: iox2_unique_notifier_id_h, + id_ptr: *mut u8, +) { + handle.assert_non_null(); + + let h = &mut *handle.as_type(); + + if let Some(Some(id)) = (h.value.internal.as_ptr() as *const Option).as_ref() + { + let bytes = id.value().to_ne_bytes(); + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len()); + } + } +} + /// This function needs to be called to destroy the unique notifier id! /// /// # Arguments diff --git a/iceoryx2-ffi/ffi/src/api/unique_subscriber_id.rs b/iceoryx2-ffi/ffi/src/api/unique_subscriber_id.rs index 65c8effc2..64487e76c 100644 --- a/iceoryx2-ffi/ffi/src/api/unique_subscriber_id.rs +++ b/iceoryx2-ffi/ffi/src/api/unique_subscriber_id.rs @@ -86,6 +86,25 @@ impl HandleToType for iox2_unique_subscriber_id_h_ref { // BEGIN C API +#[no_mangle] +unsafe extern "C" fn iox2_unique_subscriber_id_value( + handle: iox2_unique_subscriber_id_h, + id_ptr: *mut u8, +) { + handle.assert_non_null(); + + let h = &mut *handle.as_type(); + + if let Some(Some(id)) = + (h.value.internal.as_ptr() as *const Option).as_ref() + { + let bytes = id.value().to_ne_bytes(); + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len()); + } + } +} + /// This function needs to be called to destroy the unique subscriber id! /// /// # Arguments