diff --git a/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp b/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp index 2e9e0340..865b4156 100644 --- a/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp @@ -21,6 +21,7 @@ namespace iox2 { constexpr uint64_t UNIQUE_PORT_ID_LENGTH = 128; +using RawIdType = std::array; /// The system-wide unique id of a [`Publisher`]. class UniquePublisherId { @@ -31,7 +32,7 @@ class UniquePublisherId { auto operator=(UniquePublisherId&& rhs) noexcept -> UniquePublisherId&; ~UniquePublisherId(); - auto bytes() -> iox::optional>; + auto bytes() -> iox::optional&; private: template @@ -44,6 +45,7 @@ class UniquePublisherId { void drop(); iox2_unique_publisher_id_h m_handle = nullptr; + iox::optional m_raw_id; }; /// The system-wide unique id of a [`Subscriber`]. @@ -55,7 +57,7 @@ class UniqueSubscriberId { auto operator=(UniqueSubscriberId&& rhs) noexcept -> UniqueSubscriberId&; ~UniqueSubscriberId(); - auto bytes() -> iox::optional>; + auto bytes() -> iox::optional&; private: template @@ -67,6 +69,7 @@ class UniqueSubscriberId { void drop(); iox2_unique_subscriber_id_h m_handle = nullptr; + iox::optional m_raw_id; }; /// The system-wide unique id of a [`Notifier`]. @@ -78,7 +81,7 @@ class UniqueNotifierId { auto operator=(UniqueNotifierId&& rhs) noexcept -> UniqueNotifierId&; ~UniqueNotifierId(); - auto bytes() -> iox::optional>; + auto bytes() -> iox::optional&; private: template @@ -90,6 +93,7 @@ class UniqueNotifierId { void drop(); iox2_unique_notifier_id_h m_handle = nullptr; + iox::optional m_raw_id; }; /// The system-wide unique id of a [`Listener`]. @@ -101,7 +105,7 @@ class UniqueListenerId { auto operator=(UniqueListenerId&& rhs) noexcept -> UniqueListenerId&; ~UniqueListenerId(); - auto bytes() -> iox::optional>; + auto bytes() -> iox::optional&; private: template @@ -113,6 +117,7 @@ class UniqueListenerId { void drop(); iox2_unique_listener_id_h m_handle = nullptr; + iox::optional m_raw_id; }; auto operator==(const UniquePublisherId& lhs, const UniquePublisherId& rhs) -> bool; diff --git a/iceoryx2-ffi/cxx/src/unique_port_id.cpp b/iceoryx2-ffi/cxx/src/unique_port_id.cpp index 13a76c68..4055f53f 100644 --- a/iceoryx2-ffi/cxx/src/unique_port_id.cpp +++ b/iceoryx2-ffi/cxx/src/unique_port_id.cpp @@ -43,13 +43,13 @@ UniquePublisherId::UniquePublisherId(iox2_unique_publisher_id_h handle) : m_handle { handle } { } -auto UniquePublisherId::bytes() -> iox::optional> { - if (m_handle != nullptr) { - std::array bytes {}; +auto UniquePublisherId::bytes() -> iox::optional& { + if (!m_raw_id.has_value() && m_handle != nullptr) { + RawIdType bytes {}; iox2_unique_publisher_id_value(m_handle, bytes.data()); - return bytes; + m_raw_id.emplace(std::move(bytes)); } - return iox::nullopt; + return m_raw_id; }; void UniquePublisherId::drop() { @@ -90,13 +90,13 @@ UniqueSubscriberId::UniqueSubscriberId(iox2_unique_subscriber_id_h handle) : m_handle { handle } { } -auto UniqueSubscriberId::bytes() -> iox::optional> { - if (m_handle != nullptr) { - std::array bytes {}; +auto UniqueSubscriberId::bytes() -> iox::optional& { + if (!m_raw_id.has_value() && m_handle != nullptr) { + RawIdType bytes {}; iox2_unique_subscriber_id_value(m_handle, bytes.data()); - return bytes; + m_raw_id.emplace(std::move(bytes)); } - return iox::nullopt; + return m_raw_id; }; void UniqueSubscriberId::drop() { @@ -136,13 +136,13 @@ UniqueNotifierId::UniqueNotifierId(iox2_unique_notifier_id_h handle) : m_handle { handle } { } -auto UniqueNotifierId::bytes() -> iox::optional> { - if (m_handle != nullptr) { - std::array bytes {}; +auto UniqueNotifierId::bytes() -> iox::optional& { + if (!m_raw_id.has_value() && m_handle != nullptr) { + RawIdType bytes {}; iox2_unique_notifier_id_value(m_handle, bytes.data()); - return bytes; + m_raw_id.emplace(std::move(bytes)); } - return iox::nullopt; + return m_raw_id; }; void UniqueNotifierId::drop() { @@ -182,13 +182,13 @@ UniqueListenerId::UniqueListenerId(iox2_unique_listener_id_h handle) : m_handle { handle } { } -auto UniqueListenerId::bytes() -> iox::optional> { - if (m_handle != nullptr) { - std::array bytes {}; +auto UniqueListenerId::bytes() -> iox::optional& { + if (!m_raw_id.has_value() && m_handle != nullptr) { + RawIdType bytes {}; iox2_unique_listener_id_value(m_handle, bytes.data()); - return bytes; + m_raw_id.emplace(std::move(bytes)); } - return iox::nullopt; + return m_raw_id; }; void UniqueListenerId::drop() {