timer.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * As a special exception, you may use this file as part of a free
8  * software library without restriction. Specifically, if other files
9  * instantiate templates or use macros or inline functions from this
10  * file, or you compile this file and link it with other files to
11  * produce an executable, this file does not by itself cause the
12  * resulting executable to be covered by the GNU General Public
13  * License. This exception does not however invalidate any other
14  * reasons why the executable file might be covered by the GNU Library
15  * General Public License.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 #ifndef Cxxtools_System_Timer_h
27 #define Cxxtools_System_Timer_h
28 
29 #include <cxxtools/signal.h>
30 #include <cxxtools/timespan.h>
31 
32 namespace cxxtools {
33 
34  class DateTime;
35  class SelectorBase;
36 
69  class Timer
70  {
71  class Sentry;
72 
73  public:
79  explicit Timer(SelectorBase* selector = 0);
80 
85  ~Timer();
86 
88  { return _selector; }
89 
90  void setSelector(SelectorBase* s);
91 
94  bool active() const;
95 
100  const Timespan& interval() const;
101 
110  void start(const Milliseconds& interval);
111 
123  void start(const DateTime& startTime, const Milliseconds& interval);
124 
135  void after(const Milliseconds& interval);
136 
144  void at(const DateTime& tickTime);
145 
151  void stop();
152 
160  bool update();
161 
162  bool update(const Milliseconds& now);
163 
168  Signal<> timeout;
169 
175  Signal<DateTime> timeoutts;
176 
180  { return _finished; }
181 
182  private:
183  Sentry* _sentry;
184  SelectorBase* _selector;
185  bool _active;
186  Timespan _interval;
187  Timespan _finished;
188  bool _once;
189  };
190 
191 }
192 
193 
194 #endif