aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2011-05-31 10:26:32 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2011-05-31 10:26:32 +0300
commit5fb5bb1c7cc0ce266f60ab272915a48770d1443f (patch)
tree83247683ff95ba11197e3a3d337df9a4579d745c
parentb7a61ba83cbfa0f9684ce9cf476d4c819d1f04ba (diff)
downloadsxml-5fb5bb1c7cc0ce266f60ab272915a48770d1443f.tar.gz
sxml-5fb5bb1c7cc0ce266f60ab272915a48770d1443f.zip
More element::add_child() overloads, misc changes
-rw-r--r--sxml.cpp47
-rw-r--r--sxml.h26
2 files changed, 55 insertions, 18 deletions
diff --git a/sxml.cpp b/sxml.cpp
index 8b0cf0c..53bb31f 100644
--- a/sxml.cpp
+++ b/sxml.cpp
@@ -25,15 +25,25 @@
#include "sxml.h"
+#include <sstream>
+//#include <iostream>
+
using std::string;
using std::ostringstream;
+//using std::cout;
+//using std::endl;
namespace sxml {
element::element(element *parent)
: m_parent(parent),
m_modified(true)
-{}
+{
+ if (parent != NULL)
+ {
+ parent->add_child(this);
+ }
+}
element::element(const element &elem)
: m_parent(elem.m_parent),
@@ -49,7 +59,12 @@ element::element(const string &name, element *parent)
: m_parent(parent),
m_name(name),
m_modified(true)
-{}
+{
+ if (parent != NULL)
+ {
+ parent->add_child(this);
+ }
+}
element::~element()
{
@@ -128,15 +143,27 @@ string element::to_string(bool nice, int indent)
element *element::add_child(element *child)
{
+ child->m_parent = this;
m_children.push_back(child);
- m_modified = true;
- return this;
+ set_modified(true);
+ return child;
+}
+
+element *element::add_child(const element &child)
+{
+ element *c = new element(child);
+ return add_child(c);
+}
+
+element *element::add_child(const std::string &tag)
+{
+ return new element(tag, this);
}
template<> element *element::set_text<>(const string &text)
{
m_text = text;
- m_modified = true;
+ set_modified(true);
return this;
}
@@ -144,7 +171,7 @@ template<> element *element::set_attr<>(const string &name,
const string &value)
{
m_attributes[name] = value;
- m_modified = true;
+ set_modified(true);
return this;
}
@@ -155,7 +182,8 @@ element_list element::clone_children() const
element_list::const_iterator ei = m_children.begin();
for (; ei != m_children.end(); ++ei)
{
- element *clone = new element(*(*ei));
+ element *orig = *ei;
+ element *clone = new element(*orig);
clones.push_back(clone);
}
@@ -166,10 +194,9 @@ void element::set_modified(bool modified)
{
m_modified = modified;
- if (modified)
+ if (modified && m_parent != NULL)
{
- if (m_parent != NULL)
- m_parent->set_modified(true);
+ m_parent->set_modified(true);
}
}
diff --git a/sxml.h b/sxml.h
index 527288f..4f76674 100644
--- a/sxml.h
+++ b/sxml.h
@@ -46,12 +46,12 @@ class element
//! children, attributes) with the specified parent.
explicit element(element *parent = NULL);
- //! Copy constructor.
- element(const element &elem);
-
//! Constructs an element with the specified name and parent.
explicit element(const std::string &name, element *parent = NULL);
+ //! Copy constructor.
+ element(const element &elem);
+
virtual ~element();
//! Creates a textual representation of the element. If nice
@@ -59,16 +59,26 @@ class element
//! and newlines.
std::string to_string(bool nice = false, int indent = 0);
- //! Adds a child element to this element.
+ //! Adds a child element to this element. This version doesn't allocate
+ //! memory and takes the ownership of the given element. Returns a
+ //! pointer to the child.
element *add_child(element *child);
+ //! Adds a child element to this element. This version allocates memory
+ //! for a new child. Returns a pointer to the child.
+ element *add_child(const element &child);
+
+ //! Adds a child with the given tag to this element. Returns a pointer
+ //! to the child.
+ element *add_child(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().
+ //! when calling to_string(). Returns a pointer to this element.
template<class T> element *set_text(const T &text);
//! Set an attribute for this element with the specified name and
- //! value.
+ //! value. Returns a pointer to this element.
template<class T> element *set_attr(const std::string &name,
const T &value);
@@ -102,7 +112,7 @@ template<class T> element *element::set_text(const T &text)
template<> element *element::set_text<>(const std::string &text);
template<class T> element *element::set_attr(const std::string &name,
- const T &value)
+ const T &value)
{
std::string s;
std::stringstream ss(s);
@@ -112,7 +122,7 @@ template<class T> element *element::set_attr(const std::string &name,
}
template<> element *element::set_attr(const std::string &name,
- const std::string &value);
+ const std::string &value);
} // namespace sxml