Mercurial > code
changeset 456:a1cec0345d76
Ini: add some asserts because operator[] does not throw anymore
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 03 Nov 2015 11:34:46 +0100 |
parents | 03778e85f455 |
children | 060acd5945a3 |
files | C++/modules/Ini/Ini.cpp C++/modules/Ini/Ini.h |
diffstat | 2 files changed, 21 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Ini/Ini.cpp Tue Nov 03 10:58:44 2015 +0100 +++ b/C++/modules/Ini/Ini.cpp Tue Nov 03 11:34:46 2015 +0100 @@ -16,7 +16,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <cassert> #include <cctype> #include <cstring> #include <iostream> @@ -139,6 +138,9 @@ if (it == end) { throw Error{line, column, "section name expected after '[', got <EOF>"}; } + if (value.empty()) { + throw Error{line, column, "empty section name"}; + } /* Remove ']' */ ++ it;
--- a/C++/modules/Ini/Ini.h Tue Nov 03 10:58:44 2015 +0100 +++ b/C++/modules/Ini/Ini.h Tue Nov 03 11:34:46 2015 +0100 @@ -25,6 +25,7 @@ */ #include <algorithm> +#include <cassert> #include <exception> #include <stdexcept> #include <string> @@ -291,10 +292,12 @@ * Construct a section with its name. * * @param key the key + * @pre key must not be empty */ inline Section(std::string key) noexcept : m_key{std::move(key)} { + assert(!m_key.empty()); } /** @@ -323,22 +326,26 @@ * * @param key the key * @return the option - * @throw std::out_of_range if the key does not exist + * @pre contains(key) must return true */ inline Option &operator[](const std::string &key) { + assert(contains(key)); + return *find(key); } /** - * Access an option at the specified key. + * Overloaded function. * * @param key the key * @return the option - * @throw std::out_of_range if the key does not exist + * @pre contains(key) must return true */ inline const Option &operator[](const std::string &key) const { + assert(contains(key)); + return *find(key); } @@ -467,6 +474,7 @@ * Check if a document has a specific section. * * @param key the key + * @return true if the document contains the section */ inline bool contains(const std::string &key) const noexcept { @@ -478,22 +486,26 @@ * * @param key the key * @return the section - * @throw std::out_of_range if the key does not exist + * @pre contains(key) must return true */ inline Section &operator[](const std::string &key) { + assert(contains(key)); + return *find(key); } /** - * Access a section at the specified key. + * Overloaded function. * * @param key the key * @return the section - * @throw std::out_of_range if the key does not exist + * @pre contains(key) must return true */ inline const Section &operator[](const std::string &key) const { + assert(contains(key)); + return *find(key); }