diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 56d7c75..33df473 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5172,6 +5172,13 @@ namespace pugi } #endif +#ifndef PUGIXML_NO_STL + PUGI__FN istringstream_t xml_attribute::as_stringstream() const + { + return (_attr && _attr->value) ? istringstream_t(string_t(static_cast(_attr->value))) : istringstream_t(); + } +#endif + PUGI__FN bool xml_attribute::empty() const { return !_attr; @@ -6422,6 +6429,15 @@ namespace pugi } #endif +#ifndef PUGIXML_NO_STL + PUGI__FN istringstream_t xml_text::as_stringstream() const + { + xml_node_struct* d = _data(); + + return (d && d->value) ? istringstream_t(string_t(static_cast(d->value))) : istringstream_t(); + } +#endif + 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..05de015 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -32,6 +32,7 @@ // Include STL headers #ifndef PUGIXML_NO_STL +# include # include # include # include @@ -107,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 } @@ -373,6 +375,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 + 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 + { + istringstream_t 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 +408,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 +752,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 + 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 + { + istringstream_t 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 +784,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);