Timer.h
1/*
2 * Copyright (C) 2006-2013 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_System_Timer_h
30#define Pt_System_Timer_h
31
32#include <Pt/System/Api.h>
33#include <Pt/Signal.h>
34#include <Pt/Timespan.h>
35#include <cstddef>
36
37#if __cplusplus >= 202002L
38#include <Pt/Coroutine.h>
39#endif
40
41namespace Pt {
42
43namespace System {
44
45class AsyncWait;
46
73class PT_SYSTEM_API Timer
74{
75 class Sentry;
76
77 public:
83
89
91 EventLoop* parent()
92 { return _loop; }
93
97 { return _loop; }
98
102
105 void detach();
106
109 bool isStarted() const;
110
113 std::size_t interval() const;
114
123 void start(std::size_t interval);
124
132
138 void stop();
139
147 bool update();
148
150 bool update(const Timespan& now);
151
157 { return _timeout; }
158
160 const Timespan& finished() const
161 { return _finished; }
162
163#if __cplusplus >= 202002L
176 AsyncWait waitAsync(std::size_t ms);
177#endif
178
179 private:
180#if __cplusplus >= 202002L
181 friend class AsyncWait;
182
183 void attachAwaiter(AsyncWait& awaiter);
184
185 void detachAwaiter(AsyncWait& awaiter);
186#endif
187
188 Sentry* _sentry;
189 EventLoop* _loop;
190 std::size_t _interval;
191 Timespan _finished;
192 Signal<> _timeout;
193 void* _reserved;
194
195 #if __cplusplus >= 202002L
196 AsyncWait* _awaiter = nullptr;
197 #endif
198};
199
200#if __cplusplus >= 202002L
201
210class PT_SYSTEM_API AsyncWait : public Pt::BasicAwaiter<void>
211 , public Pt::Connectable
212{
213 friend class Timer;
214
215 public:
221 AsyncWait(Timer& timer, std::size_t ms);
222
223 ~AsyncWait();
224
225 private:
226 void onBegin() override;
227
228 void onCancel() override;
229
230 void onReady() override;
231
232 void onDetach();
233
234 Timer& timer();
235
236 Timer* _timer;
237 std::size_t _ms;
238};
239
240#endif // __cplusplus >= 202002L
241
242} // namespace System
243
244} // namespace Pt
245
246#endif // Pt_System_Timer_h
Provides an awaitable that delivers a result through onReady().
Definition Coroutine.h:208
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Multicast Signal to call multiple slots.
Definition Signal.h:190
Awaitable for async one-shot timer delay.
Definition Timer.h:212
AsyncWait(Timer &timer, std::size_t ms)
Construct an awaitable for a one-shot timer delay.
Event loop of a thread or process.
Definition EventLoop.h:83
void setActive(EventLoop &loop)
Sets the used event loop.
EventLoop * loop()
Returns the used event loop.
Definition Timer.h:96
~Timer()
Destructor.
Timer()
Default constructor.
void start(std::size_t interval)
Starts the timer.
void stop()
Stops the timer.
Signal & timeout()
Notifies about interval timeouts.
Definition Timer.h:156
std::size_t interval() const
Returns the current timer interval in milliseconds.
bool isStarted() const
Returs true if timer was started.
void detach()
Detach from used event loop.
AsyncWait waitAsync(std::size_t ms)
Start a one-shot timer delay as a C++20 awaitable.
void start(const Pt::Timespan &interval)
Starts the timer.
Represents time spans in microsecond resolution.
Definition Timespan.h:63
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33