xbmc
Stopwatch.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <chrono>
12 #include <stdint.h>
13 
15 {
16 public:
17  CStopWatch() = default;
18  ~CStopWatch() = default;
19 
25  inline bool IsRunning() const
26  {
27  return m_isRunning;
28  }
29 
33  inline void StartZero()
34  {
35  m_startTick = std::chrono::steady_clock::now();
36  m_isRunning = true;
37  }
38 
42  inline void Start()
43  {
44  if (!m_isRunning)
45  StartZero();
46  }
47 
51  inline void Stop()
52  {
53  if(m_isRunning)
54  {
55  m_stopTick = std::chrono::steady_clock::now();
56  m_isRunning = false;
57  }
58  }
59 
63  void Reset()
64  {
65  if (m_isRunning)
66  m_startTick = std::chrono::steady_clock::now();
67  else
68  m_startTick = m_stopTick;
69  }
70 
77  float GetElapsedSeconds() const
78  {
79  std::chrono::duration<float> elapsed;
80 
81  if (m_isRunning)
82  elapsed = std::chrono::steady_clock::now() - m_startTick;
83  else
84  elapsed = m_stopTick - m_startTick;
85 
86  return elapsed.count();
87  }
88 
95  float GetElapsedMilliseconds() const
96  {
97  std::chrono::duration<float, std::milli> elapsed;
98 
99  if (m_isRunning)
100  elapsed = std::chrono::steady_clock::now() - m_startTick;
101  else
102  elapsed = m_stopTick - m_startTick;
103 
104  return elapsed.count();
105  }
106 
107 private:
108  std::chrono::time_point<std::chrono::steady_clock> m_startTick;
109  std::chrono::time_point<std::chrono::steady_clock> m_stopTick;
110  bool m_isRunning = false;
111 };
Definition: Stopwatch.h:14
void Start()
Record start time and change state to running, only if the stopwatch is stopped.
Definition: Stopwatch.h:42
float GetElapsedMilliseconds() const
Retrieve time elapsed between the last call to Start(), StartZero() or Reset() and; if running...
Definition: Stopwatch.h:95
float GetElapsedSeconds() const
Retrieve time elapsed between the last call to Start(), StartZero() or Reset() and; if running...
Definition: Stopwatch.h:77
void Stop()
Record stop time and change state to not running.
Definition: Stopwatch.h:51
void Reset()
Set the start time such that time elapsed is now zero.
Definition: Stopwatch.h:63
bool IsRunning() const
Retrieve the running state of the stopwatch.
Definition: Stopwatch.h:25
void StartZero()
Record start time and change state to running.
Definition: Stopwatch.h:33