|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // -------------------------------------------------------------------------- 00020 //---------------------------------------------------------------------------- 00021 # ifndef CallbackTimer_H_ 00022 # define CallbackTimer_H_ 00023 //---------------------------------------------------------------------------- 00024 #include <list> 00025 #include "Exceptions.h" 00026 #include "ThreadCreator.h" 00027 #include "PassiveTimer.h" 00028 //----------------------------------------------------------------------------- 00029 namespace uniset 00030 { 00031 class LimitTimers: 00032 public uniset::Exception 00033 { 00034 public: 00035 LimitTimers(): Exception("LimitTimers") {} 00036 00038 LimitTimers(const std::string& err): Exception(err) {} 00039 }; 00040 //---------------------------------------------------------------------------------------- 00041 00069 template <class Caller> 00070 class CallbackTimer 00071 // public PassiveTimer 00072 { 00073 public: 00074 00076 static const size_t MAXCallbackTimer = 20; 00077 00079 typedef void(Caller::* Action)( size_t id ); 00080 00081 CallbackTimer(Caller* r, Action a); 00082 ~CallbackTimer(); 00083 00084 // Управление таймером 00085 void run(); 00086 void terminate(); 00088 // Работа с таймерами (на основе интерфейса PassiveTimer) 00089 void reset(size_t id); 00090 void setTiming(size_t id, timeout_t timrMS); 00091 timeout_t getInterval(size_t id); 00092 timeout_t getCurrent(size_t id); 00095 void add( size_t id, timeout_t timeMS )throw(uniset::LimitTimers); 00096 void remove( size_t id ); 00098 protected: 00099 00100 CallbackTimer(); 00101 void work(); 00102 00103 void startTimers(); 00104 void clearTimers(); 00105 00106 private: 00107 00108 typedef CallbackTimer<Caller> CBT; 00109 friend class ThreadCreator<CBT>; 00110 Caller* cal; 00111 Action act; 00112 ThreadCreator<CBT>* thr; 00113 00114 bool terminated; 00115 00116 struct TimerInfo 00117 { 00118 TimerInfo(size_t id, PassiveTimer& pt): 00119 id(id), pt(pt) {}; 00120 00121 size_t id; 00122 PassiveTimer pt; 00123 }; 00124 00125 typedef std::list<TimerInfo> TimersList; 00126 TimersList lst; 00127 00128 // функция-объект для поиска по id 00129 struct FindId_eq: public std::unary_function<TimerInfo, bool> 00130 { 00131 FindId_eq(const size_t id): id(id) {} 00132 inline bool operator()(const TimerInfo& ti) const 00133 { 00134 return ti.id == id; 00135 } 00136 size_t id; 00137 }; 00138 }; 00139 //---------------------------------------------------------------------------------------- 00140 #include "CallbackTimer.tcc" 00141 //---------------------------------------------------------------------------------------- 00142 } // end of uniset namespace 00143 //---------------------------------------------------------------------------------------- 00144 # endif //CallbackTimer_H_
1.7.6.1