UniSet  1.4.0
CallBackTimer_template.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_TEMPLATE_H_
00025 # define CallBackTimer_TEMPLATE_H_
00026 // -------------------------------------------------------------------------- 
00027 #include <unistd.h>
00028 #include <sstream>
00029 #include "CallBackTimer.h"
00030 
00031 // ------------------------------------------------------------------------------------------
00032 template <class Caller> class CallBackTimer;
00033 
00034 // ------------------------------------------------------------------------------------------
00038 template <class Caller>
00039 CallBackTimer<Caller>::CallBackTimer( Caller* r, Action a ):
00040     cal(r),
00041     act(a),
00042     terminated(false)
00043 {
00044     thr = new ThreadCreator<CallBackTimer>(this, &CallBackTimer<Caller>::work);
00045 }
00046 
00047 // ------------------------------------------------------------------------------------------
00048 
00049 template <class Caller>
00050 CallBackTimer<Caller>::CallBackTimer():
00051     cal(null),
00052     terminated(false)
00053 {
00054     thr = new ThreadCreator<CallBackTimer>(this, &CallBackTimer<Caller>::work);
00055 }
00056 // ------------------------------------------------------------------------------------------
00057 template <class Caller>
00058 CallBackTimer<Caller>::~CallBackTimer()
00059 {
00060     terminate();
00061     clearTimers();
00062     delete thr;
00063 }
00064 
00065 // ------------------------------------------------------------------------------------------
00066 template <class Caller>
00067 void CallBackTimer<Caller>::work()
00068 {
00069     terminated = false;
00070     while( !terminated )
00071     {
00072         usleep(UniSetTimer::MIN_QUANTITY_TIME_MKS); 
00073 
00074         for( typename TimersList::iterator li=lst.begin(); li!=lst.end(); ++li )
00075         {
00076             if( li->pt.checkTime() )
00077             {
00078                 (cal->*act)( li->id );
00079                 li->pt.reset();
00080             }
00081         }
00082 
00083     }
00084 }
00085 // ------------------------------------------------------------------------------------------
00086 template <class Caller>
00087 void CallBackTimer<Caller>::run()
00088 {
00089     if( !terminated )
00090         terminate();
00091 
00092     startTimers();
00093 //  PosixThread::start(static_cast<PosixThread*>(this));
00094     thr->start();
00095 }
00096 // ------------------------------------------------------------------------------------------
00097 template <class Caller>
00098 void CallBackTimer<Caller>::terminate()
00099 {
00100 //  timeAct = 0;
00101     terminated = true;
00102     usleep(1000);
00103 }
00104 // ------------------------------------------------------------------------------------------
00105 
00106 template <class Caller>
00107 void CallBackTimer<Caller>::add( int id, int timeMS )throw(UniSetTypes::LimitTimers)
00108 {
00109     if( lst.size() >= MAXCallBackTimer )
00110     {
00111         ostringstream err;
00112         err << "CallBackTimers: превышено максимальное количество таймеров" << MAXCallBackTimer;
00113         throw UniSetTypes::LimitTimers(err.str()); 
00114     }
00115     
00116     PassiveTimer pt(timeMS);
00117     TimerInfo ti(id, pt);
00118     lst.push_back(ti);
00119 //  lst[id] = ti;
00120 }
00121 // ------------------------------------------------------------------------------------------
00122 
00123 template <class Caller>
00124 void CallBackTimer<Caller>::remove( int id )
00125 {
00126     // STL - способ поиска
00127     typename TimersList::iterator li= find_if(lst.begin(),lst.end(),FindId_eq(id)); 
00128     if( li!=lst.end() )
00129         lst.erase(li);
00130 }
00131 // ------------------------------------------------------------------------------------------
00132 template <class Caller>
00133 void CallBackTimer<Caller>::startTimers()
00134 {
00135     for( typename TimersList::iterator li=lst.begin(); li!=lst.end(); ++li)
00136     {
00137         li->pt.reset();
00138     }
00139 }
00140 // ------------------------------------------------------------------------------------------
00141 template <class Caller>
00142 void CallBackTimer<Caller>::clearTimers()
00143 {
00144     lst.clear();
00145 }
00146 // ------------------------------------------------------------------------------------------
00147 template <class Caller>
00148 void CallBackTimer<Caller>::reset( int id )
00149 {
00150     typename TimersList::iterator li= find_if(lst.begin(),lst.end(),FindId_eq(id)); 
00151     if( li!=lst.end() )
00152         li->pt.reset();
00153 }
00154 // ------------------------------------------------------------------------------------------
00155 template <class Caller>
00156 void CallBackTimer<Caller>::setTiming( int id, int timeMS )
00157 {
00158     typename TimersList::iterator li= find_if(lst.begin(),lst.end(),FindId_eq(id));     
00159     if( li!=lst.end() )
00160         li->pt.setTimer(timeMS);
00161 }
00162 // ------------------------------------------------------------------------------------------
00163 template <class Caller>
00164 int CallBackTimer<Caller>::getInterval( int id )
00165 {
00166     typename TimersList::iterator li= find_if(lst.begin(),lst.end(),FindId_eq(id));     
00167     if( li!=lst.end() )
00168         return li->pt.getInterval();
00169     return -1;
00170 }
00171 // ------------------------------------------------------------------------------------------
00172 template <class Caller>
00173 int CallBackTimer<Caller>::getCurrent( int id )
00174 {
00175     typename TimersList::iterator li= find_if(lst.begin(),lst.end(),FindId_eq(id));         
00176     if( li!=lst.end() )
00177         return li->pt.getCurrent();
00178     
00179     return -1;
00180 }
00181 // ------------------------------------------------------------------------------------------
00182 
00183 # endif //CallBackTimer_H_