Max OS 0.3
Loading...
Searching...
No Matches
eventHandler.h
Go to the documentation of this file.
1
12#ifndef MAXOS_COMMON_EVENTHANDLER_H
13#define MAXOS_COMMON_EVENTHANDLER_H
14
15#include <cstdint>
16#include <common/vector.h>
17
18
19namespace MaxOS::common {
20
21
31 template<typename EventType> class Event {
32 public:
33 explicit Event(EventType type);
34 ~Event();
35
36 EventType type;
37
38 union {
39 uint8_t* buffer_value;
40 uint32_t int_value;
43 };
44
51 template<typename EventType> class EventHandler {
52 public:
54 ~EventHandler() = default;
56 };
57
66 template<typename EventType> class EventManager {
67 protected:
69
70 public:
76 };
77
79
86 template<typename EventType> Event<EventType>::Event(EventType type) {
87 this->type = type;
88 }
89
90
91 template<typename EventType> Event<EventType>::~Event() = default;
92
93 template<typename EventType> EventHandler<EventType>::EventHandler() = default;
94
95
96
97
107 return event;
108 }
109
110 template<typename EventType> EventManager<EventType>::EventManager() = default;
111
112 template<typename EventType> EventManager<EventType>::~EventManager() = default;
113
114
122 // If the handler is already connected, return
123 if (m_handlers.find(handler) != m_handlers.end()) {
124 return;
125 }
126
127 m_handlers.push_back(handler);
128
129 }
130
138 // If the handler is not connected, return
139 if (m_handlers.find(handler) == m_handlers.end()) {
140 return;
141 }
142
143 m_handlers.erase(handler);
144 }
145
154
155
156 // Store a list of the results of the event handlers
157 Vector<Event<EventType>*> results;
158 for (auto& handler : m_handlers) {
159 results.push_back(handler->on_event(event));
160 }
161
162 // Free the memory used by the event
163 delete event;
164
165 return results;
166 }
167}
168
169
170#endif //MAXOS_COMMON_EVENTHANDLER_H
Used to handle an event.
virtual Event< EventType > * on_event(Event< EventType > *event)
This function is called when an event is raised.
Manages the m_handlers for a type of event, raises events and calls the m_handlers.
Vector< Event< EventType > * > raise_event(Event< EventType > *event)
Calls the on_event function of all the event m_handlers connected to the event manager and returns a ...
void disconnect_event_handler(EventHandler< EventType > *handler)
disconnect an event handler from the event manager if it is connected
void connect_event_handler(EventHandler< EventType > *handler)
connect an event handler to the event manager if it is not already connected
Vector< EventHandler< EventType > * > m_handlers
Used to store information about an event, has a type and a return value.
union MaxOS::common::Event::@0 return_value
Event(EventType type)
Constructs a new Event object.
Dynamically stores an array of elements.
Definition vector.h:39
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333
Defines a Vector class for dynamically storing an array of elements.