summaryrefslogtreecommitdiffstats
path: root/generators/boostpython/hppgenerator.cpp
blob: f6d576d08367944517a091b5d97ff1e691b57d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * This file is part of the Boost Python Generator project.
 *
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: PySide team <contact@pyside.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */
#include "hppgenerator.h"
#include <apiextractor/reporthandler.h>

#include <QtCore/QDir>
#include <QtCore/QTextStream>
#include <QtCore/QVariant>
#include <QtCore/QRegExp>
#include <QtCore/QDebug>

static Indentor INDENT;

QString HppGenerator::fileNameForClass(const AbstractMetaClass *cppClass) const
{
    return getWrapperName(cppClass) + QLatin1String(".hpp");
}

void HppGenerator::writeCopyCtor(QTextStream &s, const AbstractMetaClass *cppClass)
{
    s << INDENT <<  getWrapperName(cppClass) << "(PyObject *py_self, const " << cppClass->qualifiedCppName() << "& self)"
      << " :  " << cppClass->qualifiedCppName() << "(self), wrapper(py_self)" << endl
      << INDENT << "{" << endl
      << INDENT << "}" << endl;
}

void HppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *cppClass)
{
    ReportHandler::debugSparse("Generating header for " + cppClass->fullName());
    Indentation indent(INDENT);

    // write license comment
    s << licenseComment() << endl;

    QString wrapperName = HppGenerator::getWrapperName(cppClass);
    // Header
    s << "#ifndef __" << wrapperName.toUpper() << "__" << endl;
    s << "#define __" << wrapperName.toUpper() << "__" << endl << endl;

    s << "#include <pyside.hpp>" << endl;
    //Includes
    if (cppClass->typeEntry()->include().isValid())
        s << cppClass->typeEntry()->include().toString() << endl << endl;

    s << "using namespace PySide;" << endl << endl;

    if (!cppClass->isPolymorphic() || cppClass->hasPrivateDestructor() || cppClass->isNamespace())
        s << "namespace " << wrapperName << " {" << endl << endl;

    bool needWriteBackReference = false;
    if (cppClass->isNamespace()) {
        s << INDENT << "struct Namespace {};" << endl;
    } else {
        QString className;
        bool create_wrapper = canCreateWrapperFor(cppClass);
        bool is_wrapper = false;
        // detect the held type
        QString held_type = cppClass->typeEntry()->heldTypeValue();
        if (held_type.isEmpty() && create_wrapper)
            held_type = "qptr";

        writeCodeSnips(s, cppClass->typeEntry()->codeSnips(),
                       CodeSnip::Declaration, TypeSystem::NativeCode);

        if (cppClass->isPolymorphic() && !cppClass->hasPrivateDestructor()) {
            // Class
            s << "class PYSIDE_LOCAL " << wrapperName;
            if (create_wrapper) {
                s << " : public " << cppClass->qualifiedCppName() << ", public PySide::wrapper";
            }
            s << endl;
            s << "{" << endl;
        }

        writeCodeSnips(s, cppClass->typeEntry()->codeSnips(),
                       CodeSnip::Declaration, TypeSystem::ShellDeclaration);

        if (cppClass->isPolymorphic() && !cppClass->hasPrivateDestructor()) {
            s << endl << "private:" << endl;
            className = wrapperName;
            is_wrapper = true;
        } else {
            className = cppClass->qualifiedCppName();
        }

        // print the huge boost::python::class_ typedef
        s << INDENT << "typedef boost::python::class_< " << cppClass->qualifiedCppName();

        writeBaseClass(s, cppClass);

        if (!held_type.isEmpty())
            s << ", PySide::" << held_type << " < " << className << ", qptr_base::no_check_cache | qptr_base::"
              << ( is_wrapper ? "wrapper_pointer" : "no_wrapper_pointer") << "> ";

        if (!isCopyable(cppClass))
            s << ", boost::noncopyable";

        s << " > class_type;" << endl;

        if (cppClass->isPolymorphic() && !cppClass->hasPrivateDestructor()) {
            s << "public:" << endl;

            if (isCopyable(cppClass))
                writeCopyCtor(s, cppClass);

            foreach (AbstractMetaFunction *func, filterFunctions(cppClass))
                writeFunction(s, func);

            if (create_wrapper) {
                //destructor
                s << INDENT << "~" << wrapperName << "();" << endl;

                if (cppClass->isQObject() && (cppClass->name() != "QObject"))
                    s << INDENT << "using QObject::parent;" << endl;
            }
        }

        writeCodeSnips(s, cppClass->typeEntry()->codeSnips(),
                       CodeSnip::End, TypeSystem::ShellDeclaration);
    }

    QString staticKeyword = cppClass->isNamespace() ? QLatin1String("") : QLatin1String("static ");
    s << INDENT;
    if (cppClass->isPolymorphic() && !cppClass->hasPrivateDestructor()) {
        s << "//static member used to export class" << endl;
        s << INDENT << staticKeyword;
    }
    s << "void define_python_class() throw();" << endl << endl;

    writeCodeSnips(s, cppClass->typeEntry()->codeSnips(),
                   CodeSnip::PrototypeInitialization, TypeSystem::NativeCode);


    s << "};" << endl << endl;

    s << "#endif // __" << wrapperName.toUpper() << "__" << endl << endl;
}

