Only one signal per object?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Oct 24 17:24:14 PDT 2011


It seems neither std.signal nor the new signals module support ref
parameters, which is a shame because I've found a very good use for
them.

The std.signal won't compile for handlers with ref parameters, the new
one compiles but throws an access violation at runtime.

Example usage: If you have a child widget and its parent widget which
is a Layout widget, when the child emits a "doMove(Widget widget,
Point newPoint)" signal the layout widget could handle it first by
calling connectFirst when the parenting takes place. This allows the
layout 'doMove' handler to modify the parameter so it fits some given
limit, after which the next handler is called to further process the
signal.

In code this would look something like:
auto button = new Widget;
auto layout = new Layout(...);
layout.addWidget(layout);
// internally layout would do: widget.doMove.connectFirst(&layout.doMove);
widget.move(Point(10, 10));

The last call translates to, say:
-> widget.doMove.emit(this, Point(10, 10));

Which calls all the handlers sequentially until one of them returns 0:
-> widget.doMove.handler[0]  // layout.doMove()
-> widget.doMove.handler[1]  // handler 2..

and in Layout:
bool doMove(Widget widget, ref Point newPoint)
{
    newPoint.x = max(newPoint.x, 10);  // limit to 10
    newPoint.y = max(newPoint.y, 10);
    return true;
}

and then the other handlers would be called, but with potentially
modified arguments. This would be super-useful to me right now. I
haven't yet gone through the new signal implementation, but maybe I'll
figure out how to add this feature myself.


More information about the Digitalmars-d-learn mailing list