summaryrefslogtreecommitdiffstats
path: root/sigmod/Object.h
blob: e7e0b4d11d8e96a1e05b76b8130712c088eba8d6 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright 2007-2009 Ben Boeckel <MathStuf@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file sigmod/Object.h
 */

#ifndef SIGMOD_OBJECT
#define SIGMOD_OBJECT

// Sigcore includes
#include <sigcore/Fraction.h>
#include <sigcore/Matrix.h>
#include <sigcore/Script.h>

// Sigmod includes
#include "Global.h"

// Qt includes
#include <QtCore/QBuffer>
#include <QtCore/QObject>
#include <QtCore/QPoint>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtCore/QVarLengthArray>
#include <QtGui/QPainterPath>
#include <QtXml/QDomElement>

// C includes
#include <climits>

namespace Sigmod
{
// Forward decarations
class Game;

/**
 * \class Sigmod::Object Object.h sigmod/Object.h
 * \brief Base class for all data structures in a game.
 * 
 * The Object class sets up a parenting structure for all Sigmod classes.
 * It also provides an interface for validation, saving to XML format,
 * as well as loading from XML.
 */
class SIGMOD_EXPORT Object : public QObject
{
    Q_OBJECT
    
    public:
        /**
         * Constructor.
         * 
         * \param parent The parent of the object.
         * \param id The id of the object.
         */
        Object(const Object* parent, const int id);
        /**
         * Destructor.
         */
        virtual ~Object();
        
        /**
         * \return The parent of the object.
         */
        const Object* parent() const;
        /**
         * \return The game that the object belongs to.
         */
        const Game* game() const;
        
        /**
         * \return The id of the object.
         */
        int id() const;
        /**
         * \return The name of the class of the object without the namespace.
         */
        QString className() const;
        
        /**
         * Convenience function to create a QDomDocument from the QDomElement that is returned from \link Object::save save \endlink.
         * 
         * \param object The object to create an XML structure from.
         * \return The XML representation of \p object.
         */
        static QDomDocument xml(const Object* object);
        
        /**
         * Method for validating the object.
         */
        virtual void validate() = 0;
        
        /**
         * Loads data from an XML structure.
         * 
         * \param xml The XML data structure to load data from.
         */
        virtual void load(const QDomElement& xml) = 0;
        /**
         * Saves the data of the object to XML.
         * 
         * \return An XML representation of the class.
         */
        virtual QDomElement save() const = 0;
    signals:
        /**
         * Signal for when the object encounters something unexpected, but it does not invalid when validating.
         * 
         * \param message The message.
         */
        void valMessage(const QString& message) const;
        /**
         * Signal for when the object encounters a value that is either confusing or not advised, but does not
         * render the game as inconsistent.
         * 
         * \param message The warning.
         */
        void valWarning(const QString& message) const;
        /**
         * Signal for when the object encounters an unexpected value that introduces an inconsistency in the game.
         * 
         * \param message The error.
         */
        void valError(const QString& message) const;
        /**
         * Signal for when a value was set, but it may not be optimal.
         * 
         * \param message The warning.
         */
        void warning(const QString& message) const;
        /**
         * Signal for when a value assignment was attempted, but the value was invalid.
         * 
         * \param message The error.
         */
        void error(const QString& message) const;
        
        /**
         * Signal for when the object has changed.
         */
        void changed() const;
    protected:
        void setId(const int id);
        bool idCheck(const int id) const;
        
        static QString unused(const QString& variable);
        static QString bounds(const QString& variable, const int min, const int max, const int value);
        static QString bounds(const QString& variable, const int min, const int max, const Sigcore::Fraction& value);
        static QString bounds(const QString& variable, const QString& min, const QString& max, const int value);
        static QString bounds(const QString& variable, const int value);
        static QString bounds(const QString& variable, const QString& value);
        static QString subclass(const QString& subclass, const int id);
        static QString subclass(const QString& subclass, const QString& name);
        
