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