changeset 296:5173835bb372 signals

iteration over QList with opApply
author eldar
date Sun, 22 Nov 2009 20:43:10 +0000
parents 463563fc9e17
children bc783e20da2b
files cpp/qt_core/QString_shell.cpp qt/core/QList.d qt/core/QString.d qt/core/QTypeInfo.d
diffstat 4 files changed, 153 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/cpp/qt_core/QString_shell.cpp	Sun Nov 22 11:01:19 2009 +0000
+++ b/cpp/qt_core/QString_shell.cpp	Sun Nov 22 20:43:10 2009 +0000
@@ -1,24 +1,57 @@
 #include <QString>
 #include "qtd_core.h"
 
-extern "C" DLL_PUBLIC const ushort* __qtd_QString_utf16
+extern "C" DLL_PUBLIC const ushort* qtd_QString_utf16
 (void* __this_nativeId)
 {
     QString *__qt_this = (QString *) __this_nativeId;
     return __qt_this->utf16();
 }
 
-extern "C" DLL_PUBLIC int __qtd_QString_size
+extern "C" DLL_PUBLIC int qtd_QString_size
 (void* __this_nativeId)
 {
     QString *__qt_this = (QString *) __this_nativeId;
     return __qt_this->size();
 }
 
-extern "C" DLL_PUBLIC void __qtd_QString_operatorAssign
+extern "C" DLL_PUBLIC void qtd_QString_operatorAssign
 (void* __this_nativeId,
  DArray text)
 {
     QString *__qt_this = (QString *) __this_nativeId;
     *__qt_this = QString::fromUtf8((const char *)text.ptr, text.length);
 }
+
+extern "C" DLL_PUBLIC void qtd_QString_destructor(void *ptr)
+{
+    delete (QString *)ptr;
+}
+
+extern "C" DLL_PUBLIC void qtd_QString_call_destructor(QString *ptr)
+{
+    ptr->~QString();
+}
+
+
+extern "C" DLL_PUBLIC void* qtd_QString_QString_QString
+(void* string0)
+{
+    const QString&  __qt_string0 = (const QString& ) *(QString *)string0;
+    QString *__qt_this = new QString((const QString& )__qt_string0);
+    return (void *) __qt_this;
+}
+
+extern "C" DLL_PUBLIC void* qtd_QString_new_fromUtf8_at
+(void* place, DArray text)
+{
+    QString *__qt_this = new(place) QString;
+    *__qt_this = QString::fromUtf8((const char *)text.ptr, text.length);
+    return __qt_this;
+}
+
+extern "C" DLL_PUBLIC void* qtd_QString_placed_copy(void* string0, void* place) {
+    const QString&  __qt_string0 = (const QString& ) *(QString *)string0;
+    QString *result = new (place)QString((const QString& )__qt_string0);
+    return (void *) result;
+}
--- a/qt/core/QList.d	Sun Nov 22 11:01:19 2009 +0000
+++ b/qt/core/QList.d	Sun Nov 22 20:43:10 2009 +0000
@@ -5,6 +5,7 @@
 import qt.qtd.Atomic;
 import qt.qtd.MetaMarshall;
 import qt.core.QTypeInfo;
+import qt.core.QString;
 
 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc;
 import core.stdc.string : memcpy, memmove;
@@ -31,6 +32,11 @@
     return T.QTypeInfo.isLarge;
 }
 
+template isQtReference(T)
+{
+    enum isQtReference = isQObjectType!T || isObjectType!T || isValueType!T || is(T == string);
+}
+
 int qAllocMore(int alloc, int extra)
 {
     if (alloc == 0 && extra == 0)
@@ -323,11 +329,16 @@
     {
         void *v;
         
-        static if (isQObjectType!T || isObjectType!T || isValueType!T) // binded Qt types
+        static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) // binded Qt types
         {
             T t()
             {
-                static if (isValueType!T)
+                static if(is(T == string))
+                {
+                    void* ptr = cast(void*)(TI.isLarge || TI.isStatic ? v : &this);
+                    return QString.toNativeString(ptr);
+                }
+                else static if (isValueType!T)
                 {
                     void* ptr = cast(void*)(isLarge!T() || isStatic!T() ? v : &this);
                     return new T(ptr, QtdObjectFlags.nativeOwnership);
@@ -430,13 +441,18 @@
         }
     }
 
-    static if (isQObjectType!T || isObjectType!T || isValueType!T)
+    static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string))
     {
         T at(int i) const
         {
             assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
             return (cast(Node*)(p.at(i))).t();
         }
+        T opIndex(int i)
+        {
+            assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
+            return (cast(Node*)(p.at(i))).t();
+        }
     }
     else
     {
@@ -445,6 +461,11 @@
             assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
             return (cast(Node*)(p.at(i))).t();
         }
+        ref T opIndex(int i)
+        {
+            assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
+            return (cast(Node*)(p.at(i))).t();
+        }
     }   
     
     static if (isQObjectType!T || isObjectType!T || isValueType!T) //binded types
@@ -462,16 +483,22 @@
             else // in case of QObject or Object Qt types we place a pointer to a native object in the node
                 n = cast(Node*) t.__nativeId;
         }
+    else static if (is(T == string))
+    {
+        void node_construct(Node *n, T t)
+        {
+            QString.__constructPlacedQString(n, t);
+        }
+    }
     else // native types
         void node_construct(Node *n, const ref T t)
         {
-            static if (TI.isLarge || TI.isStatic) { pragma(msg, "node construct, large " ~ T.stringof);
+            static if (TI.isLarge || TI.isStatic)
                 n.v = q_new!T(t); // n.v = new T(t);
-}            else static if (TI.isComplex) {pragma(msg, "node construct, complex " ~ T.stringof);
+            else static if (TI.isComplex)
                 q_new_at(n, t); // new (n) T(t);
-}            else {pragma(msg, "node construct, other " ~ T.stringof);
+            else
                 *cast(T*)(n) = cast(T)(t);
-}
         }
     
     void node_copy(Node *from, Node *to, Node *src)
