![]() |
ezEngine
Release 25.03
|
This class propagates event information to registered event handlers. More...
#include <Event.h>
Classes | |
class | Unsubscriber |
An object that can be passed to ezEvent::AddEventHandler to store the subscription information and automatically remove the event handler upon destruction. More... | |
Public Types | |
enum | { RecursionDepthSupported = (EventType == ezEventType::Default || ezConversionTest<MutexType, ezNoMutex>::sameType == 1) ? 1 : 0, MaxRecursionDepthDefault = RecursionDepthSupported ? 0 : 255 } |
Implementation specific constants. More... | |
using | Handler = ezDelegate< void(EventData)> |
Notification callback type for events. | |
Public Member Functions | |
void | Broadcast (EventData pEventData, ezUInt8 uiMaxRecursionDepth=MaxRecursionDepthDefault) |
This function will broadcast to all registered users, that this event has just happened. Setting uiMaxRecursionDepth will allow you to permit recursions. When broadcasting consider up to what depth you want recursions to be permitted. By default no recursion is allowed. More... | |
ezEventSubscriptionID | AddEventHandler (Handler handler) const |
Adds a function as an event handler. All handlers will be notified in the order that they were registered. More... | |
void | AddEventHandler (Handler handler, Unsubscriber &inout_unsubscriber) const |
An overload that adds an event handler and initializes the given Unsubscriber object. More... | |
void | RemoveEventHandler (const Handler &handler) const |
Removes a previously registered handler. It is an error to remove a handler that was not registered. More... | |
void | RemoveEventHandler (ezEventSubscriptionID &inout_id) const |
Removes a previously registered handler via the returned subscription ID. More... | |
bool | HasEventHandler (const Handler &handler) const |
Checks whether an event handler has already been registered. | |
void | Clear () |
Removes all registered event handlers. | |
bool | IsEmpty () const |
Returns true, if no event handlers are registered. | |
EZ_DISALLOW_COPY_AND_ASSIGN (ezEventBase) | |
Protected Member Functions | |
ezEventBase (ezAllocator *pAllocator) | |
Constructor. | |
This class propagates event information to registered event handlers.
An event can be anything that "happens" that might be of interest to other code, such that it can react on it in some way. Just create an instance of ezEvent and call Broadcast() on it. Other interested code needs const access to the event variable to be able to call AddEventHandler() and RemoveEventHandler(). To pass information to the handlers, create a custom struct with event information and then pass a (const) reference to that data through Broadcast().
If you need to modify the event while broadcasting, for example inside one of the registered event handlers, set EventType = ezEventType::CopyOnBroadcast. Each broadcast will then copy the event handler array before signaling them, allowing modifications during broadcasting.
anonymous enum |
Implementation specific constants.
Enumerator | |
---|---|
RecursionDepthSupported | Whether the uiMaxRecursionDepth parameter to Broadcast() is supported in this implementation or not. |
MaxRecursionDepthDefault | Default value for the maximum recursion depth of Broadcast. As limiting the recursion depth is not supported when EventType == ezEventType::CopyAndBroadcast and MutexType != ezNoMutex the default value for that case is the maximum. |
ezEventSubscriptionID ezEventBase< EventData, MutexType, EventType >::AddEventHandler | ( | Handler | handler | ) | const |
Adds a function as an event handler. All handlers will be notified in the order that they were registered.
The return value can be stored and used to remove the event handler later again.
A callback can be registered multiple times with different pass-through data (or even with the same, though that is less useful).
void ezEventBase< EventData, MutexType, EventType >::AddEventHandler | ( | Handler | handler, |
Unsubscriber & | inout_unsubscriber | ||
) | const |
An overload that adds an event handler and initializes the given Unsubscriber object.
When the Unsubscriber is destroyed, it will automatically remove the event handler.
void ezEventBase< EventData, MutexType, EventType >::Broadcast | ( | EventData | pEventData, |
ezUInt8 | uiMaxRecursionDepth = MaxRecursionDepthDefault |
||
) |
This function will broadcast to all registered users, that this event has just happened. Setting uiMaxRecursionDepth will allow you to permit recursions. When broadcasting consider up to what depth you want recursions to be permitted. By default no recursion is allowed.
The notification is sent to all event handlers in the order that they were registered.
void ezEventBase< EventData, MutexType, EventType >::RemoveEventHandler | ( | const Handler & | handler | ) | const |
Removes a previously registered handler. It is an error to remove a handler that was not registered.
Use exactly the same combination of callback/pass-through-data to unregister an event handlers. Otherwise an error occurs.
void ezEventBase< EventData, MutexType, EventType >::RemoveEventHandler | ( | ezEventSubscriptionID & | inout_id | ) | const |
Removes a previously registered handler via the returned subscription ID.
The ID will be reset to zero. If this is called with a zero ID, nothing happens.