BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Streamer Class Reference

Description

Supports serialization to or deserialization from QXmlStream.

Can be either a writer or a reader, depending on the constructor.

Definition at line 36 of file Streamer.h.

Public Member Functions

 Streamer (QXmlStreamReader *r)
 
 Streamer (QXmlStreamWriter *w)
 
void assertCurrentTag (const QString &expectedTag) const
 
void assertVersion (unsigned expectedVersion) const
 As reader, throws DeserializationException unless the expected version is read. As writer, does nothing. More...
 
void finish (const QString &tag)
 
void gotoEndElementOfTag (const QString &tag)
 
void gotoStartElementOfTag (const QString &tag)
 
template<typename Catalog , typename... Args>
void read (const QString &tag, typename Catalog::CatalogedType *&p, Args... argsForConstructor)
 
void start (const QString &tag)
 
template<typename Catalog >
void write (const QString &tag, typename Catalog::CatalogedType *p)
 
void writeVersion (unsigned version) const
 As writer, serializes the given version number. As reader, does nothing. More...
 
QXmlStreamReader * xmlReader ()
 Returns stream reader or nullptr. More...
 
QXmlStreamWriter * xmlWriter ()
 Returns stream writer or nullptr. More...
 

Private Member Functions

void assertCurrentToken (QXmlStreamReader::TokenType token) const
 

Private Attributes

QXmlStreamReader * m_r = nullptr
 
QXmlStreamWriter * m_w = nullptr
 

Constructor & Destructor Documentation

◆ Streamer() [1/2]

Streamer::Streamer ( QXmlStreamWriter *  w)
inline

Definition at line 38 of file Streamer.h.

39  : m_w(w)
40  {
41  }
QXmlStreamWriter * m_w
Definition: Streamer.h:73

◆ Streamer() [2/2]

Streamer::Streamer ( QXmlStreamReader *  r)
inline

Definition at line 42 of file Streamer.h.

43  : m_r(r)
44  {
45  }
QXmlStreamReader * m_r
Definition: Streamer.h:74

Member Function Documentation

◆ assertCurrentTag()

void Streamer::assertCurrentTag ( const QString &  expectedTag) const

Definition at line 62 of file Streamer.cpp.

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 }
static DeserializationException streamError()

References m_r, and DeserializationException::streamError().

Referenced by gotoEndElementOfTag(), Serialize::rwCatalogized(), Serialize::rwProperty(), Serialize::rwValue(), and Serialize::rwVector().

Here is the call graph for this function:

◆ assertCurrentToken()

void Streamer::assertCurrentToken ( QXmlStreamReader::TokenType  token) const
private

Definition at line 76 of file Streamer.cpp.

77 {
78  if (m_r->tokenType() != token)
80 }

References m_r, and DeserializationException::streamError().

Referenced by gotoEndElementOfTag(), and gotoStartElementOfTag().

Here is the call graph for this function:

◆ assertVersion()

void Streamer::assertVersion ( unsigned  expectedVersion) const

As reader, throws DeserializationException unless the expected version is read. As writer, does nothing.

Definition at line 26 of file Streamer.cpp.

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 }
static DeserializationException tooOld()
static DeserializationException tooNew()
constexpr auto Version("version")

References m_r, DeserializationException::tooNew(), DeserializationException::tooOld(), and XML::Tags::Version().

Referenced by ProjectDocument::readProject(), AxisProperty::rwAxisProperty(), DistributionItem::serialize(), BasicAxisItem::serialize(), BeamItem::serialize(), InstrumentCollection::serialize(), InstrumentItem::serialize(), PolygonPointItem::serialize(), MaskItems::serialize(), FormFactorItem::serialize(), LayerItem::serialize(), LayerBasicRoughnessItem::serialize(), MaterialItem::serialize(), MaterialItems::serialize(), MultiLayerItem::serialize(), MultiLayerItems::serialize(), ParticleLayoutItem::serialize(), Profile1DItem::serialize(), Profile2DItem::serialize(), AmplitudeAxisItem::serialize(), ConstantBackgroundItem::serialize(), BeamAzimuthalAngleItem::serialize(), BeamInclinationAngleItem::serialize(), SpecularBeamItem::serialize(), BeamWavelengthItem::serialize(), FootprintGaussianItem::serialize(), FootprintSquareItem::serialize(), DepthProbeInstrumentItem::serialize(), Instrument2DItem::serialize(), OffspecInstrumentItem::serialize(), RectangleItem::serialize(), PolygonItem::serialize(), VerticalLineItem::serialize(), HorizontalLineItem::serialize(), EllipseItem::serialize(), MaskAllItem::serialize(), PointwiseAxisItem::serialize(), RectangularDetectorItem::serialize(), ResolutionFunction2DGaussianItem::serialize(), SpecularBeamInclinationItem::serialize(), SphericalDetectorItem::serialize(), Interference1DLatticeItem::serialize(), Interference2DLatticeItem::serialize(), Interference2DParaCrystalItem::serialize(), InterferenceFinite2DLatticeItem::serialize(), InterferenceHardDiskItem::serialize(), InterferenceRadialParaCrystalItem::serialize(), BasicLattice2DItem::serialize(), SquareLattice2DItem::serialize(), HexagonalLattice2DItem::serialize(), MesoCrystalItem::serialize(), ParticleCompositionItem::serialize(), ParticleCoreShellItem::serialize(), ParticleItem::serialize(), Profile1DVoigtItem::serialize(), Profile2DVoigtItem::serialize(), XRotationItem::serialize(), YRotationItem::serialize(), ZRotationItem::serialize(), EulerRotationItem::serialize(), and InterferenceItem::serialize().

