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:
Qt Code:
  1. class Object {
  2. public:
  3. virtual void serialize(XmlStreamWriter &writer) {
  4. openTag(writer);
  5. writeContents(writer);
  6. closeTag(writer);
  7. }
  8. protected:
  9. virtual void openTag(XmlStreamWriter &writer, bool open = true) {}
  10. virtual void closeTag(XmlStreamWriter &writer) { writer.writeEndElement();}
  11. virtual void writeContents(XmlStreamWriter &writer) {}
  12. };
  13.  
  14. class SubObject : public Object {
  15. protected:
  16. virtual void openTag(XmlStreamWriter &writer, bool open = true) {
  17. if(open) { stream.writeStartElement(...); }
  18. writer.writeAttribute(...);
  19. Object::openTag(writer, false); // pass false to mark the parent should not open the tag by itself
  20. }
  21. virtual void closeTag(XmlStreamWriter &writer) {
  22. // this should work the other way round, the top-most class should close the tag
  23. writer....
  24. Object::closeTag(writer);
  25. }
  26. virtual void writeContents(XmlStreamWriter &writer) {
  27. Object::writeContents(writer);
  28. writer...
  29. }
  30. };
To copy to clipboard, switch view to plain text mode