libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
Signal< Args > Class Template Reference

Type-safe signal/slot mechanism for decoupled event notification. More...

#include <signal.h>

Classes

struct  removeConstAndRef
 Metafunction that strips const and reference qualifiers from a type. More...
 

Public Types

using Function = std::function< void(Args...)>
 Callable type that slots must conform to.
 
template<typename T >
using RemoveConstAndRef = typename removeConstAndRef< T >::type
 Convenience alias for removeConstAndRef.
 

Public Member Functions

 Signal (void *owner=nullptr, const char *prototype=nullptr)
 Signal constructor.
 
voidowner () const
 Returns the owner of this signal, or nullptr if not defined.
 
const charprototype () const
 Returns the prototype string, or nullptr if not defined.
 
size_t connect (Function slot, void *ptr=nullptr)
 Connects this signal to a callable (lambda or function).
 
template<typename T >
size_t connect (T *obj, void(T::*memberFunction)(Args...))
 Connects this signal to an object member function.
 
void disconnect (size_t slotID)
 Disconnects a slot by its connection ID.
 
template<typename T >
void disconnect (const T *object, void(T::*memberFunction)(Args...))
 Disconnects a slot by its object and member function.
 
template<typename T >
void disconnectFromObject (const T *object)
 Disconnects all slots connected from the given object.
 
void emit (Args... args) const
 Emits this signal.
 

Static Public Member Functions

static VariantList pack (Args... args)
 Packs the arguments into a VariantList.
 

Detailed Description

template<typename... Args>
class Signal< Args >

Type-safe signal/slot mechanism for decoupled event notification.

Allows connecting callable slots (lambdas or member functions) that are invoked when the signal is emitted. Template parameters define the argument types passed through the signal.

Template Parameters
ArgsThe argument types carried by the signal.

Constructor & Destructor Documentation

◆ Signal()

template<typename... Args>
Signal< Args >::Signal ( void owner = nullptr,
const char prototype = nullptr 
)
inline

Signal constructor.

Parameters
[in]ownerPointer to the object/data that owns this signal.
[in]prototypeOptional prototype string describing the signal signature.

Member Function Documentation

◆ connect() [1/2]

template<typename... Args>
size_t Signal< Args >::connect ( Function  slot,
void ptr = nullptr 
)
inline

Connects this signal to a callable (lambda or function).

The callable prototype must match the argument types used to define this signal.

Parameters
slotThe callable to invoke when the signal is emitted.
ptrOptional pointer associated with this connection, used for later disconnection by object.
Returns
The slot connection ID.

◆ connect() [2/2]

template<typename... Args>
template<typename T >
size_t Signal< Args >::connect ( T obj,
void(T::*)(Args...)  memberFunction 
)
inline

Connects this signal to an object member function.

The member function prototype must match the argument types used to define this signal.

Example usage:

Dynamic array container wrapping std::vector.
Definition list.h:40
List()=default
Default constructor. Creates an empty list.
Template Parameters
TThe object type.
Parameters
objPointer to the object instance.
memberFunctionPointer to the member function to call.
Returns
The slot connection ID.

◆ disconnect() [1/2]

template<typename... Args>
template<typename T >
void Signal< Args >::disconnect ( const T object,
void(T::*)(Args...)  memberFunction 
)
inline

Disconnects a slot by its object and member function.

Example usage:

Template Parameters
TThe object type.
Parameters
objectPointer to the object whose slot should be removed.
memberFunctionPointer to the member function to disconnect.

◆ disconnect() [2/2]

template<typename... Args>
void Signal< Args >::disconnect ( size_t  slotID)
inline

Disconnects a slot by its connection ID.

Use the ID returned by connect() to disconnect that slot from this signal.

Parameters
slotIDThe connection ID returned by connect().

◆ disconnectFromObject()

template<typename... Args>
template<typename T >
void Signal< Args >::disconnectFromObject ( const T object)
inline

Disconnects all slots connected from the given object.

Template Parameters
TThe object type.
Parameters
objectPointer to the object whose slots should be removed.

◆ emit()

template<typename... Args>
void Signal< Args >::emit ( Args...  args) const
inline

Emits this signal.

Invokes every connected slot with the supplied arguments, in the order they were connected.

Parameters
argsThe arguments to forward to each slot.

◆ pack()

template<typename... Args>
static VariantList Signal< Args >::pack ( Args...  args)
inlinestatic

Packs the arguments into a VariantList.

Packs all the signal parameters into a VariantList object that can be used to marshal the data to either defer the emit or pass it as an event to another object (e.g. on another thread). Note, this will also make copies of any arguments passed by reference as the references probably won't be valid by the time you want to use the container.

Parameters
argsThe signal arguments to pack.
Returns
A VariantList containing copies of all arguments.

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