Skip to content

Commit

Permalink
move the forward declarations of the callback tokens into UwbConnecto…
Browse files Browse the repository at this point in the history
…r.hxx, and check for session event callback first
  • Loading branch information
forwardpointer committed Apr 12, 2023
1 parent 3c82f04 commit 08aecba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 15 additions & 12 deletions windows/devices/uwb/UwbConnector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -934,18 +934,9 @@ UwbConnector::DeregisterEventCallback(std::weak_ptr<::uwb::RegisteredCallbackTok
}
std::lock_guard eventCallbacksLockExclusive{ m_eventCallbacksGate };

auto deviceCallback = std::dynamic_pointer_cast<::uwb::RegisteredDeviceCallbackToken>(tokenShared);
if (deviceCallback) {
// treat it as a device callback
auto tokenIt = std::find_if(std::cbegin(m_deviceEventCallbacks), std::cend(m_deviceEventCallbacks), [deviceCallback](const auto& token) {
return token.get() == deviceCallback.get();
});
if (tokenIt == std::cend(m_deviceEventCallbacks)) {
return; // no associated token found, bail
}
m_deviceEventCallbacks.erase(tokenIt);
} else {
auto sessionCallback = dynamic_pointer_cast<::uwb::RegisteredSessionCallbackToken>(tokenShared);
auto sessionCallback = dynamic_pointer_cast<::uwb::RegisteredSessionCallbackToken>(tokenShared);
if (sessionCallback) {
// treat it as a session callback
auto sessionId = sessionCallback->SessionId;

auto node = m_sessionEventCallbacks.extract(sessionId);
Expand All @@ -962,6 +953,18 @@ UwbConnector::DeregisterEventCallback(std::weak_ptr<::uwb::RegisteredCallbackTok
}
tokens.erase(tokenIt);
m_sessionEventCallbacks.insert(std::move(node));
} else {
auto deviceCallback = std::dynamic_pointer_cast<::uwb::RegisteredDeviceCallbackToken>(tokenShared);
if (deviceCallback == nullptr) {
throw std::runtime_error("invalid callback type used, this is a bug!");
}
auto tokenIt = std::find_if(std::cbegin(m_deviceEventCallbacks), std::cend(m_deviceEventCallbacks), [deviceCallback](const auto& token) {
return token.get() == deviceCallback.get();
});
if (tokenIt == std::cend(m_deviceEventCallbacks)) {
return; // no associated token found, bail
}
m_deviceEventCallbacks.erase(tokenIt);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

namespace uwb
{
/**
* @brief The following are opaque class declarations that are used in this file
*
*/
class RegisteredSessionCallbackToken;
class RegisteredDeviceCallbackToken;
} // namespace uwb
Expand Down

0 comments on commit 08aecba

Please sign in to comment.