From d26a9d87673bda9a17075493ac39a69732bffda6 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 16 Sep 2010 11:06:04 +0200 Subject: [PATCH] evals that can throw exceptions --- eo/src/eo | 2 + eo/src/eoEvalCounterThrowException.h | 2 +- eo/src/eoEvalTimeThrowException.h | 47 +++++++++++++++++ eo/src/eoExceptions.h | 79 ++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 eo/src/eoEvalTimeThrowException.h create mode 100644 eo/src/eoExceptions.h diff --git a/eo/src/eo b/eo/src/eo index cb05a898..61868d16 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -75,6 +75,8 @@ // Evaluation functions (all include eoEvalFunc.h) #include #include +#include +#include // Continuators - all include eoContinue.h #include diff --git a/eo/src/eoEvalCounterThrowException.h b/eo/src/eoEvalCounterThrowException.h index f2230c85..bf19e73e 100644 --- a/eo/src/eoEvalCounterThrowException.h +++ b/eo/src/eoEvalCounterThrowException.h @@ -26,7 +26,7 @@ Caner Candan #ifndef __eoEvalCounterThrowException_h__ #define __eoEvalCounterThrowException_h__ -#include +#include #include #include diff --git a/eo/src/eoEvalTimeThrowException.h b/eo/src/eoEvalTimeThrowException.h new file mode 100644 index 00000000..b1ac9662 --- /dev/null +++ b/eo/src/eoEvalTimeThrowException.h @@ -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 +*/ + +#include + +#include + +template< class EOT > +class eoEvalTimeThrowException : public eoEvalFuncCounter< EOT > +{ +public: + eoEvalTimeThrowException( eoEvalFunc & func, time_t max ) : _max(max), _start( std::time(NULL) ), eoEvalFuncCounter( func, "Eval.") {} + + virtual bool operator() (const eoPop < EOT > & _pop) + { + time_t elapsed = static_cast( std::difftime( std::time(NULL) , _start ) ); + + if( elapsed >= _max ) { + throw eoMaxTimeException(elapsed); + } + } + +protected: + time_t _max; + + time_t _start; +}; diff --git a/eo/src/eoExceptions.h b/eo/src/eoExceptions.h new file mode 100644 index 00000000..7a72f4c5 --- /dev/null +++ b/eo/src/eoExceptions.h @@ -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 +*/ + +#ifndef __eoExceptions_h__ +#define __eoExceptions_h__ + +#include +#include + +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__