UniSet  1.4.0
CallBackTimer.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00023 //----------------------------------------------------------------------------
00024 # ifndef CallBackTimer_H_
00025 # define CallBackTimer_H_
00026 //----------------------------------------------------------------------------
00027 #include <list>
00028 #include "Exceptions.h"
00029 #include "ThreadCreator.h"
00030 #include "PassiveTimer.h"
00031 //-----------------------------------------------------------------------------
00037 namespace UniSetTypes
00038 {
00039     class LimitTimers: 
00040         public UniSetTypes::Exception
00041     {
00042         public:
00043             LimitTimers():Exception("LimitTimers"){ printException(); }
00044     
00046             LimitTimers(const std::string err):Exception(err){ printException(); }
00047     };
00048 };
00050 // end of UniSetException group
00051 //----------------------------------------------------------------------------------------
00052 
00080 template <class Caller>
00081 class CallBackTimer
00082 //  public PassiveTimer
00083 { 
00084     public:
00085 
00087         static const int MAXCallBackTimer = 20;
00088 
00090         typedef void(Caller::* Action)( int id );   
00091 
00092         CallBackTimer(Caller* r, Action a);
00093         ~CallBackTimer();
00094 
00095         // Управление таймером
00096         void run();         
00097         void terminate();   
00099         // Работа с таймерами (на основе интерфейса PassiveTimer)
00100         void reset(int id);                 
00101         void setTiming(int id, int timrMS); 
00102         int getInterval(int id);            
00103         int getCurrent(int id);             
00106         void add( int id, int timeMS )throw(UniSetTypes::LimitTimers); 
00107         void remove( int id ); 
00109     protected:
00110 
00111         CallBackTimer();
00112         void work();
00113         
00114         void startTimers();
00115         void clearTimers();
00116 
00117     private:
00118 
00119         typedef CallBackTimer<Caller> CBT;
00120         friend class ThreadCreator<CBT>;
00121         Caller* cal;
00122         Action act;
00123         ThreadCreator<CBT> *thr;
00124         
00125         bool terminated;
00126 
00127         struct TimerInfo
00128         {
00129             TimerInfo(int id, PassiveTimer& pt):
00130                 id(id), pt(pt){};
00131 
00132             int id;
00133             PassiveTimer pt;
00134         };
00135         
00136         typedef std::list<TimerInfo> TimersList;
00137         TimersList lst;
00138 
00139         // функция-объект для поиска по id
00140         struct FindId_eq: public unary_function<TimerInfo, bool>
00141         {
00142             FindId_eq(const int id):id(id){}
00143             inline bool operator()(const TimerInfo& ti) const{return ti.id==id;}
00144             int id;
00145         };
00146 };
00147 
00148 #include "CallBackTimer_template.h"
00149 # endif //CallBackTimer_H_