From b790bbc35bd2adf426a86f8acbd28a06ba9713f0 Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Tue, 31 Aug 2010 16:32:19 +0200 Subject: [PATCH] + eoEvalFuncCounterBounder.h: inherits of eoEvalFuncCounter in adding an exception throwing when a threshold of evaluation has been reached --- eo/src/eoEvalFuncCounterBounded.h | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 eo/src/eoEvalFuncCounterBounded.h diff --git a/eo/src/eoEvalFuncCounterBounded.h b/eo/src/eoEvalFuncCounterBounded.h new file mode 100644 index 000000000..7ba6c465c --- /dev/null +++ b/eo/src/eoEvalFuncCounterBounded.h @@ -0,0 +1,56 @@ +#ifndef eoEvalFuncCounterBounder_H +#define eoEvalFuncCounterBounder_H + +#include +#include + +class eoEvalFuncCounterBounderException : public std::exception +{ +public: + eoEvalFuncCounterBounderException(unsigned long threshold) : _threshold(threshold){} + + const char* what() const throw() + { + std::ostringstream ss; + ss << "STOP in eoEvalFuncCounterBounderException: the maximum number of evaluation has been reached (" << _threshold << ")."; + return ss.str().c_str(); + } + +private: + unsigned long _threshold; +}; + +/** +Counts the number of evaluations actually performed, thus checks first +if it has to evaluate.. etc. +*/ +template < typename EOT > +class eoEvalFuncCounterBounder : public eoEvalFuncCounter< EOT > +{ +public : + eoEvalFuncCounterBounder(eoEvalFunc& func, unsigned long threshold, std::string name = "Eval. ") + : eoEvalFuncCounter< EOT >( func, name ), _threshold( threshold ) + {} + + using eoEvalFuncCounter< EOT >::value; + + virtual void operator()(EOT& _eo) + { + if (_eo.invalid()) + { + value()++; + + if (_threshold > 0 && value() >= threshold) + { + throw eoEvalFuncCounterBounderException(threshold); + } + + func(_eo); + } + } + +private : + unsigned long _threshold; +}; + +#endif