So why don't you split this single method into three methods (open tag, write contents, close tag) just like the stream writer does? Possibly even with a way to allow the parent class to append attributes to the opening tag created by the child class using writeAttributes. This should work:
class Object {
public:
virtual void serialize(XmlStreamWriter &writer) {
openTag(writer);
writeContents(writer);
closeTag(writer);
}
protected:
virtual void openTag(XmlStreamWriter &writer, bool open = true) {}
virtual void closeTag(XmlStreamWriter &writer) { writer.writeEndElement();}
virtual void writeContents(XmlStreamWriter &writer) {}
};
class SubObject : public Object {
protected:
virtual void openTag(XmlStreamWriter &writer, bool open = true) {
if(open) { stream.writeStartElement(...); }
writer.writeAttribute(...);
Object::openTag(writer, false); // pass false to mark the parent should not open the tag by itself
}
virtual void closeTag(XmlStreamWriter &writer) {
// this should work the other way round, the top-most class should close the tag
writer....
Object::closeTag(writer);
}
virtual void writeContents(XmlStreamWriter &writer) {
Object::writeContents(writer);
writer...
}
};
class Object {
public:
virtual void serialize(XmlStreamWriter &writer) {
openTag(writer);
writeContents(writer);
closeTag(writer);
}
protected:
virtual void openTag(XmlStreamWriter &writer, bool open = true) {}
virtual void closeTag(XmlStreamWriter &writer) { writer.writeEndElement();}
virtual void writeContents(XmlStreamWriter &writer) {}
};
class SubObject : public Object {
protected:
virtual void openTag(XmlStreamWriter &writer, bool open = true) {
if(open) { stream.writeStartElement(...); }
writer.writeAttribute(...);
Object::openTag(writer, false); // pass false to mark the parent should not open the tag by itself
}
virtual void closeTag(XmlStreamWriter &writer) {
// this should work the other way round, the top-most class should close the tag
writer....
Object::closeTag(writer);
}
virtual void writeContents(XmlStreamWriter &writer) {
Object::writeContents(writer);
writer...
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks