00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00094 thr->start();
00095 }
00096
00097 template <class Caller>
00098 void CallBackTimer<Caller>::terminate()
00099 {
00100
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
00120 }
00121
00122
00123 template <class Caller>
00124 void CallBackTimer<Caller>::remove( int id )
00125 {
00126
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_