Signal

Mixin to create a signal within a class object.

Different signals can be added to a class by naming the mixins.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Aliases

slot_t
alias slot_t = void delegate(T1)

A slot is implemented as a delegate. The slot_t is the type of the delegate. The delegate must be to an instance of a class or an interface to a class instance. Delegates to struct instances or nested functions must not be used as slots. This applies even if the nested function does not access it's parent function variables.

Functions

connect
void connect(slot_t slot)

Add a slot to the list of slots to be called when emit() is called.

disconnect
void disconnect(slot_t slot)

Remove a slot from the list of slots to be called when emit() is called.

disconnectAll
void disconnectAll()

Disconnect all the slots.

emit
void emit(T1 i)

Call each of the connected slots, passing the argument(s) i to them. Nested call will be ignored.

Examples

1 import std.signals;
2 
3 int observedMessageCounter = 0;
4 
5 class Observer
6 {   // our slot
7     void watch(string msg, int value)
8     {
9         switch (observedMessageCounter++)
10         {
11             case 0:
12                 assert(msg == "setting new value");
13                 assert(value == 4);
14                 break;
15             case 1:
16                 assert(msg == "setting new value");
17                 assert(value == 6);
18                 break;
19             default:
20                 assert(0, "Unknown observation");
21         }
22     }
23 }
24 
25 class Observer2
26 {   // our slot
27     void watch(string msg, int value)
28     {
29     }
30 }
31 
32 class Foo
33 {
34     int value() { return _value; }
35 
36     int value(int v)
37     {
38         if (v != _value)
39         {   _value = v;
40             // call all the connected slots with the two parameters
41             emit("setting new value", v);
42         }
43         return v;
44     }
45 
46     // Mix in all the code we need to make Foo into a signal
47     mixin Signal!(string, int);
48 
49   private :
50     int _value;
51 }
52 
53 Foo a = new Foo;
54 Observer o = new Observer;
55 auto o2 = new Observer2;
56 auto o3 = new Observer2;
57 auto o4 = new Observer2;
58 auto o5 = new Observer2;
59 
60 a.value = 3;                // should not call o.watch()
61 a.connect(&o.watch);        // o.watch is the slot
62 a.connect(&o2.watch);
63 a.connect(&o3.watch);
64 a.connect(&o4.watch);
65 a.connect(&o5.watch);
66 a.value = 4;                // should call o.watch()
67 a.disconnect(&o.watch);     // o.watch is no longer a slot
68 a.disconnect(&o3.watch);
69 a.disconnect(&o5.watch);
70 a.disconnect(&o4.watch);
71 a.disconnect(&o2.watch);
72 a.value = 5;                // so should not call o.watch()
73 a.connect(&o2.watch);
74 a.connect(&o.watch);        // connect again
75 a.value = 6;                // should call o.watch()
76 destroy(o);                 // destroying o should automatically disconnect it
77 a.value = 7;                // should not call o.watch()
78 
79 assert(observedMessageCounter == 2);

Meta