Skip to content

Instantly share code, notes, and snippets.

@johnmcfarlane
Created November 2, 2015 22:59
Show Gist options
  • Save johnmcfarlane/194729e80ddf16224242 to your computer and use it in GitHub Desktop.
Save johnmcfarlane/194729e80ddf16224242 to your computer and use it in GitHub Desktop.
For yaml-cpp: `bool isEqual(YAML::Node const &lhs, YAML::Node const &rhs)`
bool isEqual(YAML::Node const &lhs, YAML::Node const &rhs) {
if (lhs == rhs) {
return true;
}
YAML::NodeType::value lhs_type = lhs.Type();
if (lhs_type != rhs.Type()) {
return false;
}
if (lhs.size() != rhs.size()) {
return false;
}
switch (lhs_type) {
case YAML::NodeType::Scalar: {
std::string const &lhs_as_string = lhs.as<std::string>();
std::string const &rhs_as_string = rhs.as<std::string>();
return lhs_as_string == rhs_as_string;
}
case YAML::NodeType::Sequence:
return lhs.size() == rhs.size()
&& std::equal(boost::begin(lhs), boost::end(lhs), boost::begin(rhs), isEqual);
case YAML::NodeType::Map:
BOOST_FOREACH(yaml_map_pair const &lhs_element, lhs) {
std::string key = lhs_element.first.as<std::string>();
if (!isEqual(lhs_element.second, rhs[key])) {
return false;
}
}
return true;
default:
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment