Example for creating multiple signals?

Juan Manuel Cabo juanmanuel.cabo at gmail.com
Thu May 23 08:38:36 PDT 2013


On Thursday, 23 May 2013 at 15:22:06 UTC, Gary Willoughby wrote:
> I'm just looking at the signals example here: 
> http://dlang.org/phobos/std_signals.html#.Signal where it says 
> "Different signals can be added to a class by naming the 
> mixins.". Has anyone got an example of this please? I'd like to 
> have a class raise different signals if possible.


class A {
     mixin Signal!() onChanged;
     mixin Signal!(int, string) onErrorMsg;

     ...
}

class B {
     private void a_ErrorMsg(int code, string msg) {
         writeln("Error code: ", code, ": ", msg);
     }

     void attachToA(A a) {
         a.onErrorMsg.connect(&a_ErrorMsg);
     }

     void detachFromA(A a) {
         a.onErrorMsg.disconnect(&a_ErrorMsg);
     }
}



It works perfectly. BUT, keep in mind that a signal connection 
doesn't keep your class alive if the GC wants to take it (if the 
GC collects your class, it is disconnected). This means that if 
you want to keep your listener classes around, they must be 
referenced somewhere.

--jm




More information about the Digitalmars-d mailing list