summaryrefslogtreecommitdiff
path: root/src/port/hooks/impl/EventSystem.cpp
blob: 44c58ec02de4349b3b4b2451389a5b3da475f32e (plain)
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
#include "EventSystem.h"
#include <stdexcept>

EventSystem* EventSystem::Instance = new EventSystem();

size_t EventSystem::RegisterListener(EventID id, EventPriority priority, EventCallback callback) {
    if (std::find(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), callback) != this->mEventListeners[id].end()) {
        throw std::runtime_error("Listener already registered");
    }

    this->mEventListeners[id].push_back(callback);
}

void EventSystem::CallEvent(EventID id, IEvent* event) {
    if (this->mEventListeners[id].empty()) {
        return;
    }

    for (auto& callback : this->mEventListeners[id]) {
        callback(event);
    }
}

extern "C" size_t EventSystem_RegisterListener(EventID id, EventPriority priority, EventCallback callback) {
    return EventSystem::Instance->RegisterListener(id, priority, callback);
}

extern "C" void EventSystem_CallEvent(EventID id, void* event) {
    EventSystem::Instance->CallEvent(id, static_cast<IEvent*>(event));
}