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