Instrument Neutral Distributed Interface INDI  2.0.2
inditimer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 by Pawel Soja <kernel32.pl@gmail.com>
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  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "inditimer.h"
20 #include "inditimer_p.h"
21 #include <algorithm>
22 
23 #include "eventloop.h"
24 
25 namespace INDI
26 {
27 
29  : p(p)
30 { }
31 
33 {
34  stop();
35 }
36 
38 {
39  if (singleShot)
40  {
41  timerId = addTimer(interval, [](void *arg)
42  {
43  TimerPrivate *d = static_cast<TimerPrivate*>(arg);
44  d->timerId = -1;
45  d->p->timeout();
46  }, this);
47  }
48  else
49  {
50  timerId = addPeriodicTimer(interval, [](void *arg)
51  {
52  TimerPrivate *d = static_cast<TimerPrivate*>(arg);
53  d->p->timeout();
54  }, this);
55  }
56 }
57 
59 {
60  int id = timerId.exchange(-1);
61  if (id != -1)
62  rmTimer(id);
63 }
64 
66  : d_ptr(new TimerPrivate(this))
67 { }
68 
70  : d_ptr(&dd)
71 { }
72 
74 { }
75 
76 void Timer::callOnTimeout(const std::function<void()> &callback)
77 {
78  D_PTR(Timer);
79  d->callback = callback;
80 }
81 
83 {
84  D_PTR(Timer);
85  d->stop();
86  d->start();
87 }
88 
89 void Timer::start(int msec)
90 {
91  D_PTR(Timer);
92  d->stop();
93  d->interval = msec;
94  d->start();
95 }
96 
98 {
99  D_PTR(Timer);
100  d->stop();
101 }
102 
103 void Timer::setInterval(int msec)
104 {
105  D_PTR(Timer);
106  d->interval = msec;
107 }
108 
109 void Timer::setSingleShot(bool singleShot)
110 {
111  D_PTR(Timer);
112  d->singleShot = singleShot;
113 }
114 
115 bool Timer::isActive() const
116 {
117  D_PTR(const Timer);
118  return d->timerId != -1;
119 }
120 
122 {
123  D_PTR(const Timer);
124  return d->singleShot;
125 }
126 
128 {
129  D_PTR(const Timer);
130  return d->timerId != -1 ? std::max(remainingTimer(d->timerId), 0) : 0;
131 }
132 
133 int Timer::interval() const
134 {
135  D_PTR(const Timer);
136  return d->interval;
137 }
138 
140 {
141  D_PTR(Timer);
142  if (d->callback != nullptr)
143  d->callback();
144 }
145 
146 void Timer::singleShot(int msec, const std::function<void()> &callback)
147 {
148  Timer *timer = new Timer();
149  timer->setSingleShot(true);
150  timer->setInterval(msec);
151  timer->callOnTimeout([callback, timer]()
152  {
153  callback();
154  delete timer;
155  });
156  timer->start();
157 }
158 
159 }
std::atomic< int > timerId
Definition: inditimer_p.h:42
virtual ~TimerPrivate()
Definition: inditimer.cpp:32
TimerPrivate(Timer *p)
Definition: inditimer.cpp:28
The Timer class provides repetitive and single-shot timers.
Definition: inditimer.h:41
int interval() const
Returns the timeout interval in milliseconds.
Definition: inditimer.cpp:133
void setSingleShot(bool singleShot)
Set whether the timer is a single-shot timer.
Definition: inditimer.cpp:109
void callOnTimeout(const std::function< void()> &callback)
Definition: inditimer.cpp:76
void start()
Starts or restarts the timer with the timeout specified in interval.
Definition: inditimer.cpp:82
static void singleShot(int msec, const std::function< void()> &callback)
This static function calls a the given function after a given time interval.
Definition: inditimer.cpp:146
bool isActive() const
Returns true if the timer is running (pending); otherwise returns false.
Definition: inditimer.cpp:115
int remainingTime() const
Returns the timer's remaining value in milliseconds left until the timeout. If the timer not exists,...
Definition: inditimer.cpp:127
void setInterval(int msec)
Set the timeout interval in milliseconds.
Definition: inditimer.cpp:103
virtual ~Timer()
Definition: inditimer.cpp:73
void stop()
Stops the timer.
Definition: inditimer.cpp:97
virtual void timeout()
This function is called when the timer times out.
Definition: inditimer.cpp:139
bool isSingleShot() const
Returns whether the timer is a single-shot timer.
Definition: inditimer.cpp:121
void rmTimer(int timer_id)
Definition: eventloop.c:286
int addTimer(int ms, TCF *fp, void *ud)
Definition: eventloop.c:248
int addPeriodicTimer(int ms, TCF *fp, void *ud)
Definition: eventloop.c:253
int remainingTimer(int timer_id)
Definition: eventloop.c:311
Public interface to INDI's eventloop mechanism.
double max(void)
Namespace to encapsulate INDI client, drivers, and mediator classes.