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 <jhun.ahnn@gmail.com>
This commit is contained in:
Jihun Ahn 2020-03-15 00:49:04 +09:00
parent 6d646262c6
commit 62f5049a10
2 changed files with 43 additions and 0 deletions

View File

@ -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();

View File

@ -38,6 +38,8 @@
# include <string>
#endif
#include <type_traits>
// 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 <typename T> xml_node& operator=(const T& rhs)
{
if (std::is_arithmetic<T>::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 <typename T> bool set_value(const T& rhs)
{
if (!std::is_arithmetic<T>::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);