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