15 #ifndef BORNAGAIN_GUI_SUPPORT_XML_SERIALIZE_H
16 #define BORNAGAIN_GUI_SUPPORT_XML_SERIALIZE_H
43 template <
typename BaseClass>
48 template <
typename T,
typename... Args>
49 void rwVector(
Streamer& s,
const QString& tag, QVector<T>& vec, Args... argsForConstructor);
53 template <
typename Catalog,
typename... Args>
55 Args... argsForConstructor);
59 template <
typename ItemClass,
typename... ArgsForConstructor>
61 ArgsForConstructor... argsForConstructor);
66 template <
typename ItemClass>
68 const std::function<
void(ItemClass*)>& initializer);
84 template <
typename BaseClass>
88 p->BaseClass::serialize(s);
92 template <
typename T,
typename... Args>
94 Args... argsForConstructor)
96 if (QXmlStreamWriter* w = s.
xmlWriter()) {
97 w->writeStartElement(tag);
98 w->writeAttribute(
"n", QString::number(vec.size()));
102 w->writeEndElement();
103 }
else if (QXmlStreamReader* r = s.
xmlReader()) {
104 r->readNextStartElement();
106 const int n = r->attributes().value(
"n").toInt();
107 for (
int i = 0; i < n; i++) {
108 vec << new typename std::remove_pointer<T>::type(argsForConstructor...);
115 template <
typename Catalog,
typename... Args>
117 QVector<typename Catalog::CatalogedType*>& vec,
118 Args... argsForConstructor)
120 if (QXmlStreamWriter* w = s.
xmlWriter()) {
121 w->writeStartElement(tag);
122 w->writeAttribute(
"n", QString::number(vec.size()));
124 for (
auto* p : vec) {
125 s.
write<Catalog>(
"E" + QString::number(i++), p);
127 w->writeEndElement();
128 }
else if (QXmlStreamReader* r = s.
xmlReader()) {
129 r->readNextStartElement();
131 const int n = r->attributes().value(
"n").toInt();
132 for (
int i = 0; i < n; i++) {
133 typename Catalog::CatalogedType* p =
nullptr;
134 s.
read<Catalog>(
"E" + QString::number(i), p, argsForConstructor...);
141 template <
typename ItemClass,
typename... ArgsForConstructor>
143 ArgsForConstructor... argsForConstructor)
145 if (QXmlStreamWriter* w = s.
xmlWriter()) {
146 w->writeStartElement(tag);
147 w->writeAttribute(
"valid", up ?
"1" :
"0");
150 w->writeEndElement();
151 }
else if (QXmlStreamReader* r = s.
xmlReader()) {
153 const bool valid = r->attributes().value(
"valid").toUInt() > 0;
155 up.reset(
new ItemClass(argsForConstructor...));
163 template <
typename ItemClass>
165 const std::function<
void(ItemClass*)>& initializer)
167 if (QXmlStreamWriter* w = s.
xmlWriter()) {
168 w->writeStartElement(tag);
169 w->writeAttribute(
"valid", up ?
"1" :
"0");
172 w->writeEndElement();
173 }
else if (QXmlStreamReader* r = s.
xmlReader()) {
175 const bool valid = r->attributes().value(
"valid").toUInt() > 0;
177 up.reset(
new ItemClass());
179 initializer(up.get());
Supports serialization to or deserialization from QXmlStream.
void gotoEndElementOfTag(const QString &tag)
void start(const QString &tag)
void write(const QString &tag, typename Catalog::CatalogedType *p)
void assertCurrentTag(const QString &expectedTag) const
void gotoStartElementOfTag(const QString &tag)
QXmlStreamWriter * xmlWriter()
Returns stream writer or nullptr.
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
void read(const QString &tag, typename Catalog::CatalogedType *&p, Args... argsForConstructor)
void finish(const QString &tag)
Functions to serialize various data types.
void rwVector(Streamer &s, const QString &tag, QVector< T > &vec, Args... argsForConstructor)
Serializes a list of items of known and fixed type. Passes optional arguments to the constructor.
void rwBaseClass(Streamer &s, const QString &tag, BaseClass *p)
Serializes part of an items that belongs to a given base class.
void rwValue(Streamer &s, const QString &tag, bool &val)
void rwClass(Streamer &s, const QString &tag, T &t)
Serializes an item from a class that provides the function void serialize(Streamer&).
void rwCatalogized(Streamer &s, const QString &tag, QVector< typename Catalog::CatalogedType * > &vec, Args... argsForConstructor)
Serializes a list of items from a class hierarchy described by a catalog. Passes optional arguments t...
void rwOptional(Streamer &s, const QString &tag, std::unique_ptr< ItemClass > &up, ArgsForConstructor... argsForConstructor)
Serializes an optional item of known type. Passes optional arguments to the constructor.
void rwInitializable(Streamer &s, const QString &tag, std::unique_ptr< ItemClass > &up, const std::function< void(ItemClass *)> &initializer)
Serializes an optional item of known type that needs an initializer.