Signal
Connect and emit events.
| Header | #include "nativeui/signal.h" |
| Namespace | namespace nu |
| Type | template class<typename Sig> |
The template class Signal<Sig> implements the signal/slot pattern, which
is used as event type.
It can be used to register a list of callbacks, and then dispatch the event to all the callbacks.
#include "base/logging.h"
#include "nativeui/nativeui.h"
void Main() {
nu::Signal<void(const std::string&)> event;
event.Connect([](const std::string&) {
LOG(ERROR) << "OnEvent: " << arg;
});
event.Emit("Emitted");
}
Methods
int Connect(const std::function<Sig>& slot)
Connect slot to the signal, and return an ID that can be used to
disconnect it.
Parameters
const std::function<Sig>&slot
Return
intvoid Disconnect(int id)
Disconnect the id from the signal.
Parameters
intid
void DisconnectAll()
Disconnect all slots in the signal.
bool IsEmpty() const
Return true if there is no slot connected to the signal.
Return
boolvoid Emit(Args... args)
Emit the event by passing args... to callbacks. The Sig must be in the
form of void(Args...).
Parameters
Args...args
bool Emit(Args... args)
Emit the event by passing args... to callbacks. The Sig must be in the
form of bool(Args...).
If any of the callbacks returns true, Emit would return true
immediately without executing other callbacks.
Parameters
Args...args
Return
bool