aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventManager.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api/EventManager.hpp')
-rw-r--r--src/crepe/api/EventManager.hpp87
1 files changed, 42 insertions, 45 deletions
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp
index 481017d..a04a43a 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/api/EventManager.hpp
@@ -3,7 +3,8 @@
namespace crepe {
template <typename EventType>
-void EventManager::subscribe(const EventHandler<EventType> & callback, int channel, int priority) {
+void EventManager::subscribe(const EventHandler<EventType> & callback, int channel,
+ int priority) {
std::type_index event_type = typeid(EventType);
std::unique_ptr<EventHandlerWrapper<EventType>> handler
@@ -14,69 +15,65 @@ void EventManager::subscribe(const EventHandler<EventType> & callback, int chann
.channel = channel,
.priority = priority,
});
- // Sort handlers by priority (highest first)
- std::sort(handlers.begin(), handlers.end(), [](const CallbackEntry &a, const CallbackEntry &b) {
- return a.priority > b.priority;
- });
+ // Sort handlers by priority (highest first)
+ std::sort(handlers.begin(), handlers.end(),
+ [](const CallbackEntry & a, const CallbackEntry & b) {
+ return a.priority > b.priority;
+ });
}
template <typename EventType>
-void EventManager::queue_event(const EventType & event, int channel,int priority) {
- static_assert(std::is_base_of<Event, EventType>::value, "EventType must derive from Event");
+void EventManager::queue_event(const EventType & event, int channel, int priority) {
+ static_assert(std::is_base_of<Event, EventType>::value,
+ "EventType must derive from Event");
std::type_index event_type = typeid(EventType);
auto event_ptr = std::make_unique<EventType>(event);
- this->events_queue.push_back(
- QueueEntry{
- .event = std::move(event_ptr),
- .channel = channel,
- .type = event_type,
- .priority = priority
- }
- );
+ this->events_queue.push_back(QueueEntry{.event = std::move(event_ptr),
+ .channel = channel,
+ .type = event_type,
+ .priority = priority});
}
template <typename EventType>
void EventManager::trigger_event(const EventType & event, int channel) {
- std::type_index event_type = typeid(EventType);
+ std::type_index event_type = typeid(EventType);
- auto handlers_it = this->subscribers.find(event_type);
- if (handlers_it != this->subscribers.end()) {
- const std::vector<CallbackEntry> &handlers = handlers_it->second;
+ auto handlers_it = this->subscribers.find(event_type);
+ if (handlers_it != this->subscribers.end()) {
+ const std::vector<CallbackEntry> & handlers = handlers_it->second;
- for (const CallbackEntry &handler : handlers) {
- if (handler.channel != channel && handler.channel != CHANNEL_ALL) {
- continue;
- }
- if (handler.callback->exec(event)) {
- break;
- }
- }
- }
+ for (const CallbackEntry & handler : handlers) {
+ if (handler.channel != channel && handler.channel != CHANNEL_ALL) {
+ continue;
+ }
+ if (handler.callback->exec(event)) {
+ break;
+ }
+ }
+ }
}
-
template <typename EventType>
void EventManager::unsubscribe(const EventHandler<EventType> & callback, int channel) {
- std::type_index event_type = typeid(EventType);
- std::string handler_name = callback.target_type().name();
+ std::type_index event_type = typeid(EventType);
+ std::string handler_name = callback.target_type().name();
- // Find the list of handlers for this event type
- auto handlers_it = this->subscribers.find(event_type);
- if (handlers_it != this->subscribers.end()) {
- std::vector<CallbackEntry> & handlers = handlers_it->second;
+ // Find the list of handlers for this event type
+ auto handlers_it = this->subscribers.find(event_type);
+ if (handlers_it != this->subscribers.end()) {
+ std::vector<CallbackEntry> & handlers = handlers_it->second;
- for (auto it = handlers.begin(); it != handlers.end(); ) {
- // Match based on handler type and channel
- if (it->callback->get_type() == handler_name && it->channel == channel) {
- it = handlers.erase(it);
- return;
- }
- ++it;
- }
- }
+ for (auto it = handlers.begin(); it != handlers.end();) {
+ // Match based on handler type and channel
+ if (it->callback->get_type() == handler_name && it->channel == channel) {
+ it = handlers.erase(it);
+ return;
+ }
+ ++it;
+ }
+ }
}
-
} // namespace crepe