add operators == (attribute_value, std::string)

This commit is contained in:
gfgtdf 2014-06-27 02:21:33 +02:00
parent 2b42441214
commit 4d73031680
2 changed files with 21 additions and 0 deletions

View File

@ -31,6 +31,7 @@
#include <deque>
#include <istream>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/get.hpp>
@ -390,6 +391,16 @@ bool config::attribute_value::operator==(const config::attribute_value &other) c
return boost::apply_visitor(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).
*/
bool config::attribute_value::equals(const std::string &str) const
{
return boost::apply_visitor(boost::bind( equality_visitor(), _1, boost::cref(str) ), value_);
}
std::ostream &operator<<(std::ostream &os, const config::attribute_value &v)
{
// Simple implementation, but defined out-of-line because of the templating

View File

@ -280,6 +280,16 @@ public:
bool operator==(const attribute_value &other) const;
bool operator!=(const attribute_value &other) const
{ return !operator==(other); }
// We don't want strange conversions to t_string when doing like (c["a"] == "b")
bool equals(const std::string& str) const;
friend bool operator==(const attribute_value &val, const std::string &str)
{ return val.equals(str); }
friend bool operator==(const std::string &str, const attribute_value &val)
{ return val.equals(str); }
friend bool operator==(const attribute_value &val, const char* str)
{ return val.equals(std::string(str)); }
friend bool operator==(const char* str, const attribute_value &val)
{ return val.equals(std::string(str)); }
// Streaming:
friend std::ostream& operator<<(std::ostream &os, const attribute_value &v);