Mercurial > code
changeset 205:9f22ce5f1b39
Parser: rename getValue, add operator==
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 16 Feb 2014 12:39:03 +0100 |
parents | 7086e93bc4ea |
children | b54c83ed71ca |
files | C++/Parser.cpp C++/Parser.h |
diffstat | 2 files changed, 23 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Parser.cpp Sun Feb 16 12:38:49 2014 +0100 +++ b/C++/Parser.cpp Sun Feb 16 12:39:03 2014 +0100 @@ -1,7 +1,7 @@ /* * Parser.h -- config file parser * - * Copyright (c) 2011, 2012, 2013 David Demelier <markand@malikania.fr> + * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -69,6 +69,11 @@ return m_options.end(); } +bool operator==(const Section &s1, const Section &s2) +{ + return s1.m_name == s2.m_name && s1.m_options == s2.m_options; +} + /* -------------------------------------------------------- * Parser private members * -------------------------------------------------------- */ @@ -210,7 +215,7 @@ file.open(m_path.c_str()); if (!file.is_open()) - throw std::runtime_error(m_path + std::string(std::strerror(errno))); + throw std::runtime_error(m_path + ": " + std::string(std::strerror(errno))); while (std::getline(file, line)) readLine(lineno++, line); @@ -288,3 +293,11 @@ { std::cout << "line " << number << ": " << message << std::endl; } + +bool operator==(const Parser &p1, const Parser &p2) +{ + return p1.m_sections == p2.m_sections && + p1.m_path == p2.m_path && + p1.m_tuning == p2.m_tuning && + p1.m_commentChar == p2.m_commentChar; +}
--- a/C++/Parser.h Sun Feb 16 12:38:49 2014 +0100 +++ b/C++/Parser.h Sun Feb 16 12:39:03 2014 +0100 @@ -1,7 +1,7 @@ /* * Parser.h -- config file parser * - * Copyright (c) 2011, 2012, 2013 David Demelier <markand@malikania.fr> + * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -46,7 +46,6 @@ Map m_options; /*! list of options inside */ bool m_allowed; /*! is authorized to push */ - const std::string findOption(const std::string &name) const; public: template <typename T> struct Converter { @@ -115,10 +114,10 @@ * @return the value if found */ template <typename T> - T getValue(const std::string &name) const + T getOption(const std::string &name) const { try { - return requireValue<T>(name); + return requireOption<T>(name); } catch (...) { // Catch any conversion error. } @@ -137,12 +136,14 @@ * @throw std::invalid_argument on conversion failures */ template <typename T> - T requireValue(const std::string &name) const + T requireOption(const std::string &name) const { static_assert(Converter<T>::supported, "invalid type requested"); return Converter<T>::convert(m_options.at(name)); } + + friend bool operator==(const Section &s1, const Section &s2); }; template <> @@ -327,6 +328,8 @@ * @param message the message */ virtual void log(int number, const std::string §ion, const std::string &message); + + friend bool operator==(const Parser &p1, const Parser &p2); }; #endif // !_PARSER_H_