Mercurial > projects > qtd
comparison generator/dgenerator.cpp @ 152:4d1c5d1d1bbf
fix some inconsistencies with conversion functions
author | eldar |
---|---|
date | Mon, 15 Jun 2009 23:16:42 +0000 |
parents | 7ae9bc9d6935 |
children | ae06da58ec25 |
comparison
equal
deleted
inserted
replaced
151:a28893622ff4 | 152:4d1c5d1d1bbf |
---|---|
2472 | 2472 |
2473 if (d_class->isQObject()) | 2473 if (d_class->isQObject()) |
2474 writeQObjectFunctions(s, d_class); | 2474 writeQObjectFunctions(s, d_class); |
2475 | 2475 |
2476 | 2476 |
2477 if (d_class->needsConversionFunc) | 2477 // if (d_class->needsConversionFunc) |
2478 writeConversionFunction(s, d_class); | 2478 writeConversionFunction(s, d_class); |
2479 | 2479 |
2480 if (d_class->hasConstructors()) | 2480 if (d_class->hasConstructors()) |
2481 s << "extern (C) void qtd_" << d_class->name() << "_destructor(void *ptr);" << endl << endl; | 2481 s << "extern (C) void qtd_" << d_class->name() << "_destructor(void *ptr);" << endl << endl; |
2482 | 2482 |
2588 } | 2588 } |
2589 | 2589 |
2590 void DGenerator::writeConversionFunction(QTextStream &s, const AbstractMetaClass *d_class) | 2590 void DGenerator::writeConversionFunction(QTextStream &s, const AbstractMetaClass *d_class) |
2591 { | 2591 { |
2592 const ComplexTypeEntry *ctype = d_class->typeEntry(); | 2592 const ComplexTypeEntry *ctype = d_class->typeEntry(); |
2593 if(!ctype->isQObject() && !ctype->isObject()) | |
2594 return; | |
2593 QString class_name = ctype->name(); | 2595 QString class_name = ctype->name(); |
2594 QString return_type_name = class_name; | 2596 QString return_type_name = class_name; |
2595 if(ctype->designatedInterface()) | 2597 if(ctype->designatedInterface()) |
2596 return_type_name = ctype->designatedInterface()->name(); | 2598 return_type_name = ctype->designatedInterface()->name(); |
2597 s << return_type_name << " qtd_" << class_name << "_from_ptr(void* __qt_return_value) {" << endl; | 2599 s << return_type_name << " qtd_" << class_name << "_from_ptr(void* __qt_return_value) {" << endl; |
2612 << INDENT << "} else" << endl | 2614 << INDENT << "} else" << endl |
2613 << INDENT << " return cast(" << class_name << ") d_obj;" << endl; | 2615 << INDENT << " return cast(" << class_name << ") d_obj;" << endl; |
2614 } else if (ctype->isObject()) { | 2616 } else if (ctype->isObject()) { |
2615 QString type_name = class_name; | 2617 QString type_name = class_name; |
2616 if(ctype->isAbstract()) | 2618 if(ctype->isAbstract()) |
2617 type_name = type_name + "_ConcreteWrapper"; | 2619 type_name = ctype->targetLangName() + "_ConcreteWrapper"; |
2618 | 2620 |
2619 // if class has virtual functions then it has classname_entity function so | 2621 // if class has virtual functions then it has classname_entity function so |
2620 // we can look for D Object pointer. otherwise create new wrapper | 2622 // we can look for D Object pointer. otherwise create new wrapper |
2621 if (d_class->hasVirtualFunctions()) { | 2623 if (d_class->hasVirtualFunctions()) { |
2622 s << INDENT << "void* d_obj = __" << ctype->name() << "_entity(__qt_return_value);" << endl | 2624 s << INDENT << "void* d_obj = __" << ctype->targetLangName() << "_entity(__qt_return_value);" << endl |
2623 << INDENT << "if (d_obj !is null) {" << endl | |
2624 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl | |
2625 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl | |
2626 << INDENT << "} else {" << endl | |
2627 << INDENT << " auto return_value = new " << type_name << "(__qt_return_value, true);" << endl | |
2628 << INDENT << " return_value.__no_real_delete = true;" << endl | |
2629 << INDENT << " return return_value;" << endl | |
2630 << INDENT << "}"; | |
2631 } else { | |
2632 s << INDENT << "auto return_value = new " << type_name << "(__qt_return_value, true);" << endl | |
2633 << INDENT << "return_value.__no_real_delete = true;" << endl | |
2634 << INDENT << "return return_value;" << endl; | |
2635 } | |
2636 } | |
2637 s << "}" << endl << endl; | |
2638 } | |
2639 | |
2640 | |
2641 void DGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *d_class) | |
2642 { | |
2643 s << "extern(C) void* qtd_" << d_class->name() << "_d_pointer(void *obj);" << endl | |
2644 << "extern(C) void qtd_" << d_class->name() << "_create_link(void *obj, void* d_obj);" << endl << endl; | |
2645 s << "private extern (C) void qtd_D_" << d_class->name() << "_delete(void *d_ptr) {" << endl | |
2646 << " auto d_ref = cast(QObject) d_ptr;" << endl | |
2647 << " d_ref.__no_real_delete = true;" << endl | |
2648 << " delete d_ref;" << endl | |
2649 << "}" << endl << endl; | |
2650 } | |
2651 | |
2652 /* | |
2653 void DGenerator::writeMarshallFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
2654 { | |
2655 | |
2656 } | |
2657 */ | |
2658 void DGenerator::marshallFromCppToD(QTextStream &s, const ComplexTypeEntry* ctype) | |
2659 { | |
2660 if(ctype->isQObject()) { | |
2661 QString type_name = ctype->name(); | |
2662 | |
2663 if(ctype->isAbstract()) | |
2664 type_name = type_name + "_ConcreteWrapper"; | |
2665 | |
2666 s << INDENT << "if (__qt_return_value is null)" << endl | |
2667 << INDENT << " return null;" << endl | |
2668 << INDENT << "void* d_obj = __QObject_entity(__qt_return_value);" << endl | |
2669 << INDENT << "if (d_obj is null) {" << endl | |
2670 << INDENT << " auto new_obj = new " << type_name << "(__qt_return_value, true);" << endl | |
2671 << INDENT << " new_obj.__no_real_delete = true;" << endl | |
2672 << INDENT << " return new_obj;" << endl | |
2673 << INDENT << "} else" << endl | |
2674 << INDENT << " return cast(" << ctype->name() << ") d_obj;" << endl; | |
2675 } else if (ctype->isValue() && !ctype->isStructInD()) | |
2676 s << INDENT << "return new " << ctype->name() << "(__qt_return_value, false);" << endl; | |
2677 else if (ctype->isVariant()) | |
2678 s << INDENT << "return new QVariant(__qt_return_value, false);" << endl; | |
2679 else if (ctype->name() == "QModelIndex" || ctype->isStructInD()) | |
2680 s << INDENT << "return __qt_return_value;" << endl; | |
2681 else if (ctype->isObject()) { | |
2682 QString type_name = ctype->name(); | |
2683 | |
2684 if(ctype->isAbstract()) | |
2685 type_name = type_name + "_ConcreteWrapper"; | |
2686 | |
2687 QString return_type_name = ctype->name(); | |
2688 if(ctype->designatedInterface()) | |
2689 return_type_name = ctype->designatedInterface()->name(); | |
2690 | |
2691 AbstractMetaClass *d_class = NULL; | |
2692 | |
2693 d_class = ClassFromEntry::get(ctype); | |
2694 | |
2695 // if class has virtual functions then it has classname_entity function so | |
2696 // we can look for D Object pointer. otherwise create new wrapper | |
2697 if (d_class != NULL && d_class->hasVirtualFunctions()) { | |
2698 s << INDENT << "void* d_obj = __" << ctype->name() << "_entity(__qt_return_value);" << endl | |
2699 << INDENT << "if (d_obj !is null) {" << endl | 2625 << INDENT << "if (d_obj !is null) {" << endl |
2700 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl | 2626 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl |
2701 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl | 2627 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl |
2702 << INDENT << "} else {" << endl | 2628 << INDENT << "} else {" << endl |
2703 << INDENT << " auto return_value = new " << type_name << "(__qt_return_value, true);" << endl | 2629 << INDENT << " auto return_value = new " << type_name << "(__qt_return_value, true);" << endl |
2708 s << INDENT << "auto return_value = new " << type_name << "(__qt_return_value, true);" << endl | 2634 s << INDENT << "auto return_value = new " << type_name << "(__qt_return_value, true);" << endl |
2709 << INDENT << "return_value.__no_real_delete = true;" << endl | 2635 << INDENT << "return_value.__no_real_delete = true;" << endl |
2710 << INDENT << "return return_value;" << endl; | 2636 << INDENT << "return return_value;" << endl; |
2711 } | 2637 } |
2712 } | 2638 } |
2713 | 2639 s << "}" << endl << endl; |
2640 } | |
2641 | |
2642 | |
2643 void DGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *d_class) | |
2644 { | |
2645 s << "extern(C) void* qtd_" << d_class->name() << "_d_pointer(void *obj);" << endl | |
2646 << "extern(C) void qtd_" << d_class->name() << "_create_link(void *obj, void* d_obj);" << endl << endl; | |
2647 s << "private extern (C) void qtd_D_" << d_class->name() << "_delete(void *d_ptr) {" << endl | |
2648 << " auto d_ref = cast(QObject) d_ptr;" << endl | |
2649 << " d_ref.__no_real_delete = true;" << endl | |
2650 << " delete d_ref;" << endl | |
2651 << "}" << endl << endl; | |
2652 } | |
2653 | |
2654 /* | |
2655 void DGenerator::writeMarshallFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
2656 { | |
2657 | |
2658 } | |
2659 */ | |
2660 void DGenerator::marshallFromCppToD(QTextStream &s, const ComplexTypeEntry* ctype) | |
2661 { | |
2662 if(ctype->isQObject()) { | |
2663 QString type_name = ctype->name(); | |
2664 s << "return qtd_" << type_name << "_from_ptr(__qt_return_value);" << endl; | |
2665 } else if (ctype->isValue() && !ctype->isStructInD()) { | |
2666 s << INDENT << "return new " << ctype->name() << "(__qt_return_value, false);" << endl; | |
2667 } else if (ctype->isVariant()) { | |
2668 s << INDENT << "return new QVariant(__qt_return_value, false);" << endl; | |
2669 } else if (ctype->name() == "QModelIndex" || ctype->isStructInD()) { | |
2670 s << INDENT << "return __qt_return_value;" << endl; | |
2671 } else if (ctype->isObject()) { | |
2672 QString type_name = ctype->name(); | |
2673 s << "return qtd_" << type_name << "_from_ptr(__qt_return_value);" << endl; | |
2674 } | |
2714 } | 2675 } |
2715 | 2676 |
2716 void DGenerator::writeNativeField(QTextStream &s, const AbstractMetaField *field) | 2677 void DGenerator::writeNativeField(QTextStream &s, const AbstractMetaField *field) |
2717 { | 2678 { |
2718 Q_ASSERT(field->isPublic() || field->isProtected()); | 2679 Q_ASSERT(field->isPublic() || field->isProtected()); |
2987 ctype_child->addedTo = cls->name(); | 2948 ctype_child->addedTo = cls->name(); |
2988 } | 2949 } |
2989 | 2950 |
2990 foreach (AbstractMetaFunction *function, cls->functions()) | 2951 foreach (AbstractMetaFunction *function, cls->functions()) |
2991 function->checkStoreResult(); | 2952 function->checkStoreResult(); |
2992 | 2953 /* we don't need this anymore |
2993 // generate QObject conversion functions only those that are required | 2954 // generate QObject conversion functions only those that are required |
2994 AbstractMetaFunctionList d_funcs = cls->functionsInTargetLang(); | 2955 AbstractMetaFunctionList d_funcs = cls->functionsInTargetLang(); |
2995 for (int i=0; i<d_funcs.size(); ++i) { | 2956 for (int i=0; i<d_funcs.size(); ++i) { |
2996 AbstractMetaType *f_type = d_funcs.at(i)->type(); | 2957 AbstractMetaType *f_type = d_funcs.at(i)->type(); |
2997 if (!f_type) | 2958 if (!f_type) |
3000 const ComplexTypeEntry* cte = static_cast<const ComplexTypeEntry *>(f_type->typeEntry()); | 2961 const ComplexTypeEntry* cte = static_cast<const ComplexTypeEntry *>(f_type->typeEntry()); |
3001 AbstractMetaClass* d_class = ClassFromEntry::get(cte); | 2962 AbstractMetaClass* d_class = ClassFromEntry::get(cte); |
3002 if (d_class) | 2963 if (d_class) |
3003 d_class->needsConversionFunc = true; | 2964 d_class->needsConversionFunc = true; |
3004 } | 2965 } |
3005 } | 2966 }*/ |
3006 } | 2967 } |
3007 | 2968 |
3008 Generator::generate(); | 2969 Generator::generate(); |
3009 | 2970 |
3010 { | 2971 { |