EvolvingObjects
eoUpdater.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoUpdater.h
00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00022              Marc.Schoenauer@polytechnique.fr
00023              mkeijzer@dhi.dk
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _eoUpdater_h
00028 #define _eoUpdater_h
00029 
00030 #include <string>
00031 #include <eoFunctor.h>
00032 #include <utils/eoState.h>
00033 #include <utils/eoParam.h>
00034 
00035 template <class EOT> class eoCheckPoint;
00036 
00043 class eoUpdater : public eoF<void>
00044 {
00045 public:
00046   virtual void lastCall() {}
00047   virtual std::string className(void) const { return "eoUpdater"; }
00048 
00049    template <class EOT>
00050     eoUpdater& addTo(eoCheckPoint<EOT>& cp)        { cp.add(*this);  return *this; }
00051 };
00052 
00058 template <class T>
00059 class eoIncrementor : public eoUpdater
00060 {public :
00062     eoIncrementor(T& _counter, T _stepsize = 1) : counter(_counter), stepsize(_stepsize) {}
00063 
00065     virtual void operator()()
00066     {
00067         counter += stepsize;
00068     }
00069 
00070   virtual std::string className(void) const { return "eoIncrementor"; }
00071 private:
00072     T& counter;
00073     T stepsize;
00074 };
00075 
00081 template <class T>
00082 class eoIncrementorParam : public eoUpdater, public eoValueParam<T>
00083 {
00084 public:
00085 
00086     using eoValueParam<T>::value;
00087 
00089   eoIncrementorParam( std::string _name, T _stepsize = 1) :
00090     eoValueParam<T>(T(0), _name), stepsize(_stepsize) {}
00091 
00095   eoIncrementorParam( std::string _name, T _countValue, T _stepsize) :
00096     eoValueParam<T>(_countValue, _name), stepsize(_stepsize) {}
00097 
00099   virtual void operator()()
00100   {
00101       value() += stepsize;
00102   }
00103 
00104   virtual std::string className(void) const { return "eoIncrementorParam"; }
00105 
00106 private:
00107   T stepsize;
00108 };
00109 
00110 #include <time.h>
00111 
00117 class eoTimedStateSaver : public eoUpdater
00118 {
00119 public :
00120     eoTimedStateSaver(time_t _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") : state(_state),
00121         interval(_interval), last_time(time(0)), first_time(time(0)),
00122     prefix(_prefix), extension(_extension) {}
00123 
00124     void operator()(void);
00125 
00126   virtual std::string className(void) const { return "eoTimedStateSaver"; }
00127 private :
00128     const eoState& state;
00129 
00130     const time_t interval;
00131     time_t last_time;
00132     const time_t first_time;
00133     const std::string prefix;
00134     const std::string extension;
00135 };
00136 
00142 class eoCountedStateSaver : public eoUpdater
00143 {
00144 public :
00145     eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix, bool _saveOnLastCall, std::string _extension = "sav", unsigned _counter = 0)
00146       : state(_state), interval(_interval), counter(_counter),
00147       saveOnLastCall(_saveOnLastCall),
00148       prefix(_prefix), extension(_extension) {}
00149 
00150     eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav", unsigned _counter = 0)
00151       : state(_state), interval(_interval), counter(_counter),
00152         saveOnLastCall(true),
00153         prefix(_prefix), extension(_extension) {}
00154 
00155     virtual void lastCall(void);
00156     void operator()(void);
00157 
00158   virtual std::string className(void) const { return "eoCountedStateSaver"; }
00159 private :
00160     void doItNow(void);
00161 
00162     const eoState& state;
00163     const unsigned interval;
00164     unsigned counter;
00165     bool saveOnLastCall;
00166 
00167     const std::string prefix;
00168     const std::string extension;
00169 };
00170 
00171 
00172 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends