Add explicit boolean type for store as 'true' or 'false'

This commit is contained in:
halx99 2021-09-29 11:21:31 +08:00
parent 15697a7b86
commit 3a975d2f8e
4 changed files with 34 additions and 22 deletions

View File

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

View File

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

View File

@ -30,8 +30,8 @@ TEST_XML(dom_attr_assign, "<node/>")
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("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\"/>"));
}
@ -67,8 +67,8 @@ TEST_XML(dom_attr_set_value, "<node/>")
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("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\"/>"));
}

View File

@ -270,7 +270,7 @@ TEST_XML(dom_text_assign, "<node/>")
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("<node><text1>v1</text1><text2>-2147483647</text2><text3>-2147483648</text3><text4>4294967295</text4><text5>4294967294</text5><text6>0.5</text6><text7>0.25</text7><text8>true</text8></node>"));
@ -297,8 +297,8 @@ TEST_XML(dom_text_set_value, "<node/>")
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("<node><text1>v1</text1><text2>-2147483647</text2><text3>-2147483648</text3><text4>4294967295</text4><text5>4294967294</text5><text6>0.5</text6><text7>0.25</text7><text8>true</text8></node>"));
}