diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2011-05-25 15:04:58 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2011-05-25 15:04:58 +0300 |
| commit | 279a262eab521434cdcf1ab33dab84bb9c0df923 (patch) | |
| tree | ba7efce4ebce6c8bbb2199c8b8f9ed48614923a8 | |
| parent | d0f9a4a26cfae97cf6d9129e4a84d6b6fbb85052 (diff) | |
| download | sxml-templates.tar.gz sxml-templates.zip | |
Change sxml::element to use templatestemplates
| -rw-r--r-- | sxml.cpp | 29 | ||||
| -rw-r--r-- | sxml.h | 43 |
2 files changed, 36 insertions, 36 deletions
@@ -25,8 +25,6 @@ #include "sxml.h" -#include <sstream> - namespace sxml { element::element() @@ -39,9 +37,8 @@ element::element(const element &elem) m_text(elem.m_text) {} -element::element(const std::string &name, const std::string &text) - : m_name(name), - m_text(text) +element::element(const std::string &name) + : m_name(name) {} std::string element::to_string(bool nice, int indent) const @@ -107,34 +104,18 @@ element &element::add_child(const element &child) return *this; } -element &element::set_text(const std::string &text) +template<> element &element::set_text<>(const std::string &text) { m_text = text; return *this; } -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) { m_attributes[name] = value; return *this; } -element &element::set_attr(const std::string &name, long value) -{ - std::string s; - std::stringstream ss(s); - ss << value; - return set_attr(name, ss.str()); -} - -element &element::set_attr(const std::string &name, double value) -{ - std::string s; - std::stringstream ss(s); - ss << value; - return set_attr(name, ss.str()); -} - } // namespace sxml @@ -27,6 +27,7 @@ #define SXML_H #include <string> +#include <sstream> #include <map> #include <vector> @@ -49,8 +50,7 @@ class element element(const element &elem); //! Construcst an element with the specified name and text. - element(const std::string &name, - const std::string &text = std::string()); + element(const std::string &name); //! Creates a textual representation of the element. If nice //! is true, the returned string is formatted with indentations @@ -63,19 +63,14 @@ class element //! 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(). - element &set_text(const std::string &text); - + template<class T> + element &set_text(const T &text); + //! Set an attribute for this element with the specified name and //! value. - element &set_attr(const std::string &name, - const std::string &value); + template<class T> + element &set_attr(const std::string &name, const T &value); - //! This is an overloaded function. - element &set_attr(const std::string &name, long value); - - //! This is an overloaded function. - element &set_attr(const std::string &name, double value); - private: element_list m_children; @@ -85,6 +80,30 @@ class element std::string m_text; }; +template<class T> element &element::set_text(const T &text) +{ + std::string s; + std::stringstream ss(s); + ss << text; + + return set_text<std::string>(ss.str()); +} + +template<> element &element::set_text<>(const std::string &text); + +template<class T> +element &element::set_attr(const std::string &name, const T &value) +{ + std::string s; + std::stringstream ss(s); + ss << value; + + return set_attr<std::string>(name, ss.str()); +} + +template<> +element &element::set_attr(const std::string &name, const std::string &value); + } // namespace sxml #endif // SXML_H |
