diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2011-06-01 11:04:57 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2011-06-01 11:04:57 +0300 |
| commit | 8eba476357c8488be510f012594f305848a7eeb6 (patch) | |
| tree | 42677f805d20d4b5d0b217aaacf1acedca29d2bd | |
| parent | a71564984253ade92fc4d3c9bf185807efc570bf (diff) | |
| download | sxml-8eba476357c8488be510f012594f305848a7eeb6.tar.gz sxml-8eba476357c8488be510f012594f305848a7eeb6.zip | |
Move add_element() to node and add add_comment()
| -rw-r--r-- | example.cpp | 8 | ||||
| -rw-r--r-- | sxml.cpp | 15 | ||||
| -rw-r--r-- | sxml.h | 19 |
3 files changed, 26 insertions, 16 deletions
diff --git a/example.cpp b/example.cpp index bbc3bbd..166ae98 100644 --- a/example.cpp +++ b/example.cpp @@ -35,13 +35,13 @@ int main() root->add_element("node")->set_attr("id", 0); - root->add_child(new comment("yay, a comment!")); + root->add_comment("yay, a comment"); root->add_element("node")->set_attr("id", 1); element *node = root->add_element("node")->set_attr("id", 3.1); - node = node->add_element("subnode") - node->add_element("subsubnode")->add_child( - new comment("i am in pretty deep waters here, man ..")); + node = node->add_element("subnode"); + node->add_element("subsubnode")->add_comment( + "i am in pretty deep waters here, man .."); std::cout << root->to_string(true) << std::endl; @@ -65,6 +65,16 @@ node *node::add_child(node *child) return child; } +element *node::add_element(const std::string &tag) +{ + return new element(tag, this); +} + +comment *node::add_comment(const std::string &text) +{ + return new comment(text, this); +} + string node::to_string(bool nice , int indent) const { nice = nice; @@ -220,11 +230,6 @@ string element::to_string(bool nice, int indent) const return xml.str(); } -element *element::add_element(const std::string &tag) -{ - return new element(tag, this); -} - template<> element *element::set_text<>(const string &text) { m_text = text; @@ -34,6 +34,8 @@ namespace sxml { class node; +class element; +class comment; typedef std::vector<node *> node_list; @@ -52,13 +54,20 @@ class node //! Destructor. Frees the children of this node. virtual ~node(); + //! Returns a string representation of this node. Derived classes should + //! implement this. + virtual std::string to_string(bool nice , int indent = 0) const; + //! Adds a child node to this node. Takes the ownership of the given //! node. Returns a pointer to the child. node *add_child(node *child); - //! Returns a string representation of this node. Derived classes should - //! implement this. - virtual std::string to_string(bool nice , int indent = 0) const; + //! Adds an element with the given tag to this node. Returns a pointer to + //! the element node. + element *add_element(const std::string &tag); + + //! Adds a comment to this node. Returns a pointer to the comment node. + comment *add_comment(const std::string &text); protected: @@ -128,10 +137,6 @@ class element: public node //! and newlines. virtual std::string to_string(bool nice = false, int indent = 0) const; - //! Adds a child with the given tag to this element. Returns a pointer - //! to the child. - element *add_element(const std::string &tag); - //! 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(). Returns a pointer to this element. |
