summaryrefslogtreecommitdiffstats
path: root/pokemod/Object.h
blob: fc13dc6ecff43a90001b8f7cf30b51f2bca5b6ba (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
/*
 * Copyright 2007-2008 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/>.
 */

#ifndef __POKEMOD_OBJECT__
#define __POKEMOD_OBJECT__

// Qt includes
#include <QDomElement>

// C includes
#include <climits>

class Object
{
    public:
        inline Object(const Object& object) :
                m_id(object.id()),
                m_className(object.className()),
                m_parent(object.parent())
        {
        }
        inline Object(const QString& className, const Object* parent, const int id) :
                m_id(id),
                m_className(className),
                m_parent(parent)
        {
        }
        virtual ~Object()
        {
        }
        virtual void load(const QDomElement& xml, int id = INT_MAX) = 0;
        virtual QDomElement save() const = 0;
        
        inline const Object* parent() const
        {
            return m_parent;
        }
        
        inline const Object* pokemod() const
        {
            if (m_parent)
                return m_parent->pokemod();
            return this;
        }
        
        inline int id() const
        {
            return m_id;
        }
        inline bool isValid() const
        {
            if (isGood())
                return validate();
            return false;
        }
        inline bool isGood() const
        {
            return ((0 <= m_id) && (INT_MAX != m_id));
        }
        
        inline QString className() const
        {
            return m_className;
        }
    protected:
        inline void setId(const int id)
        {
            m_id = id;
        }
        virtual bool validate() const = 0;
        template<class T> inline void warning(const QString& message) const throw(T)
        {
            throw(T(m_className, message));
        }
        template<class T> inline void error(const QString& message) throw(T)
        {
            m_valid = false;
            warning<T>(message);
        }
        virtual void clear() = 0;
    private:
        int m_id;
        const QString m_className;
        const Object* m_parent;
        bool m_valid;
};

#define LOAD_NODE(variable) xml.firstChildElement(variable)
#define LOAD_DATA(node) node.firstChild().toText().data()
#define LOAD_ID() \
    if (id == INT_MAX) \
        id = xml.attribute("id", "2147483647").toInt(); \
    setId(id); \
    clear()
#define LOAD(type, variable) m_##variable = LOAD_##type(LOAD_NODE(#variable))
#define LOAD_bool(node) ((LOAD_DATA(node) == "true") ? true : false)
#define LOAD_int(node) LOAD_DATA(node).toInt()
#define LOAD_QString LOAD_DATA
#define LOAD_Fraction(node) Fraction(node.attribute("numerator", "1").toInt(), node.attribute("denominator", "1").toInt())
#define LOAD_Flag(node) Flag(node.attribute("flag", "0").toInt(), node.attribute("status", "1").toInt())
#define LOAD_Point(node) Point(node.attribute("x", "0").toInt(), node.attribute("y", "0").toInt())
// FIXME: QPixmap::fromData static member would be nice
#define LOAD_QPixmap(node) QPixmap::fromImage(QImage::fromData(QByteArray::fromBase64(LOAD_DATA(node).toUtf8())))
#define LOAD_Rules(node) Rules(node, this)
#define LOAD_ARRAY(type, variable, size) \
    QDomElement xml_##variable = LOAD_NODE(#variable).firstChildElement("element"); \
    while (!xml_##variable.isNull()) \
    { \
        int index = xml_##variable.attribute("index", "-1").toInt(); \
        if (0 <= index) \
            m_##variable[index] = LOAD_##type(xml_##variable); \
        xml_##variable = xml_##variable.nextSiblingElement("element"); \
    }
#define LOAD_LIST(type, variable) \
    QDomElement xml_##variable = LOAD_NODE(#variable); \
    xml_##variable = LOAD_NODE(#variable).firstChildElement("element"); \
    while (!xml_##variable.isNull()) \
    { \
        int element = LOAD_int(xml_##variable); \
        if (!m_##variable.contains(element)) \
            m_##variable.append(element); \
        xml_##variable = xml_##variable.nextSiblingElement("element"); \
    }
#define LOAD_MATRIX(setter, type, variable) \
    QDomElement xml_##variable = LOAD_NODE(#variable); \
    int height_##variable = xml_##variable.attribute("height", "1").toInt(); \
    int width_##variable = xml_##variable.attribute("width", "1").toInt(); \
    m_##variable.resize(width_##variable, height_##variable); \
    xml_##variable = LOAD_NODE(#variable).firstChildElement("element"); \
    while (!xml_##variable.isNull()) \
    { \
        int row = xml_##variable.attribute("row", "-1").toInt(); \
        int column = xml_##variable.attribute("column", "-1").toInt(); \
        if ((0 <= row) && (0 <= column)) \
            setter(row, column, LOAD_##type(xml_##variable)); \
        xml_##variable = xml_##variable.nextSiblingElement("element"); \
    }
#define LOAD_SUB(setter, class) \
    QDomElement xml_##class = xml.firstChildElement(#class); \
    while (!xml_##class.isNull()) \
    { \
        setter(xml_##class); \
        xml_##class = xml_##class.nextSiblingElement(#class); \
    }

#define SAVE_CREATE() \
    QDomElement xml = QDomDocument().createElement(className()); \
    xml.setAttribute("id", id())
#define SAVE_VALUE(element, value) element.appendChild(QDomDocument().createTextNode(value))
#define SAVE(type, variable) \
    QDomElement xml_##variable = QDomDocument().createElement(#variable); \
    SAVE_##type(variable, variable()); \
    xml.appendChild(xml_##variable)
#define SAVE_bool(variable, value) SAVE_VALUE(xml_##variable, value ? "true" : "false")
#define SAVE_int(variable, value) SAVE_VALUE(xml_##variable, QString::number(value))
#define SAVE_QString(variable, value) SAVE_VALUE(xml_##variable, value)
#define SAVE_Fraction(variable, value) \
    Fraction frac_##variable = value; \
    xml_##variable.setAttribute("numerator", frac_##variable.numerator()); \
    xml_##variable.setAttribute("denominator", frac_##variable.denominator())
#define SAVE_Flag(variable, value) \
    Flag flag_##variable = value; \
    xml_##variable.setAttribute("flag", flag_##variable.flag()); \
    xml_##variable.setAttribute("status", flag_##variable.status())
#define SAVE_Point(variable, value) \
    Point point_##variable = value; \
    xml_##variable.setAttribute("x", point_##variable.x()); \
    xml_##variable.setAttribute("y", point_##variable.y())
#define SAVE_QPixmap(variable, value) \
    QByteArray bytes_##variable; \
    QBuffer buffer_##variable(&bytes_##variable); \
    buffer_##variable.open(QIODevice::WriteOnly); \
    value.save(&buffer_##variable, "PNG"); \
    SAVE_VALUE(xml_##variable, bytes_##variable.toBase64())
#define SAVE_ARRAY(type, variable, size) \
    QDomElement xml_array_##variable = QDomDocument().createElement(#variable); \
    for (int i = 0; i < size; ++i) \
    { \
        QDomElement xml_##variable = QDomDocument().createElement("element"); \
        xml_##variable.setAttribute("index", QString::number(i)); \
        SAVE_##type(variable, variable(i)); \
        xml_array_##variable.appendChild(xml_##variable); \
    } \
    xml.appendChild(xml_array_##variable)
#define SAVE_LIST(type, variable) \
    QDomElement xml_list_##variable = QDomDocument().createElement(#variable); \
    xml_list_##variable.setAttribute("size", m_##variable.size()); \
    for (int i = 0; i < m_##variable.size(); ++i) \
    { \
        QDomElement xml_##variable = QDomDocument().createElement("element"); \
        SAVE_##type(variable, m_##variable[i]); \
        xml_list_##variable.appendChild(xml_##variable); \
    } \
    xml.appendChild(xml_list_##variable)
#define SAVE_MATRIX(type, variable) \
    QDomElement xml_matrix_##variable = QDomDocument().createElement(#variable); \
    xml_matrix_##variable.setAttribute("height", m_##variable.height()); \
    xml_matrix_##variable.setAttribute("width", m_##variable.width()); \
    for (int i = 0; i < m_##variable.height(); ++i) \
    { \
        for (int j = 0; j < m_##variable.width(); ++j) \
        { \
            QDomElement xml_##variable = QDomDocument().createElement("element"); \
            xml_##variable.setAttribute("row", QString::number(i)); \
            xml_##variable.setAttribute("column", QString::number(j)); \
            SAVE_##type(variable, variable(i, j)); \
            xml_matrix_##variable.appendChild(xml_##variable); \
        } \
    } \
    xml.appendChild(xml_matrix_##variable)
#define SAVE_SUB(class, variable) \
    foreach (class* sub, m_##variable) \
        xml.appendChild(sub->save())

#define COPY(variable) m_##variable = rhs.m_##variable
#define COPY_ARRAY(variable, size) \
    for (int i = 0; i < size; ++i) \
        COPY(variable[i])
#define COPY_SUB(class, variable) \
    foreach (class* subclass, rhs.m_##variable) \
        m_##variable.append(new class(*subclass, this, subclass->id()))

#endif