summaryrefslogtreecommitdiff
path: root/g_object.cpp
blob: 09cbc27116648fc0d30d8d9ea68793908d96a498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "g_object.h"

// qt
#include <QDataStream>
#include <QLineF>
#include <QPointF>

G_Object::G_Object(QObject* parent)
    : QObject(parent)
{
}

G_Object::G_Object(const QJsonObject& json, QObject* parent)
    : QObject(parent)
{
    // use G_Object static offset, not virtual
    constructFromJson(json);
}

G_Object::G_Object(const QJsonValue& json, QObject* parent)
    : QObject(parent)
{
    constructFromJson(json.toObject());
}

G_Object::G_Object(const G_Object& other, QObject* parent)
    : QObject(parent)
{
    // NOTE: always G_Object's assignment operator will be called, not derived
    *this = other;
}

G_Object& G_Object::operator=(const G_Object& other)
{
    // use G_Object static offset, not virtual
    for (int i = staticMetaObject.propertyOffset();
         i < metaObject()->propertyCount();
         i++)
    {
        auto key = metaObject()->property(i).name();
        auto value = metaObject()->property(i).read(this);
        if (value != other.property(key))
        {
            setProperty(key, other.property(key));
        }
    }

    return *this;
}

G_Object& G_Object::operator=(const QJsonObject& json)
{
    constructFromJson(json);

    return *this;
}

G_Object& G_Object::operator=(const QJsonValue& json)
{
    constructFromJson(json.toObject());

    return *this;
}

G_Object::operator const QJsonValue() const
{
    return this->operator const QJsonObject();
}

bool G_Object::operator==(const G_Object& other) const
{
    for (int i = staticMetaObject.propertyOffset();
         i < metaObject()->propertyCount();
         i++)
    {
        auto key = metaObject()->property(i).name();

        if (property(key) != other.property(key))
        {
            return false;
        }
    }

    return true;
}

bool G_Object::constructFromJson(const QJsonObject& json)
{
    auto offset = staticMetaObject.propertyOffset();
    auto properiesCount = metaObject()->propertyCount();

    for (int i = offset; i < properiesCount; i++)
    {
        auto key = metaObject()->property(i).name();
        auto type = metaObject()->property(i).userType();
        auto jsonValue = json[key];
        auto variantValue = jsonValue.toVariant();

        if (variantValue.isNull())
        {
            if (type == QMetaType::Double)
            {
                setProperty(key, std::numeric_limits<double>::quiet_NaN());
            }
            else if (type == QMetaType::Float)
            {
                setProperty(key, std::numeric_limits<float>::quiet_NaN());
            }
            else
            {
                return false;
            }

            continue;
        }

        auto converted = variantValue.convert(type);

        if (!converted)
        {
            qDebug() << Q_FUNC_INFO << "cannot convert" << key
                     << "property from type"
                     << QMetaType::typeName(variantValue.userType())
                     << "to type" << QMetaType::typeName(type);

            return false;
        }

        setProperty(key, variantValue);
    }

    return true;
}

G_Object::operator const QJsonObject() const
{
    QJsonObject result;

    // use G_Object static offset, not virtual
    for (int i = staticMetaObject.propertyOffset();
         i < metaObject()->propertyCount();
         i++)
    {
        auto key = metaObject()->property(i).name();
        auto type = metaObject()->property(i).userType();
        auto value = metaObject()->property(i).read(this);

        if (value.isNull())
        {
            continue;
        }

        auto converted = value.convert(type);

        if (!converted)
        {
            qDebug() << "G_Object::operator const QJsonObject() const:"
                     << "cannot convert" << key << "property from type"
                     << QMetaType::typeName(value.userType()) << "to type"
                     << QMetaType::typeName(type);
        }

        result[key] = QJsonValue::fromVariant(value);
    }

    return result;
}

QDebug operator<<(QDebug d, const G_Object& o)
{
    d << o.metaObject()->className() << ":\n";

    for (int i = 0; i < o.metaObject()->propertyCount(); i++)
    {
        d << "\t" << o.metaObject()->property(i).name() << ":\t"
          << o.metaObject()->property(i).read(&o) << Qt::endl;
    }

    return d;
}