Mercurial > code
diff C++/Parser.h @ 180:2bcdee0fe8d4
Update parser
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 26 Sep 2013 19:39:24 +0200 |
parents | 3648e9e6935b |
children | 600754c27c88 |
line wrap: on
line diff
--- a/C++/Parser.h Sun Sep 22 19:57:59 2013 +0200 +++ b/C++/Parser.h Thu Sep 26 19:39:24 2013 +0200 @@ -21,6 +21,7 @@ #include <cstdlib> #include <exception> +#include <functional> #include <iostream> #include <string> #include <vector> @@ -35,7 +36,7 @@ public: NotFoundException(const std::string &key) - :m_key(key) + : m_key(key) { } @@ -66,41 +67,34 @@ * options are allowed (default behavior), the root * section is "". */ -struct Section +class Section { +private: + const std::string findOption(const std::string &name) const; + +public: std::string m_name; /*! name of section */ std::vector<Option> m_options; /*! list of options inside */ bool m_allowed; /*! is authorized to push */ + /** + * Default constructor. + */ Section(); - ~Section(); - - /** - * Copy constructor - */ - Section(const Section &s); /** * Get the section name * * @return the section name */ - const std::string & getName() const; - - /** - * Search an option value. - * - * @param name the option name - * @return the value or "" if not found - */ - const std::string findOption(const std::string &name) const; + const std::string &getName() const; /** * Get all options from that section. * * @return the list of options */ - const std::vector<Option> & getOptions() const; + const std::vector<Option> &getOptions() const; /** * Tells if that section has the specified option name. @@ -117,7 +111,7 @@ * @return the value if found */ template <typename T> - T getOption(const std::string &name) const; + T getValue(const std::string &name) const; /** * Requires an option, this works like getOption except @@ -125,16 +119,16 @@ * thrown. * * @param name the name + * @return the value * @throw NotFoundException if not found - * @return the value */ template <typename T> - T requireOption(const std::string &name) const + T requireValue(const std::string &name) const { if (!hasOption(name)) throw NotFoundException(name); - return getOption<T>(name); + return getValue<T>(name); } friend std::ostream & operator<<(std::ostream & stream, const Section §ion) @@ -163,6 +157,8 @@ DisableVerbosity = 4 /*! be verbose by method */ }; + typedef std::function<void (const Section &)> FindFunc; + private: std::vector<Section> m_sections; /*! list of sections found */ std::string m_error; /*! if an error occured */ @@ -214,24 +210,23 @@ * * @return the error message */ - const std::string & getError() const; + const std::string &getError() const; /** * Get all sections found * * @return all sections */ - const std::vector<Section> & getSections() const; + const std::vector<Section> &getSections() const; /** - * Get a list of sections for config which multiple - * definitions are allowed. This does a full copy of sections - * and options. + * Find all sections matching the name. * * @param name the sections name + * @param func the function * @return a list of section with the options */ - std::vector<Section> findSections(const std::string &name) const; + void findSections(const std::string &name, FindFunc func) const; /** * Tell if a section is existing. @@ -245,18 +240,19 @@ * * @param name the section name * @return a section + * @throw NotFoundException if not found */ - Section getSection(const std::string &name) const; + const Section &getSection(const std::string &name) const; /** * Same as getSection except that throws an exception if * the section is not found. * * @param name the section name + * @return the section * @throw NotFoundException if not found - * @return the section */ - Section requireSection(const std::string &name) const; + const Section &requireSection(const std::string &name) const; /** * Logging function, used only if DisableVerbosity is not set. The @@ -294,7 +290,7 @@ */ virtual void dumpOption(const Option &option); - friend std::ostream & operator<<(std::ostream & stream, const Parser &parser) + friend std::ostream &operator<<(std::ostream & stream, const Parser &parser) { for (auto s : parser.m_sections) stream << s;;