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

# eoUpdatable.h

    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
    00036 class eoUpdatable
    00037 {
    00038 public:
    00039
    00041     virtual ~eoUpdatable() {};
    00042
    00043     virtual void update() = 0;
    00044 };
    00045
    00046
    00047
    00051 class eoDynUpdater : public eoUpdater
    00052 {public :
    00053     eoDynUpdater(eoUpdatable & _toUpdate) : toUpdate(_toUpdate) {};
    00054
    00055     virtual void operator()()
    00056     {
    00057         toUpdate.update();
    00058     }
    00059
    00060 private:
    00061     eoUpdatable& toUpdate;
    00062 };
    00063
    00067 class eoTimedDynUpdate : public eoDynUpdater
    00068 {
    00069 public :
    00070     eoTimedDynUpdate(eoUpdatable & _toUpdate, time_t _interval) :
    00071     eoDynUpdater(_toUpdate),
    00072         interval(_interval), last_time(time(0)), first_time(time(0)) {}
    00073
    00074     void operator()(void)
    00075       {
    00076         time_t now = time(0);
    00077
    00078         if (now >= last_time + interval)
    00079           {
    00080             last_time = now;
    00081             eoDynUpdater::operator() ();
    00082           }
    00083       }
    00084 private :
    00085     const time_t interval;
    00086     time_t last_time;
    00087     const time_t first_time;
    00088 };
    00089
    00093 class eoCountedDynUpdate : public eoDynUpdater
    00094 {
    00095 public :
    00096     eoCountedDynUpdate(eoUpdatable & _toUpdate, unsigned _interval)
    00097         : eoDynUpdater(_toUpdate), interval(_interval), counter(0) {}
    00098
    00099     void operator()(void)
    00100       {
    00101         if (++counter % interval == 0)
    00102           {
    00103             eoDynUpdater::operator() ();
    00104           }
    00105       }
    00106 private :
    00107     const unsigned interval;
    00108     unsigned counter;
    00109 };
    00110
    00111 #endif