From 62f5049a10791cef797b718d4aa45a4a049f15d5 Mon Sep 17 00:00:00 2001 From: Jihun Ahn Date: Sun, 15 Mar 2020 00:49:04 +0900 Subject: [PATCH] Modify to override operator= to set values as datatype Currently, set_value() can be specified only as string data. However, if use STL, it can to specify various types of data more easily using std::to_string (or to_wstring). This commit modifies to override the assignment operator to make it more usefully to set values when using STL. Signed-off-by: Jihun Ahn --- src/pugixml.cpp | 10 ++++++++++ src/pugixml.hpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index e26cf37..7504564 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5691,6 +5691,16 @@ namespace pugi return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs)); } + PUGI__FN bool xml_node::set_value(const bool rhs) + { + #ifdef PUGIXML_WCHAR_MODE + const char_t* text = rhs ? L"true" : L"false"; + #else + const char_t* text = rhs ? "true" : "false"; + #endif + return set_value(text); + } + PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_) { if (!impl::allow_insert_attribute(type())) return xml_attribute(); diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 46c9066..f1fa829 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -38,6 +38,8 @@ # include #endif +#include + // Macro for deprecated features #ifndef PUGIXML_DEPRECATED # if defined(__GNUC__) @@ -491,6 +493,21 @@ namespace pugi xml_node operator[](const char_t* name_); xml_node& operator=(const char_t* rhs); + #ifndef PUGIXML_NO_STL + template xml_node& operator=(const T& rhs) + { + if (std::is_arithmetic::value) + { + xml_node node = append_child(node_pcdata); + + node.set_value(rhs); + } + + return *this; + } + + #endif + // Check if node is empty. bool empty() const; @@ -543,6 +560,22 @@ namespace pugi // Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value) bool set_name(const char_t* rhs); bool set_value(const char_t* rhs); + bool set_value(const bool rhs); + + #ifndef PUGIXML_NO_STL + template bool set_value(const T& rhs) + { + if (!std::is_arithmetic::value) + return false; + +# ifdef PUGIXML_WCHAR_MODE + const string_t& _str = std::to_wstring(rhs); +# else + const string_t& _str = std::to_string(rhs); +# endif + return set_value(_str.c_str()); + } + #endif // Add attribute with specified name. Returns added attribute, or empty attribute on errors. xml_attribute append_attribute(const char_t* name);