~ubuntu-branches/ubuntu/utopic/kdevplatform/utopic-proposed

« back to all changes in this revision

Viewing changes to shell/kross/xmltokross/main.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2014-08-30 03:52:11 UTC
  • mfrom: (0.3.26)
  • Revision ID: package-import@ubuntu.com-20140830035211-wndqlc843eu2v8nk
Tags: 1.7.0-0ubuntu1
* New upstream release
* Add XS-Testsuite: autopkgtest

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright 2008 Aleix Pol <aleixpol@gmail.com>                         *
3
 
 *                                                                         *
4
 
 *   This program is free software; you can redistribute it and/or modify  *
5
 
 *   it under the terms of the GNU Library General Public License as       *
6
 
 *   published by the Free Software Foundation; either version 2 of the    *
7
 
 *   License, or (at your option) any later version.                       *
8
 
 *                                                                         *
9
 
 *   This program is distributed in the hope that it will be useful,       *
10
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 
 *   GNU General Public License for more details.                          *
13
 
 *                                                                         *
14
 
 *   You should have received a copy of the GNU Library General Public     *
15
 
 *   License along with this program; if not, write to the                 *
16
 
 *   Free Software Foundation, Inc.,                                       *
17
 
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18
 
 ***************************************************************************/
19
 
 
20
 
#include <QFile>
21
 
#include <QDebug>
22
 
#include <QStringList>
23
 
#include "cppxmlparse.h"
24
 
 
25
 
class KrossWrapper : public XmlToKross
26
 
{
27
 
    public:
28
 
        KrossWrapper(QXmlStreamReader& _xml) : XmlToKross(_xml) {}
29
 
        QString output;
30
 
        QString handlersHeader;
31
 
        
32
 
        void writeDocument()
33
 
        {
34
 
            handlersHeader += "#ifndef "+filename.toUpper()+"_H\n";
35
 
            handlersHeader += "#define "+filename.toUpper()+"_H\n\n";
36
 
            handlersHeader += "#include<QtCore/QVariant>\n\n";
37
 
            handlersHeader += "//This is file has been generated by xmltokross, "
38
 
                              "you should not edit this file but the files used to generate it.\n\n";
39
 
                              
40
 
            output += "//This is file has been generated by xmltokross, you should not edit this file but the files used to generate it.\n\n"
41
 
                      "#include <QtCore/QObject>\n"
42
 
                      "#include <QtCore/QVariant>\n"
43
 
                      "#include <kross/core/manager.h>\n"
44
 
                      "#include <kross/core/wrapperinterface.h>\n";
45
 
            foreach(const QString & include, includes)
46
 
            {
47
 
                output += "#include <"+include+">\n";
48
 
                handlersHeader += "#include <"+include+">\n";
49
 
            }
50
 
            output +='\n';
51
 
        }
52
 
        
53
 
        void writeClass(const QString& classname, const QString& baseclass, const QList<QStringList>& enums)
54
 
        {
55
 
            classNamespace[classname]=inNamespace;
56
 
            definedClasses.append(classname);
57
 
            output += "class Kross" + classname + " : public QObject, public Kross::WrapperInterface\n"
58
 
                      "{\n"
59
 
                      "\tQ_OBJECT\n"
60
 
                      "\tpublic:\n"
61
 
                      "\t\tKross"+classname+'('+/*inNamespace.isEmpty() ? QString() : inNamespace+"::"
62
 
                                 +*/classname+"* obj, QObject* parent=0) : QObject(parent), wrapped(obj) {}\n"
63
 
                      "\t\tvoid* wrappedObject() const { return wrapped; }\n\n";
64
 
        }
65
 
        
66
 
        void writeEndClass()
67
 
        { output += "\tprivate:\n"
68
 
                    "\t\t"+(inNamespace.isEmpty() ? QString() : inNamespace+"::")+definedClasses.last()+"* wrapped;\n"
69
 
                    "};\n\n"; }
70
 
                    
71
 
        void writeVariable(const QString& name, const QString& type, bool isConst)
72
 
        {
73
 
            QString write;
74
 
            if(!isConst)
75
 
                write=" WRITE set"+name;
76
 
            output += "\t\tQ_PROPERTY("+type+' '+name+" READ get"+name+write+" SCRIPTABLE true)\n";
77
 
            if(!isConst) output += "\t\tQ_SCRIPTABLE void set"+name+"(const "+type+"& val) { wrapped->"+name+"=val; }\n";
78
 
            output += "\t\tQ_SCRIPTABLE "+type+" get"+name+"() const { return wrapped->"+name+"; }\n";
79
 
        }
80
 
        
81
 
        void writeNamespace(const QString& name)
82
 
        {
83
 
            output += "using namespace "+name+";\n\n";
84
 
        }
85
 
        
86
 
        void writeEndEnum(const QStringList& fl)
87
 
        {
88
 
            QStringList flags=fl;
89
 
            output += "\t\tQ_ENUMS("+(inNamespace.isEmpty() ? QString() : inNamespace+"::")+definedClasses.last()+flags.takeFirst()+");\n"
90
 
                      "\t\tQ_FLAGS(";
91
 
            
92
 
            foreach(const QString& flag, flags)
93
 
                output += ' '+(inNamespace.isEmpty() ? QString() : inNamespace+"::")+definedClasses.last()+flag;
94
 
            output += ");\n\n";
95
 
        }
96
 
        
97
 
        void createHandler(const QString& classname)
98
 
        {
99
 
            //TODO: Should improve the memory management. Use harald's script tools.
100
 
            QString classNS;
101
 
            if(classNamespace.contains(classname) && !classNamespace[classname].isEmpty())
102
 
                classNS=classNamespace[classname]+"::";
103
 
            
104
 
            QString handlername=classname;
105
 
            handlername[0]=handlername[0].toLower();
106
 
            
107
 
            handlersHeader += "\tQVariant _"+handlername+"Handler(void* type);\n";
108
 
            handlersHeader += "\tQVariant "+handlername+"Handler("+classNS+classname+"* type) { return _"+handlername+"Handler((void*) type); }\n";
109
 
            handlersHeader += "\tQVariant "+handlername+"Handler(const "+classNS+classname+"* type) "
110
 
                                "{ return _"+handlername+"Handler((void*) type); }\n\n";
111
 
            
112
 
            output += "QVariant _"+handlername+"Handler(void* type)\n"
113
 
            "{\n"
114
 
            "\tif(!type) return QVariant();\n"
115
 
            '\t'+(classNS.isEmpty() ? QString() : (classNS))+classname+"* t=static_cast<"+classNS+classname+"*>(type);\n"
116
 
            "\treturn qVariantFromValue((QObject*) new Kross"+classname+"(t, 0));\n"
117
 
            "}\n"
118
 
            "bool b_"+classname+"="+filename+"_registerHandler(\""+classNS+classname+"*\", _"+handlername+"Handler);\n\n";
119
 
        }
120
 
        
121
 
        void writeEndDocument()
122
 
        {
123
 
            output += "namespace Handlers\n{\n";
124
 
            
125
 
            output += "\tbool "+filename+"_registerHandler(const QByteArray& name, Kross::MetaTypeHandler::FunctionPtr* handler)\n"
126
 
                      "{ Kross::Manager::self().registerMetaTypeHandler(name, handler); return false; }\n\n";
127
 
                      
128
 
            handlersHeader += "namespace Handlers\n{\n";
129
 
            foreach(const QString& aclass, definedClasses)
130
 
                createHandler(aclass);
131
 
            output += "}\n";
132
 
            handlersHeader += "}\n\n";
133
 
            output += "#include \""+filename+".moc\"\n";
134
 
            
135
 
            handlersHeader += "#endif\n";
136
 
        }
137
 
        
138
 
        void writeEndFunction(const method& m)
139
 
        {
140
 
            QString rettype=m.returnType;
141
 
            rettype=rettype.remove('&');
142
 
            output += "\t\tQ_SCRIPTABLE " + rettype +' '+ m.funcname+'(';
143
 
            QStringList values;
144
 
            
145
 
            int param=0;
146
 
            foreach(const method::argument& arg, m.args)
147
 
            {
148
 
                QString varname=arg.name;
149
 
                if(varname.isEmpty()) {
150
 
                    qWarning() << "The paramenter number "+QString::number(param)+" in method: "+
151
 
                            inNamespace+"::"+definedClasses.last()+"::"+m.funcname+" does not have a name";
152
 
                    varname=QString("x%1").arg(param);
153
 
                }
154
 
                values += varname;
155
 
                output += arg.type +' '+ varname;
156
 
                if(!arg.def.isEmpty())
157
 
                    output+='='+arg.def;
158
 
                output += ", ";
159
 
                param++;
160
 
            }
161
 
            
162
 
            if(!values.isEmpty())
163
 
                output.resize(output.size()-2);
164
 
            output += ')';
165
 
            if(m.isConst)
166
 
                output+=" const";
167
 
            
168
 
            QString shouldReturn= m.returnType=="void" ? QString() : QString("return ");
169
 
            
170
 
            output += " { "+shouldReturn+"wrapped->"+m.funcname+'(';
171
 
            foreach(const QString& val, values)
172
 
            {
173
 
                output+=val+", ";
174
 
            }
175
 
            
176
 
            if(!values.isEmpty())
177
 
                output.resize(output.size()-2);
178
 
            
179
 
            output += "); }\n";
180
 
        }
181
 
};
182
 
 
183
 
