MythTV  master
mythtimer.cpp
Go to the documentation of this file.
1 /*
2  * Class MythTimer
3  *
4  * Copyright (C) Rune Petersen 2012
5  * Copyright (C) Daniel Kristjansson 2013
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <cstdint>
23 
24 // MythTV includes
25 #include "mythtimer.h"
26 
27 //#define DEBUG_TIMER_API_USAGE
28 #ifdef DEBUG_TIMER_API_USAGE
29 #undef NDEBUG
30 #include <cassert>
31 #endif
32 
39 {
40  if (kStartRunning == state)
41  start();
42  else
43  stop();
44 }
45 
47 void MythTimer::start(void)
48 {
49  m_timer.start();
50  m_offset = 0ms;
51 }
52 
62 std::chrono::milliseconds MythTimer::restart(void)
63 {
64  if (m_timer.isValid())
65  {
66  auto val = std::chrono::milliseconds(m_timer.restart()) + m_offset;
67  m_offset = 0ms;
68  return val;
69  }
70  start();
71  return 0ms;
72 }
73 
78 void MythTimer::stop(void)
79 {
80  m_timer.invalidate();
81 }
82 
91 std::chrono::milliseconds MythTimer::elapsed(void)
92 {
93  if (!m_timer.isValid())
94  {
95 #ifdef DEBUG_TIMER_API_USAGE
96  assert(0 == "elapsed called without timer being started");
97 #endif
98  return 0ms;
99  }
100 
101  auto e = std::chrono::milliseconds(m_timer.elapsed());
102  if (!QElapsedTimer::isMonotonic() && (e > (24h - 100s)))
103  {
104  start();
105  e = 0ms;
106  }
107 
108  return e + m_offset;
109 }
110 
119 std::chrono::nanoseconds MythTimer::nsecsElapsed(void) const
120 {
121  if (!m_timer.isValid())
122  {
123 #ifdef DEBUG_TIMER_API_USAGE
124  assert(0 == "elapsed called without timer being started");
125 #endif
126  return 0ns;
127  }
128 
129  return std::chrono::nanoseconds(m_timer.nsecsElapsed()) + m_offset;
130 }
131 
135 bool MythTimer::isRunning(void) const
136 {
137  return m_timer.isValid();
138 }
139 
146 void MythTimer::addMSecs(std::chrono::milliseconds ms)
147 {
148  m_offset += ms;
149 }
MythTimer::m_timer
QElapsedTimer m_timer
Definition: mythtimer.h:34
MythTimer::elapsed
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:91
MythTimer::stop
void stop(void)
Stops timer, next call to isRunning() will return false and any calls to elapsed() or restart() will ...
Definition: mythtimer.cpp:78
MythTimer::isRunning
bool isRunning(void) const
Returns true if start() or restart() has been called at least once since construction and since any c...
Definition: mythtimer.cpp:135
MythTimer::start
void start(void)
starts measuring elapsed time.
Definition: mythtimer.cpp:47
MythTimer::StartState
StartState
Definition: mythtimer.h:16
MythTimer::MythTimer
MythTimer(StartState state=kStartInactive)
Creates a timer.
Definition: mythtimer.cpp:38
MythTimer::restart
std::chrono::milliseconds restart(void)
Returns milliseconds elapsed since last start() or restart() and resets the count.
Definition: mythtimer.cpp:62
MythTimer::m_offset
std::chrono::milliseconds m_offset
Definition: mythtimer.h:35
MythTimer::nsecsElapsed
std::chrono::nanoseconds nsecsElapsed(void) const
Returns nanoseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:119
assert
#define assert(x)
Definition: audiooutputalsa.cpp:16
MythTimer::addMSecs
void addMSecs(std::chrono::milliseconds ms)
Adds an offset to the last call to start() or restart().
Definition: mythtimer.cpp:146
MythTimer::kStartRunning
@ kStartRunning
Definition: mythtimer.h:17
mythtimer.h