evals that can throw exceptions

This commit is contained in:
Johann Dreo 2010-09-16 11:06:04 +02:00
commit d26a9d8767
4 changed files with 129 additions and 1 deletions

View file

@ -75,6 +75,8 @@
// Evaluation functions (all include eoEvalFunc.h)
#include <eoPopEvalFunc.h>
#include <eoEvalFuncPtr.h>
#include <eoEvalCounterThrowException.h>
#include <eoEvalTimeThrowException.h>
// Continuators - all include eoContinue.h
#include <eoCombinedContinue.h>

View file

@ -26,7 +26,7 @@ Caner Candan <caner.candan@thalesgroup.com>
#ifndef __eoEvalCounterThrowException_h__
#define __eoEvalCounterThrowException_h__
#include <eoEvalFunc.h>
#include <eoEvalFuncCounter.h>
#include <utils/eoParam.h>
#include <eoExceptions.h>

View file

@ -0,0 +1,47 @@
/*
(c) Thales group, 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 2 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: http://eodev.sourceforge.net
Authors:
Johann Dréo <johann.dreo@thalesgroup.com>
*/
#include <ctime>
#include <eoExceptions.h>
template< class EOT >
class eoEvalTimeThrowException : public eoEvalFuncCounter< EOT >
{
public:
eoEvalTimeThrowException( eoEvalFunc<EOT> & func, time_t max ) : _max(max), _start( std::time(NULL) ), eoEvalFuncCounter<EOT>( func, "Eval.") {}
virtual bool operator() (const eoPop < EOT > & _pop)
{
time_t elapsed = static_cast<time_t>( std::difftime( std::time(NULL) , _start ) );
if( elapsed >= _max ) {
throw eoMaxTimeException(elapsed);
}
}
protected:
time_t _max;
time_t _start;
};

79
eo/src/eoExceptions.h Normal file
View file

@ -0,0 +1,79 @@
/*
(c) Thales group, 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 2 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: http://eodev.sourceforge.net
Authors:
Johann Dréo <johann.dreo@thalesgroup.com>
*/
#ifndef __eoExceptions_h__
#define __eoExceptions_h__
#include <ctime>
#include <stdexcept>
class eoMaxException : public std::exception {};
/*!
An error that signals that a maximum elapsed time has been reached.
Thrown by @see eoEvalTimeThrowException
*/
class eoMaxTimeException : public eoMaxException
{
public:
eoMaxTimeException( time_t elapsed) : _elapsed(elapsed) {}
virtual const char* what() const throw()
{
std::ostringstream ss;
ss << "STOP in eoMaxTimeException: the maximum number of seconds has been reached (" << _elapsed << ").";
return ss.str().c_str();
}
private:
time_t _elapsed;
};
/*!
An error that signals that a maximum number of evaluations has been reached.
Thrown by @see eoEvalEvalThrowException
*/
class eoMaxEvalException : public eoMaxException
{
public:
eoMaxEvalException(unsigned long threshold) : _threshold(threshold){}
virtual const char* what() const throw()
{
std::ostringstream ss;
ss << "STOP in eoMaxEvalException: the maximum number of evaluation has been reached (" << _threshold << ").";
return ss.str().c_str();
}
private:
unsigned long _threshold;
};
#endif // __eoExceptions_h__