diff --git a/eo/src/eoExceptions.h b/eo/src/eoExceptions.h index d38b6cb1..4d56ddb9 100644 --- a/eo/src/eoExceptions.h +++ b/eo/src/eoExceptions.h @@ -103,4 +103,30 @@ public: private: std::string _name; }; + +/*! + * An error that signals a bad parameter type + * + * Thrown by eoParser::valueOf + * + * @ingroup Parameters + */ +class eoWrongParamTypeException : public std::exception +{ +public: + eoWrongParamTypeException(std::string name) : _name(name){} + + virtual const char* what() const throw() + { + std::ostringstream ss; + ss << "You asked for the parameter " << _name << " but it has not been declared under this type"; + return ss.str().c_str(); + } + + ~eoWrongParamTypeException() throw() {} + +private: + std::string _name; +}; + #endif // __eoExceptions_h__ diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index 7489cf65..cc306bf7 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -40,7 +40,6 @@ Authors: #include #include #include -#include using namespace std; diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index b33f956e..be680ab0 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -32,6 +32,7 @@ Authors: #include "eoParam.h" #include "eoObject.h" #include "eoPersistent.h" +#include "eoExceptions.h" /** Parameter saving and loading @@ -191,17 +192,24 @@ public: * * Remember to specify the expected return type with a templated call: * unsigned int popSize = eoparser.value("popSize"); + * + * If the template type is not the good one, an eoWrongParamTypeException is raised. */ template - ValueType value(const std::string& _name) const + ValueType valueOf(const std::string& _name) const { eoParam* param = getParam(_name); - eoValueParam* vparam( - dynamic_cast< eoValueParam* >(param) - ); + // Note: eoParam is the polymorphic base class of eoValueParam, thus we can do a dynamix cast + eoValueParam* vparam = dynamic_cast< eoValueParam* >(param); - return vparam->value(); + if( vparam == NULL ) { + // if the dynamic cast has failed, chances are that ValueType + // is not the same than the one used at declaration. + throw eoWrongParamTypeException( _name ); + } else { + return vparam->value(); + } }