roll back qt5cpp template

This commit is contained in:
wing328 2015-06-09 12:42:07 +08:00
parent 263b4080ee
commit 6253bbff3d
9 changed files with 454 additions and 512 deletions

View File

@ -2,26 +2,23 @@
#include "{{prefix}}Helpers.h"
#include "{{prefix}}ModelFactory.h"
#include
<QJsonArray>
#include
<QJsonDocument>
#include <QJsonArray>
#include <QJsonDocument>
namespace Swagger {
{{classname}}::{{classname}}() {}
namespace Swagger {
{{classname}}::{{classname}}() {}
{{classname}}::~{{classname}}() {}
{{classname}}::~{{classname}}() {}
{{classname}}::{{classname}}(QString host, QString basePath) {
{{classname}}::{{classname}}(QString host, QString basePath) {
this->host = host;
this->basePath = basePath;
}
}
{{#operations}}
{{#operation}}
void
{{classname}}::{{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}
, {{/hasMore}}{{/allParams}}) {
{{#operations}}
{{#operation}}
void
{{classname}}::{{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("{{path}}");
@ -98,9 +95,7 @@
{{#bodyParams}}
{{#isContainer}}
QJsonArray* {{paramName}}Array = new QJsonArray();
toJsonArray((QList
<void
*>*){{paramName}}, {{paramName}}Array, QString("body"), QString("SWGUser*"));
toJsonArray((QList<void*>*){{paramName}}, {{paramName}}Array, QString("body"), QString("SWGUser*"));
QJsonDocument doc(*{{paramName}}Array);
QByteArray bytes = doc.toJson();
@ -122,10 +117,10 @@
&{{classname}}::{{nickname}}Callback);
worker->execute(&input);
}
}
void
{{classname}}::{{nickname}}Callback(HttpRequestWorker * worker) {
void
{{classname}}::{{nickname}}Callback(HttpRequestWorker * worker) {
QString msg;
if (worker->error_type == QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length());
@ -171,8 +166,7 @@
{{/isMapContainer}}
{{^isMapContainer}}
{{^returnTypeIsPrimitive}}QString json(worker->response);
{{{returnType}}} output = static_cast<{{{returnType}}}>(create(json,
QString("{{{returnBaseType}}}")));
{{{returnType}}} output = static_cast<{{{returnType}}}>(create(json, QString("{{{returnBaseType}}}")));
{{/returnTypeIsPrimitive}}
{{/isMapContainer}}
{{/isListContainer}}{{/returnType}}
@ -181,7 +175,7 @@
{{#returnType}}emit {{nickname}}Signal(output);{{/returnType}}
{{^returnType}}emit {{nickname}}Signal();{{/returnType}}
}
{{/operation}}
{{/operations}}
} /* namespace Swagger */
}
{{/operation}}
{{/operations}}
} /* namespace Swagger */

View File

@ -6,15 +6,14 @@
{{#imports}}{{{import}}}
{{/imports}}
#include
<QObject>
#include <QObject>
namespace Swagger {
namespace Swagger {
class {{classname}}: public QObject {
class {{classname}}: public QObject {
Q_OBJECT
public:
public:
{{classname}}();
{{classname}}(QString host, QString basePath);
~{{classname}}();
@ -22,15 +21,14 @@
QString host;
QString basePath;
{{#operations}}{{#operation}}void {{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}
, {{/hasMore}}{{/allParams}});
{{#operations}}{{#operation}}void {{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{/operation}}{{/operations}}
private:
private:
{{#operations}}{{#operation}}void {{nickname}}Callback (HttpRequestWorker * worker);
{{/operation}}{{/operations}}
signals:
signals:
{{#operations}}{{#operation}}void {{nickname}}Signal({{#returnType}}{{{returnType}}} summary{{/returnType}});
{{/operation}}{{/operations}}
};
}
#endif
};
}
#endif

View File

@ -1,43 +1,32 @@
#include "SWGHelpers.h"
#include "SWGModelFactory.h"
#include "SWGObject.h"
#import
<QDebug>
#import
<QJsonArray>
#import
<QJsonValue>
#import <QDebug>
#import <QJsonArray>
#import <QJsonValue>
namespace Swagger {
namespace Swagger {
void
setValue(void* value, QJsonValue obj, QString type, QString complexType) {
void
setValue(void* value, QJsonValue obj, QString type, QString complexType) {
if(value == NULL) {
// can't set value with a null pointer
return;
}
if(QStringLiteral("bool").compare(type) == 0) {
bool * val = static_cast
<bool
*>(value);
bool * val = static_cast<bool*>(value);
*val = obj.toBool();
}
else if(QStringLiteral("qint32").compare(type) == 0) {
qint32 *val = static_cast
<qint32
*>(value);
qint32 *val = static_cast<qint32*>(value);
*val = obj.toInt();
}
else if(QStringLiteral("qint64").compare(type) == 0) {
qint64 *val = static_cast
<qint64
*>(value);
qint64 *val = static_cast<qint64*>(value);
*val = obj.toVariant().toLongLong();
}
else if (QStringLiteral("QString").compare(type) == 0) {
QString **val = static_cast
<QString
**>(value);
QString **val = static_cast<QString**>(value);
if(val != NULL) {
if(!obj.isNull()) {
@ -62,20 +51,14 @@
SWGObject * so = (SWGObject*)Swagger::create(type);
if(so != NULL) {
so->fromJsonObject(jsonObj);
SWGObject **val = static_cast
<SWGObject
**>(value);
SWGObject **val = static_cast<SWGObject**>(value);
delete *val;
*val = so;
}
}
else if(type.startsWith("QList") && QString("").compare(complexType) != 0 && obj.isArray()) {
// list of values
QList
<void
*>* output = new QList
<void
*>();
QList<void*>* output = new QList<void*>();
QJsonArray arr = obj.toArray();
foreach (const QJsonValue & jval, arr) {
if(complexType.startsWith("SWG")) {
@ -105,25 +88,19 @@
}
}
}
QList
<void
*> **val = static_cast
<QList
<void
*>**>(value);
QList<void*> **val = static_cast<QList<void*>**>(value);
delete *val;
*val = output;
}
}
}
void
toJsonValue(QString name, void* value, QJsonObject* output, QString type) {
void
toJsonValue(QString name, void* value, QJsonObject* output, QString type) {
if(value == NULL) {
return;
}
if(type.startsWith("SWG")) {
SWGObject *swgObject = reinterpret_cast
<SWGObject *>(value);
SWGObject *swgObject = reinterpret_cast<SWGObject *>(value);
if(swgObject != NULL) {
QJsonObject* o = (*swgObject).asJsonObject();
if(name != NULL) {
@ -139,63 +116,51 @@
}
}
else if(QStringLiteral("QString").compare(type) == 0) {
QString* str = static_cast
<QString
*>(value);
QString* str = static_cast<QString*>(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("qint32").compare(type) == 0) {
qint32* str = static_cast
<qint32
*>(value);
qint32* str = static_cast<qint32*>(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("qint64").compare(type) == 0) {
qint64* str = static_cast
<qint64
*>(value);
qint64* str = static_cast<qint64*>(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("bool").compare(type) == 0) {
bool* str = static_cast
<bool
*>(value);
bool* str = static_cast<bool*>(value);
output->insert(name, QJsonValue(*str));
}
}
}
void
toJsonArray(QList
<void
*>* value, QJsonArray* output, QString innerName, QString innerType) {
void
toJsonArray(QList<void*>* value, QJsonArray* output, QString innerName, QString innerType) {
foreach(void* obj, *value) {
QJsonObject element;
toJsonValue(NULL, obj, &element, innerType);
output->append(element);
}
}
}
QString
stringValue(QString* value) {
QString* str = static_cast
<QString
*>(value);
QString
stringValue(QString* value) {
QString* str = static_cast<QString*>(value);
return QString(*str);
}
}
QString
stringValue(qint32 value) {
QString
stringValue(qint32 value) {
return QString::number(value);
}
}
QString
stringValue(qint64 value) {
QString
stringValue(qint64 value) {
return QString::number(value);
}
}
QString
stringValue(bool value) {
QString
stringValue(bool value) {
return QString(value ? "true" : "false");
}
} /* namespace Swagger */
}
} /* namespace Swagger */

View File

@ -1,20 +1,17 @@
#ifndef SWGHELPERS_H
#define SWGHELPERS_H
#include
<QJsonValue>
#include <QJsonValue>
namespace Swagger {
namespace Swagger {
void setValue(void* value, QJsonValue obj, QString type, QString complexType);
void toJsonArray(QList
<void
*>* value, QJsonArray* output, QString innerName, QString innerType);
void toJsonArray(QList<void*>* value, QJsonArray* output, QString innerName, QString innerType);
void toJsonValue(QString name, void* value, QJsonObject* output, QString type);
bool isCompatibleJsonValue(QString type);
QString stringValue(QString* value);
QString stringValue(qint32 value);
QString stringValue(qint64 value);
QString stringValue(bool value);
}
}
#endif // SWGHELPERS_H
#endif // SWGHELPERS_H

View File

@ -1,41 +1,37 @@
{{#models}}{{#model}}
#include "{{classname}}.h"
#include "{{classname}}.h"
#include "SWGHelpers.h"
#include "SWGHelpers.h"
#include
<QJsonDocument>
#include
<QJsonArray>
#include
<QObject>
#include
<QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QObject>
#include <QDebug>
namespace Swagger {
namespace Swagger {
{{classname}}::{{classname}}(QString* json) {
{{classname}}::{{classname}}(QString* json) {
init();
this->fromJson(*json);
}
}
{{classname}}::{{classname}}() {
{{classname}}::{{classname}}() {
init();
}
}
{{classname}}::~{{classname}}() {
{{classname}}::~{{classname}}() {
this->cleanup();
}
}
void
{{classname}}::init() {
void
{{classname}}::init() {
{{#vars}}{{name}} = {{{defaultValue}}};
{{/vars}}
}
}
void
{{classname}}::cleanup() {
void
{{classname}}::cleanup() {
{{#vars}}{{#complexType}}if({{name}} != NULL) {
{{#isContainer}}QList<{{complexType}}*>* arr = {{name}};
foreach({{complexType}}* o, *arr) {
@ -44,35 +40,35 @@
{{/isContainer}}delete {{name}};
}{{/complexType}}
{{/vars}}
}
}
{{classname}}*
{{classname}}::fromJson(QString &json) {
{{classname}}*
{{classname}}::fromJson(QString &json) {
QByteArray array (json.toStdString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonObject jsonObject = doc.object();
this->fromJsonObject(jsonObject);
return this;
}
}
void
{{classname}}::fromJsonObject(QJsonObject &pJson) {
void
{{classname}}::fromJsonObject(QJsonObject &pJson) {
{{#vars}}setValue(&{{name}}, pJson["{{name}}"], "{{baseType}}", "{{complexType}}");
{{/vars}}
}
}
QString
{{classname}}::asJson ()
{
QString
{{classname}}::asJson ()
{
QJsonObject* obj = this->asJsonObject();
QJsonDocument doc(*obj);
QByteArray bytes = doc.toJson();
return QString(bytes);
}
}
QJsonObject*
{{classname}}::asJsonObject() {
QJsonObject*
{{classname}}::asJsonObject() {
QJsonObject* obj = new QJsonObject();
{{#vars}}{{#complexType}}
{{^isContainer}}{{#complexType}}
@ -84,8 +80,7 @@
{{/isContainer}}{{#isContainer}}
QList<{{complexType}}*>* {{name}}List = {{name}};
QJsonArray {{name}}JsonArray;
toJsonArray((QList
<void*>*){{name}}, &{{name}}JsonArray, "{{name}}", "{{complexType}}");
toJsonArray((QList<void*>*){{name}}, &{{name}}JsonArray, "{{name}}", "{{complexType}}");
obj->insert("{{name}}", {{name}}JsonArray);
{{/isContainer}}
@ -93,22 +88,22 @@
{{/vars}}
return obj;
}
}
{{#vars}}
{{{datatype}}}
{{classname}}::{{getter}}() {
{{#vars}}
{{{datatype}}}
{{classname}}::{{getter}}() {
return {{name}};
}
void
{{classname}}::{{setter}}({{{datatype}}} {{name}}) {
}
void
{{classname}}::{{setter}}({{{datatype}}} {{name}}) {
this->{{name}} = {{name}};
}
}
{{/vars}}
{{/vars}}
} /* namespace Swagger */
} /* namespace Swagger */
{{/model}}
{{/models}}

View File

@ -1,26 +1,25 @@
{{#models}}{{#model}}/*
* {{classname}}.h
*
* {{description}}
*/
* {{classname}}.h
*
* {{description}}
*/
#ifndef {{classname}}_H_
#define {{classname}}_H_
#include
<QJsonObject>
#include <QJsonObject>
{{/model}}{{/models}}
{{#imports}}{{{import}}}
{{/imports}}
#include "SWGObject.h"
#include "SWGObject.h"
{{#models}}{{#model}}
namespace Swagger {
namespace Swagger {
class {{classname}}: public SWGObject {
public:
class {{classname}}: public SWGObject {
public:
{{classname}}();
{{classname}}(QString* json);
virtual ~{{classname}}();
@ -36,13 +35,13 @@
void {{setter}}({{{datatype}}} {{name}});
{{/vars}}
private:
private:
{{#vars}}{{{datatype}}} {{name}};
{{/vars}}
};
};
} /* namespace Swagger */
} /* namespace Swagger */
#endif /* {{classname}}_H_ */
#endif /* {{classname}}_H_ */
{{/model}}
{{/models}}

View File

@ -1,34 +1,30 @@
{{#models}}{{#model}}/*
* {{classname}}.h
*
* {{description}}
*/
* {{classname}}.h
*
* {{description}}
*/
#ifndef {{classname}}_H_
#define {{classname}}_H_
#include
<FApp.h>
#include
<FBase.h>
#include
<FSystem.h>
#include
<FWebJson.h>
#include "{{prefix}}Helpers.h"
#include "{{prefix}}Object.h"
#include <FApp.h>
#include <FBase.h>
#include <FSystem.h>
#include <FWebJson.h>
#include "{{prefix}}Helpers.h"
#include "{{prefix}}Object.h"
using namespace Tizen::Web::Json;
using namespace Tizen::Web::Json;
{{/model}}{{/models}}
{{#imports}}{{{import}}}
{{/imports}}
{{#models}}{{#model}}
namespace Swagger {
namespace Swagger {
class {{classname}}: public {{prefix}}Object {
public:
class {{classname}}: public {{prefix}}Object {
public:
{{classname}}();
{{classname}}(String* json);
virtual ~{{classname}}();
@ -50,13 +46,13 @@
void {{setter}}({{datatype}} {{name}});
{{/vars}}
private:
private:
{{#vars}}{{datatype}} {{name}};
{{/vars}}
};
};
} /* namespace Swagger */
} /* namespace Swagger */
#endif /* {{classname}}_H_ */
#endif /* {{classname}}_H_ */
{{/model}}
{{/models}}

View File

@ -2,29 +2,28 @@
#define ModelFactory_H_
{{#models}}{{#model}}
#include "{{classname}}.h"{{/model}}{{/models}}
#include "{{classname}}.h"{{/model}}{{/models}}
namespace Swagger {
inline void* create(QString type) {
{{#models}}{{#model}}if(QString("{{classname}}").compare(type) == 0) {
return new {{classname}}();
}
{{/model}}{{/models}}
return NULL;
}
inline void* create(QString type) {
{{#models}}{{#model}}if(QString("{{classname}}").compare(type) == 0) {
return new {{classname}}();
}
{{/model}}{{/models}}
return NULL;
}
inline void* create(QString json, QString type) {
void* val = create(type);
if(val != NULL) {
SWGObject* obj = static_cast
<SWGObject*>(val);
return obj->fromJson(json);
}
if(type.startsWith("QString")) {
return new QString();
}
return NULL;
}
inline void* create(QString json, QString type) {
void* val = create(type);
if(val != NULL) {
SWGObject* obj = static_cast<SWGObject*>(val);
return obj->fromJson(json);
}
if(type.startsWith("QString")) {
return new QString();
}
return NULL;
}
} /* namespace Swagger */
#endif /* ModelFactory_H_ */

View File

@ -1,10 +1,9 @@
#ifndef _{{prefix}}_OBJECT_H_
#define _{{prefix}}_OBJECT_H_
#include
<QJsonValue>
#include <QJsonValue>
class {{prefix}}Object {
class {{prefix}}Object {
public:
virtual QJsonObject* asJsonObject() {
return NULL;
@ -20,6 +19,6 @@
virtual QString asJson() {
return QString("");
}
};
};
#endif /* _{{prefix}}_OBJECT_H_ */
#endif /* _{{prefix}}_OBJECT_H_ */