This short article demonstrates the use of Signals, Slots and Notifications in the Kigs framework. Signals, Slots and Notifications are used to communicate between instances.
Table of Contents
Now that we have seen how
CoreModifiable methods (Kigs Framework Introduction (4/8) - Methods) works, we are going to see in this short article how to take advantage of this mechanism to easily connect instances together.
A list of signals can be declared at compile time for a given class using helper macro
It is then possible to retrieve the list of declared signals for this class using method "
std::cout << "simpleclass instance has following signals available" << std::endl;
for (const auto& s : signallist)
std::cout << s._id_name << std::endl;
std::cout << s._id << std::endl;
A declared signal can be emitted using
It's also possible with the same mechanism to send undeclared signals:
Signals can be emitted with or without parameters.
For an instance to receive a signal from another one, a connection must be setup :
KigsCore::Connect(app, "doSomething", this, "doSomething");
MethodWithParams" is a
CoreModifiable method declared with
WRAP_METHODS macro or with
CoreModifiable method article in this series for details.
void MethodWithParams(float p1, float p2);
To disconnect two instances,
KigsCore::Disconnect method is also available :
KigsCore::Disconnect(simplecass.get(), "SendSignal1", this, "MethodWithParams");
It's also possible to connect signal to a lambda function directly:
KigsCore::Connect(simpleclass.get(), "SendSignal2", this, "lambda", [this](int p1)
std::cout << "lambda received parameter " << p1 << std::endl;
It's possible to ask instance factory to create a connection for each created instance of a particular class:
("PreInit", this, "OnSimpleClassPreInit", "SimpleClass");
If the last parameter is not set, the connection is added for all types of created instances.
To remove the automatic connection:
("PreInit", this, "OnSimpleClassPreInit");
Another way to create connections is to use the
NotificationCenter can connect two instances like with signal/slot mechanism, but also notify an instance to listen for notification posted by any sender instance.
NotificationCenter can register observer instances:
CoreModifiable* parameter is possible to listen to notifications only coming from the given sender instance.
To remove an observer:
A notification can then be sent using
A signal/slot connection can be set in XML adding this kind of item to an instance:
<Connect Si="SignalName" E="EmitterPath" SL="SlotName" R="ReceiverPath"/>
Emitter path and receiver path are classic
CoreModifiable search paths. "
this" or "
self" can also be used to indicate owning instance.
An observer can also be set on an instance adding this item:
<OnE N="NotificationName" A="CalledMethod"/>
Find all the sample code from this wiki section in Sample6 project (browse the code).
- Kigs Framework Introduction (1/8) - Overview
- Kigs Framework Introduction (2/8) - CoreModifiable
- Kigs Framework Introduction (3/8) - Attributes
- Kigs Framework Introduction (4/8) - Methods
- Kigs Framework Introduction (5/8) - CoreItem
- Kigs Framework Introduction (6/8) - Signal, Slot, Notification
- Kigs Framework Introduction (7/8) - Lua Binding
- Kigs Framework Introduction (8/8) - Data Driven Application
- 26th February, 2020: Initial version
- 19th March, 2020: Article (7/8) added to the series and little fix in sample code
- 17th June, 2020 : Added final article of the series