EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoUpdatable.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 _eoUpdatable_h 00028 #define _eoUpdatable_h 00029 00030 #include <utils/eoUpdater.h> 00031 00038 class eoUpdatable 00039 { 00040 public: 00041 00043 virtual ~eoUpdatable() {}; 00044 00045 virtual void update() = 0; 00046 }; 00047 00048 00049 00055 class eoDynUpdater : public eoUpdater 00056 {public : 00057 eoDynUpdater(eoUpdatable & _toUpdate) : toUpdate(_toUpdate) {}; 00058 00059 virtual void operator()() 00060 { 00061 toUpdate.update(); 00062 } 00063 00064 private: 00065 eoUpdatable& toUpdate; 00066 }; 00067 00073 class eoTimedDynUpdate : public eoDynUpdater 00074 { 00075 public : 00076 eoTimedDynUpdate(eoUpdatable & _toUpdate, time_t _interval) : 00077 eoDynUpdater(_toUpdate), 00078 interval(_interval), last_time(time(0)), first_time(time(0)) {} 00079 00080 void operator()(void) 00081 { 00082 time_t now = time(0); 00083 00084 if (now >= last_time + interval) 00085 { 00086 last_time = now; 00087 eoDynUpdater::operator() (); 00088 } 00089 } 00090 private : 00091 const time_t interval; 00092 time_t last_time; 00093 const time_t first_time; 00094 }; 00095 00101 class eoCountedDynUpdate : public eoDynUpdater 00102 { 00103 public : 00104 eoCountedDynUpdate(eoUpdatable & _toUpdate, unsigned _interval) 00105 : eoDynUpdater(_toUpdate), interval(_interval), counter(0) {} 00106 00107 void operator()(void) 00108 { 00109 if (++counter % interval == 0) 00110 { 00111 eoDynUpdater::operator() (); 00112 } 00113 } 00114 private : 00115 const unsigned interval; 00116 unsigned counter; 00117 }; 00118 00119 #endif