refactor: use std::function for Event callback

based on 6d347337c3
This commit is contained in:
sithlord48
2025-06-18 00:17:54 -04:00
committed by Nick Bolton
parent c15214aee7
commit 3a35b183d3
53 changed files with 476 additions and 680 deletions

View File

@ -1,5 +1,6 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
@ -8,7 +9,6 @@
#include "base/EventQueue.h"
#include "arch/Arch.h"
#include "base/IEventJob.h"
#include "base/Log.h"
#include "base/SimpleEventQueueBuffer.h"
#include "mt/Lock.h"
@ -159,12 +159,12 @@ bool EventQueue::getEvent(Event &event, double timeout)
bool EventQueue::dispatchEvent(const Event &event)
{
void *target = event.getTarget();
IEventJob *job = getHandler(event.getType(), target);
if (job == nullptr) {
job = getHandler(EventTypes::Unknown, target);
if (const auto *type_handler = getHandler(event.getType(), target); type_handler) {
(*type_handler)(event);
return true;
}
if (job != nullptr) {
job->run(event);
if (const auto *any_handler = getHandler(EventTypes::Unknown, target); any_handler) {
(*any_handler)(event);
return true;
}
return false;
@ -257,46 +257,32 @@ void EventQueue::deleteTimer(EventQueueTimer *timer)
m_buffer->deleteTimer(timer);
}
void EventQueue::adoptHandler(EventTypes type, void *target, IEventJob *handler)
void EventQueue::addHandler(EventTypes type, void *target, const EventHandler &handler)
{
std::scoped_lock lock{m_mutex};
m_handlers[target][type].reset(handler);
m_handlers[target][type] = handler;
}
void EventQueue::removeHandler(EventTypes type, void *target)
{
std::unique_ptr<IEventJob> handler;
{
std::scoped_lock lock{m_mutex};
HandlerTable::iterator index = m_handlers.find(target);
if (index != m_handlers.end()) {
TypeHandlerTable &typeHandlers = index->second;
TypeHandlerTable::iterator index2 = typeHandlers.find(type);
if (index2 != typeHandlers.end()) {
handler = std::move(index2->second);
typeHandlers.erase(index2);
}
std::scoped_lock lock{m_mutex};
HandlerTable::iterator index = m_handlers.find(target);
if (index != m_handlers.end()) {
TypeHandlerTable &typeHandlers = index->second;
TypeHandlerTable::iterator index2 = typeHandlers.find(type);
if (index2 != typeHandlers.end()) {
typeHandlers.erase(index2);
}
}
// handler is erased here. It is done outside of lock in order to avoid potential deadlock.
}
void EventQueue::removeHandlers(void *target)
{
std::vector<std::unique_ptr<IEventJob>> handlers;
{
std::scoped_lock lock{m_mutex};
HandlerTable::iterator index = m_handlers.find(target);
if (index != m_handlers.end()) {
// copy to handlers array and clear table for target
TypeHandlerTable &typeHandlers = index->second;
for (auto &[key, value] : typeHandlers) {
handlers.push_back(std::move(value));
}
typeHandlers.clear();
}
std::scoped_lock lock{m_mutex};
HandlerTable::iterator index = m_handlers.find(target);
if (index != m_handlers.end()) {
index->second.clear();
}
// handler is erased here. It is done outside of lock in order to avoid potential deadlock.
}
bool EventQueue::isEmpty() const
@ -304,14 +290,14 @@ bool EventQueue::isEmpty() const
return (m_buffer->isEmpty() && getNextTimerTimeout() != 0.0);
}
IEventJob *EventQueue::getHandler(EventTypes type, void *target) const
const EventQueue::EventHandler *EventQueue::getHandler(EventTypes type, void *target) const
{
std::scoped_lock lock{m_mutex};
if (HandlerTable::const_iterator index = m_handlers.find(target); index != m_handlers.end()) {
const TypeHandlerTable &typeHandlers = index->second;
TypeHandlerTable::const_iterator index2 = typeHandlers.find(type);
if (index2 != typeHandlers.end()) {
return index2->second.get();
return &index2->second;
}
}
return nullptr;