www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Example for creating multiple signals?

reply "Gary Willoughby" <dev kalekold.net> writes:
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.
May 23 2013
next sibling parent "Juan Manuel Cabo" <juanmanuel.cabo gmail.com> writes:
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
May 23 2013
prev sibling parent "Gary Willoughby" <dev kalekold.net> writes:
Thanks.
May 23 2013