LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
LiteFX::Event< TEventArgs > Class Template Referencefinal

A class that is used to declare an event, which a number of listeners can subscribe to. More...

#include <app.hpp>

Public Types

using event_args_type = TEventArgs
 
using delegate_type = Delegate<void, const void*, TEventArgs>
 
using function_type = typename delegate_type::function_type
 
using event_token_type = typename delegate_type::token_type
 

Public Member Functions

constexpr Event ()=default
 Initializes a new event.
 
constexpr Event (const Event &_other)
 Creates a copy of a event.
 
constexpr Event (Event &&_other) noexcept=default
 Takes over another instance of a event.
 
constexpr Eventoperator= (const Event &_other)
 Assigns a event by copying it.
 
constexpr Eventoperator= (Event &&_other) noexcept=default
 Assigns a event by taking it over.
 
constexpr ~Event () noexcept=default
 Releases the event instance.
 
event_token_type add (const function_type &subscriber)
 Subscribes an event handler to the event.
 
bool remove (const delegate_type &subscriber) noexcept
 Unsubscribes an event handler from the event.
 
bool remove (event_token_type token) noexcept
 Unsubscribes an event handler from the event.
 
void clear () noexcept
 Clears the event handlers.
 
void invoke (const void *sender, const TEventArgs &args) const
 Invokes all event handlers of the event.
 
bool contains (event_token_type token) const noexcept
 Returns true, if the event contains a subscriber with the provided token .
 
const delegate_typehandler (event_token_type token) const
 Returns the delegate associated with token .
 
 operator bool () const noexcept
 Returns true, if any event handler is attached to the event, false otherwise.
 
event_token_type operator+= (const function_type &subscriber)
 Subscribes an event handler to the event.
 
bool operator-= (const delegate_type &subscriber) noexcept
 Unsubscribes an event handler from the event.
 
bool operator-= (event_token_type token) noexcept
 Unsubscribes an event handler from the event.
 
void operator() (const void *sender, const TEventArgs &args) const
 Invokes all event handlers of the event.
 
const delegate_typeoperator[] (event_token_type token) const
 Returns the delegate associated with token .
 

Detailed Description

template<typename TEventArgs>
class LiteFX::Event< TEventArgs >

A class that is used to declare an event, which a number of listeners can subscribe to.

A listener that subscribes to the event is called event handler. The event handler needs to be invokable and identifiable. Because of this, a Delegate is created for the event handler. A delegate stores the event handler, as well as a token to identify the event handler. Event handlers must expose the a common signature: they do not return anything and accept two parameters. The first parameter is an unformatted pointer to the event sender (i.e., the object that invoked the event handlers). The second parameter contains additional arguments (TEventArgs ), that are passed to all handlers. Note that the sender can also be nullptr.

Template Parameters
TEventArgsThe type of the additional event arguments.
See also
EventArgs

Member Typedef Documentation

◆ delegate_type

template<typename TEventArgs >
using LiteFX::Event< TEventArgs >::delegate_type = Delegate<void, const void*, TEventArgs>

◆ event_args_type

template<typename TEventArgs >
using LiteFX::Event< TEventArgs >::event_args_type = TEventArgs

◆ event_token_type

template<typename TEventArgs >
using LiteFX::Event< TEventArgs >::event_token_type = typename delegate_type::token_type

◆ function_type

template<typename TEventArgs >
using LiteFX::Event< TEventArgs >::function_type = typename delegate_type::function_type

Constructor & Destructor Documentation

◆ Event() [1/3]

template<typename TEventArgs >
LiteFX::Event< TEventArgs >::Event ( )
constexprdefault

Initializes a new event.

◆ Event() [2/3]

template<typename TEventArgs >
LiteFX::Event< TEventArgs >::Event ( const Event< TEventArgs > & _other)
inlineconstexpr

Creates a copy of a event.

This constructor is implemented in order to support copying of types that contain events. Subscribers are not copied!

Parameters
_otherThe event instance to copy.

◆ Event() [3/3]

template<typename TEventArgs >
LiteFX::Event< TEventArgs >::Event ( Event< TEventArgs > && _other)
constexprdefaultnoexcept

Takes over another instance of a event.

Parameters
_otherThe event instance to take over.

◆ ~Event()

template<typename TEventArgs >
LiteFX::Event< TEventArgs >::~Event ( )
constexprdefaultnoexcept

Releases the event instance.

Member Function Documentation

◆ add()

template<typename TEventArgs >
event_token_type LiteFX::Event< TEventArgs >::add ( const function_type & subscriber)
inline

