diff generator/cppimplgenerator.cpp @ 298:adae77fdc1ea signals

Native QList implementation is now used throughout QtD
author eldar
date Sun, 06 Dec 2009 17:26:37 +0000
parents bb37b0ed94c9
children 4d8481385c71
line wrap: on
line diff
--- a/generator/cppimplgenerator.cpp	Sun Nov 22 22:18:06 2009 +0000
+++ b/generator/cppimplgenerator.cpp	Sun Dec 06 17:26:37 2009 +0000
@@ -721,18 +721,23 @@
           << "}" << endl;
     }
 
-    if (java_class->typeEntry()->isValue() && java_class->hasCloneOperator())
+    if (java_class->typeEntry()->isValue())
     {
-        AbstractMetaFunction *ctor = java_class->copyConstructor();
-        if(ctor)
         {
-            QString argName = ctor->arguments().at(0)->indexedName();
+            QString argName = "orig";
             s << endl << endl
-              << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->name() << "_placed_copy(void* "
-              << argName << ", " << "void* place) {" << endl
-              << QString("    const %1&  __qt_%2 = (const %1& ) *(%1 *)%2;").arg(java_class->name()).arg(argName) << endl;
-
-            writeFinalConstructor(s, ctor, "result", "original", "(place)");
+              << "extern \"C\" DLL_PUBLIC void qtd_" << java_class->name() << "_placed_copy(void* "
+              << argName << ", void* place) {" << endl
+              << QString("    const %1&  __qt_%2 = (const %1& ) *(%1 *)%2;").arg(shellClassName(java_class)).arg(argName) << endl
+              << QString("    %1 *result = new (place) %1 (__qt_%2);").arg(java_class->qualifiedCppName()).arg(argName) << endl;
+//            writeFinalConstructor(s, ctor, "result", "original", "(place)");
+            s << "}";
+
+            s << endl << endl
+              << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->name() << "_native_copy(void* " << argName << ") {" << endl
+              << QString("    const %1&  __qt_%2 = (const %1& ) *(%1 *)%2;").arg(shellClassName(java_class)).arg(argName) << endl
+              << QString("    %1 *result = new %1 (__qt_%2);").arg(java_class->qualifiedCppName()).arg(argName) << endl
+              << "    return result;" << endl;
             s << "}";
         }
     }
@@ -744,11 +749,11 @@
 
 void CppImplGenerator::writeValueFunctions(QTextStream &s, const AbstractMetaClass *java_class)
 {
-    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isComplex() { return (bool) QTypeInfo<%1>::isComplex; }\n").arg(java_class->name());
-    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isStatic() { return (bool) QTypeInfo<%1>::isStatic; }\n").arg(java_class->name());
-    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isLarge() { return (bool) QTypeInfo<%1>::isLarge; }\n").arg(java_class->name());
-    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isPointer() { return (bool) QTypeInfo<%1>::isPointer; }\n").arg(java_class->name());
-    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isDummy() { return (bool) QTypeInfo<%1>::isDummy; }\n").arg(java_class->name());
+    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isComplex() { return (bool) QTypeInfo<%2>::isComplex; }\n").arg(java_class->name()).arg(java_class->qualifiedCppName());
+    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isStatic() { return (bool) QTypeInfo<%2>::isStatic; }\n").arg(java_class->name()).arg(java_class->qualifiedCppName());
+    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isLarge() { return (bool) QTypeInfo<%2>::isLarge; }\n").arg(java_class->name()).arg(java_class->qualifiedCppName());
+    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isPointer() { return (bool) QTypeInfo<%2>::isPointer; }\n").arg(java_class->name()).arg(java_class->qualifiedCppName());
+    s << QString("extern \"C\" DLL_PUBLIC bool qtd_%1_QTypeInfo_isDummy() { return (bool) QTypeInfo<%2>::isDummy; }\n").arg(java_class->name()).arg(java_class->qualifiedCppName());
 }
 
 void CppImplGenerator::writeVirtualDispatchFunction(QTextStream &s, const AbstractMetaFunction *function, bool d_export)
@@ -811,27 +816,35 @@
                     s << INDENT << f_type->typeEntry()->qualifiedCppName() << " __d_return_value;" << endl;
 
                 if (f_type->isContainer())
