From bad91eb3f5de502af2ff20a1e11723341d528b62 Mon Sep 17 00:00:00 2001 From: Oliver Sander <sander@dune-project.org> Date: Tue, 28 Aug 2007 12:22:27 +0000 Subject: [PATCH] new method get() which does not take a default parameter and throws an exception if the key is not found in the parameter set [[Imported from SVN: r4980]] --- common/configparser.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ common/configparser.hh | 10 ++++++++++ 2 files changed, 52 insertions(+) diff --git a/common/configparser.cc b/common/configparser.cc index ece3ef9b4..9b885c2cb 100644 --- a/common/configparser.cc +++ b/common/configparser.cc @@ -229,6 +229,48 @@ bool ConfigParser::get(const string& key, bool defaultValue) return (atoi(ret.c_str()) !=0 ); } +// This namespace here is needed to make the code compile... +namespace Dune { + + template<> + string ConfigParser::get<string>(const string& key) + { + if (hasKey(key)) + return (*this)[key]; + + DUNE_THROW(RangeError, "Key '" << key << "' not found in parameter file!"); + } + + template<> + int ConfigParser::get<int>(const string& key) + { + if (hasKey(key)) + return std::atoi((*this)[key].c_str()); + + DUNE_THROW(RangeError, "Key '" << key << "' not found in parameter file!"); + } + + template<> + double ConfigParser::get<double>(const string& key) + { + if (hasKey(key)) + return std::atof((*this)[key].c_str()); + + DUNE_THROW(RangeError, "Key '" << key << "' not found in parameter file!"); + } + + template<> + bool ConfigParser::get<bool>(const string& key) + { + if (hasKey(key)) + return (std::atoi((*this)[key].c_str()) !=0 ); + + DUNE_THROW(RangeError, "Key '" << key << "' not found in parameter file!"); + } + + +} // end namespace Dune + string ConfigParser::trim(string s) { int i = 0; diff --git a/common/configparser.hh b/common/configparser.hh index 092b7e478..c1736b6c9 100644 --- a/common/configparser.hh +++ b/common/configparser.hh @@ -183,6 +183,16 @@ namespace Dune { */ bool get(const std::string& key, bool defaultValue); + /** \brief Get value + * + * \param T Type of the value + * \param key Key name + * \throws RangeError if key does not exist + * \throws NotImplemented Type is not supported + * \return value as T + */ + template <class T> + T get(const std::string& key); /** \brief get value keys * -- GitLab