Instrument Neutral Distributed Interface INDI  2.0.2
indielapsedtimer.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 "indielapsedtimer.h"
20 #include "indielapsedtimer_p.h"
21 
22 namespace INDI
23 {
24 
26  : d_ptr(new ElapsedTimerPrivate)
27 {
28  start();
29 }
30 
32  : d_ptr(&dd)
33 {
34  start();
35 }
36 
38 { }
39 
41 {
42  D_PTR(ElapsedTimer);
43  d->start = std::chrono::steady_clock::now();
44 }
45 
47 {
48  D_PTR(ElapsedTimer);
49  std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
50  int64_t result = std::chrono::duration_cast<std::chrono::milliseconds>(now - d->start).count();
51  d->start = now;
52  return result;
53 }
54 
55 int64_t ElapsedTimer::elapsed() const
56 {
57  D_PTR(const ElapsedTimer);
58  std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
59  return std::chrono::duration_cast<std::chrono::milliseconds>(now - d->start).count();
60 }
61 
63 {
64  D_PTR(const ElapsedTimer);
65  std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
66  return std::chrono::duration_cast<std::chrono::nanoseconds>(now - d->start).count();
67 }
68 
69 bool ElapsedTimer::hasExpired(int64_t timeout) const
70 {
71  return elapsed() > timeout;
72 }
73 
74 void ElapsedTimer::nsecsRewind(int64_t nsecs)
75 {
76  D_PTR(ElapsedTimer);
77  d->start += std::chrono::nanoseconds(nsecs);
78 }
79 
80 }
The ElapsedTimer class provides a fast way to calculate elapsed times.
int64_t nsecsElapsed() const
Returns the number of nanoseconds since this ElapsedTimer was last started.
int64_t elapsed() const
Returns the number of milliseconds since this ElapsedTimer was last started.
int64_t restart()
Restarts the timer and returns the number of milliseconds elapsed since the previous start.
void nsecsRewind(int64_t nsecs)
Rewind elapsed time of nsec nanoseconds.
bool hasExpired(int64_t timeout) const
Returns true if this ElapsedTimer has already expired by timeout milliseconds.
void start()
Starts this timer. Once started, a timer value can be checked with elapsed().
Namespace to encapsulate INDI client, drivers, and mediator classes.