UniSet  2.7.0
CallbackTimer.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 # ifndef CallbackTimer_H_
22 # define CallbackTimer_H_
23 //----------------------------------------------------------------------------
24 #include <list>
25 #include "Exceptions.h"
26 #include "ThreadCreator.h"
27 #include "PassiveTimer.h"
28 //-----------------------------------------------------------------------------
29 namespace uniset
30 {
31  class LimitTimers:
32  public uniset::Exception
33  {
34  public:
35  LimitTimers(): Exception("LimitTimers") {}
36 
38  LimitTimers(const std::string& err): Exception(err) {}
39  };
40  //----------------------------------------------------------------------------------------
41 
71  template <class Caller>
73  // public PassiveTimer
74  {
75  public:
76 
78  static const size_t MAXCallbackTimer = 20;
79 
81  typedef void(Caller::* Action)( size_t id );
82 
83  CallbackTimer(Caller* r, Action a);
84  ~CallbackTimer();
85 
86  // Управление таймером
87  void run();
88  void terminate();
90  // Работа с таймерами (на основе интерфейса PassiveTimer)
91  void reset(size_t id);
92  void setTiming(size_t id, timeout_t timrMS);
93  timeout_t getInterval(size_t id);
94  timeout_t getCurrent(size_t id);
100  void add( size_t id, timeout_t timeMS );
101 
103  void remove( size_t id );
104 
105  protected:
107  CallbackTimer();
108  void work();
109 
110  void startTimers();
111  void clearTimers();
112 
113  private:
114 
115  typedef CallbackTimer<Caller> CBT;
116  friend class ThreadCreator<CBT>;
117  Caller* cal;
118  Action act;
119  ThreadCreator<CBT>* thr;
120 
121  bool terminated;
122 
123  struct TimerInfo
124  {
125  TimerInfo(size_t id, PassiveTimer& pt):
126  id(id), pt(pt) {}
127 
128  size_t id;
129  PassiveTimer pt;
130  };
131 
132  typedef std::list<TimerInfo> TimersList;
133  TimersList lst;
134 
135  // функция-объект для поиска по id
136  struct FindId_eq: public std::unary_function<TimerInfo, bool>
137  {
138  FindId_eq(const size_t id): id(id) {}
139  inline bool operator()(const TimerInfo& ti) const
140  {
141  return ti.id == id;
142  }
143  size_t id;
144  };
145  };
146  //----------------------------------------------------------------------------------------
147 #include "CallbackTimer.tcc"
148  //----------------------------------------------------------------------------------------
149 } // end of uniset namespace
150 //----------------------------------------------------------------------------------------
151 # endif //CallbackTimer_H_
Пассивный таймер
Definition: PassiveTimer.h:92
Definition: CallbackTimer.h:29
Definition: Exceptions.h:44
void run()
Definition: CallbackTimer.h:85
Definition: ThreadCreator.h:87
LimitTimers(const std::string &err)
Definition: CallbackTimer.h:38
Таймер
Definition: CallbackTimer.h:72
Definition: CallbackTimer.h:31