added an new exception to handle cases where a wrong template is given to eoParser::valueOf
This commit is contained in:
parent
1bc2c5cc46
commit
8c121bdafb
3 changed files with 39 additions and 6 deletions
|
|
@ -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__
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ Authors:
|
|||
#include <utils/compatibility.h>
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoLogger.h>
|
||||
#include <eoExceptions.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
|
|
|||
|
|
@ -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<unsigned int>("popSize");
|
||||
*
|
||||
* If the template type is not the good one, an eoWrongParamTypeException is raised.
|
||||
*/
|
||||
template<class ValueType>
|
||||
ValueType value(const std::string& _name) const
|
||||
ValueType valueOf(const std::string& _name) const
|
||||
{
|
||||
eoParam* param = getParam(_name);
|
||||
|
||||
eoValueParam<ValueType>* vparam(
|
||||
dynamic_cast< eoValueParam<ValueType>* >(param)
|
||||
);
|
||||
// Note: eoParam is the polymorphic base class of eoValueParam, thus we can do a dynamix cast
|
||||
eoValueParam<ValueType>* vparam = dynamic_cast< eoValueParam<ValueType>* >(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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Reference in a new issue