        virtual void clear();
    private:
        int m_id;
        const Object* const m_parent;
};

template<typename T> inline void loadValue(const QDomElement& xml, T* value)
{
    *value = QVariant(xml.firstChild().toText().data()).value<T>();
}

template<> inline void loadValue<bool>(const QDomElement& xml, bool* value)
{
    *value = (xml.firstChild().toText().data() == "true");
}

template<> inline void loadValue<int>(const QDomElement& xml, int* value)
{
    *value = xml.firstChild().toText().data().toInt();
}

template<> inline void loadValue<Sigcore::Fraction>(const QDomElement& xml, Sigcore::Fraction* value)
{
    value->set(xml.attribute("numerator", "1").toInt(), xml.attribute("denominator", "1").toInt());
    value->reduce();
}

template<> inline void loadValue<QPoint>(const QDomElement& xml, QPoint* value)
{
    value->setX(xml.attribute("x", "0").toInt());
    value->setY(xml.attribute("y", "0").toInt());
}

template<> inline void loadValue<QPainterPath>(const QDomElement& xml, QPainterPath* value)
{
    QDataStream stream(QByteArray::fromBase64(xml.firstChild().toText().data().toUtf8()));
    stream >> *value;
}

template<> inline void loadValue<QByteArray>(const QDomElement& xml, QByteArray* value)
{
    *value = QByteArray::fromBase64(xml.firstChild().toText().data().toUtf8());
}

template<> inline void loadValue<Sigcore::Script>(const QDomElement& xml, Sigcore::Script* value)
{
    value->setInterpreter(xml.attribute("interpreter", ""));
    value->setScript(xml.firstChild().toText().data());
}

template<typename T> inline void loadEnum(const QDomElement& xml, T* value, const QStringList& array)
{
    *value = static_cast<T>(array.indexOf(xml.firstChild().toText().data()));
}

template<typename T, int S> inline void loadArray(const QDomElement& xml, QVarLengthArray<T, S>* value)
{
    QDomElement node = xml.firstChildElement("element");
    while (!node.isNull())
    {
        int index = node.attribute("index", "-1").toInt();
        if ((0 <= index) && (index < value->size()))
            loadValue(node, value->data() + index);
        node = node.nextSiblingElement("element");
    }
}

inline void loadList(const QDomElement& xml, QList<int>* value)
{
    QDomElement node = xml.firstChildElement("element");
    int element;
    while (!node.isNull())
    {
        loadValue(node, &element);
        if (!value->contains(element))
            value->append(element);
        node = node.nextSiblingElement("element");
    }
}

inline void loadMap(const QDomElement& xml, QMap<int, int>* value, const QString& valueName)
{
    QDomElement node = xml.firstChildElement("element");
    int key;
    int keyValue;
    while (!node.isNull())
    {
        loadValue(node.firstChildElement("key"), &key);
        loadValue(node.firstChildElement(valueName), &keyValue);
        (*value)[key] = keyValue;
        node = node.nextSiblingElement("element");
    }
}

template<typename T> inline void loadMatrix(const QDomElement& xml, Sigcore::Matrix<T>* value)
{
    int height = xml.attribute("height", "1").toInt();
    int width = xml.attribute("width", "1").toInt();
    value->resize(width, height);
    QDomElement node = xml.firstChildElement("element");
    while (!node.isNull())
    {
        int row = node.attribute("row", "-1").toInt();
        int column = node.attribute("column", "-1").toInt();
        if ((0 <= row) && (row < height) && (0 <= column) && (column < width))
            loadValue(node, &value->operator()(row, column));
        node = node.nextSiblingElement("element");
    }
}

template<typename T> inline QDomElement saveValue(const QString& name, const T& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.appendChild(QDomDocument().createTextNode(QVariant::fromValue<T>(value).toString()));
    return element;
}

template<> inline QDomElement saveValue<bool>(const QString& name, const bool& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.appendChild(QDomDocument().createTextNode(value ? "true" : "false"));
    return element;
}

template<> inline QDomElement saveValue<int>(const QString& name, const int& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.appendChild(QDomDocument().createTextNode(QString::number(value)));
    return element;
}

template<> inline QDomElement saveValue<QString>(const QString& name, const QString& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.appendChild(QDomDocument().createTextNode(value));
    return element;
}

template<> inline QDomElement saveValue<Sigcore::Fraction>(const QString& name, const Sigcore::Fraction& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.setAttribute("numerator", value.numerator());
    element.setAttribute("denominator", value.denominator());
    return element;
}

template<> inline QDomElement saveValue<QPoint>(const QString& name, const QPoint& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.setAttribute("x", value.x());
    element.setAttribute("y", value.y());
    return element;
}

template<> inline QDomElement saveValue<QPainterPath>(const QString& name, const QPainterPath& value)
{
    QByteArray array;
    QDataStream stream(&array, QIODevice::WriteOnly);
    stream << value;
    QDomElement element = QDomDocument().createElement(name);
    element.appendChild(QDomDocument().createTextNode(array.toBase64()));
    return element;
}

template<> inline QDomElement saveValue<QByteArray>(const QString& name, const QByteArray& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.appendChild(QDomDocument().createTextNode(value.toBase64()));
    return element;
}

template<> inline QDomElement saveValue<Sigcore::Script>(const QString& name, const Sigcore::Script& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.setAttribute("interpreter", value.interpreter());
    element.appendChild(QDomDocument().createCDATASection(value.script()));
    return element;
}

template<typename T> inline QDomElement saveEnum(const QString& name, const T& value, const QStringList& array)
{
    return saveValue<QString>(name, array[value]);
}

template<typename T, int S> inline QDomElement saveArray(const QString& name, const QVarLengthArray<T, S>& value)
{
    QDomElement element = QDomDocument().createElement(name);
    for (int i = 0; i < value.size(); ++i)
    {
        QDomElement valueElement = saveValue("element", value[i]);
        valueElement.setAttribute("index", QString::number(i));
        element.appendChild(valueElement);
    }
    return element;
}

inline QDomElement saveList(const QString& name, const QList<int>& value)
{
    QDomElement element = QDomDocument().createElement(name);
    for (int i = 0; i < value.size(); ++i)
        element.appendChild(saveValue("element", value.at(i)));
    return element;
}

inline QDomElement saveMap(const QString& name, const QMap<int, int>& value, const QString& valueName)
{
    QDomElement element = QDomDocument().createElement(name);
    const QList<int> keys = value.keys();
    foreach (int key, keys)
    {
        QDomElement data = QDomDocument().createElement("element");
        data.appendChild(saveValue("key", key));
        data.appendChild(saveValue(valueName, value[key]));
        element.appendChild(data);
    }
    return element;
}

template<typename T> inline QDomElement saveMatrix(const QString& name, const Sigcore::Matrix<T>& value)
{
    QDomElement element = QDomDocument().createElement(name);
    element.setAttribute("height", value.height());
    element.setAttribute("width", value.width());
    for (int i = 0; i < value.height(); ++i)
    {
        for (int j = 0; j < value.width(); ++j)
        {
            QDomElement valueElement = saveValue("element", value(i, j));
            valueElement.setAttribute("row", QString::number(i));
            valueElement.setAttribute("column", QString::number(j));
            element.appendChild(valueElement);
        }
    }
    return element;
}

}

#endif