From fb867d5d65d5f806392b10d08c801d926930daa1 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Thu, 10 Feb 2022 13:46:58 +0100
Subject: [PATCH] DoubleProperty + fct rwXML

---
 GUI/Model/Descriptor/DoubleProperty.cpp |  25 ++++++
 GUI/Model/Descriptor/DoubleProperty.h   |   3 +
 GUI/Model/XML/Serializer.cpp            | 103 +++++++++++-------------
 GUI/Model/XML/Serializer.h              |  17 +++-
 4 files changed, 87 insertions(+), 61 deletions(-)

diff --git a/GUI/Model/Descriptor/DoubleProperty.cpp b/GUI/Model/Descriptor/DoubleProperty.cpp
index 8d529d43b7a..ad5318dbf28 100644
--- a/GUI/Model/Descriptor/DoubleProperty.cpp
+++ b/GUI/Model/Descriptor/DoubleProperty.cpp
@@ -13,6 +13,8 @@
 //  ************************************************************************************************
 
 #include "GUI/Model/Descriptor/DoubleProperty.h"
+#include "GUI/Model/XML/Serializer.h"
+#include "GUI/Support/Data/XML.h"
 #include <QUuid>
 
 void DoubleProperty::init(const QString& label, const QString& tooltip, double value,
@@ -59,3 +61,26 @@ bool DoubleProperty::isInitialized() const
 {
     return !m_uid.isEmpty();
 }
+
+void DoubleProperty::rwXML(Serializer& s)
+{
+    if (QXmlStreamWriter* w = s.xmlWriter()) {
+        w->writeStartElement(persistentTag());
+        GUI::Session::XML::writeAttribute(w, XML::Tags::Value, get());
+        w->writeAttribute(XML::Tags::Id, uid());
+        GUI::Session::XML::writeAttribute(w, XML::Tags::Decimals, decimals());
+        w->writeEndElement();
+    } else if (QXmlStreamReader* r = s.xmlReader()) {
+        QString tagBeforeReadNextStartElement = r->name().toString();
+        r->readNextStartElement();
+        s.assertCurrentTag(persistentTag());
+        const double val = r->attributes().value(XML::Tags::Value).toDouble();
+        const QString uid = r->attributes().value(XML::Tags::Id).toString();
+        unsigned decimals = r->attributes().value(XML::Tags::Decimals).toUInt();
+        set(val);
+        setUid(uid);
+        setDecimals(decimals);
+        s.gotoEndElementOfTag(persistentTag());
+    } else
+        ASSERT(0);
+}
diff --git a/GUI/Model/Descriptor/DoubleProperty.h b/GUI/Model/Descriptor/DoubleProperty.h
index 559a32920fb..f32d6c9e91d 100644
--- a/GUI/Model/Descriptor/DoubleProperty.h
+++ b/GUI/Model/Descriptor/DoubleProperty.h
@@ -17,6 +17,8 @@
 
 #include "GUI/Model/Descriptor/DoubleDescriptor.h"
 
+class Serializer;
+
 // #baMigration docu
 class DoubleProperty {
 public:
@@ -42,6 +44,7 @@ public:
     void setLimits(const RealLimits& limits);
 
     bool isInitialized() const;
+    void rwXML(Serializer& s);
 
 private:
     double m_value = 0.0;
diff --git a/GUI/Model/XML/Serializer.cpp b/GUI/Model/XML/Serializer.cpp
index c0a53a274e4..87bb65acb46 100644
--- a/GUI/Model/XML/Serializer.cpp
+++ b/GUI/Model/XML/Serializer.cpp
@@ -21,22 +21,11 @@
 #include "GUI/Util/DeserializationException.h"
 #include <QColor>
 
-namespace {
-
-namespace Tags {
-constexpr auto Id("id");
-constexpr auto Value("value");
-constexpr auto Decimals("decimals");
-constexpr auto Version("version");
-} // namespace Tags
-
-} // namespace
-
 void Serializer::writeVersion(unsigned version) const
 {
     if (!m_w)
         return;
-    m_w->writeAttribute(Tags::Version, QString::number(version));
+    m_w->writeAttribute(XML::Tags::Version, QString::number(version));
 }
 
 void Serializer::assertVersion(unsigned expectedVersion) const
@@ -44,7 +33,7 @@ void Serializer::assertVersion(unsigned expectedVersion) const
     if (!m_r)
         return;
 
-    const auto foundAsStr = m_r->attributes().value(Tags::Version);
+    const auto foundAsStr = m_r->attributes().value(XML::Tags::Version);
     const unsigned found = foundAsStr.isEmpty() ? 0 : foundAsStr.toUInt();
     if (found < expectedVersion)
         throw DeserializationException::tooOld();
@@ -56,13 +45,13 @@ void Serializer::rw(const QString& tag, bool& val)
 {
     if (m_w) {
         m_w->writeStartElement(tag);
-        m_w->writeAttribute(Tags::Value, val ? "1" : "0");
+        m_w->writeAttribute(XML::Tags::Value, val ? "1" : "0");
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(tag);
-        val = m_r->attributes().value(Tags::Value).toUInt() > 0;
+        val = m_r->attributes().value(XML::Tags::Value).toUInt() > 0;
         gotoEndElementOfTag(tag);
     }
 }
@@ -71,13 +60,13 @@ void Serializer::rw(const QString& tag, int& val)
 {
     if (m_w) {
         m_w->writeStartElement(tag);
-        m_w->writeAttribute(Tags::Value, QString::number(val));
+        m_w->writeAttribute(XML::Tags::Value, QString::number(val));
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(tag);
-        val = m_r->attributes().value(Tags::Value).toInt();
+        val = m_r->attributes().value(XML::Tags::Value).toInt();
         gotoEndElementOfTag(tag);
     }
 }
@@ -86,13 +75,13 @@ void Serializer::rw(const QString& tag, double& val)
 {
     if (m_w) {
         m_w->writeStartElement(tag);
-        GUI::Session::XML::writeAttribute(m_w, Tags::Value, val);
+        GUI::Session::XML::writeAttribute(m_w, XML::Tags::Value, val);
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(tag);
-        val = m_r->attributes().value(Tags::Value).toDouble();
+        val = m_r->attributes().value(XML::Tags::Value).toDouble();
         gotoEndElementOfTag(tag);
     }
 }
@@ -101,13 +90,13 @@ void Serializer::rw(const QString& tag, QString& val)
 {
     if (m_w) {
         m_w->writeStartElement(tag);
-        m_w->writeAttribute(Tags::Value, val);
+        m_w->writeAttribute(XML::Tags::Value, val);
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(tag);
-        GUI::Session::XML::readAttribute(m_r, Tags::Value, &val);
+        GUI::Session::XML::readAttribute(m_r, XML::Tags::Value, &val);
         gotoEndElementOfTag(tag);
     }
 }
@@ -116,13 +105,13 @@ void Serializer::rw(const QString& tag, QColor& col)
 {
     if (m_w) {
         m_w->writeStartElement(tag);
-        m_w->writeAttribute(Tags::Value, col.isValid() ? col.name(QColor::HexArgb) : "");
+        m_w->writeAttribute(XML::Tags::Value, col.isValid() ? col.name(QColor::HexArgb) : "");
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(tag);
-        QString colName = m_r->attributes().value(Tags::Value).toString();
+        QString colName = m_r->attributes().value(XML::Tags::Value).toString();
         col = QColor(colName);
         gotoEndElementOfTag(tag);
     }
@@ -148,11 +137,11 @@ void Serializer::rw(const QString& tag, R3& val)
 {
     if (m_w) {
         m_w->writeStartElement(tag);
-        GUI::Session::XML::writeAttribute(m_w, Tags::Value, val);
+        GUI::Session::XML::writeAttribute(m_w, XML::Tags::Value, val);
         m_w->writeEndElement();
     } else {
         goToStartElementOfTag(tag);
-        GUI::Session::XML::readAttribute(m_r, Tags::Value, &val);
+        GUI::Session::XML::readAttribute(m_r, XML::Tags::Value, &val);
         gotoEndElementOfTag(tag);
     }
 }
@@ -161,17 +150,17 @@ void Serializer::rw(DoubleProperty& d)
 {
     if (m_w) {
         m_w->writeStartElement(d.persistentTag());
-        GUI::Session::XML::writeAttribute(m_w, Tags::Value, d.get());
-        m_w->writeAttribute(Tags::Id, d.uid());
-        GUI::Session::XML::writeAttribute(m_w, Tags::Decimals, d.decimals());
+        GUI::Session::XML::writeAttribute(m_w, XML::Tags::Value, d.get());
+        m_w->writeAttribute(XML::Tags::Id, d.uid());
+        GUI::Session::XML::writeAttribute(m_w, XML::Tags::Decimals, d.decimals());
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(d.persistentTag());
-        const double val = m_r->attributes().value(Tags::Value).toDouble();
-        const QString uid = m_r->attributes().value(Tags::Id).toString();
-        unsigned decimals = m_r->attributes().value(Tags::Decimals).toUInt();
+        const double val = m_r->attributes().value(XML::Tags::Value).toDouble();
+        const QString uid = m_r->attributes().value(XML::Tags::Id).toString();
+        unsigned decimals = m_r->attributes().value(XML::Tags::Decimals).toUInt();
         d.set(val);
         d.setUid(uid);
         d.setDecimals(decimals);
@@ -183,15 +172,15 @@ void Serializer::rw(UIntProperty& d)
 {
     if (m_w) {
         m_w->writeStartElement(d.persistentTag());
-        GUI::Session::XML::writeAttribute(m_w, Tags::Value, d.get());
+        GUI::Session::XML::writeAttribute(m_w, XML::Tags::Value, d.get());
         GUI::Session::XML::writeUid(m_w, d.uid());
         m_w->writeEndElement();
     } else {
         QString tagBeforeReadNextStartElement = m_r->name().toString();
         m_r->readNextStartElement();
         assertCurrentTag(d.persistentTag());
-        const uint val = m_r->attributes().value(Tags::Value).toUInt();
-        const QString uid = m_r->attributes().value(Tags::Id).toString();
+        const uint val = m_r->attributes().value(XML::Tags::Value).toUInt();
+        const QString uid = m_r->attributes().value(XML::Tags::Id).toString();
         d.set(val);
         d.setUid(uid);
         gotoEndElementOfTag(d.persistentTag());
@@ -202,7 +191,7 @@ void Serializer::rw(VectorProperty& d)
 {
     if (m_w) {
         m_w->writeStartElement(d.persistentTag());
-        GUI::Session::XML::writeAttribute(m_w, Tags::Value, d.get());
+        GUI::Session::XML::writeAttribute(m_w, XML::Tags::Value, d.get());
         GUI::Session::XML::writeUid(m_w, d.uid());
         m_w->writeEndElement();
     } else {
@@ -211,8 +200,8 @@ void Serializer::rw(VectorProperty& d)
         assertCurrentTag(d.persistentTag());
         R3 vec;
         QString uid;
-        GUI::Session::XML::readAttribute(m_r, Tags::Value, &vec);
-        GUI::Session::XML::readAttribute(m_r, Tags::Id, &uid);
+        GUI::Session::XML::readAttribute(m_r, XML::Tags::Value, &vec);
+        GUI::Session::XML::readAttribute(m_r, XML::Tags::Id, &uid);
         d.set(vec);
         d.setUid(uid);
         gotoEndElementOfTag(d.persistentTag());
@@ -232,26 +221,6 @@ void Serializer::rw(const QString& tag, AxisProperty& d)
     }
 }
 
-void Serializer::assertCurrentTag(const QString& expectedTag) const
-{
-    ASSERT(m_r);
-
-    if (m_r->name() != expectedTag) {
-#ifdef _DEBUG
-        // to simplify debugging: what is the current tag
-        QString foundTag = m_r->name().toString();
-        Q_UNUSED(foundTag);
-#endif
-        throw DeserializationException::streamError();
-    }
-}
-
-void Serializer::assertCurrentToken(QXmlStreamReader::TokenType token) const
-{
-    if (m_r->tokenType() != token)
-        throw DeserializationException::streamError();
-}
-
 void Serializer::gotoEndElementOfTag(const QString& tag)
 {
     if (m_r->name() != tag) {
@@ -274,3 +243,23 @@ void Serializer::goToStartElementOfTag(const QString& tag)
         m_r->readNextStartElement();
     assertCurrentToken(QXmlStreamReader::StartElement);
 }
+
+void Serializer::assertCurrentTag(const QString& expectedTag) const
+{
+    ASSERT(m_r);
+
+    if (m_r->name() != expectedTag) {
+#ifdef _DEBUG
+        // to simplify debugging: what is the current tag
+        QString foundTag = m_r->name().toString();
+        Q_UNUSED(foundTag);
+#endif
+        throw DeserializationException::streamError();
+    }
+}
+
+void Serializer::assertCurrentToken(QXmlStreamReader::TokenType token) const
+{
+    if (m_r->tokenType() != token)
+        throw DeserializationException::streamError();
+}
diff --git a/GUI/Model/XML/Serializer.h b/GUI/Model/XML/Serializer.h
index f26c3417037..1daa54c08ea 100644
--- a/GUI/Model/XML/Serializer.h
+++ b/GUI/Model/XML/Serializer.h
@@ -25,6 +25,15 @@ class VectorProperty;
 class UIntProperty;
 class AxisProperty;
 
+namespace XML {
+namespace Tags {
+constexpr auto Id("id");
+constexpr auto Value("value");
+constexpr auto Decimals("decimals");
+constexpr auto Version("version");
+} // namespace Tags
+} // namespace XML
+
 //! Supports serialization to or deserialization from QXmlStream.
 //!
 //! Can be either a writer or a reader, depending on the constructor.
@@ -114,13 +123,13 @@ public:
     template <typename Catalog, typename... Args>
     void read(const QString& tag, typename Catalog::CatalogedType*& p, Args... argsForConstructor);
 
-private:
-    void assertCurrentTag(const QString& expectedTag) const;
-    void assertCurrentToken(QXmlStreamReader::TokenType token) const;
     void gotoEndElementOfTag(const QString& tag);
-    void goToStartElementOfTag(const QString& tag);
+    void assertCurrentTag(const QString& expectedTag) const;
 
 private:
+    void goToStartElementOfTag(const QString& tag);
+    void assertCurrentToken(QXmlStreamReader::TokenType token) const;
+
     QXmlStreamWriter* m_w = nullptr;
     QXmlStreamReader* m_r = nullptr;
 };
-- 
GitLab