comparison qt/core/QList.d @ 296:5173835bb372 signals

iteration over QList with opApply
author eldar
date Sun, 22 Nov 2009 20:43:10 +0000
parents 463563fc9e17
children bc783e20da2b
comparison
equal deleted inserted replaced
295:463563fc9e17 296:5173835bb372
3 import qt.QGlobal; 3 import qt.QGlobal;
4 import qt.QtdObject; 4 import qt.QtdObject;
5 import qt.qtd.Atomic; 5 import qt.qtd.Atomic;
6 import qt.qtd.MetaMarshall; 6 import qt.qtd.MetaMarshall;
7 import qt.core.QTypeInfo; 7 import qt.core.QTypeInfo;
8 import qt.core.QString;
8 9
9 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc; 10 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc;
10 import core.stdc.string : memcpy, memmove; 11 import core.stdc.string : memcpy, memmove;
11 12
12 import std.traits; 13 import std.traits;
27 28
28 bool isLarge(T)() 29 bool isLarge(T)()
29 if (is(T.QTypeInfo)) 30 if (is(T.QTypeInfo))
30 { 31 {
31 return T.QTypeInfo.isLarge; 32 return T.QTypeInfo.isLarge;
33 }
34
35 template isQtReference(T)
36 {
37 enum isQtReference = isQObjectType!T || isObjectType!T || isValueType!T || is(T == string);
32 } 38 }
33 39
34 int qAllocMore(int alloc, int extra) 40 int qAllocMore(int alloc, int extra)
35 { 41 {
36 if (alloc == 0 && extra == 0) 42 if (alloc == 0 && extra == 0)
321 327
322 struct Node 328 struct Node
323 { 329 {
324 void *v; 330 void *v;
325 331
326 static if (isQObjectType!T || isObjectType!T || isValueType!T) // binded Qt types 332 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) // binded Qt types
327 { 333 {
328 T t() 334 T t()
329 { 335 {
330 static if (isValueType!T) 336 static if(is(T == string))
337 {
338 void* ptr = cast(void*)(TI.isLarge || TI.isStatic ? v : &this);
339 return QString.toNativeString(ptr);
340 }
341 else static if (isValueType!T)
331 { 342 {
332 void* ptr = cast(void*)(isLarge!T() || isStatic!T() ? v : &this); 343 void* ptr = cast(void*)(isLarge!T() || isStatic!T() ? v : &this);
333 return new T(ptr, QtdObjectFlags.nativeOwnership); 344 return new T(ptr, QtdObjectFlags.nativeOwnership);
334 } 345 }
335 else 346 else
428 const T cpy = t; 439 const T cpy = t;
429 node_construct(cast(Node*)(p.append()), cpy); 440 node_construct(cast(Node*)(p.append()), cpy);
430 } 441 }
431 } 442 }
432 443
433 static if (isQObjectType!T || isObjectType!T || isValueType!T) 444 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string))
434 { 445 {
435 T at(int i) const 446 T at(int i) const
436 { 447 {
437 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); 448 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
438 return (cast(Node*)(p.at(i))).t(); 449 return (cast(Node*)(p.at(i))).t();
439 } 450 }
451 T opIndex(int i)
452 {
453 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
454 return (cast(Node*)(p.at(i))).t();
455 }
440 } 456 }
441 else 457 else
442 { 458 {
443 ref const (T) at(int i) const 459 ref const (T) at(int i) const
460 {
461 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
462 return (cast(Node*)(p.at(i))).t();
463 }
464 ref T opIndex(int i)
444 { 465 {
445 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); 466 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
446 return (cast(Node*)(p.at(i))).t(); 467 return (cast(Node*)(p.at(i))).t();
447 } 468 }
448 } 469 }
460 T.__constructPlacedNativeCopy(t.__nativeId, n); // TODO should be *cast(T*)(n) = cast(T)(t); as it is a primitive type. fix when they are implemented with structs 481 T.__constructPlacedNativeCopy(t.__nativeId, n); // TODO should be *cast(T*)(n) = cast(T)(t); as it is a primitive type. fix when they are implemented with structs
461 } 482 }
462 else // in case of QObject or Object Qt types we place a pointer to a native object in the node 483 else // in case of QObject or Object Qt types we place a pointer to a native object in the node
463 n = cast(Node*) t.__nativeId; 484 n = cast(Node*) t.__nativeId;
464 } 485 }
486 else static if (is(T == string))
487 {
488 void node_construct(Node *n, T t)
489 {
490 QString.__constructPlacedQString(n, t);
491 }
492 }
465 else // native types 493 else // native types
466 void node_construct(Node *n, const ref T t) 494 void node_construct(Node *n, const ref T t)
467 { 495 {
468 static if (TI.isLarge || TI.isStatic) { pragma(msg, "node construct, large " ~ T.stringof); 496 static if (TI.isLarge || TI.isStatic)
469 n.v = q_new!T(t); // n.v = new T(t); 497 n.v = q_new!T(t); // n.v = new T(t);
470 } else static if (TI.isComplex) {pragma(msg, "node construct, complex " ~ T.stringof); 498 else static if (TI.isComplex)
471 q_new_at(n, t); // new (n) T(t); 499 q_new_at(n, t); // new (n) T(t);
472 } else {pragma(msg, "node construct, other " ~ T.stringof); 500 else
473 *cast(T*)(n) = cast(T)(t); 501 *cast(T*)(n) = cast(T)(t);
474 }
475 } 502 }
476 503
477 void node_copy(Node *from, Node *to, Node *src) 504 void node_copy(Node *from, Node *to, Node *src)
478 { 505 {
479 writeln("QList node_copy"); 506 writeln("QList node_copy");
480 static if (isQObjectType!T || isObjectType!T) 507 static if (isQObjectType!T || isObjectType!T)
481 {} // ensure to do nothing. copying only a pointer 508 {} // ensure to do nothing. copying only a pointer
509 else static if(is(T == string))
510 {
511 while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false
512 QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++));
513 }
482 else static if (isValueType!T) 514 else static if (isValueType!T)
483 { 515 {
484 if (TI.isLarge || TI.isStatic) // TODO should be static if 516 if (TI.isLarge || TI.isStatic) // TODO should be static if
485 while(from != to) 517 while(from != to)
486 (from++).v = T.__constructNativeCopy((src++).v); // (from++)->v = new T(*reinterpret_cast<T*>((src++)->v)); 518 (from++).v = T.__constructNativeCopy((src++).v); // (from++)->v = new T(*reinterpret_cast<T*>((src++)->v));
507 539
508 void node_destruct(Node *from, Node *to) 540 void node_destruct(Node *from, Node *to)
509 { 541 {
510 static if (isQObjectType!T || isObjectType!T) //binded types 542 static if (isQObjectType!T || isObjectType!T) //binded types
511 {} // removing just pointers, do nothing 543 {} // removing just pointers, do nothing
512 static if (isValueType!T) //binded value types 544 else static if (is(T == string))
545 {
546 while (from != to)
547 --to, QString.__callNativeDestructor(to);
548 }
549 else static if (isValueType!T) //binded value types
513 { 550 {
514 if (isLarge!T() || isStatic!T()) // TODO should be static if 551 if (isLarge!T() || isStatic!T()) // TODO should be static if
515 while (from != to) 552 while (from != to)
516 --to, T.__deleteNativeObject(to.v); 553 --to, T.__deleteNativeObject(to.v);
517 else if (isComplex!T()) 554 else if (isComplex!T())
524 while (from != to) --to, q_delete(cast(T*)(to.v)); 561 while (from != to) --to, q_delete(cast(T*)(to.v));
525 else static if (TI.isComplex) 562 else static if (TI.isComplex)
526 while (from != to) --to, cast(T*)(to).__dtor(); 563 while (from != to) --to, cast(T*)(to).__dtor();
527 } 564 }
528 } 565 }
566
567 //iteration support
568 int opApply(int delegate(ref T) dg)
569 {
570 int result = 0;
571 int sz = this.length;
572 for (int i = 0; i < sz; i++)
573 {
574 static if (isQtReference!T)
575 {
576 T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T
577 result = dg(t);
578 }
579 else
580 result = dg(this[i]);
581
582 if (result)
583 break;
584 }
585 return result;
586 }
529 } 587 }
530 588
531 extern(C) void qtd_create_QList(void *nativeId); 589 extern(C) void qtd_create_QList(void *nativeId);
532 extern(C) void qtd_create_QList_double(void *nativeId); 590 extern(C) void qtd_create_QList_double(void *nativeId);
533 591