std.signals with multiple signals?
Serg Kovrov
kovrov at no.spam
Wed Nov 15 22:37:54 PST 2006
Hello, D-folks!
First, my understanding of S&S:
Instance of 'emitter' class could emit predefined set of signals.
And arbitrary class could be a 'listener', e.g. connect any of those
'signals' to a it's 'slot' (arbitrary method with particular signature).
And I expect something like this pseudo code:
connect emitter.clicked to listener.do_something
connect emitter.double_clicked to listener.do_something_else
With current std.signals
(http://digitalmars.com/d/phobos/std_signals.html) I don't get how to
choose signal to connect to particular slot. I mean, how to define more
than one signal?
Meanwhile, before std.signals and all S&S buzz, I used followed
(simplified, but you could see the idea) approach:
> class Emitter
> {
> enum EV {CLICKED, DBLCLICKED}
> void delegate()[EV] subscribers;
>
> void emit(EV ev) {
> auto fn = ev in subscribers;
> try { if (fn != null) (*fn)(); }
> catch (Exception wtf) { writefln("WTF? %s", wtf); }
> }
> void subscribe(EV ev, void delegate() fn) {
> subscribers[ev] = fn;
> }
> void click() { emit(EV.CLICKED); }
> void dblclick() { emit(EV.DBLCLICKED); }
> }
>
> class Listener
> {
> void do_something() { writefln("do_something"); }
> void do_something_else() { writefln("do_something_else"); }
> }
>
> void main()
> {
> auto emitter = new Emitter();
> auto listener = new Listener();
> emitter.subscribe(Emitter.EV.CLICKED,
> &listener.do_something);
> emitter.subscribe(Emitter.EV.DBLCLICKED,
> &listener.do_something_else);
> emitter.click();
> delete listener;
> emitter.dblclick();
> }
And it quite works for me, is there something wrong with it?
--
serg.
More information about the Digitalmars-d
mailing list