Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# 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
    00039 class eoUpdater : public eoF<void>
    00040 {
    00041 public:
    00042   virtual void lastCall() {}
    00043   virtual std::string className(void) const { return "eoUpdater"; }
    00044 };
    00045
    00049 template <class T>
    00050 class eoIncrementor : public eoUpdater
    00051 {public :
    00053     eoIncrementor(T& _counter, T _stepsize = 1) : counter(_counter), stepsize(_stepsize) {}
    00054
    00056     virtual void operator()()
    00057     {
    00058         counter += stepsize;
    00059     }
    00060
    00061   virtual std::string className(void) const { return "eoIncrementor"; }
    00062 private:
    00063     T& counter;
    00064     T stepsize;
    00065 };
    00066
    00070 template <class T>
    00071 class eoIncrementorParam : public eoUpdater, public eoValueParam<T>
    00072 {
    00073 public:
    00074
    00075     using eoValueParam<T>::value;
    00076
    00078   eoIncrementorParam( std::string _name, T _stepsize = 1) :
    00079     eoValueParam<T>(T(0), _name), stepsize(_stepsize) {}
    00080
    00084   eoIncrementorParam( std::string _name, T _countValue, T _stepsize) :
    00085     eoValueParam<T>(_countValue, _name), stepsize(_stepsize) {}
    00086
    00088   virtual void operator()()
    00089   {
    00090       value() += stepsize;
    00091   }
    00092
    00093   virtual std::string className(void) const { return "eoIncrementorParam"; }
    00094
    00095 private:
    00096   T stepsize;
    00097 };
    00098
    00099 #include <time.h>
    00100
    00104 class eoTimedStateSaver : public eoUpdater
    00105 {
    00106 public :
    00107     eoTimedStateSaver(time_t _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") : state(_state),
    00108         interval(_interval), last_time(time(0)), first_time(time(0)),
    00109     prefix(_prefix), extension(_extension) {}
    00110
    00111     void operator()(void);
    00112
    00113   virtual std::string className(void) const { return "eoTimedStateSaver"; }
    00114 private :
    00115     const eoState& state;
    00116
    00117     const time_t interval;
    00118     time_t last_time;
    00119     const time_t first_time;
    00120     const std::string prefix;
    00121     const std::string extension;
    00122 };
    00123
    00127 class eoCountedStateSaver : public eoUpdater
    00128 {
    00129 public :
    00130     eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix, bool _saveOnLastCall, std::string _extension = "sav", unsigned _counter = 0)
    00131       : state(_state), interval(_interval), counter(_counter),
    00132       saveOnLastCall(_saveOnLastCall),
    00133       prefix(_prefix), extension(_extension) {}
    00134
    00135     eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav", unsigned _counter = 0)
    00136       : state(_state), interval(_interval), counter(_counter),
    00137         saveOnLastCall(true),
    00138         prefix(_prefix), extension(_extension) {}
    00139
    00140     virtual void lastCall(void);
    00141     void operator()(void);
    00142
    00143   virtual std::string className(void) const { return "eoCountedStateSaver"; }
    00144 private :
    00145     void doItNow(void);
    00146
    00147     const eoState& state;
    00148     const unsigned interval;
    00149     unsigned counter;
    00150     bool saveOnLastCall;
    00151
    00152     const std::string prefix;
    00153     const std::string extension;
    00154 };
    00155
    00156
    00157 #endif