From b7a61ba83cbfa0f9684ce9cf476d4c819d1f04ba Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Thu, 26 May 2011 16:02:22 +0300 Subject: Use element* instead of element& and introduce parenting --- sxml.h | 62 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 26 deletions(-) (limited to 'sxml.h') diff --git a/sxml.h b/sxml.h index 2080a59..527288f 100644 --- a/sxml.h +++ b/sxml.h @@ -35,52 +35,62 @@ namespace sxml { class element; -typedef std::vector element_list; +typedef std::vector element_list; typedef std::map attribute_map; class element { public: - + //! Constructs an empty element (i.e., no name, - //! children, attributes). - element(); - + //! children, attributes) with the specified parent. + explicit element(element *parent = NULL); + //! Copy constructor. element(const element &elem); - - //! Construcst an element with the specified name and text. - element(const std::string &name); - + + //! Constructs an element with the specified name and parent. + explicit element(const std::string &name, element *parent = NULL); + + virtual ~element(); + //! Creates a textual representation of the element. If nice //! is true, the returned string is formatted with indentations //! and newlines. - std::string to_string(bool nice = false, int indent = 0) const; - - //! Adds a child element to this element. - element &add_child(const element &child); - + std::string to_string(bool nice = false, int indent = 0); + + //! Adds a child element to this element. + element *add_child(element *child); + //! Set the text for this element. An element can either have text //! or children. If an element has both, children take precedence //! when calling to_string(). - template - element &set_text(const T &text); + template element *set_text(const T &text); //! Set an attribute for this element with the specified name and //! value. - template - element &set_attr(const std::string &name, const T &value); + template element *set_attr(const std::string &name, + const T &value); private: - + + element_list clone_children() const; + + void set_modified(bool modified); + + element *m_parent; + element_list m_children; attribute_map m_attributes; - + std::string m_name; std::string m_text; + + std::string m_cached; + bool m_modified; }; -template element &element::set_text(const T &text) +template element *element::set_text(const T &text) { std::string s; std::stringstream ss(s); @@ -89,10 +99,10 @@ template element &element::set_text(const T &text) return set_text(ss.str()); } -template<> element &element::set_text<>(const std::string &text); +template<> element *element::set_text<>(const std::string &text); -template -element &element::set_attr(const std::string &name, const T &value) +template element *element::set_attr(const std::string &name, + const T &value) { std::string s; std::stringstream ss(s); @@ -101,8 +111,8 @@ element &element::set_attr(const std::string &name, const T &value) return set_attr(name, ss.str()); } -template<> -element &element::set_attr(const std::string &name, const std::string &value); +template<> element *element::set_attr(const std::string &name, + const std::string &value); } // namespace sxml -- cgit v1.2.3