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 1/4] 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); From 90eeacf606b6cc60ffc4f27d97e4d6d38bc13d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aymeric=20PELL=C3=89?= Date: Thu, 9 Mar 2017 12:03:36 -0500 Subject: [PATCH 2/4] Fix istringtream construction. --- src/pugixml.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 834f73c..75d5842 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5168,10 +5168,12 @@ namespace pugi } #endif +#ifndef PUGIXML_NO_STL PUGI__FN std::istringstream xml_attribute::as_stringstream() const { - return (_attr && _attr->value) ? std::istringstream(_attr->value) : std::istringstream(); + return (_attr && _attr->value) ? std::istringstream(std::string(static_cast(_attr->value))) : std::istringstream(); } +#endif PUGI__FN bool xml_attribute::empty() const { @@ -6423,12 +6425,14 @@ namespace pugi } #endif +#ifndef PUGIXML_NO_STL PUGI__FN std::istringstream xml_text::as_stringstream() const { xml_node_struct* d = _data(); - return (d && d->value) ? std::istringstream(d->value) : std::istringstream(); + return (d && d->value) ? std::istringstream(std::string(static_cast(d->value))) : std::istringstream(); } +#endif PUGI__FN bool xml_text::set(const char_t* rhs) { From 39d814d4171e0e919e6a6a5cc11d0c1354e085ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aymeric=20PELL=C3=89?= Date: Thu, 9 Mar 2017 12:24:37 -0500 Subject: [PATCH 3/4] Fix istringtream type declaration. --- src/pugixml.cpp | 8 ++++---- src/pugixml.hpp | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 75d5842..511b5b7 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5169,9 +5169,9 @@ namespace pugi #endif #ifndef PUGIXML_NO_STL - PUGI__FN std::istringstream xml_attribute::as_stringstream() const + PUGI__FN istringstream_t xml_attribute::as_stringstream() const { - return (_attr && _attr->value) ? std::istringstream(std::string(static_cast(_attr->value))) : std::istringstream(); + return (_attr && _attr->value) ? istringstream_t(string_t(static_cast(_attr->value))) : istringstream_t(); } #endif @@ -6426,11 +6426,11 @@ namespace pugi #endif #ifndef PUGIXML_NO_STL - PUGI__FN std::istringstream xml_text::as_stringstream() const + PUGI__FN istringstream_t xml_text::as_stringstream() const { xml_node_struct* d = _data(); - return (d && d->value) ? std::istringstream(std::string(static_cast(d->value))) : std::istringstream(); + return (d && d->value) ? istringstream_t(string_t(static_cast(d->value))) : istringstream_t(); } #endif diff --git a/src/pugixml.hpp b/src/pugixml.hpp index a4e3379..e3e24bc 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -108,6 +108,7 @@ namespace pugi #ifndef PUGIXML_NO_STL // String type used for operations that work with STL string; depends on PUGIXML_WCHAR_MODE typedef std::basic_string, std::allocator > string_t; + typedef std::basic_istringstream, std::allocator > istringstream_t; #endif } @@ -376,13 +377,13 @@ namespace pugi #ifndef PUGIXML_NO_STL // Get attribute value as std::istringstream, an empty std::istringstream if the attribute is empty - std::istringstream as_stringstream() const; + istringstream_t 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(); + istringstream_t stream = as_stringstream(); Type value; stream >> value; return value; @@ -753,13 +754,13 @@ namespace pugi #ifndef PUGIXML_NO_STL // Get attribute value as std::istringstream, an empty std::istringstream if the attribute is empty - std::istringstream as_stringstream() const; + istringstream_t 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(); + istringstream_t stream = as_stringstream(); Type value; stream >> value; return value; From 883af0827c3a5ce57d545d3bd9482f97e40712b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aymeric=20PELL=C3=89?= Date: Thu, 9 Mar 2017 12:51:51 -0500 Subject: [PATCH 4/4] Try to fix windows compile error. --- src/pugixml.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pugixml.hpp b/src/pugixml.hpp index e3e24bc..05de015 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -383,7 +383,7 @@ namespace pugi template Type as() const { - istringstream_t stream = as_stringstream(); + istringstream_t stream(as_stringstream()); Type value; stream >> value; return value; @@ -760,7 +760,7 @@ namespace pugi template Type as() const { - istringstream_t stream = as_stringstream(); + istringstream_t stream(as_stringstream()); Type value; stream >> value; return value;