ezEngine  Release 25.03
ezEventBase< EventData, MutexType, EventType > Class Template Reference

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.
 

Detailed Description

template<typename EventData, typename MutexType, ezEventType EventType>
class ezEventBase< EventData, MutexType, EventType >

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.

Note
A class holding an ezEvent member needs to provide public access to the member for external code to be able to register as an event handler. To make it possible to prevent external code from also raising events, all functions that are needed for listening are const, and all others are non-const. Therefore, simply make event members private and provide const reference access through a public getter.

Member Enumeration Documentation

◆ anonymous enum

template<typename EventData , typename MutexType , ezEventType EventType>
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.

Member Function Documentation

◆ AddEventHandler() [1/2]

template<typename EventData , typename MutexType , ezEventType EventType>
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).

◆ AddEventHandler() [2/2]

template<typename EventData , typename MutexType , ezEventType EventType>
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.

◆ Broadcast()

template<typename EventData , typename MutexType , ezEventType EventType>
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.

◆ RemoveEventHandler() [1/2]

template<typename EventData , typename MutexType , ezEventType EventType>
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.

◆ RemoveEventHandler() [2/2]

template<typename EventData , typename MutexType , ezEventType EventType>
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.


The documentation for this class was generated from the following files: