Skip to content

Commit

Permalink
add operators == (attribute_value, std::string)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgtdf committed Jun 27, 2014
1 parent 2b42441 commit 4d73031
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/config.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/config.hpp
Expand Up @@ -280,6 +280,16 @@ class config
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);
Expand Down

0 comments on commit 4d73031

Please sign in to comment.