int main(int argc, char** argv)
184
 
{
185
 
    QStringList includes;
186
 
    QString filename;
187
 
    QString directory;
188
 
    int i;
189
 
    for(i=1; i<argc; i++)
190
 
    {
191
 
        if(argv[i][0]=='-' && argv[i][1]!=0) {
192
 
            QString param=argv[i];
193
 
            switch(argv[i][1])
194
 
            {
195
 
                case 'I':
196
 
                    param=param.right(param.size()-2);
197
 
                    includes += param.split(';');
198
 
                    break;
199
 
                case 'F':
200
 
                    filename=param.right(param.size()-2);
201
 
                    break;
202
 
                case 'D':
203
 
                    directory=param.right(param.size()-2);
204
 
                    break;
205
 
            }
206
 
        }
207
 
        else
208
 
        {
209
 
            qDebug() << "error. Unrecognized parameter: " << argv[i];
210
 
            return 1;
211
 
        }
212
 
    }
213
 
    
214
 
    QFile f;
215
 
    if(!f.open(stdin, QIODevice::ReadOnly)) {
216
 
        qDebug() << "error. can't read the input: " << argv[i];
217
 
        return 33;
218
 
    }
219
 
    
220
 
    QXmlStreamReader xml(&f);
221
 
    KrossWrapper p(xml);
222
 
    p.setIncludes(includes);
223
 
    p.setFileName(filename);
224
 
    
225
 
    int ret=p.start();
226
 
    
227
 
    fprintf(stdout, "%s", qPrintable(p.output));
228
 
    
229
 
    if(!filename.isEmpty()) {
230
 
        QFile headerFile(directory+'/'+filename+".h");
231
 
        if(!headerFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
232
 
            qDebug() << "error. can't write the header: " << filename+".h";
233
 
            return 33;
234
 
        }
235
 
        
236
 
        QTextStream out(&headerFile);
237
 
        out << p.handlersHeader;
238
 
        headerFile.close();
239
 
    }
240
 
    return ret;
241
 
}