BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Streamer.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/XML/Streamer.cpp
6 //! @brief Implements class Streamer
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 
19 void Streamer::writeVersion(unsigned version) const
20 {
21  if (!m_w)
22  return;
23  m_w->writeAttribute(XML::Tags::Version, QString::number(version));
24 }
25 
26 void Streamer::assertVersion(unsigned expectedVersion) const
27 {
28  if (!m_r)
29  return;
30 
31  const auto foundAsStr = m_r->attributes().value(XML::Tags::Version);
32  const unsigned found = foundAsStr.isEmpty() ? 0 : foundAsStr.toUInt();
33  if (found < expectedVersion)
35  if (found > expectedVersion)
37 }
38 
39 void Streamer::gotoEndElementOfTag(const QString& tag)
40 {
41  if (m_r->name() != tag) {
42  if (!m_r->isEndElement())
43  m_r->skipCurrentElement();
44  m_r->skipCurrentElement();
45  }
46  assertCurrentTag(tag);
47  if (!m_r->isEndElement())
48  m_r->skipCurrentElement();
49 
50  assertCurrentToken(QXmlStreamReader::EndElement);
51  assertCurrentTag(tag);
52 }
53 
54 void Streamer::gotoStartElementOfTag(const QString& tag)
55 {
56  ASSERT(m_r);
57  while (m_r->name() != tag && !m_r->hasError())
58  m_r->readNextStartElement();
59  assertCurrentToken(QXmlStreamReader::StartElement);
60 }
61 
62 void Streamer::assertCurrentTag(const QString& expectedTag) const
63 {
64  ASSERT(m_r);
65 
66  if (m_r->name() != expectedTag) {
67 #ifdef _DEBUG
68  // to simplify debugging: what is the current tag
69  QString foundTag = m_r->name().toString();
70  Q_UNUSED(foundTag);
71 #endif
73  }
74 }
75 
76 void Streamer::assertCurrentToken(QXmlStreamReader::TokenType token) const
77 {
78  if (m_r->tokenType() != token)
80 }
81 
82 void Streamer::start(const QString& tag)
83 {
84  if (m_w)
85  m_w->writeStartElement(tag);
86  else
88 }
89 
90 void Streamer::finish(const QString& tag)
91 {
92  if (m_w)
93  m_w->writeEndElement();
94  else
96 }
Defines class DeserializationException.
Defines class Streamer.
Defines.
static DeserializationException streamError()
static DeserializationException tooOld()
static DeserializationException tooNew()
void gotoEndElementOfTag(const QString &tag)
Definition: Streamer.cpp:39
void start(const QString &tag)
Definition: Streamer.cpp:82
void assertCurrentTag(const QString &expectedTag) const
Definition: Streamer.cpp:62
void writeVersion(unsigned version) const
As writer, serializes the given version number. As reader, does nothing.
Definition: Streamer.cpp:19
void assertCurrentToken(QXmlStreamReader::TokenType token) const
Definition: Streamer.cpp:76
void gotoStartElementOfTag(const QString &tag)
Definition: Streamer.cpp:54
QXmlStreamWriter * m_w
Definition: Streamer.h:73
void finish(const QString &tag)
Definition: Streamer.cpp:90
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
Definition: Streamer.cpp:26
QXmlStreamReader * m_r
Definition: Streamer.h:74
constexpr auto Version("version")