Added ValueAsBool(), ValueAsBoolWithDefault(), and ValueAsUnsignedWithDefault()

This commit is contained in:
Bart Trzynadlowski 2016-08-10 03:27:01 +00:00
parent f4311a0ab2
commit 718c237063
2 changed files with 28 additions and 1 deletions

View file

@ -80,6 +80,23 @@ namespace Util
{ {
Node Node::s_empty_node; Node Node::s_empty_node;
bool Node::ValueAsBool() const
{
const char *value = m_value.c_str();
if (!value || !stricmp(value, "false") || !stricmp(value, "off") || !stricmp(value, "no"))
return false;
else if (!stricmp(value, "true") || !stricmp(value, "on") || !stricmp(value, "yes"))
return true;
return bool(ValueAsUnsigned());
}
bool Node::ValueAsBoolWithDefault(bool default_value) const
{
if (this == &s_empty_node)
return default_value;
return ValueAsBool();
}
uint64_t Node::ValueAsUnsigned() const uint64_t Node::ValueAsUnsigned() const
{ {
if (m_value.find("0x") == 0 || m_value.find("0X") == 0) if (m_value.find("0x") == 0 || m_value.find("0X") == 0)
@ -87,6 +104,13 @@ namespace Util
return strtoull(m_value.c_str(), 0, 10); return strtoull(m_value.c_str(), 0, 10);
} }
uint64_t Node::ValueAsUnsignedWithDefault(uint64_t default_value) const
{
if (this == &s_empty_node)
return default_value;
return ValueAsUnsigned();
}
const Node &Node::operator[](const std::string &path) const const Node &Node::operator[](const std::string &path) const
{ {
const Node *e = this; const Node *e = this;

View file

@ -161,7 +161,10 @@ namespace Util
return m_value; return m_value;
} }
bool ValueAsBool() const;
bool ValueAsBoolWithDefault(bool default_value) const;
uint64_t ValueAsUnsigned() const; uint64_t ValueAsUnsigned() const;
uint64_t ValueAsUnsignedWithDefault(uint64_t default_value) const;
inline void SetValue(const std::string &value) inline void SetValue(const std::string &value)
{ {