summaryrefslogtreecommitdiffstats
path: root/sigmod/Macros.h
blob: 6d61526af2fb57eecff5a5667b4693f8503dbcab (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
/*
 * Copyright 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 SIGMOD_MACROS
#define SIGMOD_MACROS

#ifdef MAKE_SIGMOD_LIB

// TODO: The macros should probably be replaced by protected members of Object

#define LOAD_ID() \
    int newId = id; \
    if ((id == -1) && xml.hasAttribute("id")) \
        newId = xml.attribute("id", "-1").toInt(); \
    setId(newId)
#define LOAD_BEGIN() clear()
#define LOAD(variable) loadValue(xml.firstChildElement(#variable), &m_##variable)
#define LOAD_ARRAY(variable) loadArray(xml.firstChildElement(#variable), &m_##variable)
#define LOAD_LIST(variable) loadList(xml.firstChildElement(#variable), &m_##variable)
#define LOAD_MATRIX(variable) loadMatrix(xml.firstChildElement(#variable), &m_##variable)
#define LOAD_Rules() m_rules->load(xml.firstChildElement("Rules"))
#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(variable) xml.appendChild(saveValue(#variable, m_##variable))
#define SAVE_ARRAY(variable) xml.appendChild(saveArray(#variable, m_##variable))
#define SAVE_LIST(variable) xml.appendChild(saveList(#variable, m_##variable))
#define SAVE_MATRIX(variable) xml.appendChild(saveMatrix(#variable, m_##variable))
#define SAVE_Rules(variable) xml.appendChild(m_##variable->save())
#define SAVE_SUB(class, variable) \
    foreach (class* sub, m_##variable) \
        xml.appendChild(sub->save())

#define COPY(variable) m_##variable = rhs.m_##variable
#define COPY_Rules(variable) *m_##variable = *rhs.m_##variable
#define COPY_SUB(class, variable) \
    foreach (class* subclass, rhs.m_##variable) \
        m_##variable.append(new class(*subclass, this, subclass->id()))

#define TEST_BEGIN() \
    connect(this, SIGNAL(warning(QString)), SIGNAL(valWarning(QString))); \
    connect(this, SIGNAL(error(QString)), SIGNAL(valError(QString))); \
    emit(valMessage(QString("++%1 (%2)").arg(className()).arg(id())))
#define TEST_END() \
    emit(valMessage(QString("--%1 (%2)").arg(className()).arg(id()))); \
    disconnect(this, SIGNAL(warning(QString)), this, SIGNAL(valWarning(QString))); \
    disconnect(this, SIGNAL(error(QString)), this, SIGNAL(valError(QString)))
#define TEST(setter, variable) setter(m_##variable)
#define TEST_ARRAY(setter, variable, size) \
    for (int i = 0; i < size; ++i) \
        setter(m_##variable[i])
#define TEST_LIST(setter, variable) \
    foreach (int variable, m_##variable) \
        setter(variable, true)
#define TEST_MATRIX(setter, variable) \
    for (int i = 0; i < m_##variable.height(); ++i) \
    { \
        for (int j = 0; j < m_##variable.width(); ++j) \
            setter(i, j, m_##variable(i, j)); \
    }
#define TEST_CHILD(object) \
    connect(object, SIGNAL(valMessage(QString)), this, SIGNAL(valMessage(QString))); \
    connect(object, SIGNAL(valWarning(QString)), this, SIGNAL(valWarning(QString))); \
    connect(object, SIGNAL(valError(QString)), this, SIGNAL(valError(QString))); \
    object->validate(); \
    disconnect(object, SIGNAL(valMessage(QString)), this, SIGNAL(valMessage(QString))); \
    disconnect(object, SIGNAL(valWarning(QString)), this, SIGNAL(valWarning(QString))); \
    disconnect(object, SIGNAL(valError(QString)), this, SIGNAL(valError(QString)))
#define TEST_SUB_BEGIN(type, variable) \
    foreach (type* object, m_##variable) \
    { \
        TEST_CHILD(object)
#define TEST_SUB(name, value) TEST_SUB_RAW(value, name, value)
#define TEST_SUB_RAW(checking, name, value) \
        if (checking##Checker.contains(object->value())) \
            emit(error(subclass(name, object->id()))); \
        checking##Checker.insert(object->value())
#define TEST_SUB_END() }

#define CHECK(variable) CHECK_ARRAY(variable, variable)
#define CHECK_ARRAY(variable, value) \
    if (m_##variable != value) \
    { \
        m_##variable = value; \
        emit(changed()); \
    }

#endif

#endif