]>
Commit | Line | Data |
---|---|---|
01873032 | 1 | // RUNNABLE_PHOBOS_TEST |
b4c522fa IB |
2 | import std.stdio; |
3 | import std.signals; | |
4 | ||
5 | class Observer | |
6 | { // our slot | |
7 | void watch(string msg, int i) | |
8 | { | |
9 | writefln("Observed msg '%s' and value %s", msg, i); | |
10 | } | |
11 | ||
12 | void watch2(int i, int j) | |
13 | { | |
14 | writefln("Observed msg %s,%s", i, j); | |
15 | } | |
16 | } | |
17 | ||
18 | class Foo | |
19 | { | |
20 | int value() { return _value; } | |
21 | ||
22 | int value(int v) | |
23 | { | |
24 | if (v != _value) | |
25 | { _value = v; | |
26 | // call all the connected slots with the two parameters | |
27 | emit("setting new value", v); | |
28 | } | |
29 | return v; | |
30 | } | |
31 | ||
32 | // Mix in all the code we need to make Foo into a signal | |
33 | mixin Signal!(string, int); | |
34 | ||
35 | private : | |
36 | int _value; | |
37 | } | |
38 | ||
39 | void test1() | |
40 | { | |
41 | Foo a = new Foo; | |
42 | Observer o = new Observer; | |
43 | ||
44 | a.value = 3; // should not call o.watch() | |
45 | a.connect(&o.watch); // o.watch is the slot | |
46 | a.value = 4; // should call o.watch() | |
47 | a.disconnect(&o.watch); // o.watch is no longer a slot | |
48 | a.value = 5; // so should not call o.watch() | |
49 | a.connect(&o.watch); // connect again | |
50 | a.value = 6; // should call o.watch() | |
51 | delete o; // destroying o should automatically disconnect it | |
52 | a.value = 7; // should not call o.watch() | |
53 | } | |
54 | ||
55 | /******************************************/ | |
56 | ||
57 | class Input | |
58 | { | |
59 | mixin Signal!(int, int) click; | |
60 | mixin Signal!(char) keyDown; | |
61 | } | |
62 | ||
63 | void test2() | |
64 | { | |
65 | Observer o = new Observer(); | |
66 | Input a = new Input(); | |
67 | a.click.connect(&o.watch2); | |
68 | a.click.emit(5,6); | |
69 | } | |
70 | ||
71 | /******************************************/ | |
72 | ||
73 | class Args3 | |
74 | { | |
75 | int foo; | |
76 | } | |
77 | ||
78 | class Base3 | |
79 | { | |
80 | ~this() | |
81 | { | |
82 | writefln("Base3 dtor!"); | |
83 | } | |
84 | } | |
85 | ||
86 | class Test3 : Base3 | |
87 | { | |
88 | mixin Signal!(Args3) A; | |
89 | mixin Signal!(Args3) B; | |
90 | ||
91 | ~this() | |
92 | { | |
93 | writefln("Test3 dtor"); | |
94 | } | |
95 | } | |
96 | ||
97 | ||
98 | void test3() | |
99 | { | |
100 | auto test = new Test3; | |
101 | } | |
102 | ||
103 | ||
104 | /******************************************/ | |
105 | ||
106 | int main() | |
107 | { | |
108 | test1(); | |
109 | test2(); | |
110 | test3(); | |
111 | ||
112 | printf("Success\n"); | |
113 | return 0; | |
114 | } |