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

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

There is a newer version: 3.0.0-rc1
Show newest version
{{>licenseInfo}}
#include "{{prefix}}Helpers.h"
#include "{{prefix}}ModelFactory.h"
#include "{{prefix}}Object.h"
#include 
#include 
#include 
#include 


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

void
setValue(void* value, QJsonValue obj, QString type, QString complexType) {
    if((value == nullptr) || (obj.isUndefined())) {
        // can't set value with a null pointer
        return;
    }
    if(QStringLiteral("bool").compare(type) == 0) {
        bool * val = static_cast(value);
        *val = obj.toBool();
    }
    else if(QStringLiteral("qint32").compare(type) == 0) {
        qint32 *val = static_cast(value);
        *val = obj.toInt();
    }
    else if(QStringLiteral("qint64").compare(type) == 0) {
        qint64 *val = static_cast(value);
        *val = obj.toVariant().toLongLong();
    }
    else if(QStringLiteral("float").compare(type) == 0) {
        float *val = static_cast(value);
        *val = static_cast(obj.toDouble());
    }
    else if(QStringLiteral("double").compare(type) == 0) {
        double *val = static_cast(value);
        *val = obj.toDouble();
    }
    else if (QStringLiteral("QString").compare(type) == 0) {
        QString **val = static_cast(value);
        if(val != nullptr) {
            if(!obj.isNull()) {
                (*val)->clear();
                (*val)->append(obj.toString());
                return;
            }
            else {
                (*val)->clear();
            }
        }
    }
    else if (QStringLiteral("QDateTime").compare(type) == 0) {
        QDateTime **val = static_cast(value);

        if(val != nullptr) {
            if(!obj.isNull()) {
                // create a new value and return
                if(*val != nullptr) delete *val;
                *val = new QDateTime(QDateTime::fromString(obj.toString(), Qt::ISODate));
                return;
            }
            else {
                // set target to nullptr
                if(*val != nullptr) delete *val;
                *val = nullptr;
            }
        }
        else {
            qDebug() << "Can't set value because the target pointer is nullptr";
        }
    }
    else if (QStringLiteral("QDate").compare(type) == 0) {
        QDate **val = static_cast(value);

        if(val != nullptr) {
            if(!obj.isNull()) {
                // create a new value and return
                if(*val != nullptr) delete *val;
                *val = new QDate(QDate::fromString(obj.toString(), Qt::ISODate));
                return;
            }
            else {
                // set target to nullptr
                if(*val != nullptr) delete *val;
                *val = nullptr;
            }
        }
        else {
            qDebug() << "Can't set value because the target pointer is nullptr";
        }
    }
    else if (QStringLiteral("QByteArray").compare(type) == 0) {
        QByteArray **val = static_cast(value);

        if(val != nullptr) {
            if(!obj.isNull()) {
                // create a new value and return
                if(*val != nullptr) delete *val;

                *val = new QByteArray(QByteArray::fromBase64(QByteArray::fromStdString(obj.toString().toStdString())));
                return;
            }
            else {
                // set target to nullptr
                if(*val != nullptr) delete *val;
                *val = nullptr;
            }
        }
        else {
            qDebug() << "Can't set value because the target pointer is nullptr";
        }
    }
    else if(type.startsWith("{{prefix}}") && obj.isObject()) {
        // complex type
        QJsonObject jsonObj = obj.toObject();
        {{prefix}}Object * so = ({{prefix}}Object*)::{{cppNamespace}}::create(complexType);
        if(so != nullptr) {
            so->fromJsonObject(jsonObj);
            {{prefix}}Object **val = static_cast<{{prefix}}Object**>(value);
            if(*val != nullptr) delete *val;
            *val = so;
        }
    }
    else if(type.startsWith("QList") && QString("").compare(complexType) != 0 && obj.isArray()) {
        // list of values
        if(complexType.startsWith("{{prefix}}")) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr) {
                // it's an object
                {{prefix}}Object * val = ({{prefix}}Object*)::{{cppNamespace}}::create(complexType);
                QJsonObject t = jval.toObject();
                val->fromJsonObject(t);
                (*output)->append(val);
            }
        }
        else if(QStringLiteral("qint32").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                qint32 val;
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("qint32"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("qint64").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                qint64 val;
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("qint64"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("bool").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                bool val;
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("bool"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("float").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                float val;
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("float"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("double").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                double val;
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("double"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("QString").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                QString * val = new QString();
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("QString"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("QDate").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                QDate * val = new QDate();
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("QDate"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
        else if(QStringLiteral("QDateTime").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            QJsonArray arr = obj.toArray();
            for (const QJsonValue & jval : arr){
                QDateTime * val = new QDateTime();
                ::{{cppNamespace}}::setValue(&val, jval, QStringLiteral("QDateTime"), QStringLiteral(""));
                (*output)->push_back(val);
            }
        }
    }
    else if(type.startsWith("QMap") && QString("").compare(complexType) != 0 && obj.isObject()) {
        // list of values
        if(complexType.startsWith("{{prefix}}")) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    auto  val = ({{prefix}}Object*)::{{cppNamespace}}::create(complexType);
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, complexType, complexType);
                    (*output)->insert(itemkey, val);
                }
            }
        }
        else if(QStringLiteral("qint32").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    qint32 val;
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("qint32"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
        else if(QStringLiteral("qint64").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    qint64 val;
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("qint64"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
        else if(QStringLiteral("bool").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    bool val;
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("bool"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
        else if(QStringLiteral("float").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    float val;
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("float"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
        else if(QStringLiteral("double").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    double val;
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("double"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
        else if(QStringLiteral("QString").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    QString * val = new QString();
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("QString"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
        else if(QStringLiteral("QDate").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    QDate * val = new QDate();
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("QDate"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
         else if(QStringLiteral("QDateTime").compare(complexType) == 0) {
            auto output = reinterpret_cast **> (value);
            for (auto item : **output) {
                if(item != nullptr) delete item;
            }
            (*output)->clear();
            auto varmap = obj.toObject().toVariantMap();
            if(varmap.count() > 0){
                for(auto itemkey : varmap.keys() ){
                    QDateTime * val = new QDateTime();
                    auto  jsonval = QJsonValue::fromVariant(varmap.value(itemkey));
                    ::{{cppNamespace}}::setValue(&val, jsonval, QStringLiteral("QDateTime"), QStringLiteral(""));
                    (*output)->insert( itemkey, val);
                }
            }
        }
    }
}

void
toJsonValue(QString name, void* value, QJsonObject& output, QString type) {
    if(value == nullptr) {
        return;
    }
    if(type.startsWith("{{prefix}}")) {
        {{prefix}}Object *{{prefix}}object = reinterpret_cast<{{prefix}}Object *>(value);
        if({{prefix}}object != nullptr) {
            QJsonObject o = {{prefix}}object->asJsonObject();
            if(!name.isNull()) {
                output.insert(name, o);
            }
            else {
                output.empty();
                for(QString key : o.keys()) {
                    output.insert(key, o.value(key));
                }
            }
        }
    }
    else if(QStringLiteral("QString").compare(type) == 0) {
        QString* str = static_cast(value);
        output.insert(name, QJsonValue(*str));
    }
    else if(QStringLiteral("qint32").compare(type) == 0) {
        qint32* str = static_cast(value);
        output.insert(name, QJsonValue(*str));
    }
    else if(QStringLiteral("qint64").compare(type) == 0) {
        qint64* str = static_cast(value);
        output.insert(name, QJsonValue(*str));
    }
    else if(QStringLiteral("bool").compare(type) == 0) {
        bool* str = static_cast(value);
        output.insert(name, QJsonValue(*str));
    }
    else if(QStringLiteral("float").compare(type) == 0) {
        float* str = static_cast(value);
        output.insert(name, QJsonValue((double)*str));
    }
    else if(QStringLiteral("double").compare(type) == 0) {
        double* str = static_cast(value);
        output.insert(name, QJsonValue(*str));
    }
    else if(QStringLiteral("QDate").compare(type) == 0) {
        QDate* date = static_cast(value);
        output.insert(name, QJsonValue(date->toString(Qt::ISODate)));
    }
    else if(QStringLiteral("QDateTime").compare(type) == 0) {
        QDateTime* datetime = static_cast(value);
        output.insert(name, QJsonValue(datetime->toString(Qt::ISODate)));
    }
    else if(QStringLiteral("QByteArray").compare(type) == 0) {
        QByteArray* byteArray = static_cast(value);
        output.insert(name, QJsonValue(QString(byteArray->toBase64())));
    }
}

void
toJsonArray(QList* value, QJsonObject& output, QString innerName, QString innerType) {
    if(value == nullptr) {
        return;
    }
    QJsonArray outputarray;
    if(innerType.startsWith("{{prefix}}")){
        for(void* obj : *value) {
            {{prefix}}Object *{{prefix}}object = reinterpret_cast<{{prefix}}Object *>(obj);
            if({{prefix}}object != nullptr) {
                outputarray.append({{prefix}}object->asJsonObject());
            }
        }
    }
    else if(QStringLiteral("QString").compare(innerType) == 0) {
        for(QString* obj : *(reinterpret_cast*>(value))){
            outputarray.append(QJsonValue(*obj));
        }
    }
    else if(QStringLiteral("QDate").compare(innerType) == 0) {
        for(QDate* obj : *(reinterpret_cast*>(value))){
            outputarray.append(QJsonValue(obj->toString(Qt::ISODate)));
        }
    }
    else if(QStringLiteral("QDateTime").compare(innerType) == 0) {
        for(QDateTime* obj : *(reinterpret_cast*>(value))){
            outputarray.append(QJsonValue(obj->toString(Qt::ISODate)));        }
    }
    else if(QStringLiteral("QByteArray").compare(innerType) == 0) {
        for(QByteArray* obj : *(reinterpret_cast*>(value))){
            outputarray.append(QJsonValue(QString(obj->toBase64())));
        }
    }
    else if(QStringLiteral("qint32").compare(innerType) == 0) {
        for(qint32 obj : *(reinterpret_cast*>(value)))
            outputarray.append(QJsonValue(obj));
    }
    else if(QStringLiteral("qint64").compare(innerType) == 0) {
        for(qint64 obj : *(reinterpret_cast*>(value)))
            outputarray.append(QJsonValue(obj));
    }
    else if(QStringLiteral("bool").compare(innerType) == 0) {
        for(bool obj : *(reinterpret_cast*>(value)))
            outputarray.append(QJsonValue(obj));
    }
    else if(QStringLiteral("float").compare(innerType) == 0) {
        for(float obj : *(reinterpret_cast*>(value)))
            outputarray.append(QJsonValue(obj));
    }
    else if(QStringLiteral("double").compare(innerType) == 0) {
        for(double obj : *(reinterpret_cast*>(value)))
            outputarray.append(QJsonValue(obj));
    }
    output.insert(innerName, outputarray);
}

void
toJsonMap(QMap* value, QJsonObject& output, QString innerName, QString innerType) {
    if(value == nullptr)  {
        return;
    }
    QJsonObject mapobj;
    if(innerType.startsWith("{{prefix}}")){
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            ::{{cppNamespace}}::toJsonValue(itemkey, items->value(itemkey),mapobj, innerType);
        }
    }
    else if(QStringLiteral("QString").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            ::{{cppNamespace}}::toJsonValue(itemkey, items->value(itemkey), mapobj, innerType);
        }
    }
    else if(QStringLiteral("QDate").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            ::{{cppNamespace}}::toJsonValue(itemkey, items->value(itemkey), mapobj, innerType);
        }
    }
    else if(QStringLiteral("QDateTime").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            ::{{cppNamespace}}::toJsonValue(itemkey, items->value(itemkey), mapobj, innerType);
        }
    }
    else if(QStringLiteral("QByteArray").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            ::{{cppNamespace}}::toJsonValue(itemkey, items->value(itemkey), mapobj, innerType);
        }
    }
    else if(QStringLiteral("qint32").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            auto val = items->value(itemkey);
            ::{{cppNamespace}}::toJsonValue(itemkey, &val, mapobj, innerType);
        }
    }
    else if(QStringLiteral("qint64").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            auto val = items->value(itemkey);
            ::{{cppNamespace}}::toJsonValue(itemkey, &val, mapobj, innerType);
        }
    }
    else if(QStringLiteral("bool").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            auto val = items->value(itemkey);
            ::{{cppNamespace}}::toJsonValue(itemkey, &val, mapobj, innerType);
        }
    }
    else if(QStringLiteral("float").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys()) {
            auto val = items->value(itemkey);
            ::{{cppNamespace}}::toJsonValue(itemkey, &val, mapobj, innerType);
        }
    }
    else if(QStringLiteral("double").compare(innerType) == 0) {
        auto items = reinterpret_cast< QMap *>(value);
        for(auto itemkey: items->keys() ) {
            auto val = items->value(itemkey);
            ::{{cppNamespace}}::toJsonValue(itemkey, &val, mapobj, innerType);
        }
    }
    output.insert(innerName, mapobj);
}

QString
stringValue(QString* value) {
    QString* str = static_cast(value);
    return QString(*str);
}

QString
stringValue(qint32 value) {
    return QString::number(value);
}

QString
stringValue(qint64 value) {
    return QString::number(value);
}

QString
stringValue(bool value) {
    return QString(value ? "true" : "false");
}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy