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) shared

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.

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.

emit
void emit(T1 i)

Call each of the connected slots, passing the argument(s) i to them.

Examples

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

Meta