Created
January 26, 2017 15:06
-
-
Save kropp/5a4dfab90b4335923c0dfbc39ec7a2f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "QMetaTypes.h" | |
#include "Api.h" | |
QString jsType(const QString& name); | |
void generateSignalClasses(QFile &out, const QMetaObject &apiMeta); | |
void generateParameterList(QFile &out, const QMetaMethod &methodMeta); | |
void generateApi(QFile &out, const QMetaObject &apiMeta); | |
int main(int argc, char *argv[]) { | |
qRegisterMetaTypes(); | |
QFile out; | |
out.open(stdout, QFile::WriteOnly); | |
auto& apiMeta = Api::staticMetaObject; | |
generateSignalClasses(out, apiMeta); | |
out.write("\n"); | |
generateApi(out, apiMeta); | |
return 0; | |
} | |
void generateSignalClasses(QFile &out, const QMetaObject &apiMeta) { | |
for (int i = 0; i < apiMeta.methodCount(); ++i) { | |
auto methodMeta = apiMeta.method(i); | |
if (methodMeta.enclosingMetaObject()->className() == apiMeta.className()) { | |
if (methodMeta.methodType() == QMetaMethod::Signal) { | |
out.write("class "); | |
out.write(methodMeta.name()); | |
out.write(" {\n connect(callback: ("); | |
generateParameterList(out, methodMeta); | |
out.write(") => void): void;\n}\n"); | |
} | |
} | |
} | |
} | |
void generateParameterList(QFile &out, const QMetaMethod &methodMeta) { | |
auto parameterNames = methodMeta.parameterNames(); | |
auto parameterTypes = methodMeta.parameterTypes(); | |
for (int j = 0; j < methodMeta.parameterCount(); ++j) { | |
if (j > 0) { | |
out.write(", "); | |
} | |
out.write(parameterNames.at(j)); | |
out.write(": "); | |
out.write(jsType(parameterTypes.at(j)).toUtf8()); | |
} | |
} | |
void generateApi(QFile &out, const QMetaObject &apiMeta) { | |
out.write("class Api {\n"); | |
for (int i = 0; i < apiMeta.methodCount(); ++i) { | |
auto methodMeta = apiMeta.method(i); | |
if (methodMeta.enclosingMetaObject()->className() == apiMeta.className()) { | |
if (methodMeta.methodType() == QMetaMethod::Slot) { | |
out.write(" "); | |
out.write(methodMeta.name()); | |
out.write("("); | |
generateParameterList(out, methodMeta); | |
if (strcmp(methodMeta.typeName(), "void") != 0) { | |
if (methodMeta.parameterCount() > 0) { | |
out.write(", "); | |
} | |
out.write("callback: (result: "); | |
out.write(jsType(methodMeta.typeName()).toUtf8()); | |
out.write(") => void"); | |
} | |
out.write("): void;\n"); | |
} else if (methodMeta.methodType() == QMetaMethod::Signal) { | |
out.write(" "); | |
out.write(methodMeta.name()); | |
out.write(": "); | |
out.write(methodMeta.name()); | |
out.write(";\n"); | |
} | |
} | |
} | |
out.write("}\n\n" | |
"class WebChannelObjects {\n" | |
" api: Api;\n" | |
"}\n" | |
"\n" | |
"class WebChannel {\n" | |
" objects: WebChannelObjects;\n" | |
"}"); | |
} | |
QString jsType(const QString& name) { | |
if (name == "QVariantMap") { | |
return "Object"; | |
} | |
if (name == "QVariantList") { | |
return "Array"; | |
} | |
if (name == "QString") { | |
return "string"; | |
} | |
if (name == "bool") { | |
return "boolean"; | |
} | |
return name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment