Signal

A thread local signal-slot implementation.

Constructors

this
this(Signal rhs)
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

Params
alias Params = P
Undocumented in source.
Slot
alias Slot = void delegate(P) nothrow
Undocumented in source.

Functions

disconnectAll
void disconnectAll()

Detaches all connections from the signal.

emit
void emit(P params)

Emits the signal.

Properties

empty
bool empty [@property getter]

Determines if any connections are connected to the signal.

socket
SignalSocket!Params socket [@property getter]

Returns a reference to the socket of this signal.

Examples

// declare a signal
Signal!int signal;

{
	// define a connection
	SignalConnection conn;

	// establish a connection between the signal and a delegate
	signal.socket.connect(conn, (i) { assert(i == 42); });

	// emitting the signal will call the above delegate
	signal.emit(42);
}

// once there are no copies of the connection left, it will
// automatically disconnect, so the following emit will
// not have an effect.
signal.emit(13);


// if a class connects to a member function, it must store the connection,
// so that the connection life time is limited to the instance lifetime.
class Test {
	SignalConnection conn;

	this()
	@safe nothrow {
		signal.socket.connect(conn, &slot);
	}

	void slot(int i)
	@safe nothrow {
		assert(i == 32);
	}
}

auto t = new Test;
signal.emit(32);

This example shots the recommended convention for defining signals.

class Widget {
	private {
		Signal!bool m_visibilityChangeSignal;
	}

	@property ref SignalSocket!bool visibilityChangeSignal() { return m_visibilityChangeSignal.socket; }

	void setVisibility(bool v)
	{
		// ...
		m_visibilityChangeSignal.emit(v);
	}
}

class Layout {
	private {
		SignalConnection m_visConn;
		Widget m_client;
	}

	void setClient(Widget w)
	{
		m_client = w;
		// automatically disconnects a possible connection to a previous widget
		w.visibilityChangeSignal.connect(m_visConn, &onChildVisibilityChange);
	}

	void onChildVisibilityChange(bool)
	nothrow {
		// ...
	}
}

auto l = new Layout;
auto w = new Widget;
l.setClient(w);
w.setVisibility(true);

Meta