12 #include <unordered_map>
14 #include <type_traits>
59 static const std::map<EventType, std::string> InternalEventNames{
83 static const int DEFAULT_MAX_EVENT_LOG_LENGTH = 100000;
88 template <
typename EventData = NoEventData>
94 : _eventLog(eventLog), _eventType(eventType){};
100 void publish(EventData eventData) { _publish(eventData); }
105 template <
typename Function>
108 std::function<void(EventData)> callback =
function;
109 _callbacks.push_back(
function);
114 std::vector<EventType> &_eventLog;
119 void _publish(EventData eventData)
122 if (_eventLog.size() >= DEFAULT_MAX_EVENT_LOG_LENGTH)
124 _eventLog.push_back(_eventType);
126 for (std::function<
void(EventData)> callback : _callbacks)
130 std::vector<std::function<void(EventData)>> _callbacks;
141 template <
class EventData = NoEventData>
149 return getHandler<EventData>(eventType);
158 template <
class EventData = NoEventData>
161 getHandler<EventData>(eventType).publish(eventData);
167 template <
class EventData = NoEventData>
170 return std::any_cast<EventHandler<EventData> &>(_handlers[eventType]);
174 std::vector<EventType> eventLog;
179 std::unordered_map<EventType, std::any> _handlers;
class that store all the callback functions for a specific event type
Definition: Event.hpp:90
void publish(EventData eventData)
call all the subscribed functions with the given eventData
Definition: Event.hpp:100
void publish()
call all the subscribed functions with no data (use when EventData == NoEventData)
Definition: Event.hpp:102
EventHandler(const EventType eventType)
Definition: Event.hpp:96
void subscribe(const Function &function)
register the given function to be called when the event is published
Definition: Event.hpp:106
class to help create and manage all the event handlers
Definition: Event.hpp:135
EventHandler< EventData > & addHandler(EventType eventType)
create a new event handler for the given event type
Definition: Event.hpp:142
void removeHandler(const EventType eventType)
remove an event handler
Definition: Event.hpp:153
void publish(const EventType eventType, EventData eventData)
call all the subscribed functions with the given eventData
Definition: Event.hpp:159
EventHandler< EventData > & getHandler(const EventType eventType)
return an event handler by its type
Definition: Event.hpp:168
Definition: AssetManager.hpp:15
std::size_t EventType
value to describe the event type
Definition: Event.hpp:23
Event
enum of all event types
Definition: Event.hpp:30
empty struct to describe an event that dosent provide data to subscribed functions
Definition: Event.hpp:26