Signal

Connect and emit events.

Header#include "nativeui/signal.h"
Namespacenamespace nu
Typetemplate 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

Return

int

void Disconnect(int id)

Disconnect the id from the signal.

Parameters

void DisconnectAll()

Disconnect all slots in the signal.

bool IsEmpty() const

Return true if there is no slot connected to the signal.

Return

bool

void Emit(Args... args)

Emit the event by passing args... to callbacks. The Sig must be in the form of void(Args...).

Parameters

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

Return

bool