ImFusion SDK 4.3
SignalSlot.cpp
#include <ImFusion/Core/Signal.h>
using namespace ImFusion;
class MySubject
{
public:
Signal<> signal1;
};
MySubject subject;
subject.signal2.connect([](int i, std::string s) {
std::cout << "Signal2 was emitted with int " << i << " and string " << s << ".";
});
// create a connection and store the connection object.
std::shared_ptr<const SignalConnection> connection = subject.signal1.connect([]() { std::cout << "Signal1 was emitted."; });
// ...
// when needed disconnect the observer
connection->disconnect();
// Inherit from SignalReceiver to allow for lifetime-tracking.
class MyObserver : public SignalReceiver{public: void onEvent(){std::cout << "MyObserver has been notified.";
}
}
{
MyObserver observer;
// establish automatic lifetime tracking by passing a reference to the observer instance
subject.signal1.connect(&observer, [&observer]() { observer.onEvent(); });
// alternative/simpler overload for member functions:
subject.signal1.connect(&observer, &MyObserver::onEvent);
}
// Since MyObserver inherits from SignalReceiver and the object instance was provided when
// connecting to the signal, both connections will be disconnected automatically when observer
// is destroyed on scope exit.
MyObserver observer1, observer2;
auto connection1 = signal.connect(&observer1, &MyObserver::onEvent);
auto connection2 = signal.connect(&observer2, &MyObserver::onEvent);
{
SignalBlocker blocker(signal); // temporary blocks all connections of signal
signal.emitSignal(); // neither observer1 nor observer2 will be notified
}
{
SignalBlocker blocker(*connection1); // temporary blocks only connection1
signal.emitSignal(); // only observer2 will be notified
}
{
SignalBlocker b(signal, observer2); // temporary blocks all connections between signal and observer2
signal.emitSignal(); // only observer1 will be notified
}
// notifies both observer1 and observer2 since signals/connections are no longer blocked
signal.emitSignal();
Convenient scope guard class to temporarily block signals/connections so that observers are not notif...
Definition Signal.h:316
Base class for classes that can contain slots (i.e.
Definition Signal.h:28
SignalImpl< true, ArgTypes... > ProtectedSignal
Alias for a protected signal, which is thread-safe.
Definition Signal.h:346
SignalImpl< false, ArgTypes... > Signal
Alias for a non-protected signal, which is reentrant but not thread-safe.
Definition Signal.h:341
Namespace of the ImFusion SDK.
Definition Assert.h:7
T signal(T... args)
Search Tab / S to search, Esc to close