diff options
| author | manuel <manuel@mausz.at> | 2020-10-19 00:52:24 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-10-19 00:52:24 +0200 |
| commit | be933ef2241d79558f91796cc5b3a161f72ebf9c (patch) | |
| tree | fe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/Stopwatch.h | |
| parent | 5f8335c1e49ce108ef3481863833c98efa00411b (diff) | |
| download | kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2 kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip | |
sync with upstream
Diffstat (limited to 'xbmc/utils/Stopwatch.h')
| -rw-r--r-- | xbmc/utils/Stopwatch.h | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/xbmc/utils/Stopwatch.h b/xbmc/utils/Stopwatch.h new file mode 100644 index 0000000..186a54c --- /dev/null +++ b/xbmc/utils/Stopwatch.h | |||
| @@ -0,0 +1,100 @@ | |||
| 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 <stdint.h> | ||
| 12 | |||
| 13 | class CStopWatch | ||
| 14 | { | ||
| 15 | public: | ||
| 16 | explicit CStopWatch(bool useFrameTime=false); | ||
| 17 | ~CStopWatch(); | ||
| 18 | |||
| 19 | /*! | ||
| 20 | \brief Retrieve the running state of the stopwatch. | ||
| 21 | |||
| 22 | \return True if stopwatch has been started but not stopped. | ||
| 23 | */ | ||
| 24 | inline bool IsRunning() const | ||
| 25 | { | ||
| 26 | return m_isRunning; | ||
| 27 | } | ||
| 28 | |||
| 29 | /*! | ||
| 30 | \brief Record start time and change state to running. | ||
| 31 | */ | ||
| 32 | inline void StartZero() | ||
| 33 | { | ||
| 34 | m_startTick = GetTicks(); | ||
| 35 | m_isRunning = true; | ||
| 36 | } | ||
| 37 | |||
| 38 | /*! | ||
| 39 | \brief Record start time and change state to running, only if the stopwatch is stopped. | ||
| 40 | */ | ||
| 41 | inline void Start() | ||
| 42 | { | ||
| 43 | if (!m_isRunning) | ||
| 44 | StartZero(); | ||
| 45 | } | ||
| 46 | |||
| 47 | /*! | ||
| 48 | \brief Record stop time and change state to not running. | ||
| 49 | */ | ||
| 50 | inline void Stop() | ||
| 51 | { | ||
| 52 | if(m_isRunning) | ||
| 53 | { | ||
| 54 | m_stopTick = GetTicks(); | ||
| 55 | m_isRunning = false; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | /*! | ||
| 60 | \brief Set the start time such that time elapsed is now zero. | ||
| 61 | */ | ||
| 62 | void Reset() | ||
| 63 | { | ||
| 64 | if (m_isRunning) | ||
| 65 | m_startTick = GetTicks(); | ||
| 66 | else | ||
| 67 | m_startTick = m_stopTick; | ||
| 68 | } | ||
| 69 | |||
| 70 | /*! | ||
| 71 | \brief Retrieve time elapsed between the last call to Start(), StartZero() | ||
| 72 | or Reset() and; if running, now; if stopped, the last call to Stop(). | ||
| 73 | |||
| 74 | \return Elapsed time, in seconds, as a float. | ||
| 75 | */ | ||
| 76 | float GetElapsedSeconds() const | ||
| 77 | { | ||
| 78 | int64_t totalTicks = (m_isRunning ? GetTicks() : m_stopTick) - m_startTick; | ||
| 79 | return (float)totalTicks * m_timerPeriod; | ||
| 80 | } | ||
| 81 | |||
| 82 | /*! | ||
| 83 | \brief Retrieve time elapsed between the last call to Start(), StartZero() | ||
| 84 | or Reset() and; if running, now; if stopped, the last call to Stop(). | ||
| 85 | |||
| 86 | \return Elapsed time, in milliseconds, as a float. | ||
| 87 | */ | ||
| 88 | float GetElapsedMilliseconds() const | ||
| 89 | { | ||
| 90 | return GetElapsedSeconds() * 1000.0f; | ||
| 91 | } | ||
| 92 | |||
| 93 | private: | ||
| 94 | int64_t GetTicks() const; | ||
| 95 | float m_timerPeriod; // to save division in GetElapsed...() | ||
| 96 | int64_t m_startTick; | ||
| 97 | int64_t m_stopTick; | ||
| 98 | bool m_isRunning; | ||
| 99 | bool m_useFrameTime; | ||
| 100 | }; | ||
