ImFusion SDK 4.3
SignalImpl< UseMutex, ArgTypes > Class Template Referencefinal

#include <ImFusion/Core/Signal.h>

Implementation for a specific signal with the given signature. More...

+ Inheritance diagram for SignalImpl< UseMutex, ArgTypes >:

Detailed Description

template<bool UseMutex, typename... ArgTypes>
class ImFusion::SignalImpl< UseMutex, ArgTypes >

Implementation for a specific signal with the given signature.

You can connect an arbitrary amount of slots (i.e. function pointers) to this signal which will be called on emitSignal(). If you connect slots to member functions of classes inheriting from SignalReceiver, SignalImpl will track the lifetime of the object and automatically disconnect the slot if the object is deleted. Thus, in this case the order of destruction of signal and slot does not matter.

Based on the first template parameter the signal will be thread-safe or not. If you set UseMutex to false concurrent access will not be safe. Setting UseMutex to true will increase the signal's memory footprint by a mutex in order to make this class thread-safe. You can use the aliases Signal and ProtectedSignal to choose between the reentrant and thread-safe implementation.

By default, if a recursive emission of the same signal is detected, observers will not be notified and an assertion will be raised — unless the connection that initiated the recursion is blocked. This design was chosen since such situations usually indicate an accidental signal loop due to bidirectional dependencies which should be avoided. However, you can explicitly disable these checks by calling allowOneRecursion() if you expect a recursion to occur.

Furthermore, it is safe to connect or disconnect a to/from signal while it is currently emitting: disconnecting will have immediate effect while new connections will only become active after emitting has completed.

Template Parameters
UseMutexFlag whether to protect access to the signal by a mutex.
ArgTypesSignature of the signal/function to call.
Note
Similar to Qt signals, we deem (dis)connecting to/from ImFusion signals to not change the logic/semantic state of an object. Thus, the connect() and disconnect() overloads are const member functions and can be called on a const Signal object (or any const object having a Signal as member).
SignalImpl is assignable and copyable. However, it will not assign/copy any connections!
Warning
Be aware that SignalImpl does not implement any duplicate detection. Thus, if you connect the same slot (e.g. member function) multiple times to the same signal, that function will be called as many times as there are connections.
See also
Signals and Slots, SignalReceiver, Signal, ProtectedSignal, SignalConnection, DeprecatedSignal

Public Types

using FunctionType = std::function<void(ArgTypes...)>
 Typedef for a std::function matching ths signal type.
 
using ArgumentTuple = std::tuple<ArgTypes...>
 

Public Member Functions

 SignalImpl ()
 Default constructor.
 
 SignalImpl (const SignalImpl< UseMutex, ArgTypes... > &other)
 Copy constructor, will not copy any connections..
 
SignalImpl< UseMutex, ArgTypes... > & operator= (const SignalImpl< UseMutex, ArgTypes... > &rhs)
 Assignment operator, disconnects all existing connections but does not transfer the connections of rhs.
 
 ~SignalImpl () override
 Virtual destructor, deletes all connections and thereby also disconnects from all connected slots.
 
template<typename T>
std::shared_ptr< SignalConnectionconnect (const T *object, void(T::*methodptr)(ArgTypes...)) const
 Connects the given method pointer as slot to this signal.
 
std::shared_ptr< SignalConnectionconnect (const SignalReceiver *object, FunctionType func) const
 Connects the given free function as slot to this signal using object as lifetime tracker.
 
std::shared_ptr< SignalConnectionconnect (FunctionType func) const
 Connects the given free function as slot to this signal.
 
bool disconnect (SignalConnection *connection) const override
 Removes the given connection.
 
int disconnect (const SignalReceiver *object) const
 Disconnects all slots of the given object from this signal.
 
int disconnectAll () const
 Disconnects all slots from this signal.
 
void emitSignal (ArgTypes... args) const
 Calls all connected slots with the given arguments.
 
void allowOneRecursion () const
 Allows a single recursive emitting of this signal if called while the signal is emitting.
 
bool setBlocked (bool muteSignal) override
 Set the flag whether to block notifications of observers for this signal.
 
bool isBlocked () const
 Returns whether notifications of observers for this signal are currently blocked.
 
