All Downloads are FREE. Search and download functionalities are using the official Maven repository.

qt5cpp.model-body.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
{{>licenseInfo}}
{{#models}}{{#model}}
#include "{{classname}}.h"

#include "SWGHelpers.h"

#include 
#include 
#include 
#include 

{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}

{{classname}}::{{classname}}(QString* json) {
    init();
    this->fromJson(*json);
}

{{classname}}::{{classname}}() {
    init();
}

{{classname}}::~{{classname}}() {
    this->cleanup();
}

void
{{classname}}::init() {
    {{#vars}}
    {{name}} = {{{defaultValue}}};
    {{/vars}}
}

void
{{classname}}::cleanup() {
    {{#vars}}
    {{#complexType}}
    if({{name}} != nullptr) {
        {{#isContainer}}{{#isListContainer}}QList<{{complexType}}*>* arr = {{name}};{{/isListContainer}}{{#isMapContainer}}QMap* arr = {{name}};{{/isMapContainer}}
        foreach({{complexType}}* o, *arr) {
            delete o;
        }
        {{/isContainer}}delete {{name}};
    }{{/complexType}}
    {{/vars}}
}

{{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) {
    {{#vars}}
    {{^isContainer}}
    ::{{cppNamespace}}::setValue(&{{name}}, pJson["{{baseName}}"], "{{baseType}}", "{{complexType}}");
    {{/isContainer}}
    {{#isListContainer}}
    {{#complexType}}
    ::{{cppNamespace}}::setValue(&{{name}}, pJson["{{baseName}}"], "{{baseType}}", "{{complexType}}");
    {{/complexType}}
    {{^complexType}}
    ::{{cppNamespace}}::setValue(&{{name}}, pJson["{{baseName}}"], "{{baseType}}", "{{items.baseType}}");
    {{/complexType}}
    {{/isListContainer}}        
    {{#isMapContainer}}    
    if( pJson["{{baseName}}"].isObject()){
        auto varmap = pJson["{{baseName}}"].toObject().toVariantMap();
        if(varmap.count() > 0){
            for(auto val : varmap.keys() ){
                {                    
    		    {{items.baseType}} *{{name}}_item = new {{items.baseType}}();
    		    auto  jsonval = QJsonValue::fromVariant(varmap[val]);
    		    ::{{cppNamespace}}::setValue(&{{name}}_item, jsonval, "{{items.baseType}}", "{{items.baseType}}");        			                                                                       
                    {{name}}->insert({{name}}->end(), val, {{name}}_item);
                }
            }
        }
    }
           
    {{/isMapContainer}}        
    {{/vars}}
}

QString
{{classname}}::asJson ()
{
    QJsonObject* obj = this->asJsonObject();
    
    QJsonDocument doc(*obj);
    QByteArray bytes = doc.toJson();
    return QString(bytes);
}

QJsonObject*
{{classname}}::asJsonObject() {
    QJsonObject* obj = new QJsonObject();
    {{#vars}}
    {{#complexType}}
    {{^isContainer}}
    {{#complexType}}
    toJsonValue(QString("{{baseName}}"), {{name}}, obj, QString("{{complexType}}"));
    {{/complexType}}
    {{^complexType}}
    if({{name}} != nullptr && *{{name}} != nullptr) { 
        obj->insert("{{name}}", QJsonValue(*{{name}}));
    }
    {{/complexType}}
    {{/isContainer}}
    {{#isListContainer}}
    QJsonArray {{name}}JsonArray;
    toJsonArray((QList*){{name}}, &{{name}}JsonArray, "{{name}}", "{{complexType}}");
    obj->insert("{{baseName}}", {{name}}JsonArray);
    {{/isListContainer}}
    {{#isMapContainer}}
    QJsonArray {{name}}JsonArray;
    for(auto keyval : {{name}}->keys()){
	    QJsonObject {{name}}_jobj;
	    toJsonValue(keyval, ((*{{name}})[keyval]), &{{name}}_jobj, "{{complexType}}");
	    {{name}}JsonArray.append({{name}}_jobj);
	}   
    obj->insert("{{baseName}}", {{name}}JsonArray);
    {{/isMapContainer}}
    {{/complexType}}
    {{^complexType}}
    {{^isContainer}}
    obj->insert("{{baseName}}", QJsonValue({{name}}));
    {{/isContainer}}
    {{#isListContainer}}
    QJsonArray {{name}}JsonArray;
    toJsonArray((QList*){{name}}, &{{name}}JsonArray, "{{name}}", "{{items.baseType}}");
    obj->insert("{{baseName}}", {{name}}JsonArray);
    {{/isListContainer}}
    {{#isMapContainer}}
    QJsonArray {{name}}JsonArray;
    for(auto keyval : {{name}}->keys()){
	    QJsonObject {{name}}_jobj;
	    toJsonValue(keyval, ((*{{name}})[keyval]), &{{name}}_jobj, "{{items.baseType}}");
	    {{name}}JsonArray.append(portsobj);
	}     
    obj->insert("{{baseName}}", {{name}}JsonArray);
    {{/isMapContainer}}
    {{/complexType}}
    {{/vars}}

    return obj;
}

{{#vars}}
{{{datatype}}}
{{classname}}::{{getter}}() {
    return {{name}};
}
void
{{classname}}::{{setter}}({{{datatype}}} {{name}}) {
    this->{{name}} = {{name}};
}

{{/vars}}

{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}

{{/model}}
{{/models}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy