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.hpp69
1 files changed, 29 insertions, 40 deletions
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp
index 70f0a31..1e505f4 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/api/EventManager.hpp
@@ -1,17 +1,18 @@
#include "EventManager.h"
+
namespace crepe {
template <typename EventType>
void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
std::type_index event_type = typeid(EventType);
std::unique_ptr<EventHandlerWrapper<EventType>> handler
= std::make_unique<EventHandlerWrapper<EventType>>(callback);
if (channel) {
- std::unordered_map<int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = this->subscribers_by_event_id[event_type];
+ HandlersMap & handlers_map = this->subscribers_by_event_id[event_type];
auto handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
handlers->second.emplace_back(std::move(handler));
@@ -19,8 +20,7 @@ void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
handlers_map[channel].emplace_back(std::move(handler));
}
} else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = this->subscribers[event_type];
+ HandlersVec & handlers = this->subscribers[event_type];
handlers.emplace_back(std::move(handler));
}
}
@@ -29,8 +29,7 @@ template <typename EventType>
void EventManager::queue_event(EventType && event, int channel) {
std::type_index event_type = std::type_index(typeid(EventType));
- std::unique_ptr<EventType> event_ptr
- = std::make_unique<EventType>(std::forward<EventType>(event));
+ auto event_ptr = std::make_unique<EventType>(std::forward<EventType>(event));
std::tuple<std::unique_ptr<Event>, int, std::type_index> tuple(
std::move(event_ptr), channel, event_type);
@@ -39,59 +38,51 @@ void EventManager::queue_event(EventType && event, int channel) {
template <typename EventType>
void EventManager::trigger_event(const EventType & event, int channel) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
std::type_index event_type = std::type_index(typeid(EventType));
if (channel > 0) {
- std::unordered_map<int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = this->subscribers_by_event_id[event_type];
+ HandlersMap & handlers_map = this->subscribers_by_event_id[event_type];
auto handlers_it = handlers_map.find(channel);
if (handlers_it != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (auto it = handlers.begin();
- it != handlers.end();++it) {
+ HandlersVec & handlers = handlers_it->second;
+ for (auto it = handlers.begin(); it != handlers.end(); ++it) {
// stops when callback returns true
- if((*it)->exec(event)){
+ if ((*it)->exec(event)) {
break;
}
}
}
} else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = this->subscribers[event_type];
- for (auto it
- = handlers.begin();
- it != handlers.end();++it) {
+ HandlersVec & handlers = this->subscribers[event_type];
+ for (auto it = handlers.begin(); it != handlers.end(); ++it) {
// stops when callback returns true
- if((*it)->exec(event)){
- break;
- }
+ if ((*it)->exec(event)) {
+ break;
+ }
}
}
}
template <typename EventType>
-void EventManager::unsubscribe(const EventHandler<EventType> & callback,
- int channel) {
+void EventManager::unsubscribe(const EventHandler<EventType> & callback, int channel) {
+ using HandlersMap = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
+ using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
+
std::type_index event_type(typeid(EventType));
std::string handler_name = callback.target_type().name();
if (channel) {
auto subscriber_list = this->subscribers_by_event_id.find(event_type);
if (subscriber_list != this->subscribers_by_event_id.end()) {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = subscriber_list->second;
- auto handlers = handlers_map.find(channel);
+ HandlersMap & handlers_map = subscriber_list->second;
+ auto handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & callbacks
- = handlers->second;
- for (auto it = callbacks.begin();
- it != callbacks.end(); ++it) {
+ HandlersVec & callbacks = handlers->second;
+ for (auto it = callbacks.begin(); it != callbacks.end(); ++it) {
if ((*it)->get_type() == handler_name) {
it = callbacks.erase(it);
return;
@@ -102,10 +93,8 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback,
} else {
auto handlers_it = this->subscribers.find(event_type);
if (handlers_it != this->subscribers.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (auto it = handlers.begin();
- it != handlers.end(); ++it) {
+ HandlersVec & handlers = handlers_it->second;
+ for (auto it = handlers.begin(); it != handlers.end(); ++it) {
if ((*it)->get_type() == handler_name) {
it = handlers.erase(it);
return;