-                    s << INDENT << "void* __d_return_value;" << endl
-                      << INDENT << "size_t __d_return_value_size;" << endl;
+                {
+                    if (isNativeContainer(f_type))
+                    {
+                        s << INDENT;
+                        writeTypeInfo(s, f_type, ForceValueType);
+                        s << "__d_return_value;" << endl;
+                    }
+                    else
+                        s << INDENT << "void* __d_return_value;" << endl
+                          << INDENT << "size_t __d_return_value_size;" << endl;
+                }
             }
 
             AbstractMetaArgumentList arguments = function->arguments();
             foreach (AbstractMetaArgument *argument, arguments) {
                 if (!function->argumentRemoved(argument->argumentIndex()+1)) {
-                    if (!argument->type()->isPrimitive()
+                    AbstractMetaType *atype = argument->type();
+                    if (!atype->isPrimitive()
                         || !function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) {
-                        if(argument->type()->isContainer()) {
+                        if(atype->isContainer()) {
                             QString arg_name = argument->indexedName();
-                            s << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl
-                              << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name);
-
-                            writeQtToJava(s,
-                                        argument->type(),
-                                        arg_name,
-                                        "__d_" + arg_name,
-                                        function,
-                                        argument->argumentIndex() + 1,
-                                        Option(VirtualDispatch));
+                            if(!isNativeContainer(atype))
+                                writeQtToJava(s,
+                                              argument->type(),
+                                              arg_name,
+                                              "__d_" + arg_name,
+                                              function,
+                                              argument->argumentIndex() + 1,
+                                              Option(VirtualDispatch));
                         }
                     }
                 }
@@ -867,9 +880,13 @@
                     if (f_type->isTargetLangString())
                         s << ", &ret_str";
                     if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD())
-                        s << ", &__d_return_value";
-                    if (f_type->isContainer())
-                        s << ", &__d_return_value, &__d_return_value_size";
+                        s << ", &__d_return_value";       // TODO should both be refactored into isNativeType function
+                    if (f_type->isContainer()) {
+                        if ( ((const ContainerTypeEntry *)f_type->typeEntry())->isQList() )
+                            s << ", &__d_return_value";
+                        else
+                            s << ", &__d_return_value, &__d_return_value_size";
+                    }
                 }
 
                 if (function->arguments().size() > 0)
@@ -891,9 +908,14 @@
                         s << INDENT << "return __d_return_value;" << endl;
 
                     if (f_type->isContainer()) {
-                        writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value",
-                                      function, 0, GlobalRefJObject);
-                        s << INDENT << "return __qt_return_value;" << endl;
+                        if (isNativeContainer(f_type))
+                            s << INDENT << "return __d_return_value;" << endl;
+                        else
+                        {
+                            writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value",
+                                          function, 0, GlobalRefJObject);
+                            s << INDENT << "return __qt_return_value;" << endl;
+                        }
                     }
 
                     if (f_type->isTargetLangString())
@@ -927,8 +949,16 @@
         else if(ret_type->typeEntry()->isStructInD())
             s << ", " << ret_type->typeEntry()->qualifiedCppName() << " *__d_return_value";
 
-        if (ret_type->isContainer())
-            s << ", void** __d_arr_ptr, size_t* __d_arr_size";
+        if (ret_type->isContainer()) {
+            if(isNativeContainer(ret_type)) {
+                if(d_export)
+                    s << ", " << DGenerator::translateType(ret_type, d_function->ownerClass(), NoOption) << "* __d_arr";
+                else
+                    s << ", void * __d_arr";
+            }
+            else
+                s << ", void** __d_arr_ptr, size_t* __d_arr_size";
+        }
     }
 
     if (d_function->arguments().size() > 0)
@@ -948,9 +978,11 @@
             if (d_type->name() == "QModelIndex")
                 s << "QModelIndexAccessor" << QString(d_type->actualIndirections(), '*') << " " << arg_name;
             else if (d_type->isContainer()) {
-                if (d_export) {
+                if ( isNativeContainer(d_type) )
+                    s << "void* ";
+                else if (d_export)
                     s << DGenerator::translateType(d_type, d_function->ownerClass(), NoOption) << "* ";
-                } else
+                else
                     s << "DArray* ";
                 s << arg_name;
             } else if (d_type->typeEntry()->isStructInD())