Subscribes an event handler to the event.

Parameters
subscriberA delegate for the event handler.
Returns
A unique token of the event handler.

◆ clear()

template<typename TEventArgs >
void LiteFX::Event< TEventArgs >::clear ( )
inlinenoexcept

Clears the event handlers.

◆ contains()

template<typename TEventArgs >
bool LiteFX::Event< TEventArgs >::contains ( event_token_type token) const
inlinenoexcept

Returns true, if the event contains a subscriber with the provided token .

Parameters
tokenThe token of an event.
Returns
true, if the event contains a subscriber with the provided token , false otherwise.

◆ handler()

template<typename TEventArgs >
const delegate_type & LiteFX::Event< TEventArgs >::handler ( event_token_type token) const
inline

Returns the delegate associated with token .

Parameters
tokenThe token to query for.
Returns
A reference of the delegate associated with token .
Exceptions
InvalidArgumentExceptionThrown, if the event does not have a subscriber with the provided token.

◆ invoke()

template<typename TEventArgs >
void LiteFX::Event< TEventArgs >::invoke ( const void * sender,
const TEventArgs & args ) const
inline

Invokes all event handlers of the event.

Parameters
senderThe source of the event.
argsThe additional event arguments.

◆ operator bool()

template<typename TEventArgs >
LiteFX::Event< TEventArgs >::operator bool ( ) const
inlineexplicitnoexcept

Returns true, if any event handler is attached to the event, false otherwise.

Returns
true, if any event handler is attached to the event, false otherwise.

◆ operator()()

template<typename TEventArgs >
void LiteFX::Event< TEventArgs >::operator() ( const void * sender,
const TEventArgs & args ) const
inline

Invokes all event handlers of the event.

Parameters
senderThe source of the event.
argsThe additional event arguments.

◆ operator+=()

template<typename TEventArgs >
event_token_type LiteFX::Event< TEventArgs >::operator+= ( const function_type & subscriber)
inline

Subscribes an event handler to the event.

Parameters
subscriberA delegate for the event handler.
Returns
A unique token of the event handler.

◆ operator-=() [1/2]

template<typename TEventArgs >
bool LiteFX::Event< TEventArgs >::operator-= ( const delegate_type & subscriber)
inlinenoexcept

Unsubscribes an event handler from the event.

Parameters
subscriberA delegate for the event handler.
Returns
true, if the event handler has been removed, false otherwise.

◆ operator-=() [2/2]

template<typename TEventArgs >
bool LiteFX::Event< TEventArgs >::operator-= ( event_token_type token)
inlinenoexcept

Unsubscribes an event handler from the event.

Parameters
tokeThe unique token of the event handler.
Returns
true, if the event handler has been removed, false otherwise.

◆ operator=() [1/2]

template<typename TEventArgs >
Event & LiteFX::Event< TEventArgs >::operator= ( const Event< TEventArgs > & _other)
inlineconstexpr

Assigns a event by copying it.

This operator is implemented in order to support copying of types that contain events. Subscribers are not copied and previous subscriptions are cleared!

Parameters
_otherThe event instance to copy.
Returns
A reference to the current event instance.

◆ operator=() [2/2]

template<typename TEventArgs >
Event & LiteFX::Event< TEventArgs >::operator= ( Event< TEventArgs > && _other)
constexprdefaultnoexcept

Assigns a event by taking it over.

Parameters
_otherThe event to take over.
Returns
A reference to the current event instance.

◆ operator[]()

template<typename TEventArgs >
const delegate_type & LiteFX::Event< TEventArgs >::operator[] ( event_token_type token) const
inline

Returns the delegate associated with token .

Parameters
tokenThe token to query for.
Returns
A reference of the delegate associated with token .
Exceptions
InvalidArgumentExceptionThrown, if the event does not have a subscriber with the provided token.

◆ remove() [1/2]

template<typename TEventArgs >
bool LiteFX::Event< TEventArgs >::remove ( const delegate_type & subscriber)
inlinenoexcept

Unsubscribes an event handler from the event.

Parameters
subscriberA delegate for the event handler.
Returns
true, if the event handler has been removed, false otherwise.

◆ remove() [2/2]

template<typename TEventArgs >
bool LiteFX::Event< TEventArgs >::remove ( event_token_type token)
inlinenoexcept

Unsubscribes an event handler from the event.

Parameters
tokeThe unique token of the event handler.
Returns
true, if the event handler has been removed, false otherwise.