Merge pull request #26278 from basilgello/taglib2-fix-piers
[xbmc.git] / xbmc / threads / Timer.h
blobf434a385bdcd8662fefe3240c901ec41d877d6c3
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "Event.h"
12 #include "Thread.h"
14 #include <chrono>
15 #include <functional>
17 class ITimerCallback
19 public:
20 virtual ~ITimerCallback() = default;
22 virtual void OnTimeout() = 0;
25 class CTimer : protected CThread
27 public:
28 explicit CTimer(ITimerCallback *callback);
29 explicit CTimer(std::function<void()> const& callback);
30 ~CTimer() override;
32 bool Start(std::chrono::milliseconds timeout, bool interval = false);
33 bool Stop(bool wait = false);
34 bool Restart();
35 void RestartAsync(std::chrono::milliseconds timeout);
37 bool IsRunning() const { return CThread::IsRunning(); }
39 float GetElapsedSeconds() const;
40 float GetElapsedMilliseconds() const;
42 protected:
43 void Process() override;
45 private:
46 std::function<void()> m_callback;
47 std::chrono::milliseconds m_timeout;
48 bool m_interval;
49 std::chrono::time_point<std::chrono::steady_clock> m_endTime;
50 CEvent m_eventTimeout;