From 33caaa0d202d4d6a3abd1462f3d5650e9b6d506f Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 9 Jan 2015 11:30:43 +0100 Subject: added a parent reference to XML nodes. --- src/core/XML.hpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/core/XML.hpp b/src/core/XML.hpp index 51ef6fd..b4b803d 100644 --- a/src/core/XML.hpp +++ b/src/core/XML.hpp @@ -51,15 +51,20 @@ namespace ousia { namespace xml { +class Element; + /** * Node is the common super-class of actual elements (tag-bounded) and text. * It specifies the pure virtual serialize() function that the subclasses * implement. */ class Node : public Managed { +private: + Owned parent; public: - Node(Manager &mgr) : Managed(mgr){}; + Node(Manager &mgr, Handle parent) + : Managed(mgr), parent(acquire(parent)){}; /** * This method writes an XML prolog and the XML representing the current @@ -69,7 +74,7 @@ public: * @param doctype enables you to add a prefix after the XML prolog * specifying the doctype. */ - void serialize(std::ostream &out, const std::string & doctype = ""); + void serialize(std::ostream &out, const std::string &doctype = ""); /** * This method just writes the XML representation of this node to the * output stream, without the XML prolog. @@ -79,6 +84,11 @@ public: * @param tabdepth the current tabdepth for prettier output. */ virtual void doSerialize(std::ostream &out, unsigned int tabdepth) = 0; + + /** + * @return the parent XML element of this node. + */ + Rooted getParent() const { return parent; } }; /** @@ -96,13 +106,16 @@ public: std::map attributes; ManagedVector children; - Element(Manager &mgr, std::string name) : Node(mgr), name(std::move(name)) + Element(Manager &mgr, Handle parent, std::string name) + : Node(mgr, parent), name(std::move(name)) { } - Element(Manager &mgr, std::string name, + Element(Manager &mgr, Handle parent, std::string name, std::map attributes) - : Node(mgr), name(std::move(name)), attributes(std::move(attributes)) + : Node(mgr, parent), + name(std::move(name)), + attributes(std::move(attributes)) { } @@ -120,7 +133,10 @@ class Text : public Node { public: const std::string text; - Text(Manager &mgr, std::string text) : Node(mgr), text(std::move(text)) {} + Text(Manager &mgr, Handle parent, std::string text) + : Node(mgr, parent), text(std::move(text)) + { + } /** * This just writes the text to the output. -- cgit v1.2.3