OpenLexocad  27.1
entt::emitter< Derived > Class Template Reference

General purpose event emitter. More...

#include <entt.hpp>

Classes

struct  connection
 Generic connection type for events. More...
 

Public Types

template<typename Event >
using listener = typename event_handler< Event >::listener_type
 Type of listeners accepted for the given event. More...
 

Public Member Functions

 emitter () ENTT_NOEXCEPT=default
 Default constructor. More...
 
virtual ~emitter () ENTT_NOEXCEPT
 Default destructor. More...
 
 emitter (emitter &&)=default
 Default move constructor. More...
 
emitteroperator= (emitter &&)=default
 Default move assignment operator. More...
 
template<typename Event , typename... Args>
void publish (Args &&... args)
 Emits the given event. More...
 
template<typename Event >
connection< Event > on (listener< Event > instance)
 Registers a long-lived listener with the event emitter. More...
 
template<typename Event >
connection< Event > once (listener< Event > instance)
 Registers a short-lived listener with the event emitter. More...
 
template<typename Event >
void erase (connection< Event > conn) ENTT_NOEXCEPT
 Disconnects a listener from the event emitter. More...
 
template<typename Event >
void clear () ENTT_NOEXCEPT
 Disconnects all the listeners for the given event type. More...
 
void clear () ENTT_NOEXCEPT
 Disconnects all the listeners. More...
 
template<typename Event >
bool empty () const ENTT_NOEXCEPT
 Checks if there are listeners registered for the specific event. More...
 
bool empty () const ENTT_NOEXCEPT
 Checks if there are listeners registered with the event emitter. More...
 

Detailed Description

template<typename Derived>
class entt::emitter< Derived >

General purpose event emitter.

The emitter class template follows the CRTP idiom. To create a custom emitter type, derived classes must inherit directly from the base class as:

struct my_emitter: emitter<my_emitter> {
// ...
}

Handlers for the type of events are created internally on the fly. It's not required to specify in advance the full list of accepted types.
Moreover, whenever an event is published, an emitter provides the listeners with a reference to itself along with a const reference to the event. Therefore listeners have an handy way to work with it without incurring in the need of capturing a reference to the emitter.

Template Parameters
DerivedActual type of emitter that extends the class template.

Member Typedef Documentation

◆ listener

template<typename Derived >
template<typename Event >
using entt::emitter< Derived >::listener = typename event_handler<Event>::listener_type

Type of listeners accepted for the given event.

Constructor & Destructor Documentation

◆ emitter() [1/2]

template<typename Derived >
entt::emitter< Derived >::emitter ( )
default

Default constructor.

◆ ~emitter()

template<typename Derived >
virtual entt::emitter< Derived >::~emitter ( )
inlinevirtual

Default destructor.

◆ emitter() [2/2]

template<typename Derived >
entt::emitter< Derived >::emitter ( emitter< Derived > &&  )
default

Default move constructor.

Member Function Documentation

◆ clear() [1/2]

template<typename Derived >
template<typename Event >
void entt::emitter< Derived >::clear ( void  )
inline

Disconnects all the listeners for the given event type.

All the connections previously returned for the given event are invalidated. Using them results in undefined behavior.

Template Parameters
EventType of event to reset.

◆ clear() [2/2]

template<typename Derived >
void entt::emitter< Derived >::clear ( void  )
inline

Disconnects all the listeners.

All the connections previously returned are invalidated. Using them results in undefined behavior.

◆ empty() [1/2]

template<typename Derived >
template<typename Event >
bool entt::emitter< Derived >::empty ( ) const
inline

Checks if there are listeners registered for the specific event.

Template Parameters
EventType of event to test.
Returns
True if there are no listeners registered, false otherwise.

◆ empty() [2/2]

template<typename Derived >
bool entt::emitter< Derived >::empty ( ) const
inline

Checks if there are listeners registered with the event emitter.

Returns
True if there are no listeners registered, false otherwise.

◆ erase()

template<typename Derived >
template<typename Event >
void entt::emitter< Derived >::erase ( connection< Event >  conn)
inline

Disconnects a listener from the event emitter.

Do not use twice the same connection to disconnect a listener, it results in undefined behavior. Once used, discard the connection object.

Template Parameters
EventType of event of the connection.
Parameters
connA valid connection.

◆ on()

template<typename Derived >
template<typename Event >
connection<Event> entt::emitter< Derived >::on ( listener< Event >  instance)
inline

Registers a long-lived listener with the event emitter.

This method can be used to register a listener designed to be invoked more than once for the given event type.
The connection returned by the method can be freely discarded. It's meant to be used later to disconnect the listener if required.

The listener is as a callable object that can be moved and the type of which is void(const Event &, Derived &).

Note
Whenever an event is emitted, the emitter provides the listener with a reference to the derived class. Listeners don't have to capture those instances for later uses.
Template Parameters
EventType of event to which to connect the listener.
Parameters
instanceThe listener to register.
Returns
Connection object that can be used to disconnect the listener.

◆ once()

template<typename Derived >
template<typename Event >
connection<Event> entt::emitter< Derived >::once ( listener< Event >  instance)
inline

Registers a short-lived listener with the event emitter.

This method can be used to register a listener designed to be invoked only once for the given event type.
The connection returned by the method can be freely discarded. It's meant to be used later to disconnect the listener if required.

The listener is as a callable object that can be moved and the type of which is void(const Event &, Derived &).

Note
Whenever an event is emitted, the emitter provides the listener with a reference to the derived class. Listeners don't have to capture those instances for later uses.
Template Parameters
EventType of event to which to connect the listener.
Parameters
instanceThe listener to register.
Returns
Connection object that can be used to disconnect the listener.

◆ operator=()

template<typename Derived >
emitter& entt::emitter< Derived >::operator= ( emitter< Derived > &&  )
default

Default move assignment operator.

Returns
This emitter.

◆ publish()

template<typename Derived >
template<typename Event , typename... Args>
void entt::emitter< Derived >::publish ( Args &&...  args)
inline

Emits the given event.

All the listeners registered for the specific event type are invoked with the given event. The event type must either have a proper constructor for the arguments provided or be an aggregate type.

Template Parameters
EventType of event to publish.
ArgsTypes of arguments to use to construct the event.
Parameters
argsParameters to use to initialize the event.

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