EvolvingObjects
eoEvalFuncCounterBounder.h
00001 #ifndef eoEvalFuncCounterBounder_H
00002 #define eoEvalFuncCounterBounder_H
00003 
00004 #include <eoEvalFunc.h>
00005 #include <utils/eoParam.h>
00006 
00014 class eoEvalFuncCounterBounderException : public std::exception
00015 {
00016 public:
00017     eoEvalFuncCounterBounderException(unsigned long threshold) : _threshold(threshold){}
00018 
00019     const char* what() const throw()
00020     {
00021         std::ostringstream ss;
00022         ss << "STOP in eoEvalFuncCounterBounderException: the maximum number of evaluation has been reached (" << _threshold << ").";
00023         return ss.str().c_str();
00024     }
00025 
00026 private:
00027     unsigned long _threshold;
00028 };
00029 
00037 template < typename EOT >
00038 class eoEvalFuncCounterBounder : public eoEvalFuncCounter< EOT >
00039 {
00040 public :
00041     eoEvalFuncCounterBounder(eoEvalFunc<EOT>& func, unsigned long threshold, std::string name = "Eval. ")
00042         : eoEvalFuncCounter< EOT >( func, name ), _threshold( threshold )
00043     {}
00044 
00045     using eoEvalFuncCounter< EOT >::value;
00046 
00047     virtual void operator()(EOT& eo)
00048     {
00049         if (eo.invalid())
00050             {
00051                 value()++;
00052 
00053                 if (_threshold > 0 && value() >= _threshold)
00054                     {
00055                         throw eoEvalFuncCounterBounderException(_threshold);
00056                     }
00057 
00058                 func(eo);
00059             }
00060     }
00061 
00062 private :
00063     unsigned long _threshold;
00064 };
00065 
00067 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends