diff --git a/common/configparser.cc b/common/configparser.cc index ece3ef9b49adc59e66a420f574020932e9c7ba73..9b885c2cb53317e1526e00ba92fd405013e0c76c 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 092b7e47860fa06151900c7f9ac936a05c043234..c1736b6c91ee59a7622baafaa13bfd91990d9ffc 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 *