New config_attribute_value::operator== implementation

This commit is contained in:
Charles Dang 2024-07-30 09:26:02 -04:00
parent 1ba0168eb3
commit e36b675ec4
2 changed files with 15 additions and 36 deletions

View file

@ -418,23 +418,6 @@ bool config_attribute_value::operator==(const config_attribute_value& other) con
return utils::visit(equality_visitor(), value_, other.value_);
}
/**
* Checks for equality of the attribute values when viewed as strings.
* Exception: Boolean synonyms can be equal ("yes" == "true").
* Note: Blanks have no string representation, so do not equal "" (an empty string).
* Also note that translatable string are never equal to non translatable strings.
*/
bool config_attribute_value::equals(const std::string& str) const
{
config_attribute_value v;
v = str;
return *this == v;
// if c["a"] = "1" then this solution would have resulted in c["a"] == "1" being false
// because a["a"] is '1' and not '"1"'.
// return boost::apply_visitor(std::bind( equality_visitor(), std::placeholders::_1, std::cref(str) ), value_);
// that's why we don't use it.
}
std::ostream& operator<<(std::ostream& os, const config_attribute_value& v)
{
// Simple implementation, but defined out-of-line because of the templating

View file

@ -162,7 +162,6 @@ public:
/** Tests for an attribute that either was never set or was set to "". */
bool empty() const;
// Comparisons:
bool operator==(const config_attribute_value &other) const;
bool operator!=(const config_attribute_value &other) const
@ -170,39 +169,36 @@ public:
return !operator==(other);
}
bool equals(const std::string& str) const;
// These function prevent t_string creation in case of c["a"] == "b" comparisons.
// The templates are needed to prevent using these function in case of c["a"] == 0 comparisons.
template<typename T>
std::enable_if_t<std::is_same_v<const std::string, std::add_const_t<T>>, bool>
friend operator==(const config_attribute_value &val, const T &str)
bool operator==(bool comp) const
{
return val.equals(str);
const bool has_bool =
utils::holds_alternative<yes_no>(value_) ||
utils::holds_alternative<true_false>(value_);
return has_bool && to_bool() == comp;
}
template<typename T>
std::enable_if_t<std::is_same_v<const char*, T>, bool>
friend operator==(const config_attribute_value& val, T str)
bool operator==(const T& comp) const
{
return val.equals(std::string(str));
}
template<typename T>
bool friend operator==(const T& str, const config_attribute_value& val)
{
return val == str;
if constexpr(std::is_convertible_v<T, std::string>) {
config_attribute_value v;
v = comp;
return *this == v;
} else {
return utils::holds_alternative<T>(value_) && T(*this) == comp;
}
}
template<typename T>
bool friend operator!=(const config_attribute_value& val, const T& str)
{
return !(val == str);
return !val.operator==(str);
}
template<typename T>
bool friend operator!=(const T &str, const config_attribute_value& val)
{
return !(val == str);
return !val.operator==(str);
}
// Streaming: