Skip to content

Commit

Permalink
Session::operator==
Browse files Browse the repository at this point in the history
Session::getHashCode
  • Loading branch information
ttldtor committed Nov 2, 2023
1 parent f56cab3 commit 3cc86ce
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/dxfeed_graal_cpp_api/isolated/Isolated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
18 changes: 16 additions & 2 deletions include/dxfeed_graal_cpp_api/schedule/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,26 @@ 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
*/
std::string toString() const noexcept;
};

} // namespace dxfcpp
} // namespace dxfcpp

template <> struct DXFCPP_EXPORT std::hash<dxfcpp::Session> {
std::size_t operator()(const dxfcpp::Session &session) const noexcept {
return session.getHashCode();
}
};
29 changes: 29 additions & 0 deletions src/internal/Isolate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,35 @@ bool Session::containsTime(/* dxfg_session_t* */ void *session, std::int64_t tim
nullptr, dxfcpp::bit_cast<dxfg_session_t *>(session), dxfcpp::bit_cast<dxfg_session_filter_t *>(filter)));
}

std::size_t Session::getHashCode(/* dxfg_session_t* */ void *session) noexcept {
if (!session) {
return dxfcpp::bit_cast<std::size_t>(session);
}

return runIsolatedOrElse(
[](auto threadHandle, auto &&session) {
return dxfg_Session_hashCode(dxfcpp::bit_cast<graal_isolatethread_t *>(threadHandle), session);
},
0, dxfcpp::bit_cast<dxfg_session_t *>(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<graal_isolatethread_t *>(threadHandle), session,
otherSession) == 1;
},
false, dxfcpp::bit_cast<dxfg_session_t *>(session), dxfcpp::bit_cast<dxfg_session_t *>(otherSession));
}

std::string Session::toString(/* dxfg_session_t* */ void *session) noexcept {
if (!session) {
return dxfcpp::String::EMPTY;
Expand Down
20 changes: 20 additions & 0 deletions src/schedule/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(this);
}

return isolated::schedule::Session::getHashCode(handle_.get());
}

std::string Session::toString() const noexcept {
if (!handle_) {
return dxfcpp::String::EMPTY;
Expand Down

0 comments on commit 3cc86ce

Please sign in to comment.