@@ -1980,8 +2012,10 @@
             } else if (d_type->isContainer()) {
                 const ContainerTypeEntry *cte =
                         static_cast<const ContainerTypeEntry *>(te);
-                if(isLinearContainer(cte))
-                    s << QString("void *%1, size_t %1_size").arg(arg_name);
+                if(cte->isQList())
+                    s << "void* " << arg_name;
+                else if(isLinearContainer(cte))
+                    s << "DArray* " << arg_name;
             } else {
                 if (!d_type->hasNativeId()) {
                     if(d_type->isVariant()) {
@@ -2350,7 +2384,7 @@
 
         s << INDENT << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_call_destructor(" << shellClassName(cls) << " *ptr)" << endl
           << INDENT << "{" << endl
-          << INDENT << QString ("    ptr->~%1();").arg(shellClassName(cls)) << endl
+          << INDENT << "    call_destructor(ptr);" << endl
           << INDENT << "}" << endl << endl;
     }
 }
@@ -3188,9 +3222,7 @@
     const ContainerTypeEntry *type =
         static_cast<const ContainerTypeEntry *>(java_type->typeEntry());
 
-    if (type->type() == ContainerTypeEntry::ListContainer
-        || type->type() == ContainerTypeEntry::VectorContainer
-        || type->type() == ContainerTypeEntry::StringListContainer
+    if (type->type() == ContainerTypeEntry::VectorContainer
         || type->type() == ContainerTypeEntry::LinkedListContainer
         || type->type() == ContainerTypeEntry::StackContainer
         || type->type() == ContainerTypeEntry::SetContainer
@@ -3203,7 +3235,6 @@
         cls_name.remove("_ConcreteWrapper");
 
         s << endl
-//          << INDENT << "{" << endl // qtd2 hack, additional scope for avoiding duplicating of "i"
           << INDENT;
 
         switch (type->type()) {
@@ -3251,7 +3282,14 @@
             s << INDENT << "++" << index << ";" << endl;
         }
         s << INDENT << "}" << endl;
-//          << INDENT << "}" << endl;
+
+    } else if (type->isQList()) {
+//            QList<QObject*> & list2 = (*(QList<QObject*> *)nativeId);
+        writeTypeInfo(s, java_type, ForceValueType);
+        s << "&" << java_name << "_tmp = (*(";
+        writeTypeInfo(s, java_type, ForceValueType);
+        s << "*)" <<  java_name << ");" << endl
+          << INDENT << java_name << "_tmp = " << qt_name << ";" << endl;
 
     } else if (type->type() == ContainerTypeEntry::PairContainer) {
         QList<AbstractMetaType *> args = java_type->instantiations();
@@ -3378,9 +3416,7 @@
     const ContainerTypeEntry *type =
         static_cast<const ContainerTypeEntry *>(java_type->typeEntry());
 
-    if (type->type() == ContainerTypeEntry::ListContainer
-        || type->type() == ContainerTypeEntry::VectorContainer
-        || type->type() == ContainerTypeEntry::StringListContainer
+    if (type->type() == ContainerTypeEntry::VectorContainer
         || type->type() == ContainerTypeEntry::LinkedListContainer
         || type->type() == ContainerTypeEntry::StackContainer
         || type->type() == ContainerTypeEntry::SetContainer
@@ -3394,23 +3430,17 @@
         writeTypeInfo(s, java_type, ForceValueType);
         s << qt_name << ";" << endl;
 
-// qtd       s << INDENT << "if (" << java_name << " != 0) {" << endl;
         {
-/* qtd           Indentation indent(INDENT);
-            s << INDENT << "jobjectArray __qt__array = qtjambi_collection_toArray(__jni_env, "
-              << java_name << ");" << endl
-              << INDENT << "jsize __qt__size = __jni_env->GetArrayLength(__qt__array);" << endl;
-*/
             if (type->type() == ContainerTypeEntry::VectorContainer
                 || type->type() == ContainerTypeEntry::StackContainer)
-                s << INDENT << qt_name << ".reserve(" << java_name << "_size);" << endl;
-
-            s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl;
+                s << INDENT << qt_name << ".reserve(" << java_name << "->length);" << endl;
+
+            s << INDENT << "for (int i=0; i<" << java_name << "->length; ++i) {" << endl;
             {
                 Indentation indent(INDENT);
                 if(targ->isTargetLangString())
                     s << INDENT << "DArray __d_element;" << endl
-                      << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element);" << endl;
+                      << INDENT << "qtd_get_string_from_array(" << java_name << "->ptr, i, &__d_element);" << endl;
                 else {
                     s << INDENT;
                     writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer | EnumAsInts));
@@ -3421,8 +3451,6 @@
                     s << "__d_element;" << endl
                       << INDENT << "qtd_get_" << elem_type << "_from_array(" << java_name << ", i, &__d_element);" << endl;
                 }
-/* qtd                   s << INDENT << "jobject __d_element = "
-                      << "__jni_env->GetObjectArrayElement(__qt__array, i);" << endl;*/
                 writeJavaToQt(s, targ, "__qt_element", "__d_element", 0, -1, BoxedPrimitive);
                 QString cont_element = "__qt_element";
                 if(targ->typeEntry()->isStructInD() && targ->name() != "QModelIndex")
@@ -3432,6 +3460,11 @@
 // qtd            s << INDENT << "}" << endl;
         }
         s << INDENT << "}" << endl;
+    } else if (type->isQList()) {
+        writeTypeInfo(s, java_type, ForceValueType);
+        s << qt_name << " = (*(";
+        writeTypeInfo(s, java_type, ForceValueType);
+        s << "*)" <<  java_name << ");" << endl;
     } else if (type->type() == ContainerTypeEntry::PairContainer) {
         QList<AbstractMetaType *> targs = java_type->instantiations();
         Q_ASSERT(targs.size() == 2);
@@ -3597,7 +3630,10 @@
 
         if ( (options & VirtualDispatch)
              && a_type->isContainer()) {
-            s << "__d_" << argument->indexedName();
+            if ( ((const ContainerTypeEntry *)a_type->typeEntry())->isQList() )
+                s << "(void*)&" <<  argument->indexedName();
+            else
+                s << "__d_" << argument->indexedName();
             continue;
         }