Skip to content

Commit

Permalink
[eclipse-iceoryx#488] Implement std::hash for WaitSetAttachmentId
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff authored and orecham committed Oct 31, 2024
1 parent b1c7f75 commit df33977
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/waitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class WaitSetAttachmentId {
/// Returns true if the deadline for the attachment corresponding to [`WaitSetGuard`] was missed.
auto has_missed_deadline(const WaitSetGuard<S>& guard) const -> bool;

/// Returns the a non-secure hash for the [`WaitSetAttachmentId`].
auto hash() const -> std::size_t;

private:
explicit WaitSetAttachmentId(iox2_waitset_attachment_id_h handle);
template <ServiceType>
Expand Down Expand Up @@ -228,4 +231,18 @@ class WaitSetBuilder {
};
} // namespace iox2

template <>
struct std::hash<iox2::WaitSetAttachmentId<iox2::ServiceType::Ipc>> {
auto operator()(const iox2::WaitSetAttachmentId<iox2::ServiceType::Ipc>& self) -> std::size_t {
return self.hash();
}
};

template <>
struct std::hash<iox2::WaitSetAttachmentId<iox2::ServiceType::Local>> {
auto operator()(const iox2::WaitSetAttachmentId<iox2::ServiceType::Local>& self) -> std::size_t {
return self.hash();
}
};

#endif
8 changes: 8 additions & 0 deletions iceoryx2-ffi/cxx/src/waitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ void WaitSetAttachmentId<S>::drop() {
}
}

template <ServiceType S>
auto WaitSetAttachmentId<S>::hash() const -> std::size_t {
auto len = iox2_waitset_attachment_id_debug_len(&m_handle);
std::string empty(len, '\0');
iox2_waitset_attachment_id_debug(&m_handle, empty.data(), len);
return std::hash<std::string> {}(empty);
}

template <ServiceType S>
auto operator==(const WaitSetAttachmentId<S>& lhs, const WaitSetAttachmentId<S>& rhs) -> bool {
return iox2_waitset_attachment_id_equal(&lhs.m_handle, &rhs.m_handle);
Expand Down

0 comments on commit df33977

Please sign in to comment.