aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qtserialization/inc/qark/qxmloutarchive.h
blob: 28edc7e51b888804b4c1665658e628ae6f2f7df0 (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
// Copyright (C) 2016 Jochen Becher
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "archivebasics.h"
#include "tag.h"
#include "baseclass.h"
#include "attribute.h"
#include "reference.h"
#include "impl/savingrefmap.h"

#include <QXmlStreamWriter>
#include <exception>

namespace qark {

class QXmlOutArchive : public ArchiveBasics
{
public:
    class UnsupportedForwardReference : public std::exception
    {
    };

    class DanglingReferences : public std::exception
    {
    };

    static const bool inArchive = false;
    static const bool outArchive = true;

    QXmlOutArchive(QXmlStreamWriter &stream)
        : m_stream(stream)
    {
    }

    ~QXmlOutArchive()
    {
    }

    template<typename T>
    void write(T *p)
    {
        if (!m_savingRefMap.hasDefinedRef(p))
            throw UnsupportedForwardReference();
        write(m_savingRefMap.ref(p).get());
    }

#define QARK_WRITENUMBER(T) \
    void write(T i) \
    { \
        m_stream.writeCharacters(QString::number(i)); \
    }

    QARK_WRITENUMBER(char)
    QARK_WRITENUMBER(signed char)
    QARK_WRITENUMBER(unsigned char)
    QARK_WRITENUMBER(short int)
    QARK_WRITENUMBER(unsigned short int)
    QARK_WRITENUMBER(int)
    QARK_WRITENUMBER(unsigned int)
    QARK_WRITENUMBER(long int)
    QARK_WRITENUMBER(unsigned long int)
    QARK_WRITENUMBER(long long int)
    QARK_WRITENUMBER(unsigned long long int)
    QARK_WRITENUMBER(float)
    QARK_WRITENUMBER(double)

#undef QARK_WRITENUMBER

    void write(bool b)
    {
        m_stream.writeCharacters(QLatin1String(b ? "true" : "false"));
    }

    void write(const QString &s)
    {
        m_stream.writeCharacters(s);
    }

    void beginDocument()
    {
        m_stream.writeStartDocument();
    }

    void endDocument()
    {
        m_stream.writeEndDocument();
        if (m_savingRefMap.countDanglingReferences() > 0)
            throw DanglingReferences();
    }

    void beginElement(const Tag &tag)
    {
        m_stream.writeStartElement(tag.qualifiedName());
    }

    template<class T>
    void beginElement(const Object<T> &object)
    {
        m_stream.writeStartElement(object.qualifiedName());
        // TODO implement key attribute
        // Currently qmodel files do not use references at all
        // so writing reference keys are not needed. If this
        // changes keys should be implemented as a generic
        // concept getting key from object (e.g. with a function
        // registered per type in typeregistry)
    }

    void endElement(const End &)
    {
        m_stream.writeEndElement();
    }

    template<class T, class U>
    void beginBase(const Base<T, U> &base)
    {
        m_stream.writeStartElement(base.qualifiedName());
    }

    template<class T, class U>
    void endBase(const Base<T, U> &)
    {
        m_stream.writeEndElement();
    }

    template<class T>
    void beginAttribute(const Attr<T> &attr)
    {
        m_stream.writeStartElement(attr.qualifiedName());
    }

    template<class T>
    void endAttribute(const Attr<T> &)
    {
        m_stream.writeEndElement();
    }

    template<class U, typename T>
    void beginAttribute(const GetterAttr<U, T> &attr)
    {
        m_stream.writeStartElement(attr.qualifiedName());
    }

    template<class U, typename T>
    void endAttribute(const GetterAttr<U, T> &)
    {
        m_stream.writeEndElement();
    }

    template<class U, typename T, typename V>
    void beginAttribute(const GetterSetterAttr<U, T, V> &attr)
    {
        m_stream.writeStartElement(attr.qualifiedName());
    }

    template<class U, typename T, typename V>
    void endAttribute(const GetterSetterAttr<U, T, V> &)
    {
        m_stream.writeEndElement();
    }

    template<class U, typename T>
    void beginAttribute(const GetFuncAttr<U, T> &attr)
    {
        m_stream.writeStartElement(attr.qualifiedName());
    }

    template<class U, typename T>
    void endAttribute(const GetFuncAttr<U, T> &)
    {
        m_stream.writeEndElement();
    }

    template<class U, typename T, typename V>
    void beginAttribute(const GetSetFuncAttr<U, T, V> &attr)
    {
        m_stream.writeStartElement(attr.qualifiedName());
    }

    template<class U, typename T, typename V>
    void endAttribute(const GetSetFuncAttr<U, T, V> &)
    {
        m_stream.writeEndElement();
    }

    template<class T>
    void beginReference(const Ref<T> &ref)
    {
        m_stream.writeStartElement(ref.qualifiedName());
        m_isNextPointerAReference = true;
    }

    template<class T>
    void endReference(const Ref<T> &)
    {
        m_isNextPointerAReference = false;
        m_stream.writeEndElement();
    }

    template<class U, typename T>
    void beginReference(const GetterRef<U, T> &ref)
    {
        m_stream.writeStartElement(ref.qualifiedName());
        m_isNextPointerAReference = true;
    }

    template<class U, typename T>
    void endReference(const GetterRef<U, T> &)
    {
        m_isNextPointerAReference = false;
        m_stream.writeEndElement();
    }

    template<class U, typename T, typename V>
    void beginReference(const GetterSetterRef<U, T, V> &ref)
    {
        m_stream.writeStartElement(ref.qualifiedName());
        m_isNextPointerAReference = true;
    }

    template<class U, typename T, typename V>
    void endReference(const GetterSetterRef<U, T, V> &)
    {
        m_isNextPointerAReference = false;
        m_stream.writeEndElement();
    }

    template<class U, typename T>
    void beginReference(const GetFuncRef<U, T> &ref)
    {
        m_stream.writeStartElement(ref.qualifiedName());
        m_isNextPointerAReference = true;
    }

    template<class U, typename T>
    void endReference(const GetFuncRef<U, T> &)
    {
        m_isNextPointerAReference = false;
        m_stream.writeEndElement();
    }

    template<class U, typename T, typename V>
    void beginReference(const GetSetFuncRef<U, T, V> &ref)
    {
        m_stream.writeStartElement(ref.qualifiedName());
        m_isNextPointerAReference = true;
    }

    template<class U, typename T, typename V>
    void endReference(const GetSetFuncRef<U, T, V> &)
    {
        m_isNextPointerAReference = false;
        m_stream.writeEndElement();
    }

    template<typename T>
    bool isReference(T *p)
    {
        return m_isNextPointerAReference || m_savingRefMap.hasDefinedRef(p);
    }

    void beginNullPointer()
    {
        m_stream.writeStartElement(QLatin1String("null"));
    }

    void endNullPointer()
    {
        m_stream.writeEndElement();
    }

    void beginPointer()
    {
        m_stream.writeStartElement(QLatin1String("reference"));
    }

    void endPointer()
    {
        m_stream.writeEndElement();
    }

    void beginInstance()
    {
        m_stream.writeStartElement(QLatin1String("instance"));
    }

    void beginInstance(const QString &typeUid)
    {
        m_stream.writeStartElement(QLatin1String("instance"));
        m_stream.writeAttribute(QLatin1String("type"), typeUid);
    }

    void endInstance()
    {
        m_stream.writeEndElement();
    }

private:
    QXmlStreamWriter &m_stream;
    impl::SavingRefMap m_savingRefMap;
    bool m_isNextPointerAReference = false;
};

} // namespace qark