Mercurial > code
changeset 455:03778e85f455
Ini: add find functions
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 03 Nov 2015 10:58:44 +0100 |
parents | 781494d9c807 |
children | a1cec0345d76 |
files | C++/modules/Ini/Ini.h |
diffstat | 1 files changed, 61 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Ini/Ini.h Tue Nov 03 09:37:19 2015 +0100 +++ b/C++/modules/Ini/Ini.h Tue Nov 03 10:58:44 2015 +0100 @@ -55,10 +55,10 @@ * @param c the column * @param m the message */ - inline Error(int line, int column, std::string message) noexcept - : m_line{line} - , m_column{column} - , m_message{std::move(message)} + inline Error(int l, int c, std::string m) noexcept + : m_line{l} + , m_column{c} + , m_message{std::move(m)} { } @@ -286,20 +286,6 @@ private: std::string m_key; - template <typename T> - T find(const std::string &key) const - { - auto it = std::find_if(begin(), end(), [&] (const Option &o) { - return o.key() == key; - }); - - if (it == end()) { - throw std::out_of_range{"option " + key + " not found"}; - } - - return const_cast<T>(*it); - } - public: /** * Construct a section with its name. @@ -329,7 +315,7 @@ */ inline bool contains(const std::string &key) const noexcept { - return std::find_if(begin(), end(), [&] (const auto &opt) { return opt.key() == key; }) != end(); + return find(key) != end(); } /** @@ -341,7 +327,7 @@ */ inline Option &operator[](const std::string &key) { - return find<Option &>(key); + return *find(key); } /** @@ -353,7 +339,33 @@ */ inline const Option &operator[](const std::string &key) const { - return find<const Option &>(key); + return *find(key); + } + + /** + * Find an option by key and return an iterator. + * + * @param key the key + * @return the iterator or end() if not found + */ + inline iterator find(const std::string &key) noexcept + { + return std::find_if(begin(), end(), [&] (const auto &o) { + return o.key() == key; + }); + } + + /** + * Find an option by key and return an iterator. + * + * @param key the key + * @return the iterator or end() if not found + */ + inline const_iterator find(const std::string &key) const noexcept + { + return std::find_if(cbegin(), cend(), [&] (const auto &o) { + return o.key() == key; + }); } /** @@ -395,20 +407,6 @@ private: std::string m_path; - template <typename T> - T find(const std::string &key) const - { - auto it = std::find_if(begin(), end(), [&] (const Section &s) { - return s.key() == key; - }); - - if (it == end()) { - throw std::out_of_range{"section " + key + " not found"}; - } - - return const_cast<T>(*it); - } - public: /** * Analyze a file and extract tokens. If the function succeeds, that does not mean the content is valid, @@ -484,7 +482,7 @@ */ inline Section &operator[](const std::string &key) { - return find<Section &>(key); + return *find(key); } /** @@ -496,7 +494,33 @@ */ inline const Section &operator[](const std::string &key) const { - return find<Section &>(key); + return *find(key); + } + + /** + * Find a section by key and return an iterator. + * + * @param key the key + * @return the iterator or end() if not found + */ + inline iterator find(const std::string &key) noexcept + { + return std::find_if(begin(), end(), [&] (const auto &o) { + return o.key() == key; + }); + } + + /** + * Find a section by key and return an iterator. + * + * @param key the key + * @return the iterator or end() if not found + */ + inline const_iterator find(const std::string &key) const noexcept + { + return std::find_if(cbegin(), cend(), [&] (const auto &o) { + return o.key() == key; + }); } /**