Here is the call graph for this function:

◆ finish()

void Streamer::finish ( const QString &  tag)

Definition at line 90 of file Streamer.cpp.

91 {
92  if (m_w)
93  m_w->writeEndElement();
94  else
96 }
void gotoEndElementOfTag(const QString &tag)
Definition: Streamer.cpp:39

References gotoEndElementOfTag(), and m_w.

Referenced by AxisProperty::rwAxisProperty(), Serialize::rwBaseClass(), and Serialize::rwClass().

Here is the call graph for this function:

◆ gotoEndElementOfTag()

void Streamer::gotoEndElementOfTag ( const QString &  tag)

Definition at line 39 of file Streamer.cpp.

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 }
void assertCurrentTag(const QString &expectedTag) const
Definition: Streamer.cpp:62
void assertCurrentToken(QXmlStreamReader::TokenType token) const
Definition: Streamer.cpp:76

References assertCurrentTag(), assertCurrentToken(), and m_r.

Referenced by finish(), read(), Serialize::rwCatalogized(), Serialize::rwInitializable(), Serialize::rwOptional(), Serialize::rwProperty(), Serialize::rwValue(), and Serialize::rwVector().

Here is the call graph for this function:

◆ gotoStartElementOfTag()

void Streamer::gotoStartElementOfTag ( const QString &  tag)

Definition at line 54 of file Streamer.cpp.

55 {
56  ASSERT(m_r);
57  while (m_r->name() != tag && !m_r->hasError())
58  m_r->readNextStartElement();
59  assertCurrentToken(QXmlStreamReader::StartElement);
60 }

References assertCurrentToken(), and m_r.

Referenced by read(), Serialize::rwInitializable(), Serialize::rwOptional(), Serialize::rwValue(), and start().

Here is the call graph for this function:

◆ read()

template<typename Catalog , typename... Args>
void Streamer::read ( const QString &  tag,
typename Catalog::CatalogedType *&  p,
Args...  argsForConstructor 
)

Definition at line 93 of file Streamer.h.

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 }
void gotoStartElementOfTag(const QString &tag)
Definition: Streamer.cpp:54

References gotoEndElementOfTag(), gotoStartElementOfTag(), and m_r.

Referenced by JobItem::readNonSessionItems(), Serialize::rwCatalogized(), and Serialize::rwSelected().

Here is the call graph for this function:

◆ start()

void Streamer::start ( const QString &  tag)

Definition at line 82 of file Streamer.cpp.

83 {
84  if (m_w)
85  m_w->writeStartElement(tag);
86  else
88 }

References gotoStartElementOfTag(), and m_w.

Referenced by AxisProperty::rwAxisProperty(), Serialize::rwBaseClass(), and Serialize::rwClass().

Here is the call graph for this function:

◆ write()

template<typename Catalog >
void Streamer::write ( const QString &  tag,
typename Catalog::CatalogedType *  p 
)

Definition at line 82 of file Streamer.h.

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 }

References m_w.

Referenced by Serialize::rwCatalogized(), Serialize::rwSelected(), and JobItem::writeNonSessionItems().

◆ writeVersion()

void Streamer::writeVersion ( unsigned  version) const

As writer, serializes the given version number. As reader, does nothing.

Definition at line 19 of file Streamer.cpp.

20 {
21  if (!m_w)
22  return;
23  m_w->writeAttribute(XML::Tags::Version, QString::number(version));
24 }

References m_w, and XML::Tags::Version().

Referenced by ProjectDocument::writeTo().

Here is the call graph for this function:

◆ xmlReader()

◆ xmlWriter()

QXmlStreamWriter* Streamer::xmlWriter ( )
inline

Member Data Documentation

◆ m_r

QXmlStreamReader* Streamer::m_r = nullptr
private

◆ m_w

QXmlStreamWriter* Streamer::m_w = nullptr
private

Definition at line 73 of file Streamer.h.

Referenced by finish(), start(), write(), writeVersion(), and xmlWriter().


The documentation for this class was generated from the following files: