Class for object-oriented handling of user-defined events. More...
Inherits NonCopyable.
Public Member Functions | |
EventSystem () | |
Default constructor. More... | |
void | triggerEvent (const Event &event) |
Fires an event. More... | |
Connection | connect (const EventId &trigger, std::function< void(const Event &)> unaryListener) |
Connects an event to the specified unary listener. More... | |
Connection | connect0 (const EventId &trigger, std::function< void()> nullaryListener) |
Connects an event to the specified nullary listener. More... | |
void | clearConnections (EventId identifier) |
Disconnects listeners associated with a given event type. More... | |
void | clearAllConnections () |
Disconnects all listeners. More... | |
Class for object-oriented handling of user-defined events.
Event | The event type. For example, this can be a class or enum. |
EventId | The type that distinguishes different events. This isn't necessarily the event type itself. Consider SFML: The event type would be sf::Event, but the identifier is sf::Event::EventType. When you use custom events where EventId and Event are distinct types, you have to provide a global function EventId getEventId(const Event&) in the namespace of the Event type definition. Besides, the event system requires less-than operator < for EventId (enums can use the implicit conversion to int). |
thor::EventSystem< Event, EventId >::EventSystem | ( | ) |
Default constructor.
Sets up an EventSystem where Event and EventId are the same types.
void thor::EventSystem< Event, EventId >::clearAllConnections | ( | ) |
Disconnects all listeners.
Removes all event-listener connections, independently of the event type.
void thor::EventSystem< Event, EventId >::clearConnections | ( | EventId | identifier | ) |
Disconnects listeners associated with a given event type.
identifier | The event type identifier of which you want to remove all connected listeners. |
Connection thor::EventSystem< Event, EventId >::connect | ( | const EventId & | trigger, |
std::function< void(const Event &)> | unaryListener | ||
) |
Connects an event to the specified unary listener.
Duplicates are allowed (thus, the listener is invoked multiple times). Use this function if your callback should receive the event that triggered it as a parameter.
trigger | The event you want to associate with a listener function. |
unaryListener | The function that is invoked when a trigger event is fired. |
Connection thor::EventSystem< Event, EventId >::connect0 | ( | const EventId & | trigger, |
std::function< void()> | nullaryListener | ||
) |
Connects an event to the specified nullary listener.
Duplicates are allowed (thus, the listener is invoked multiple times). Use this function if you don't care about the event that triggered the callback, and thus don't need a parameter for it.
trigger | The event you want to associate with a listener function. |
nullaryListener | The function that is invoked when a trigger event is fired. |
void thor::EventSystem< Event, EventId >::triggerEvent | ( | const Event & | event | ) |
Fires an event.
Calls all listener functions that are currently associated with event.