void HppGenerator::writeFunction(QTextStream &s, const AbstractMetaFunction* func)
{
    // pure virtual functions need a default implementation
    if ((func->isPrivate() && !func->isConstructor()) || (func->isModifiedRemoved() && !func->isAbstract()))
        return;

    // do not write copy ctors here.
    if (func->isCopyConstructor())
        return;

    if (func->isConstructor() || func->isAbstract() || func->isVirtual()) {
        if (func->isVirtual() && !func->isAbstract() && !func->isConstructor()
            && !func->ownerClass()->hasPrivateDestructor()
            && func->implementingClass() == func->ownerClass()) {
            s << INDENT << "static " << signatureForDefaultVirtualMethod(func, "", "_default", Generator::SkipName) << ';' << endl;
        }

        if (func->isConstructor()) {
            s << INDENT << getWrapperName(func->ownerClass()) << "(PyObject *py_self" << (func->arguments().size() ? "," : "");
            writeFunctionArguments(s, func, Generator::OriginalTypeDescription |  Generator::SkipName);
            s << ")";
        } else {
            s << INDENT << functionSignature(func, "", "", Generator::OriginalTypeDescription |  Generator::SkipName);
        }

        if (func->isModifiedRemoved() && func->isAbstract())
            writeDefaultImplementation(s, func);
        else
            s << ';' << endl;
    }
}

void HppGenerator::writeDefaultImplementation(QTextStream& s, const AbstractMetaFunction* func)
{
    QString returnValue;
    if (func->type()) {
        if (func->type()->isObject() || func->type()->isQObject() || func->type()->name() == "void")
            returnValue = "0";
        else
            returnValue = functionReturnType(func) + "()";
    }
    s << " { return " << returnValue << "; }" << endl;
}

void HppGenerator::writeBaseClass(QTextStream& s, const AbstractMetaClass* cppClass)
{
    if (!cppClass->isNamespace() && !cppClass->isInterface()) {
        QStringList baseClass = getBaseClasses(cppClass);

        if (baseClass.isEmpty()) {
            const ComplexTypeEntry *type = cppClass->typeEntry();
            if (cppClass->name() != type->defaultSuperclass()) {
                QString sc = type->defaultSuperclass();
                if (!sc.isEmpty())
                    s << ", python::bases< " << sc << "> ";
            }
        } else {
            s << ", boost::python::bases< " << baseClass.join(", ") << " > ";
        }
    }
}