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