@@ -479,6 +506,11 @@
         writeln("QList node_copy");
         static if (isQObjectType!T || isObjectType!T)
             {} // ensure to do nothing. copying only a pointer
+        else static if(is(T == string))
+        {
+            while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false
+                QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++));
+        }
         else static if (isValueType!T)
         {
             if (TI.isLarge || TI.isStatic) // TODO should be static if
@@ -509,7 +541,12 @@
     {
         static if (isQObjectType!T || isObjectType!T) //binded types
             {} // removing just pointers, do nothing
-        static if (isValueType!T) //binded value types
+        else static if (is(T == string))
+        {
+            while (from != to)
+                --to, QString.__callNativeDestructor(to);
+        }
+        else static if (isValueType!T) //binded value types
         {
             if (isLarge!T() || isStatic!T()) // TODO should be static if
                 while (from != to)
@@ -526,6 +563,27 @@
                 while (from != to) --to, cast(T*)(to).__dtor();
         }
     }
+    
+    //iteration support
+    int opApply(int delegate(ref T) dg)
+    {
+        int result = 0;
+        int sz = this.length;
+        for (int i = 0; i < sz; i++)
+        {
+            static if (isQtReference!T)
+            {
+                T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T
+                result = dg(t);
+            }
+            else
+                result = dg(this[i]);
+
+            if (result)
+                break;
+        }
+        return result;
+    }
 }
 
 extern(C) void qtd_create_QList(void *nativeId);
--- a/qt/core/QString.d	Sun Nov 22 11:01:19 2009 +0000
+++ b/qt/core/QString.d	Sun Nov 22 20:43:10 2009 +0000
@@ -15,35 +15,67 @@
 {
     public static QString opCall(void* ptr, bool proxy) {
         QString str;
-        str.native_id = ptr;
+        str.__nativeId = ptr;
         return str;
     }
     
-    private void* native_id;
+    public void* __nativeId;
     
     public static final string toNativeString(void* qstring) {
-        wchar* arr = __qtd_QString_utf16(qstring);
-        int size = __qtd_QString_size(qstring);
+        wchar* arr = qtd_QString_utf16(qstring);
+        int size = qtd_QString_size(qstring);
         return .toUTF8(arr[0..size]);
     }
     
     public final string toNativeString() {
-        return toNativeString(native_id);
+        return toNativeString(__nativeId);
     }
     
     public void assign(string text) {
-        __qtd_QString_operatorAssign(native_id, text);
+        qtd_QString_operatorAssign(__nativeId, text);
     }
     
     public static string fromUtf8(string source) {
         return source;
     }
-/*    
-    public static string fromUtf16(wstring src) {
-        version(Tango)
-    }*/
+    
+    public static void __constructPlacedQString(void* place, string source) {
+        qtd_QString_new_fromUtf8_at(place, source);
+    }
+    
+    // service stuff
+    static void* __constructNativeCopy(const void* orig) {
+        return qtd_QString_QString_QString(cast(void*)orig);
+    }
+
+    static void* __constructPlacedNativeCopy(const void* orig, void* place) {
+        return qtd_QString_placed_copy(orig, place);
+    }
+    
+    public static void __deleteNativeObject(void* ptr) {
+        qtd_QString_destructor(ptr);
+    }
+    
+    public static void __callNativeDestructor(void* ptr) {
+        qtd_QString_call_destructor(ptr);
+    }
+    struct QTypeInfo
+    {
+        enum bool isComplex = true;
+        enum bool isStatic = false;
+        enum bool isLarge = false;
+        enum bool isPointer = false;
+        enum bool isDummy = false;
+    }
 }
+private extern(C) void* qtd_QString_placed_copy(const void* orig, void* place);
 
-private extern (C) wchar* __qtd_QString_utf16(void* __this_nativeId);
-private extern (C) int __qtd_QString_size(void* __this_nativeId);
-private extern (C) void __qtd_QString_operatorAssign(void* __this_nativeId, string text);
\ No newline at end of file
+private extern (C) void qtd_QString_destructor(void* __this_nativeId);
+private extern (C) void qtd_QString_call_destructor(void *ptr);
+
+private extern (C) void* qtd_QString_QString_QString(void* orig);
+
+private extern (C) wchar* qtd_QString_utf16(void* __this_nativeId);
+private extern (C) int qtd_QString_size(void* __this_nativeId);
+private extern (C) void qtd_QString_operatorAssign(void* __this_nativeId, string text);
+private extern (C) void qtd_QString_new_fromUtf8_at(void* place, string text);
\ No newline at end of file
--- a/qt/core/QTypeInfo.d	Sun Nov 22 11:01:19 2009 +0000
+++ b/qt/core/QTypeInfo.d	Sun Nov 22 20:43:10 2009 +0000
@@ -9,6 +9,7 @@
 import std.traits;
 
 import qt.qtd.MetaMarshall;
+import qt.core.QString;
 
 bool qIsDetached(T)(ref T) { return true; }
 
@@ -19,7 +20,11 @@
 
 template QTypeInfo(T)
 {
-    static if(isBasicType!T)
+    static if(is(T == string))
+    {
+        alias QString.QTypeInfo QTypeInfo;
+    }
+    else static if(isBasicType!T)
     {
         public enum
         {