Instrument Neutral Distributed Interface INDI  2.0.2
inditimer.h
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 #pragma once
20 
21 #include "indimacros.h"
22 #include <memory>
23 #include <functional>
24 
25 namespace INDI
26 {
27 
28 class TimerPrivate;
40 class Timer
41 {
42  DECLARE_PRIVATE(Timer)
43 
44  public:
45  Timer();
46  virtual ~Timer();
47 
48  public:
50  void start();
51 
53  void start(int msec);
54 
56  void stop();
57 
58  public:
59  void callOnTimeout(const std::function<void()> &callback);
60 
61  public:
63  void setInterval(int msec);
64 
66  void setSingleShot(bool singleShot);
67 
68  public:
70  bool isActive() const;
71 
73  bool isSingleShot() const;
74 
78  int remainingTime() const;
79 
81  int interval() const;
82 
83  public:
85  static void singleShot(int msec, const std::function<void()> &callback);
86 
87  public:
89  virtual void timeout();
90 
91  protected:
92  std::unique_ptr<TimerPrivate> d_ptr;
93  Timer(TimerPrivate &dd);
94 };
95 
96 }
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
std::unique_ptr< TimerPrivate > d_ptr
Definition: inditimer.h:92
bool isSingleShot() const
Returns whether the timer is a single-shot timer.
Definition: inditimer.cpp:121
Namespace to encapsulate INDI client, drivers, and mediator classes.