diff --git a/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp b/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp index b3608d04..9c609b6d 100644 --- a/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp +++ b/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp @@ -130,6 +130,8 @@ struct Session { /* dxfg_session_filter_t* */ void *filter) noexcept; static /* dxfg_session_t* */ void *findNextSession(/* dxfg_session_t* */ void *session, /* dxfg_session_filter_t* */ void *filter) noexcept; + static std::size_t getHashCode(/* dxfg_session_t* */ void *session) noexcept; + static bool equals(/* dxfg_session_t* */ void *session, /* dxfg_session_t* */ void *otherSession) noexcept; static std::string toString(/* dxfg_session_t* */ void *session) noexcept; }; diff --git a/include/dxfeed_graal_cpp_api/schedule/Session.hpp b/include/dxfeed_graal_cpp_api/schedule/Session.hpp index bea445f1..4eda0dbd 100644 --- a/include/dxfeed_graal_cpp_api/schedule/Session.hpp +++ b/include/dxfeed_graal_cpp_api/schedule/Session.hpp @@ -155,7 +155,15 @@ struct DXFCPP_EXPORT Session { */ Session::Ptr findNextSession(const SessionFilter &filter) const noexcept; - /** + bool operator==(const Session &other) const noexcept; + + bool operator==(const Session::Ptr &other) const noexcept { + return *this == *other; + } + + std::size_t getHashCode() const noexcept; + + /** * Returns a string representation of the current object. * * @return a string representation @@ -163,4 +171,10 @@ struct DXFCPP_EXPORT Session { std::string toString() const noexcept; }; -} // namespace dxfcpp \ No newline at end of file +} // namespace dxfcpp + +template <> struct DXFCPP_EXPORT std::hash { + std::size_t operator()(const dxfcpp::Session &session) const noexcept { + return session.getHashCode(); + } +}; \ No newline at end of file diff --git a/src/internal/Isolate.cpp b/src/internal/Isolate.cpp index ef88d64d..e66dcdc9 100644 --- a/src/internal/Isolate.cpp +++ b/src/internal/Isolate.cpp @@ -844,6 +844,35 @@ bool Session::containsTime(/* dxfg_session_t* */ void *session, std::int64_t tim nullptr, dxfcpp::bit_cast(session), dxfcpp::bit_cast(filter))); } +std::size_t Session::getHashCode(/* dxfg_session_t* */ void *session) noexcept { + if (!session) { + return dxfcpp::bit_cast(session); + } + + return runIsolatedOrElse( + [](auto threadHandle, auto &&session) { + return dxfg_Session_hashCode(dxfcpp::bit_cast(threadHandle), session); + }, + 0, dxfcpp::bit_cast(session)); +} + +bool Session::equals(/* dxfg_session_t* */ void *session, /* dxfg_session_t* */ void *otherSession) noexcept { + if (!session || !otherSession) { + return false; + } + + if (session == otherSession) { + return true; + } + + return runIsolatedOrElse( + [](auto threadHandle, auto &&session, auto &&otherSession) { + return dxfg_Session_equals(dxfcpp::bit_cast(threadHandle), session, + otherSession) == 1; + }, + false, dxfcpp::bit_cast(session), dxfcpp::bit_cast(otherSession)); +} + std::string Session::toString(/* dxfg_session_t* */ void *session) noexcept { if (!session) { return dxfcpp::String::EMPTY; diff --git a/src/schedule/Session.cpp b/src/schedule/Session.cpp index fd8908bf..dd10bd58 100644 --- a/src/schedule/Session.cpp +++ b/src/schedule/Session.cpp @@ -118,6 +118,26 @@ Session::Ptr Session::findNextSession(const SessionFilter &filter) const noexcep return Session::create(isolated::schedule::Session::findNextSession(handle_.get(), filter.handle_.get())); } +bool Session::operator==(const Session &other) const noexcept { + if (!handle_ || !other.handle_) { + return false; + } + + if (this == &other) { + return true; + } + + return isolated::schedule::Session::equals(handle_.get(), other.handle_.get()); +} + +std::size_t Session::getHashCode() const noexcept { + if (!handle_) { + return dxfcpp::bit_cast(this); + } + + return isolated::schedule::Session::getHashCode(handle_.get()); +} + std::string Session::toString() const noexcept { if (!handle_) { return dxfcpp::String::EMPTY;