blob: c8579477072206b4e3d83283d9707f3f6e959592 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include "Event.hpp"
std::list<EventListener*> EventSystem::listeners;
std::mutex EventSystem::listenersMutex;
EventListener::EventListener() {
std::lock_guard<std::mutex> listenersLock(EventSystem::listenersMutex);
EventSystem::listeners.push_back(this);
}
EventListener::~EventListener() {
std::lock_guard<std::mutex> listenersLock(EventSystem::listenersMutex);
EventSystem::listeners.remove(this);
}
void EventListener::HandleEvent() {
std::lock_guard<std::mutex> lock(eventsQueueMutex);
std::lock_guard<std::mutex> lockHandlers(handlersMutex);
Event event = events.front();
events.pop();
if (handlers[event.id]) {
handlers[event.id](event);
}
}
void EventListener::HandleAllEvents() {
std::lock_guard<std::mutex> lock(eventsQueueMutex);
std::lock_guard<std::mutex> lockHandlers(handlersMutex);
while (!events.empty()) {
Event event = events.front();
events.pop();
if (handlers[event.id]) {
handlers[event.id](event);
}
}
}
bool EventListener::NotEmpty() {
std::lock_guard<std::mutex> lock(eventsQueueMutex);
return !events.empty();
}
void EventListener::WaitEvent() {
eventsQueueMutex.lock();
while (events.empty()) {
eventsQueueMutex.unlock();
eventsQueueMutex.lock();
}
eventsQueueMutex.unlock();
}
|