signal slots (new, fixed version)
Lutger
lutger.blijdestijn at gmail.com
Fri Oct 27 07:24:12 PDT 2006
After Bastiaan Veelo's post I realized my signal slots library should be
rewritten, which is now mostly done. You can get the result or browse
the docs here:
lutger.ifastnet.com
Unreferenced objects are now properly garbage collected and
disconnected, emitting a signal (should) incurs less overhead,
both memory- and performance wise.
It still needs some testing and a little work, but it's functional.
Features:
- free functions, delegate literals and delegates can act as slots
- clean syntax, freestanding signals.
- emitting signals are locked to prevent possible stack overflow, can be
turned off with version=Unsafe.
- can set a default handler, which is invoked when a signal is emitted
that has no slots connected.
- signals can have return values.
- signals have opApply and opApplyReverse for custom iteration, mapping
and combining return values.
Example:
import std.stdio, sslot.signal;
class Button
{
this() { onClick = new Signal!(); }
Signal!() onClick;
}
class Receiver : ISlotTracking
{
void handleClick() { writefln("button clicked"); }
mixin SlotTracking;
}
Button button = new Button;
Receiver receiver = new Receiver;
button.onClick.connect(&receiver.handleClick, receiver);
button.onClick(); // prints 'button clicked'
Signal!(int,int) signal = new Signal!(int,int);
signal ~= (int num) { return num * num; };
assert(signal(3) == 9);
More information about the Digitalmars-d-announce
mailing list