int numConnections () const
 Returns the current number of connections.
 
bool isConnectedTo (const SignalReceiver *receiver) const
 Returns whether this signal is connected to receiver.
 

Member Function Documentation

◆ connect() [1/3]

template<bool UseMutex, typename... ArgTypes>
template<typename T>
std::shared_ptr< SignalConnection > connect ( const T * object,
void(T::* methodptr )(ArgTypes...) ) const

Connects the given method pointer as slot to this signal.

Parameters
objectPointer to the receiver object holding the method. Must derive from SignalReceiver. Must not be 0.
methodptrPointer to method to call on emitSignal().
Returns
SignalConnection object for identifying the established signal-slot connection. You can use this to disconnect from the signal at any time.
Note
SignalImpl will track the lifetime of object and automatically disconnect the slot if object is deleted during the lifetime of this signal.

◆ connect() [2/3]

template<bool UseMutex, typename... ArgTypes>
std::shared_ptr< SignalConnection > connect ( const SignalReceiver * object,
FunctionType func ) const

Connects the given free function as slot to this signal using object as lifetime tracker.

Parameters
objectSignalReceiver to be used for lifetime tracking. If not null the connection will be disconnected automatically when the receiver is deleted.
funcFunction to call on emitSignal().
Returns
SignalConnection object for identifying the established signal-slot connection. You can use this to disconnect from the signal at any time.
Note
SignalImpl will track the lifetime of object and automatically disconnect the slot if object is deleted during the lifetime of this signal.

◆ connect() [3/3]

template<bool UseMutex, typename... ArgTypes>
std::shared_ptr< SignalConnection > connect ( FunctionType func) const

Connects the given free function as slot to this signal.

Parameters
funcFunction to call on emitSignal().
Returns
SignalConnection object for identifying the established signal-slot connection. You can use this to disconnect from the signal at any time.
Warning
This overload is potentially dangerous, since there is no way for this signal to track the lifetime of func. Thus, you have to ensure that func will remain valid for the entire lifetime of this signal or use disconnect() on the returned SignalConnection object when needed.

◆ disconnect() [1/2]

template<bool UseMutex, typename... ArgTypes>
bool disconnect ( SignalConnection * connection) const
overridevirtual

Removes the given connection.

Parameters
connectionPointer to the SignalConnection object returned during connect().
Returns
True if a slot was found and deleted, false otherwise.

Implements SignalBase.

◆ disconnect() [2/2]

template<bool UseMutex, typename... ArgTypes>
int disconnect ( const SignalReceiver * object) const

Disconnects all slots of the given object from this signal.

Parameters
objectPointer to the object holding the slot. If the pointer is 0 all connections to free functions are disconnected.
Returns
The number of connections that were found and disconnected.

◆ disconnectAll()

template<bool UseMutex, typename... ArgTypes>
int disconnectAll ( ) const

Disconnects all slots from this signal.

Returns
The number of connections that were found and disconnected.

◆ emitSignal()

template<bool UseMutex, typename... ArgTypes>
void emitSignal ( ArgTypes... args) const

Calls all connected slots with the given arguments.

Note
This function will not allow for recursive emitting of the same signal and nested emits will be discarded. Use allowOneRecursion() to explicitly opt-in for recursive signal emitting support.

◆ allowOneRecursion()

template<bool UseMutex, typename... ArgTypes>
void allowOneRecursion ( ) const

Allows a single recursive emitting of this signal if called while the signal is emitting.

Otherwise does nothing.

Note
Call this within your slot function if you need to re-emit the same signal. If you use a signalBlocker to avoid infinite recursion in your slot function, the signal will detect and allow the recursion without the need for calling this function.

◆ setBlocked()

template<bool UseMutex, typename... ArgTypes>
bool setBlocked ( bool muteSignal)
overridevirtual

Set the flag whether to block notifications of observers for this signal.

If muteSignal is true, any subsequent call to emitSignal will do nothing. The caller is responsible to reset the blocked state to its original value when appropriate.

Returns
Previous value of isBlocked().
See also
SignalBlocker for a more convenient and exception-safe interface to temporarily block a signal.

Implements SignalBase.


The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close