UniSet  2.6.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 
69  template <class Caller>
71  // public PassiveTimer
72  {
73  public:
74 
76  static const size_t MAXCallbackTimer = 20;
77 
79  typedef void(Caller::* Action)( size_t id );
80 
81  CallbackTimer(Caller* r, Action a);
82  ~CallbackTimer();
83 
84  // Управление таймером
85  void run();
86  void terminate();
88  // Работа с таймерами (на основе интерфейса PassiveTimer)
89  void reset(size_t id);
90  void setTiming(size_t id, timeout_t timrMS);
91  timeout_t getInterval(size_t id);
92  timeout_t getCurrent(size_t id);
95  void add( size_t id, timeout_t timeMS )throw(uniset::LimitTimers);
96  void remove( size_t id );
98  protected:
99 
100  CallbackTimer();
101  void work();
102 
103  void startTimers();
104  void clearTimers();
105 
106  private:
107 
108  typedef CallbackTimer<Caller> CBT;
109  friend class ThreadCreator<CBT>;
110  Caller* cal;
111  Action act;
112  ThreadCreator<CBT>* thr;
113 
114  bool terminated;
115 
116  struct TimerInfo
117  {
118  TimerInfo(size_t id, PassiveTimer& pt):
119  id(id), pt(pt) {};
120 
121  size_t id;
122  PassiveTimer pt;
123  };
124 
125  typedef std::list<TimerInfo> TimersList;
126  TimersList lst;
127 
128  // функция-объект для поиска по id
129  struct FindId_eq: public std::unary_function<TimerInfo, bool>
130  {
131  FindId_eq(const size_t id): id(id) {}
132  inline bool operator()(const TimerInfo& ti) const
133  {
134  return ti.id == id;
135  }
136  size_t id;
137  };
138  };
139  //----------------------------------------------------------------------------------------
140 #include "CallbackTimer.tcc"
141  //----------------------------------------------------------------------------------------
142 } // end of uniset namespace
143 //----------------------------------------------------------------------------------------
144 # endif //CallbackTimer_H_