VOX
A little voxel engine
Loading...
Searching...
No Matches
EventManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "AbstractEvent.hpp"
4#include "Events.hpp"
5
6#include <string>
7#include <functional>
8#include <memory>
9#include <shared_mutex>
10#include <mutex>
11
12namespace Event
13{
14 template<typename EventType>
15 using Handler = std::function<void(const EventType & e)>;
16
18 {
19
20 public:
21
22 void exec(const AbstractEvent & e) { call(e); }
23
24 virtual std::string getType() const = 0;
25
26 private:
27
28 virtual void call(const AbstractEvent& e) = 0;
29
30 };
31
32
33 template<typename EventType>
35 {
36
37 public:
38
39 explicit HandlerWrapper(const Handler<EventType> & handler):
40 m_handler(handler),
41 m_handlerType(m_handler.target_type().name())
42 {
43 }
44
45 private:
46
47 void call(const AbstractEvent & e) override
48 {
49 if (e.getType() == EventType::getStaticType())
50 {
51 m_handler(static_cast<const EventType &>(e));
52 }
53 }
54
55 std::string getType() const override { return m_handlerType; }
56
57 Handler<EventType> m_handler;
58 const std::string m_handlerType;
59 };
60
61
62 class Manager
63 {
64
65 public:
66
67 Manager() = default;
68 virtual ~Manager() = default;
69
70 template<typename EventType>
71 void subscribe(const Handler<EventType> & handler)
72 {
73 std::unique_lock lock(m_mutex);
74
75 auto wrapper = std::make_unique<HandlerWrapper<EventType>>(handler);
76 m_subscribers[EventType::getStaticType()].push_back(std::move(wrapper));
77 }
78
79 template<typename EventType>
80 void unsubscribe(const Handler<EventType> & handler)
81 {
82 std::unique_lock lock(m_mutex);
83
84 const Type type = EventType::getStaticType();
85 const auto it = m_subscribers.find(type);
86 if (it != m_subscribers.end())
87 {
88 auto & subscribers = it->second;
89 const auto it = std::find_if(subscribers.begin(), subscribers.end(),
90 [&handler](const std::unique_ptr<HandlerWrapperInterface> & wrapper)
91 {
92 return wrapper->getType() == handler.target_type().name();
93 }
94 );
95 if (it != subscribers.end())
96 {
97 subscribers.erase(it);
98 }
99 }
100 }
101
103 {
104 std::shared_lock lock(m_mutex);
105
106 const Type type = e.getType();
107 const auto it = m_subscribers.find(type);
108 if (it != m_subscribers.end())
109 {
110 for (auto & subscriber : it->second)
111 {
112 subscriber->exec(e);
113 }
114 }
115 }
116
117 private:
118
119 std::unordered_map<Type, std::vector<std::unique_ptr<HandlerWrapperInterface>>> m_subscribers;
120 std::shared_mutex m_mutex;
121
122 };
123
124} // namespace Event
Definition: AbstractEvent.hpp:11
virtual Type getType() const noexcept=0
Definition: EventManager.hpp:18
void exec(const AbstractEvent &e)
Definition: EventManager.hpp:22
virtual std::string getType() const =0
Definition: EventManager.hpp:35
HandlerWrapper(const Handler< EventType > &handler)
Definition: EventManager.hpp:39
Definition: EventManager.hpp:63
void triggerEvent(const AbstractEvent &e)
Definition: EventManager.hpp:102
Manager()=default
virtual ~Manager()=default
void unsubscribe(const Handler< EventType > &handler)
Definition: EventManager.hpp:80
void subscribe(const Handler< EventType > &handler)
Definition: EventManager.hpp:71
Definition: AbstractEvent.cpp:4
std::type_index Type
Definition: AbstractEvent.hpp:8
std::function< void(const EventType &e)> Handler
Definition: EventManager.hpp:15