annotate d1/qtd/Signal.d @ 314:80b52f5e97b6 lifetime

Structure without common root dir
author maxter <spambox@d-coding.com>
date Wed, 23 Dec 2009 23:17:36 +0200
parents 8674fd5f34f4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
311
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
1 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
2 *
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
3 * Copyright: Copyright QtD Team, 2008-2009
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
4 * Authors: Max Samukha, Eldar Insafutdinov
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
5 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
6 *
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
7 * Copyright QtD Team, 2008-2009
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
8 * Distributed under the Boost Software License, Version 1.0.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
9 * (See accompanying file boost-license-1.0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
10 *
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
11 */
314
80b52f5e97b6 Structure without common root dir
maxter <spambox@d-coding.com>
parents: 311
diff changeset
12 module qtd.Signal;
311
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
13
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
14 public import qt.QGlobal;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
15 import tango.core.Exception;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
16 import tango.core.Traits;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
17 import tango.core.Thread;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
18 import tango.stdc.stdlib : crealloc = realloc, cfree = free;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
19 import tango.stdc.string : memmove;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
20
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
21 private: // private by default
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
22
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
23 alias void delegate(Object) DEvent;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
24
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
25 extern(C) void rt_attachDisposeEvent(Object o, DEvent e);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
26 extern(C) void rt_detachDisposeEvent(Object o, DEvent e);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
27 extern(C) Object _d_toObject(void* p);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
28
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
29 void realloc(T)(ref T[] a, size_t length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
30 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
31 a = (cast(T*)crealloc(a.ptr, length * T.sizeof))[0..length];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
32 if (!a.ptr)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
33 new OutOfMemoryException(__FILE__, __LINE__);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
34 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
35
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
36 unittest
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
37 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
38 int[] a;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
39 realloc(a, 16);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
40 assert(a.length == 16);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
41 foreach (i, ref e; a)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
42 e = i;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
43 realloc(a, 4096);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
44 assert(a.length == 4096);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
45 foreach (i, e; a[0..16])
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
46 assert(e == i);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
47 cfree(a.ptr);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
48 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
49
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
50 // TODO: This one should be replaced with an appropriate library function
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
51 char[] __toString(long v)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
52 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
53 if (v == 0)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
54 return "0";
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
55
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
56 char[] ret;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
57
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
58 bool neg;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
59 if (v < 0)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
60 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
61 neg = true;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
62 v = -v;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
63 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
64
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
65 while (v != 0)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
66 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
67 ret = cast(char)(v % 10 + '0') ~ ret;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
68 v = cast(long)(v / 10);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
69 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
70
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
71 if (neg)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
72 ret = "-" ~ ret;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
73
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
74 return ret;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
75 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
76
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
77 template ToString(long i)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
78 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
79 const string ToString = __toString(i);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
80 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
81
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
82 //TODO: should be in the standard library
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
83 struct STuple(A...)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
84 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
85 static string genSTuple()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
86 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
87 string r = "";
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
88 foreach (i, e; A)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
89 r ~= A[i].stringof ~ " _" ~ ToString!(i) ~ ";";
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
90 return r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
91 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
92
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
93 mixin (genSTuple);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
94 template at(size_t i) { mixin("alias _" ~ ToString!(i) ~ " at;"); };
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
95 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
96
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
97 void move(T)(ref T[] a, size_t src, size_t dest, size_t length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
98 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
99 if (a.length > 1)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
100 memmove(a.ptr + dest, a.ptr + src, length * T.sizeof);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
101 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
102
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
103 enum SignalEventId
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
104 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
105 firstSlotConnected,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
106 lastSlotDisconnected
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
107 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
108
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
109 public class SignalException : Exception
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
110 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
111 this(char[] msg)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
112 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
113 super(msg);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
114 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
115 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
116
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
117 struct Fn
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
118 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
119 void* funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
120
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
121 static typeof(*this) opCall(R, A...)(R function(A) fn)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
122 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
123 typeof(*this) r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
124 r.funcptr = fn;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
125 return r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
126 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
127
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
128 template call(R)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
129 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
130 R call(A...)(A args)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
131 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
132 alias R function(A) Fn;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
133 return (cast(Fn)funcptr)(args);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
134 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
135 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
136
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
137 S get(S)()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
138 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
139 static assert (is(typeof(*S.init) == function));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
140 return cast(S)funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
141 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
142 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
143
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
144 struct Dg
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
145 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
146 void* context;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
147 void* funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
148
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
149 static typeof(*this) opCall(R, A...)(R delegate(A) dg)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
150 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
151 typeof(*this) r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
152 r.context = dg.ptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
153 r.funcptr = dg.funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
154 return r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
155 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
156
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
157 template call(R)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
158 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
159 R call(A...)(A args)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
160 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
161 R delegate(A) dg; // BUG: parameter storage classes are ignored
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
162 dg.ptr = context;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
163 dg.funcptr = cast(typeof(dg.funcptr))funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
164 return dg(args);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
165 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
166 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
167
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
168 S get(S)()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
169 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
170 static assert (is(S == delegate));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
171 S r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
172 r.ptr = context;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
173 r.funcptr = cast(typeof(r.funcptr))funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
174 return r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
175 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
176 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
177
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
178 struct Slot(R)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
179 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
180 alias R Receiver;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
181
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
182 Receiver receiver;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
183 Dg invoker;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
184
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
185 static if (is(Receiver == Dg))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
186 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
187 static const isDelegate = true;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
188
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
189 void onReceiverDisposed(Object o)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
190 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
191 assert (lock !is null);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
192 synchronized(lock)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
193 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
194 receiver.context = null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
195 receiver.funcptr = null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
196 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
197 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
198
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
199 // null if receiver doesn't point to a disposable object
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
200 Object lock;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
201
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
202 bool isDisposed()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
203 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
204 return !receiver.funcptr;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
205 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
206
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
207 Object getObject()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
208 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
209 return lock ? _d_toObject(receiver.context) : null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
210 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
211 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
212 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
213 static const isDelegate = false;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
214
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
215 static typeof(*this) opCall(Receiver r, Dg c)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
216 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
217 typeof(*this) ret;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
218 ret.receiver = r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
219 ret.invoker = c;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
220 return ret;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
221 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
222 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
223
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
224 enum SlotListId
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
225 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
226 Func, // function pointers
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
227 Weak, // object delegates stored in C heap
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
228 Strong // delegates stored in GC heap
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
229 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
230
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
231 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
232 Used to specify the type of a signal-to-slot connection.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
233
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
234 Examples:
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
235 ----
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
236 class Sender
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
237 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
238 mixin Signal!("changed");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
239 void change()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
240 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
241 changed.emit;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
242 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
243 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
244
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
245
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
246 class Receiver
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
247 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
248 void alarm() {}
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
249 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
250
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
251 void main()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
252 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
253 auto s = new Sender;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
254 auto r = new Receiver;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
255 s.changed.connect(&r.alarm); // now s weakly references r
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
256
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
257 r = null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
258 // collect garbage (assume there is no more reachable pointers
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
259 // to the receiver and it gets finalized)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
260 ...
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
261
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
262 s.change;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
263 // weak reference to the receiving object
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
264 // has been removed from the sender's connection lists.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
265
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
266 r = new Receiver;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
267 s.changed.connect(&r.alarm, ConnectionFlags.Strong);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
268
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
269 r = null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
270 // collect garbage
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
271 ...
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
272 // the receiving object has not been finalized because s strongly references it.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
273
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
274 s.change; // the receiver is called.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
275 delete r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
276 s.change; // the receiver is disconnected from the sender.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
277
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
278 static void foo()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
279 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
280 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
281
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
282 s.changed.connect(&foo);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
283 s.changed.emit; // foo is called.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
284 s.changed.disconnect(&foo); // must be explicitly disconnected.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
285
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
286 void bar()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
287 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
288 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
289
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
290 // ConnectionFlags.NoObject must be specified for delegates
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
291 // to non-static local functions or struct member functions.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
292 s.changed.connect(&bar, ConnectionFlags.NoObject);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
293 s.changed.emit; // bar is called.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
294 s.changed.disconnect(&bar); // must be explicitly disconnected.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
295 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
296 ----
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
297 */
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
298 public enum ConnectionFlags
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
299 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
300 ///
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
301 None,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
302 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
303 The receiver will be stored as weak reference (implied if ConnectionFlags.NoObject is not specified).
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
304 If the signal receiver is not a function pointer or a delegate referencing a D class instance.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
305 the sender will not be notified when the receiving object is deleted and emitting the signal
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
306 connected to that receiving object will result in undefined behavior.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
307 */
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
308 Weak = 0x0001,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
309 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
310 The receiver is stored as strong reference (implied if ConnectionFlags.NoObject is specified).
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
311 */
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
312 Strong = 0x0002,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
313 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
314 Must be specified if the receiver is not a function pointer or a delegate referencing a D class instance.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
315 */
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
316 NoObject = 0x0004
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
317
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
318 // Queued = 0x0004,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
319 // BlockingQueued = 0x0008
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
320 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
321
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
322
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
323 struct SlotList(SlotT, bool strong = false)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
324 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
325 alias SlotT SlotType;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
326 SlotType[] data;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
327
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
328 void length(size_t length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
329 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
330 static if (strong)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
331 data.length = length;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
332 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
333 realloc(data, length);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
334 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
335
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
336 SlotType* add(SlotType slot)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
337 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
338 auto oldLen = data.length;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
339 length = oldLen + 1;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
340 auto p = &data[oldLen];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
341 *p = slot;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
342 return p;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
343 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
344
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
345 SlotType* get(int slotId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
346 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
347 return &data[slotId];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
348 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
349
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
350 void remove(int slotId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
351 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
352 move(data, slotId, slotId + 1, data.length - slotId - 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
353 data = data[0..$ - 1];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
354 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
355
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
356 size_t length()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
357 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
358 return data.length;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
359 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
360
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
361 void free()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
362 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
363 static if (SlotType.isDelegate)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
364 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
365 foreach (ref slot; data)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
366 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
367 if (auto obj = slot.getObject)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
368 rt_detachDisposeEvent(obj, &slot.onReceiverDisposed);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
369 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
370 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
371 static if (!strong)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
372 cfree(data.ptr);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
373 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
374 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
375
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
376 public alias void delegate(int signalId, SignalEventId event) SignalEvent;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
377
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
378 struct SignalConnections
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
379 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
380 bool isInUse;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
381
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
382 STuple!(
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
383 SlotList!(Slot!(Fn)),
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
384 SlotList!(Slot!(Dg)),
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
385 SlotList!(Slot!(Dg), true)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
386 ) slotLists;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
387
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
388 STuple!(
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
389 Fn[],
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
390 Dg[]
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
391 ) delayedDisconnects;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
392
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
393 void addDelayedDisconnect(Fn r)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
394 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
395 delayedDisconnects.at!(0) ~= r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
396 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
397
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
398 void addDelayedDisconnect(Dg r)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
399 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
400 delayedDisconnects.at!(1) ~= r;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
401 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
402
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
403 SlotListType!(slotListId)* getSlotList(int slotListId)()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
404 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
405 return &slotLists.tupleof[slotListId];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
406 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
407
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
408 bool hasSlots()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
409 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
410 foreach(i, e; slotLists.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
411 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
412 if (slotLists.tupleof[i].length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
413 return true;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
414 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
415 return false;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
416 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
417
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
418 int slotCount()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
419 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
420 int count;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
421 foreach(i, e; slotLists.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
422 count += slotLists.at!(i).length;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
423 return count;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
424 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
425
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
426 void slotListLengths(int[] lengths)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
427 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
428 foreach(i, e; slotLists.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
429 lengths[i] = slotLists.at!(i).length;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
430 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
431
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
432 SlotType!(slotListId)* addSlot(int slotListId)(SlotType!(slotListId) slot)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
433 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
434 return getSlotList!(slotListId).add(slot);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
435 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
436
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
437 void removeSlot(int slotListId)(int slotId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
438 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
439 slotLists.at!(slotListId).remove(slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
440 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
441
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
442 void free()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
443 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
444 foreach(i, e; slotLists.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
445 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
446 static if (is(typeof(slotLists.at!(i).free)))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
447 slotLists.at!(i).free;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
448 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
449 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
450
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
451 template SlotListType(int slotListId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
452 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
453 alias typeof(slotLists.tupleof)[slotListId] SlotListType;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
454 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
455
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
456 template SlotType(int slotListId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
457 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
458 alias SlotListType!(slotListId).SlotType SlotType;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
459 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
460
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
461 template ReceiverType(int slotListId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
462 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
463 alias SlotType!(slotListId).Receiver ReceiverType;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
464 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
465
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
466 static const slotListCount = slotLists.tupleof.length;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
467 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
468
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
469
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
470 private ThreadLocal!(Object) signalSender_;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
471 static this()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
472 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
473 signalSender_ = new ThreadLocal!(Object);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
474 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
475
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
476 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
477 If called from a slot, returns the object
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
478 that is emitting the signal. Otherwise, returns null.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
479 */
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
480 public Object signalSender() {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
481 return signalSender_.val;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
482 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
483
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
484 public class SignalHandler
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
485 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
486 SignalConnections[] connections;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
487 Object owner;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
488 int blocked;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
489
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
490 SignalEvent signalEvent;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
491
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
492 alias SignalConnections.SlotType SlotType;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
493 alias SignalConnections.ReceiverType ReceiverType;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
494
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
495 public this(Object owner_) {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
496 owner = owner_;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
497 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
498
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
499 private SignalConnections* getConnections(int signalId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
500 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
501 if (signalId < connections.length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
502 return &connections[signalId];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
503 return null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
504 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
505
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
506 private SlotType!(slotListId)* addSlot(int slotListId)(int signalId, ReceiverType!(slotListId) receiver,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
507 Dg invoker)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
508 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
509 if (signalId >= connections.length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
510 connections.length = signalId + 1;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
511 auto slot = connections[signalId].addSlot!(slotListId)(SlotType!(slotListId)(receiver, invoker));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
512
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
513 if (signalEvent && connections[signalId].slotCount == 1)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
514 signalEvent(signalId, SignalEventId.firstSlotConnected);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
515
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
516 return slot;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
517 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
518
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
519 private void removeSlot(int slotListId)(int signalId, int slotId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
520 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
521 connections[signalId].removeSlot!(slotListId)(slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
522
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
523 if (signalEvent && !connections[signalId].slotCount)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
524 signalEvent(signalId, SignalEventId.lastSlotDisconnected);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
525 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
526
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
527 private SlotType!(slotListId)* addObjectSlot(int slotListId)(size_t signalId, Object obj, Dg receiver,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
528 Dg invoker)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
529 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
530 auto slot = addSlot!(slotListId)(signalId, receiver, invoker);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
531 slot.lock = this;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
532 rt_attachDisposeEvent(obj, &slot.onReceiverDisposed);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
533 return slot;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
534 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
535
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
536 size_t slotCount(int signalId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
537 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
538 synchronized(this)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
539 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
540 auto con = getConnections(signalId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
541 if (con)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
542 return con.slotCount;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
543 return 0;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
544 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
545 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
546
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
547 void connect(Receiver)(size_t signalId, Receiver receiver,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
548 Dg invoker, ConnectionFlags flags)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
549 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
550 synchronized(this)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
551 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
552 static if (is(typeof(receiver.context)))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
553 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
554 Object obj;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
555 if ((flags & ConnectionFlags.NoObject) || (obj = _d_toObject(receiver.context)) is null)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
556 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
557 // strong by default
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
558 if (flags & ConnectionFlags.Weak)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
559 addSlot!(SlotListId.Weak)(signalId, receiver, invoker);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
560 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
561 addSlot!(SlotListId.Strong)(signalId, receiver, invoker);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
562 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
563 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
564 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
565 // weak by default
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
566 if (flags & ConnectionFlags.Strong)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
567 addObjectSlot!(SlotListId.Strong)(signalId, obj, receiver, invoker);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
568 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
569 addObjectSlot!(SlotListId.Weak)(signalId, obj, receiver, invoker);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
570 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
571 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
572 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
573 addSlot!(SlotListId.Func)(signalId, receiver, invoker);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
574 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
575 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
576
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
577 void disconnect(Receiver)(int signalId, Receiver receiver)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
578 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
579 synchronized(this)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
580 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
581 auto cons = getConnections(signalId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
582 if (!cons)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
583 return;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
584
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
585 // if called from a slot being executed by this signal, delay disconnection
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
586 // until all slots has been called.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
587 if (cons.isInUse)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
588 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
589 cons.addDelayedDisconnect(receiver);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
590 return;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
591 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
592
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
593 TOP:
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
594 foreach (slotListId, e; cons.slotLists.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
595 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
596 /// COMPILER BUG: ReceiverType is evaluated to expression instead of type.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
597 static if (is(typeof(cons.ReceiverType!(slotListId)) == Receiver))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
598 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
599 auto slotList = cons.getSlotList!(slotListId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
600 for (int slotId; slotId < slotList.length;)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
601 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
602 auto slot = slotList.get(slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
603 static if (slot.isDelegate)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
604 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
605 if (slot.isDisposed)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
606 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
607 removeSlot!(slotListId)(signalId, slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
608 continue;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
609 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
610 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
611
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
612 if (slot.receiver == receiver)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
613 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
614 static if (slot.isDelegate)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
615 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
616 if (auto obj = slot.getObject)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
617 rt_detachDisposeEvent(obj, &slot.onReceiverDisposed);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
618 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
619 removeSlot!(slotListId)(signalId, slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
620 break TOP;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
621 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
622
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
623 slotId++;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
624 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
625 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
626 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
627 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
628 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
629
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
630 void emit(A...)(size_t signalId, A args)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
631 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
632 synchronized(this)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
633 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
634 if (signalId >= connections.length || blocked)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
635 return;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
636 auto cons = &connections[signalId];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
637
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
638 if (cons.hasSlots)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
639 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
640 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
641 cons.isInUse = true;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
642 signalSender_.val = owner;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
643 scope(exit)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
644 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
645 cons.isInUse = false;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
646 signalSender_.val = null;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
647 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
648
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
649 // Store the lengths to avoid calling new slots
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
650 // connected in the slots being called.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
651 // dmd bug: int[cons.slotListCount] fails
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
652 static const c = cons.slotListCount;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
653 int[c] lengths = void;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
654 cons.slotListLengths(lengths);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
655
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
656 foreach (slotListId, e; cons.slotLists.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
657 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
658 auto slotList = cons.getSlotList!(slotListId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
659 for (size_t slotId; slotId < lengths[slotListId];)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
660 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
661 auto slot = slotList.get(slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
662 static if (slot.isDelegate)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
663 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
664 if (slot.isDisposed)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
665 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
666 removeSlot!(slotListId)(signalId, slotId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
667 lengths[slotListId]--;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
668 continue;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
669 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
670 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
671
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
672 slot.invoker.call!(void)(slot.receiver, args);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
673 ++slotId;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
674 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
675 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
676 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
677
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
678
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
679 // process delayed disconnects if any
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
680 foreach(i, e; cons.delayedDisconnects.tupleof)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
681 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
682 if (cons.delayedDisconnects.at!(i).length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
683 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
684 foreach (d; cons.delayedDisconnects.at!(i))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
685 disconnect(signalId, d);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
686 cons.delayedDisconnects.at!(i).length = 0;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
687 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
688 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
689 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
690 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
691 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
692
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
693 // Adjusts signal arguments and calls the slot. S - slot signature, A - signal arguments
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
694 private void invokeSlot(S, Receiver, A...)(Receiver r, A args)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
695 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
696 r.get!(S)()(args[0..ParameterTupleOf!(S).length]);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
697 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
698
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
699 void blockSignals()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
700 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
701 synchronized(this)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
702 blocked++;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
703 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
704
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
705 void unblockSignals()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
706 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
707 synchronized(this)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
708 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
709 if(!blocked)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
710 throw new SignalException("Signals are not blocked");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
711 blocked--;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
712 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
713 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
714
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
715 ~this()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
716 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
717 foreach(ref c; connections)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
718 c.free;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
719 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
720 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
721
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
722 //TODO: this could be avoided if named mixins didn't suck.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
723 public struct SignalOps(int sigId, A...)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
724 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
725 private SignalHandler sh;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
726 enum { signalId = sigId }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
727
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
728 void connect(R, B...)(R function(B) fn, ConnectionFlags flags = ConnectionFlags.None)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
729 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
730 alias CheckSlot!(typeof(fn), A) check;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
731 auto invoker = Dg(&sh.invokeSlot!(typeof(fn), Fn, A));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
732 sh.connect(signalId, Fn(fn), invoker, flags);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
733 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
734
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
735 void connect(R, B...)(R delegate(B) dg, ConnectionFlags flags = ConnectionFlags.None)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
736 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
737 alias CheckSlot!(typeof(dg), A) check;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
738 auto invoker = Dg(&sh.invokeSlot!(typeof(dg), Dg, A));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
739 sh.connect(signalId, Dg(dg), invoker, flags);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
740 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
741
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
742 void disconnect(R, B...)(R function(B) fn)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
743 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
744 sh.disconnect(signalId, Fn(fn));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
745 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
746
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
747 void disconnect(R, B...)(R delegate(B) dg)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
748 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
749 sh.disconnect(signalId, Dg(dg));
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
750 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
751
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
752 void emit(A args)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
753 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
754 sh.emit(signalId, args);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
755 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
756
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
757 debug size_t slotCount()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
758 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
759 return sh.slotCount(signalId);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
760 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
761 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
762
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
763 template CheckSlot(Slot, A...)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
764 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
765 static assert(ParameterTupleOf!(Slot).length <= A.length, "Slot " ~ ParameterTypeTuple!(Slot).stringof ~
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
766 " has more prameters than signal " ~ A.stringof);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
767 alias CheckSlotImpl!(Slot, 0, A) check;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
768 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
769
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
770 template CheckSlotImpl(Slot, int i, A...)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
771 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
772 alias ParameterTupleOf!(Slot) SlotArgs;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
773 static if (i < SlotArgs.length)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
774 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
775 static assert (is(SlotArgs[i] : A[i]), "Argument " ~ __toString(i) ~
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
776 ":" ~ A[i].stringof ~ " of signal " ~ A.stringof ~ " is not implicitly convertible to parameter "
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
777 ~ SlotArgs[i].stringof ~ " of slot " ~ SlotArgs.stringof);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
778 alias CheckSlotImpl!(Slot, i + 1, A) next;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
779 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
780 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
781
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
782 public template SignalHandlerOps()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
783 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
784 static assert (is(typeof(this.signalHandler)),
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
785 "SignalHandlerOps is already instantiated in " ~ typeof(this).stringof ~ " or one of its base classes");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
786
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
787 protected:
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
788 SignalHandler signalHandler_; // manages signal-to-slot connections
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
789
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
790 final SignalHandler signalHandler()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
791 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
792 if (!signalHandler_)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
793 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
794 signalHandler_ = new SignalHandler(this);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
795 onSignalHandlerCreated(signalHandler_);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
796 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
797 return signalHandler_;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
798 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
799
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
800 void onSignalHandlerCreated(ref SignalHandler sh)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
801 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
802 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
803
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
804 public:
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
805 final void blockSignals()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
806 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
807 signalHandler.blockSignals();
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
808 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
809
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
810 final void unblockSignals()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
811 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
812 signalHandler.unblockSignals();
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
813 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
814 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
815
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
816 /**
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
817 Examples:
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
818 ----
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
819 struct Args
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
820 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
821 bool cancel;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
822 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
823
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
824 class C
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
825 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
826 private int _x;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
827 // reference parameters are not supported yet,
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
828 // so we pass arguments by pointer.
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
829 mixin Signal!("xChanging", int, Args*);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
830 mixin Signal!("xChanged");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
831
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
832 void x(int v)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
833 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
834 if (v != _x)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
835 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
836 Args args;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
837 xChanging.emit(v, &args);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
838 if (!args.cancel)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
839 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
840 _x = v;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
841 xChanged.emit;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
842 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
843 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
844 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
845 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
846 ----
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
847 */
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
848 template Signal(string name, A...)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
849 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
850 mixin SignalImpl!(0, name, A);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
851 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
852
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
853 template SignalImpl(int index, string name, A...)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
854 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
855 static if (is(typeof(mixin(typeof(this).stringof ~ ".__sig" ~ ToString!(index)))))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
856 mixin SignalImpl!(index + 1, name, A);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
857 else
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
858 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
859 // mixed-in once
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
860 static if (!is(typeof(this.signalHandler)))
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
861 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
862 mixin SignalHandlerOps;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
863 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
864 mixin("private static const int __sig" ~ ToString!(index) ~ " = " ~ ToString!(index) ~ ";");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
865 mixin("SignalOps!(" ~ ToString!(index) ~ ", A) " ~ name ~ "(){ return SignalOps!("
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
866 ~ ToString!(index) ~ ", A)(signalHandler); }");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
867 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
868 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
869
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
870 extern(C) alias void function(void*) SlotConnector;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
871
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
872 debug (UnitTest)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
873 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
874 class A
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
875 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
876 mixin Signal!("scorched", int);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
877
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
878 int signalId1 = -1;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
879 int signalId2 = -1;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
880
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
881 void onFirstConnect(int sId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
882 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
883 signalId1 = sId;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
884 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
885
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
886 void onLastDisconnect(int sId)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
887 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
888 signalId2 = sId;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
889 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
890
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
891 this()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
892 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
893 signalHandler.firstSlotConnected = &onFirstConnect;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
894 signalHandler.lastSlotDisconnected = &onLastDisconnect;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
895 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
896 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
897
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
898 class B : A
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
899 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
900 mixin Signal!("booed", int);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
901
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
902 int bazSum;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
903 void baz(int i)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
904 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
905 bazSum += i;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
906 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
907 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
908
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
909 class C : A
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
910 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
911 mixin Signal!("cooked");
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
912 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
913 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
914
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
915 unittest
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
916 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
917 static int fooSum;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
918 static int barSum;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
919
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
920 static void foo(int i)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
921 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
922 fooSum += i;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
923 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
924
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
925 void bar(long i)
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
926 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
927 barSum += i;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
928 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
929
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
930 auto a = new A;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
931 auto b = new B;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
932 auto c = new C;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
933 assert(b.scorched.signalId == 0);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
934 assert(b.booed.signalId == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
935 assert(c.cooked.signalId == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
936
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
937 auto sh = b.signalHandler;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
938
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
939 b.scorched.connect(&foo);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
940 assert(sh.connections.length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
941 assert(b.signalId1 == 0);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
942 auto scCons = &sh.connections[0];
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
943
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
944 assert(scCons.getSlotList!(SlotListId.Func).length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
945 b.scorched.emit(1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
946 assert(fooSum == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
947
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
948 b.scorched.connect(&bar, ConnectionFlags.NoObject);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
949 assert(sh.connections.length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
950 assert(scCons.getSlotList!(SlotListId.Strong).length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
951 b.scorched.emit(1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
952 assert (fooSum == 2 && barSum == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
953
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
954 b.scorched.connect(&b.baz);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
955 assert(scCons.getSlotList!(SlotListId.Weak).length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
956 b.scorched.emit(1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
957 assert (fooSum == 3 && barSum == 2 && b.bazSum == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
958
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
959 b.scorched.disconnect(&bar);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
960 assert(scCons.slotCount == 2);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
961 b.scorched.disconnect(&b.baz);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
962 assert(scCons.slotCount == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
963 b.scorched.disconnect(&foo);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
964 assert(scCons.slotCount == 0);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
965 assert(b.signalId2 == 0);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
966
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
967 fooSum = 0;
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
968 void connectFoo()
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
969 {
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
970 b.scorched.connect(&foo);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
971 b.scorched.disconnect(&connectFoo);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
972 }
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
973
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
974 b.scorched.connect(&connectFoo, ConnectionFlags.NoObject);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
975 b.scorched.emit(1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
976 assert(scCons.getSlotList!(SlotListId.Func).length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
977 assert(scCons.getSlotList!(SlotListId.Strong).length == 0);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
978 assert(!fooSum);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
979
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
980 auto r = new B();
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
981 b.scorched.connect(&r.baz);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
982 assert(scCons.getSlotList!(SlotListId.Weak).length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
983 b.scorched.emit(1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
984 assert(r.bazSum == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
985 assert(fooSum == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
986
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
987 delete(r);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
988 assert(scCons.getSlotList!(SlotListId.Weak).length == 1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
989 b.scorched.emit(1);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
990 assert(scCons.getSlotList!(SlotListId.Weak).length == 0);
8674fd5f34f4 Added d1/d2 top directories
maxter <spambox@d-coding.com>
parents:
diff changeset
991 }