From 6d0768d1b0402eab29f38e3756e5fa1567d65c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aymeric=20PELL=C3=89?= Date: Wed, 15 Feb 2017 14:34:53 -0500 Subject: [PATCH] Add generic member functions to get/set attribute value or text. --- src/pugixml.cpp | 12 ++++++++++++ src/pugixml.hpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 7368184..834f73c 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5168,6 +5168,11 @@ namespace pugi } #endif + PUGI__FN std::istringstream xml_attribute::as_stringstream() const + { + return (_attr && _attr->value) ? std::istringstream(_attr->value) : std::istringstream(); + } + PUGI__FN bool xml_attribute::empty() const { return !_attr; @@ -6418,6 +6423,13 @@ namespace pugi } #endif + PUGI__FN std::istringstream xml_text::as_stringstream() const + { + xml_node_struct* d = _data(); + + return (d && d->value) ? std::istringstream(d->value) : std::istringstream(); + } + PUGI__FN bool xml_text::set(const char_t* rhs) { xml_node_struct* dn = _data_new(); diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 4d76bfa..a4e3379 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -32,6 +32,7 @@ // Include STL headers #ifndef PUGIXML_NO_STL +# include # include # include # include @@ -373,6 +374,21 @@ namespace pugi // Get attribute value as bool (returns true if first character is in '1tTyY' set), or the default value if attribute is empty bool as_bool(bool def = false) const; + #ifndef PUGIXML_NO_STL + // Get attribute value as std::istringstream, an empty std::istringstream if the attribute is empty + std::istringstream as_stringstream() const; + + // Get attribute value as Type (template parameter: try to read a Type value from the attribute value string) + template + Type as() const + { + std::istringstream stream = as_stringstream(); + Type value; + stream >> value; + return value; + } + #endif + // Set attribute name/value (returns false if attribute is empty or there is not enough memory) bool set_name(const char_t* rhs); bool set_value(const char_t* rhs); @@ -391,6 +407,16 @@ namespace pugi bool set_value(unsigned long long rhs); #endif + #ifndef PUGIXML_NO_STL + template + bool set_value(const Type& rhs) const + { + std::ostringstream stream; + stream << rhs; + return set_value(stream.str().c_str()); + } + #endif + // Set attribute value (equivalent to set_value without error checking) xml_attribute& operator=(const char_t* rhs); xml_attribute& operator=(int rhs); @@ -725,6 +751,21 @@ namespace pugi // Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty bool as_bool(bool def = false) const; + #ifndef PUGIXML_NO_STL + // Get attribute value as std::istringstream, an empty std::istringstream if the attribute is empty + std::istringstream as_stringstream() const; + + // Get attribute value as Type (template parameter: try to read a Type value from the attribute value string) + template + Type as() const + { + std::istringstream stream = as_stringstream(); + Type value; + stream >> value; + return value; + } + #endif + // Set text (returns false if object is empty or there is not enough memory) bool set(const char_t* rhs); @@ -742,6 +783,16 @@ namespace pugi bool set(unsigned long long rhs); #endif + #ifndef PUGIXML_NO_STL + template + bool set(const Type& rhs) + { + std::ostringstream stream; + stream << rhs; + return set(stream.str().c_str()); + } + #endif + // Set text (equivalent to set without error checking) xml_text& operator=(const char_t* rhs); xml_text& operator=(int rhs);