File: UtilXML.h

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (150 lines) | stat: -rw-r--r-- 4,932 bytes parent folder | download | duplicates (2)
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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Util/UtilXML.h
//! @brief     Defines
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_GUI_MODEL_UTIL_UTILXML_H
#define BORNAGAIN_GUI_MODEL_UTIL_UTILXML_H

#include <QColor>
#include <QXmlStreamReader> // used in every including file, also provides QXmlStreamWriter

namespace XML {

constexpr const char* LinkMimeType{"application/org.bornagainproject.fittinglink"};

const QString minimal_supported_version = "22.0";

namespace Tag {

const QString BaseData("BaseData");

} // namespace Tag

namespace Attrib {

const QString BA_Version("BA_Version");
const QString collapsed("collapsed");
const QString id("id");
const QString index("index");
const QString kind("kind");
const QString name("name");
const QString projectName("projectName");
const QString type("type");
const QString value("value");
const QString units("units");

} // namespace Attrib

void gotoEndElementOfTag(QXmlStreamReader* reader, const QString& tag);

void writeAttribute(QXmlStreamWriter* writer, const QString& attributeName, bool b);
void writeAttribute(QXmlStreamWriter* writer, const QString& attributeName, double d);
void writeAttribute(QXmlStreamWriter* writer, const QString& attributeName, QString s);
void writeAttribute(QXmlStreamWriter* writer, const QString& attributeName, QColor c);

template <typename T>
void writeAttribute(QXmlStreamWriter* writer, const QString& attributeName, const T d)
{
    writer->writeAttribute(attributeName, QString::number(d));
};

template <typename T> void writeTaggedValue(QXmlStreamWriter* w, const QString& tag, const T value)
{
    w->writeStartElement(tag);
    writeAttribute(w, XML::Attrib::value, value);
    w->writeEndElement();
};

template <typename T>
void writeTaggedElement(QXmlStreamWriter* w, const QString& tag, const T& element)
{
    w->writeStartElement(tag);
    element.writeTo(w);
    w->writeEndElement();
}

template <typename T>
void writeBaseElement(QXmlStreamWriter* w, const QString& tag, const T* element)
{
    w->writeStartElement(tag);
    element->T::writeTo(w);
    w->writeEndElement();
}

bool readBool(QXmlStreamReader* reader, const QString& attributeName);
int readInt(QXmlStreamReader* reader, const QString& attributeName);
unsigned readUInt(QXmlStreamReader* reader, const QString& attributeName);
double readDouble(QXmlStreamReader* reader, const QString& attributeName);
QString readString(QXmlStreamReader* reader, const QString& attributeName);

bool readTaggedBool(QXmlStreamReader* reader, const QString& tag);
int readTaggedInt(QXmlStreamReader* reader, const QString& tag);
unsigned readTaggedUInt(QXmlStreamReader* reader, const QString& tag);
double readTaggedDouble(QXmlStreamReader* reader, const QString& tag);
QString readTaggedString(QXmlStreamReader* reader, const QString& tag);
QColor readTaggedColor(QXmlStreamReader* reader, const QString& tag);

template <typename T, typename... Args>
void readTaggedElement(QXmlStreamReader* r, const QString& tag, T& element)
{
    element.readFrom(r);
    gotoEndElementOfTag(r, tag);
}

template <typename T> void readBaseElement(QXmlStreamReader* r, const QString& tag, T* element)
{
    element->T::readFrom(r);
    gotoEndElementOfTag(r, tag);
}

template <typename B, typename C, typename... Args>
B* readItemFrom(QXmlStreamReader* r, Args... args)
{
    const uint typeIndex = readUInt(r, XML::Attrib::type);
    const QString kind = readString(r, XML::Attrib::kind);
    const auto type = static_cast<typename C::Type>(typeIndex);
    B* t = C::create(type, args...);
    if (t)
        t->readFrom(r);
    return t;
}

template <typename B, typename C, typename... Args>
B* readChosen(QXmlStreamReader* r, const QString& tag, Args... args)
{
    B* t = readItemFrom<B, C>(r, args...);
    gotoEndElementOfTag(r, tag);
    return t;
}

template <typename B, typename C> void writeItemTo(const B* t, QXmlStreamWriter* w)
{
    const uint typeIndex = static_cast<uint>(C::type(t));
    writeAttribute(w, Attrib::type, typeIndex);
    // The next line allows to see the name of item type in XML. May be skipped while reading.
    writeAttribute(w, Attrib::kind, C::uiInfo(C::type(t)).menuEntry);
    if (t)
        t->writeTo(w);
}

template <typename B, typename C>
void writeChosen(const B* t, QXmlStreamWriter* w, const QString& tag)
{
    w->writeStartElement(tag);
    writeItemTo<B, C>(t, w);
    w->writeEndElement();
}

} // namespace XML

#endif // BORNAGAIN_GUI_MODEL_UTIL_UTILXML_H