1
/***************************************************************************
2
* Copyright 2008 Aleix Pol <aleixpol@gmail.com> *
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. *
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. *
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
***************************************************************************/
22
#include <QStringList>
23
#include "cppxmlparse.h"
25
class KrossWrapper : public XmlToKross
28
KrossWrapper(QXmlStreamReader& _xml) : XmlToKross(_xml) {}
30
QString handlersHeader;
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";
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)
47
output += "#include <"+include+">\n";
48
handlersHeader += "#include <"+include+">\n";
53
void writeClass(const QString& classname, const QString& baseclass, const QList<QStringList>& enums)
55
classNamespace[classname]=inNamespace;
56
definedClasses.append(classname);
57
output += "class Kross" + classname + " : public QObject, public Kross::WrapperInterface\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";
67
{ output += "\tprivate:\n"
68
"\t\t"+(inNamespace.isEmpty() ? QString() : inNamespace+"::")+definedClasses.last()+"* wrapped;\n"
71
void writeVariable(const QString& name, const QString& type, bool 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";
81
void writeNamespace(const QString& name)
83
output += "using namespace "+name+";\n\n";
86
void writeEndEnum(const QStringList& fl)
89
output += "\t\tQ_ENUMS("+(inNamespace.isEmpty() ? QString() : inNamespace+"::")+definedClasses.last()+flags.takeFirst()+");\n"
92
foreach(const QString& flag, flags)
93
output += ' '+(inNamespace.isEmpty() ? QString() : inNamespace+"::")+definedClasses.last()+flag;
97
void createHandler(const QString& classname)
99
//TODO: Should improve the memory management. Use harald's script tools.
101
if(classNamespace.contains(classname) && !classNamespace[classname].isEmpty())
102
classNS=classNamespace[classname]+"::";
104
QString handlername=classname;
105
handlername[0]=handlername[0].toLower();
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";
112
output += "QVariant _"+handlername+"Handler(void* type)\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"
118
"bool b_"+classname+"="+filename+"_registerHandler(\""+classNS+classname+"*\", _"+handlername+"Handler);\n\n";
121
void writeEndDocument()
123
output += "namespace Handlers\n{\n";
125
output += "\tbool "+filename+"_registerHandler(const QByteArray& name, Kross::MetaTypeHandler::FunctionPtr* handler)\n"
126
"{ Kross::Manager::self().registerMetaTypeHandler(name, handler); return false; }\n\n";
128
handlersHeader += "namespace Handlers\n{\n";
129
foreach(const QString& aclass, definedClasses)
130
createHandler(aclass);
132
handlersHeader += "}\n\n";
133
output += "#include \""+filename+".moc\"\n";
135
handlersHeader += "#endif\n";
138
void writeEndFunction(const method& m)
140
QString rettype=m.returnType;
141
rettype=rettype.remove('&');
142
output += "\t\tQ_SCRIPTABLE " + rettype +' '+ m.funcname+'(';
146
foreach(const method::argument& arg, m.args)
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);
155
output += arg.type +' '+ varname;
156
if(!arg.def.isEmpty())
162
if(!values.isEmpty())
163
output.resize(output.size()-2);
168
QString shouldReturn= m.returnType=="void" ? QString() : QString("return ");
170
output += " { "+shouldReturn+"wrapped->"+m.funcname+'(';
171
foreach(const QString& val, values)
176
if(!values.isEmpty())
177
output.resize(output.size()-2);
183
int main(int argc, char** argv)
185
QStringList includes;
189
for(i=1; i<argc; i++)
191
if(argv[i][0]=='-' && argv[i][1]!=0) {
192
QString param=argv[i];
196
param=param.right(param.size()-2);
197
includes += param.split(';');
200
filename=param.right(param.size()-2);
203
directory=param.right(param.size()-2);
209
qDebug() << "error. Unrecognized parameter: " << argv[i];
215
if(!f.open(stdin, QIODevice::ReadOnly)) {
216
qDebug() << "error. can't read the input: " << argv[i];
220
QXmlStreamReader xml(&f);
222
p.setIncludes(includes);
223
p.setFileName(filename);
227
fprintf(stdout, "%s", qPrintable(p.output));
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";
236
QTextStream out(&headerFile);
237
out << p.handlersHeader;