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