// Copyright (C) 2016 Jochen Becher // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #pragma once #include "archivebasics.h" #include "tag.h" #include "baseclass.h" #include "attribute.h" #include "reference.h" #include "impl/savingrefmap.h" #include #include namespace qark { class QXmlOutArchive : public ArchiveBasics { public: class UnsupportedForwardReference : public std::exception { }; class DanglingReferences : public std::exception { }; static const bool inArchive = false; static const bool outArchive = true; QXmlOutArchive(QXmlStreamWriter &stream) : m_stream(stream) { } ~QXmlOutArchive() { } template void write(T *p) { if (!m_savingRefMap.hasDefinedRef(p)) throw UnsupportedForwardReference(); write(m_savingRefMap.ref(p).get()); } #define QARK_WRITENUMBER(T) \ void write(T i) \ { \ m_stream.writeCharacters(QString::number(i)); \ } QARK_WRITENUMBER(char) QARK_WRITENUMBER(signed char) QARK_WRITENUMBER(unsigned char) QARK_WRITENUMBER(short int) QARK_WRITENUMBER(unsigned short int) QARK_WRITENUMBER(int) QARK_WRITENUMBER(unsigned int) QARK_WRITENUMBER(long int) QARK_WRITENUMBER(unsigned long int) QARK_WRITENUMBER(long long int) QARK_WRITENUMBER(unsigned long long int) QARK_WRITENUMBER(float) QARK_WRITENUMBER(double) #undef QARK_WRITENUMBER void write(bool b) { m_stream.writeCharacters(QLatin1String(b ? "true" : "false")); } void write(const QString &s) { m_stream.writeCharacters(s); } void beginDocument() { m_stream.writeStartDocument(); } void endDocument() { m_stream.writeEndDocument(); if (m_savingRefMap.countDanglingReferences() > 0) throw DanglingReferences(); } void beginElement(const Tag &tag) { m_stream.writeStartElement(tag.qualifiedName()); } template void beginElement(const Object &object) { m_stream.writeStartElement(object.qualifiedName()); // TODO implement key attribute // Currently qmodel files do not use references at all // so writing reference keys are not needed. If this // changes keys should be implemented as a generic // concept getting key from object (e.g. with a function // registered per type in typeregistry) } void endElement(const End &) { m_stream.writeEndElement(); } template void beginBase(const Base &base) { m_stream.writeStartElement(base.qualifiedName()); } template void endBase(const Base &) { m_stream.writeEndElement(); } template void beginAttribute(const Attr &attr) { m_stream.writeStartElement(attr.qualifiedName()); } template void endAttribute(const Attr &) { m_stream.writeEndElement(); } template void beginAttribute(const GetterAttr &attr) { m_stream.writeStartElement(attr.qualifiedName()); } template void endAttribute(const GetterAttr &) { m_stream.writeEndElement(); } template void beginAttribute(const GetterSetterAttr &attr) { m_stream.writeStartElement(attr.qualifiedName()); } template void endAttribute(const GetterSetterAttr &) { m_stream.writeEndElement(); } template void beginAttribute(const GetFuncAttr &attr) { m_stream.writeStartElement(attr.qualifiedName()); } template void endAttribute(const GetFuncAttr &) { m_stream.writeEndElement(); } template void beginAttribute(const GetSetFuncAttr &attr) { m_stream.writeStartElement(attr.qualifiedName()); } template void endAttribute(const GetSetFuncAttr &) { m_stream.writeEndElement(); } template void beginReference(const Ref &ref) { m_stream.writeStartElement(ref.qualifiedName()); m_isNextPointerAReference = true; } template void endReference(const Ref &) { m_isNextPointerAReference = false; m_stream.writeEndElement(); } template void beginReference(const GetterRef &ref) { m_stream.writeStartElement(ref.qualifiedName()); m_isNextPointerAReference = true; } template void endReference(const GetterRef &) { m_isNextPointerAReference = false; m_stream.writeEndElement(); } template void beginReference(const GetterSetterRef &ref) { m_stream.writeStartElement(ref.qualifiedName()); m_isNextPointerAReference = true; } template void endReference(const GetterSetterRef &) { m_isNextPointerAReference = false; m_stream.writeEndElement(); } template void beginReference(const GetFuncRef &ref) { m_stream.writeStartElement(ref.qualifiedName()); m_isNextPointerAReference = true; } template void endReference(const GetFuncRef &) { m_isNextPointerAReference = false; m_stream.writeEndElement(); } template void beginReference(const GetSetFuncRef &ref) { m_stream.writeStartElement(ref.qualifiedName()); m_isNextPointerAReference = true; } template void endReference(const GetSetFuncRef &) { m_isNextPointerAReference = false; m_stream.writeEndElement(); } template bool isReference(T *p) { return m_isNextPointerAReference || m_savingRefMap.hasDefinedRef(p); } void beginNullPointer() { m_stream.writeStartElement(QLatin1String("null")); } void endNullPointer() { m_stream.writeEndElement(); } void beginPointer() { m_stream.writeStartElement(QLatin1String("reference")); } void endPointer() { m_stream.writeEndElement(); } void beginInstance() { m_stream.writeStartElement(QLatin1String("instance")); } void beginInstance(const QString &typeUid) { m_stream.writeStartElement(QLatin1String("instance")); m_stream.writeAttribute(QLatin1String("type"), typeUid); } void endInstance() { m_stream.writeEndElement(); } private: QXmlStreamWriter &m_stream; impl::SavingRefMap m_savingRefMap; bool m_isNextPointerAReference = false; }; } // namespace qark