From 279a262eab521434cdcf1ab33dab84bb9c0df923 Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Wed, 25 May 2011 15:04:58 +0300 Subject: Change sxml::element to use templates --- sxml.cpp | 29 +++++------------------------ sxml.h | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/sxml.cpp b/sxml.cpp index f5be854..916352b 100644 --- a/sxml.cpp +++ b/sxml.cpp @@ -25,8 +25,6 @@ #include "sxml.h" -#include - 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 diff --git a/sxml.h b/sxml.h index 7b9ffc2..2080a59 100644 --- a/sxml.h +++ b/sxml.h @@ -27,6 +27,7 @@ #define SXML_H #include +#include #include #include @@ -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 + 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 + 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 element &element::set_text(const T &text) +{ + std::string s; + std::stringstream ss(s); + ss << text; + + return set_text(ss.str()); +} + +template<> element &element::set_text<>(const std::string &text); + +template +element &element::set_attr(const std::string &name, const T &value) +{ + std::string s; + std::stringstream ss(s); + ss << value; + + return set_attr(name, ss.str()); +} + +template<> +element &element::set_attr(const std::string &name, const std::string &value); + } // namespace sxml #endif // SXML_H -- cgit v1.2.3