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 
38 MythTimer::MythTimer(StartState state) : m_offset(0)
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 = 0;
51 }
52 
63 {
64  if (m_timer.isValid())
65  {
66  int val = static_cast<int>(m_timer.restart() + m_offset);
67  m_offset = 0;
68  return val;
69  }
70  return start(), 0;
71 }
72 
77 void MythTimer::stop(void)
78 {
79  m_timer.invalidate();
80 }
81 
90 int MythTimer::elapsed(void) const
91 {
92  if (!m_timer.isValid())
93  {
94 #ifdef DEBUG_TIMER_API_USAGE
95  assert(0 == "elapsed called without timer being started");
96 #endif
97  return 0;
98  }
99 
100  qint64 e = m_timer.elapsed();
101  if (!m_timer.isMonotonic() && (e > 86300000))
102  {
103  const_cast<MythTimer*>(this)->start();
104  e = 0;
105  }
106 
107  return static_cast<int>(e + m_offset);
108 }
109 
118 int64_t MythTimer::nsecsElapsed(void) const
119 {
120  if (!m_timer.isValid())
121  {
122 #ifdef DEBUG_TIMER_API_USAGE
123  assert(0 == "elapsed called without timer being started");
124 #endif
125  return 0;
126  }
127 
128  return m_timer.nsecsElapsed() + 1000000LL * m_offset;
129 }
130 
134 bool MythTimer::isRunning(void) const
135 {
136  return m_timer.isValid();
137 }
138 
146 {
147  m_offset += ms;
148 }
int restart(void)
Returns milliseconds elapsed since last start() or restart() and resets the count.
Definition: mythtimer.cpp:62
MythTimer(StartState state=kStartInactive)
Creates a timer.
Definition: mythtimer.cpp:38
int m_offset
Definition: mythtimer.h:35
QElapsedTimer m_timer
Definition: mythtimer.h:34
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:134
int elapsed(void) const
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:90
void addMSecs(int ms)
Adds an offset to the last call to start() or restart().
Definition: mythtimer.cpp:145
#define assert(x)
int64_t nsecsElapsed(void) const
Returns nanoseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:118
void stop(void)
Stops timer, next call to isRunning() will return false and any calls to elapsed() or restart() will ...
Definition: mythtimer.cpp:77
void start(void)
starts measuring elapsed time.
Definition: mythtimer.cpp:47