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 SIGNALS
:
SIGNALS(PreInit,
PostInit,
Uninit,
Destroy,
Update, NotifyUpdate,
AddItem,
RemoveItem,
PrepareExport,
EndExport);
It is then possible to retrieve the list of declared signals for this class using method "GetSignalList
" :
std::cout << "simpleclass instance has following signals available" << std::endl;
auto signallist=simpleclass->GetSignalList();
for (const auto& s : signallist)
{
std::cout << s.toString() << std::endl;
}
A declared signal can be emitted using EmitSignal
method:
EmitSignal(Signals::SendSignal1,32,64);
It's also possible with the same mechanism to send undeclared signals:
EmitSignal("doSomething");
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(simpleclass.get(),"SendSignal1",this, "MethodWithParams");
KigsCore::Connect(app, "doSomething", this, "doSomething");
"MethodWithParams
" is a CoreModifiable
method declared with WRAP_METHODS
macro or with DECLARE_METHOD/COREMODIFIABLE_METHODS
. See CoreModifiable
method article in this series for details.
void MethodWithParams(float p1, float p2);
WRAP_METHODS(MethodWithParams);
DECLARE_METHOD(doSomething);
COREMODIFIABLE_METHODS(doSomething);
To disconnect two instances, KigsCore::Disconnect
method is also available :
CMSP simplecass=GetInstanceByPath("simpleclass");
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:
KigsCore::Instance()->GetInstanceFactory()->addModifiableCallback
("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:
KigsCore::Instance()->GetInstanceFactory()->removeModifiableCallback
("PreInit", this, "OnSimpleClassPreInit");
Another way to create connections is to use the NotificationCenter
class. NotificationCenter
can connect two instances like with signal/slot mechanism, but also notify an instance to listen for notification posted by any sender instance.
The NotificationCenter
can register observer instances:
KigsCore::GetNotificationCenter()->addObserver(this,"CatchNotifMethod","doSomethingElseNotif");
A fourth CoreModifiable*
parameter is possible to listen to notifications only coming from the given sender instance.
To remove an observer:
KigsCore::GetNotificationCenter()->removeObserver(this, "doSomethingElseNotif");
A notification can then be sent using NotificationCenter
"postNotificationName
" method:
KigsCore::GetNotificationCenter()->postNotificationName("doSomethingElseNotif", this);
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
- 1st March, 2023 : Article updated after framework refactory