BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Streamer.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/XML/Streamer.h
6 //! @brief Defines 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 
15 #ifndef BORNAGAIN_GUI_SUPPORT_XML_STREAMER_H
16 #define BORNAGAIN_GUI_SUPPORT_XML_STREAMER_H
17 
18 #include "Base/Util/Assert.h"
19 #include <QXmlStreamWriter> // used in every including file
20 #include <functional>
21 #include <heinz/Vectors3D.h>
22 
23 namespace XML {
24 namespace Tags {
25 constexpr auto Id("id");
26 constexpr auto Value("value");
27 constexpr auto Decimals("decimals");
28 constexpr auto Version("version");
29 } // namespace Tags
30 } // namespace XML
31 
32 //! Supports serialization to or deserialization from QXmlStream.
33 //!
34 //! Can be either a writer or a reader, depending on the constructor.
35 
36 class Streamer {
37 public:
38  Streamer(QXmlStreamWriter* w)
39  : m_w(w)
40  {
41  }
42  Streamer(QXmlStreamReader* r)
43  : m_r(r)
44  {
45  }
46 
47  QXmlStreamWriter* xmlWriter() { return m_w; } //!< Returns stream writer or nullptr.
48  QXmlStreamReader* xmlReader() { return m_r; } //!< Returns stream reader or nullptr.
49 
50  //! As writer, serializes the given version number. As reader, does nothing.
51  void writeVersion(unsigned version) const;
52 
53  //! As reader, throws DeserializationException unless the expected version is read.
54  //! As writer, does nothing.
55  void assertVersion(unsigned expectedVersion) const;
56 
57  template <typename Catalog>
58  void write(const QString& tag, typename Catalog::CatalogedType* p);
59 
60  template <typename Catalog, typename... Args>
61  void read(const QString& tag, typename Catalog::CatalogedType*& p, Args... argsForConstructor);
62 
63  void gotoStartElementOfTag(const QString& tag);
64  void gotoEndElementOfTag(const QString& tag);
65  void assertCurrentTag(const QString& expectedTag) const;
66 
67  void start(const QString& tag);
68  void finish(const QString& tag);
69 
70 private:
71  void assertCurrentToken(QXmlStreamReader::TokenType token) const;
72 
73  QXmlStreamWriter* m_w = nullptr;
74  QXmlStreamReader* m_r = nullptr;
75 };
76 
77 // ************************************************************************************************
78 // Templates implementation
79 // ************************************************************************************************
80 
81 template <typename Catalog>
82 void Streamer::write(const QString& tag, typename Catalog::CatalogedType* p)
83 {
84  ASSERT(m_w);
85  m_w->writeStartElement(tag);
86  m_w->writeAttribute("type", QString::number(static_cast<uint8_t>(Catalog::type(p))));
87  if (p != nullptr)
88  p->serialize(*this);
89  m_w->writeEndElement();
90 }
91 
92 template <typename Catalog, typename... Args>
93 void Streamer::read(const QString& tag, typename Catalog::CatalogedType*& p,
94  Args... argsForConstructor)
95 {
96  ASSERT(m_r);
98  const auto type = static_cast<typename Catalog::Type>(m_r->attributes().value("type").toUInt());
99  p = Catalog::create(type, argsForConstructor...);
100  if (p != nullptr)
101  p->serialize(*this);
102  gotoEndElementOfTag(tag);
103 }
104 
105 #endif // BORNAGAIN_GUI_SUPPORT_XML_STREAMER_H
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
void gotoEndElementOfTag(const QString &tag)
Definition: Streamer.cpp:39
void start(const QString &tag)
Definition: Streamer.cpp:82
void write(const QString &tag, typename Catalog::CatalogedType *p)
Definition: Streamer.h:82
Streamer(QXmlStreamWriter *w)
Definition: Streamer.h:38
Streamer(QXmlStreamReader *r)
Definition: Streamer.h:42
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 * xmlWriter()
Returns stream writer or nullptr.
Definition: Streamer.h:47
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
Definition: Streamer.h:48
QXmlStreamWriter * m_w
Definition: Streamer.h:73
void read(const QString &tag, typename Catalog::CatalogedType *&p, Args... argsForConstructor)
Definition: Streamer.h:93
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
Definition: JobItem.cpp:33
constexpr auto Version("version")
constexpr auto Decimals("decimals")
constexpr auto Id("id")
constexpr auto Value("value")
Definition: Streamer.h:23