diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 374c8eb..e599485 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5258,7 +5258,7 @@ namespace pugi return _attr; } - PUGI__FN xml_attribute& xml_attribute::operator=(const char_t* rhs) + PUGI__FN xml_attribute& xml_attribute::operator=(string_view rhs) { set_value(rhs); return *this; @@ -5300,7 +5300,7 @@ namespace pugi return *this; } - PUGI__FN xml_attribute& xml_attribute::operator=(bool rhs) + PUGI__FN xml_attribute& xml_attribute::operator=(boolean rhs) { set_value(rhs); return *this; @@ -5390,7 +5390,7 @@ namespace pugi return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, precision); } - PUGI__FN bool xml_attribute::set_value(bool rhs) + PUGI__FN bool xml_attribute::set_value(boolean rhs) { if (!_attr) return false; @@ -6537,11 +6537,11 @@ namespace pugi } #endif - PUGI__FN bool xml_text::set(const char_t* rhs) + PUGI__FN bool xml_text::set(string_view rhs) { xml_node_struct* dn = _data_new(); - return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs)) : false; + return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs.data(), rhs.length()) : false; } PUGI__FN bool xml_text::set(int rhs) @@ -6600,7 +6600,7 @@ namespace pugi return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, precision) : false; } - PUGI__FN bool xml_text::set(bool rhs) + PUGI__FN bool xml_text::set(boolean rhs) { xml_node_struct* dn = _data_new(); @@ -6623,7 +6623,7 @@ namespace pugi } #endif - PUGI__FN xml_text& xml_text::operator=(const char_t* rhs) + PUGI__FN xml_text& xml_text::operator=(string_view rhs) { set(rhs); return *this; @@ -6665,7 +6665,7 @@ namespace pugi return *this; } - PUGI__FN xml_text& xml_text::operator=(bool rhs) + PUGI__FN xml_text& xml_text::operator=(boolean rhs) { set(rhs); return *this; diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 2213336..a0b14a3 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -291,6 +291,18 @@ namespace pugi { #endif } // namespace pugi +// explicit boolean type +namespace pugi { + struct boolean { + boolean() : value(false) {} + explicit boolean(bool bval) : value(bval) {} + bool value; + operator bool() const { return value; } + }; + static const boolean true_value{ true }; + static const boolean false_value{ false }; +} + // The PugiXML namespace namespace pugi { @@ -578,7 +590,7 @@ namespace pugi bool set_value(double rhs, int precision); bool set_value(float rhs); bool set_value(float rhs, int precision); - bool set_value(bool rhs); + bool set_value(boolean rhs); #ifdef PUGIXML_HAS_LONG_LONG bool set_value(long long rhs); @@ -586,14 +598,14 @@ namespace pugi #endif // Set attribute value (equivalent to set_value without error checking) - xml_attribute& operator=(const char_t* rhs); + xml_attribute& operator=(string_view rhs); xml_attribute& operator=(int rhs); xml_attribute& operator=(unsigned int rhs); xml_attribute& operator=(long rhs); xml_attribute& operator=(unsigned long rhs); xml_attribute& operator=(double rhs); xml_attribute& operator=(float rhs); - xml_attribute& operator=(bool rhs); + xml_attribute& operator=(boolean rhs); #ifdef PUGIXML_HAS_LONG_LONG xml_attribute& operator=(long long rhs); @@ -926,7 +938,7 @@ namespace pugi bool as_bool(bool def = false) const; // Set text (returns false if object is empty or there is not enough memory) - bool set(const char_t* rhs); + bool set(string_view rhs); // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") bool set(int rhs); @@ -937,7 +949,7 @@ namespace pugi bool set(double rhs, int precision); bool set(float rhs); bool set(float rhs, int precision); - bool set(bool rhs); + bool set(boolean rhs); #ifdef PUGIXML_HAS_LONG_LONG bool set(long long rhs); @@ -945,14 +957,14 @@ namespace pugi #endif // Set text (equivalent to set without error checking) - xml_text& operator=(const char_t* rhs); + xml_text& operator=(string_view rhs); xml_text& operator=(int rhs); xml_text& operator=(unsigned int rhs); xml_text& operator=(long rhs); xml_text& operator=(unsigned long rhs); xml_text& operator=(double rhs); xml_text& operator=(float rhs); - xml_text& operator=(bool rhs); + xml_text& operator=(boolean rhs); #ifdef PUGIXML_HAS_LONG_LONG xml_text& operator=(long long rhs); diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp index ff9d440..88554e2 100644 --- a/tests/test_dom_modify.cpp +++ b/tests/test_dom_modify.cpp @@ -30,8 +30,8 @@ TEST_XML(dom_attr_assign, "") node.append_attribute(STR("attr7")) = 0.25f; xml_attribute() = 0.25f; - node.append_attribute(STR("attr8")) = true; - xml_attribute() = true; + node.append_attribute(STR("attr8")) = true_value; + xml_attribute() = true_value; CHECK_NODE(node, STR("")); } @@ -67,8 +67,8 @@ TEST_XML(dom_attr_set_value, "") CHECK(node.append_attribute(STR("attr7")).set_value(0.25f)); CHECK(!xml_attribute().set_value(0.25f)); - CHECK(node.append_attribute(STR("attr8")).set_value(true)); - CHECK(!xml_attribute().set_value(true)); + CHECK(node.append_attribute(STR("attr8")).set_value(true_value)); + CHECK(!xml_attribute().set_value(true_value)); CHECK_NODE(node, STR("")); } diff --git a/tests/test_dom_text.cpp b/tests/test_dom_text.cpp index c71e54e..aa131e6 100644 --- a/tests/test_dom_text.cpp +++ b/tests/test_dom_text.cpp @@ -270,7 +270,7 @@ TEST_XML(dom_text_assign, "") node.append_child(STR("text7")).text() = 0.25f; xml_text() = 0.25f; - node.append_child(STR("text8")).text() = true; + node.append_child(STR("text8")).text() = true_value; xml_text() = true; CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); @@ -297,8 +297,8 @@ TEST_XML(dom_text_set_value, "") CHECK(node.append_child(STR("text7")).text().set(0.25f)); CHECK(!xml_text().set(0.25f)); - CHECK(node.append_child(STR("text8")).text().set(true)); - CHECK(!xml_text().set(true)); + CHECK(node.append_child(STR("text8")).text().set(true_value)); + CHECK(!xml_text().set(true_value)); CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); }