blob: 08c9ee70e3ef85ea09bec89621dd4187d0f0e7f8 (
plain) (
tree)
|
|
#include "Event.hpp"
std::list<EventListener*> EventSystem::listeners;
std::recursive_mutex EventSystem::listenersMutex;
EventListener::EventListener() {
std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex);
EventSystem::listeners.push_back(this);
}
EventListener::~EventListener() {
std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex);
EventSystem::listeners.remove(this);
}
void EventListener::HandleEvent() {
if (!NotEmpty())
return;
std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
Event event = events.front();
events.pop();
std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
if (handlers[event.id]) {
handlers[event.id](event);
}
}
void EventListener::HandleAllEvents() {
if (!NotEmpty())
return;
std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
while (!events.empty()) {
Event event = events.front();
events.pop();
if (handlers[event.id]) {
handlers[event.id](event);
}
}
}
bool EventListener::NotEmpty() {
PollEvents();
std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
bool ret = !events.empty();
return ret;
}
void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) {
std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
handlers[eventId] = data;
}
void EventListener::PollEvents() {
std::lock_guard<std::recursive_mutex> rawLock (rawEventsMutex);
if (rawEvents.empty())
return;
std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
while (!rawEvents.empty()) {
Event event = rawEvents.front();
rawEvents.pop();
if (handlers[event.id])
events.push(event);
}
}
|