From c2e7b1f57941cb34cb7e3d71dc040fad53a64e3e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 31 Jan 2017 16:43:45 +0000 Subject: Derive NodeMetaRef from MetaDataRef --- src/metadata.cpp | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'src/metadata.cpp') diff --git a/src/metadata.cpp b/src/metadata.cpp index 8e04aa2d3..96453d710 100644 --- a/src/metadata.cpp +++ b/src/metadata.cpp @@ -37,12 +37,39 @@ bool Metadata::empty() const return m_stringvars.size() == 0; } -std::string Metadata::getString(const std::string &name, - u16 recursion) const +size_t Metadata::size() const +{ + return m_stringvars.size(); +} + +bool Metadata::contains(const std::string &name) const +{ + return m_stringvars.find(name) != m_stringvars.end(); +} + +bool Metadata::operator==(const Metadata &other) const +{ + if (size() != other.size()) + return false; + + for (StringMap::const_iterator it = m_stringvars.begin(); + it != m_stringvars.end(); ++it) { + if (!other.contains(it->first) || + other.getString(it->first) != it->second) + return false; + } + + return true; +} + +const std::string &Metadata::getString(const std::string &name, + u16 recursion) const { StringMap::const_iterator it = m_stringvars.find(name); - if (it == m_stringvars.end()) - return ""; + if (it == m_stringvars.end()) { + static const std::string empty_string = std::string(""); + return empty_string; + } return resolveString(it->second, recursion); } @@ -56,14 +83,13 @@ void Metadata::setString(const std::string &name, const std::string &var) } } -std::string Metadata::resolveString(const std::string &str, - u16 recursion) const +const std::string &Metadata::resolveString(const std::string &str, + u16 recursion) const { - if (recursion > 1) { - return str; - } - if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') { + if (recursion <= 1 && + str.substr(0, 2) == "${" && str[str.length() - 1] == '}') { return getString(str.substr(2, str.length() - 3), recursion + 1); + } else { + return str; } - return str; } -- cgit v1.2.3