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 #include <cxxtools/api.h>
32 #include <vector>
33 #include <cstddef>
34 
35 namespace cxxtools {
36 
37  class SelectorBase;
38 
69  {
70  class Sentry;
71 
72  public:
77  Timer();
78 
83  ~Timer();
84 
85  SelectorBase* selector()
86  { return _selector; }
87 
88  void setSelector(SelectorBase* s);
89 
92  bool active() const;
93 
98  std::size_t interval() const;
99 
108  void start(std::size_t interval);
109 
115  void stop();
116 
124  bool update();
125 
126  bool update(const Timespan& now);
127 
132  Signal<> timeout;
133 
134  const Timespan& finished() const
135  { return _finished; }
136 
137  private:
138  Sentry* _sentry;
139  SelectorBase* _selector;
140  bool _active;
141  std::size_t _interval;
142  Timespan _remaining;
143  Timespan _finished;
144  };
145 
146 }
147 
148 
149 #endif