Loading...
Searching...
No Matches
Timers.hpp
1#pragma once
2#include <QObject>
3#include <memory>
4#include <mutex>
5#include <verdigris>
6#include <ossia/detail/small_flat_map.hpp>
7#include <score_lib_base_export.h>
8
9namespace score
10{
11class HighResolutionTimerPrivate;
12
13class SCORE_LIB_BASE_EXPORT HighResolutionTimer : public QObject
14{
15 W_OBJECT(HighResolutionTimer)
16
17public:
18 explicit HighResolutionTimer(double frequencyHz, QObject* parent = nullptr);
20
21 void start();
22 void stop();
23
24 bool isRunning() const;
25 double frequency() const;
26
27 void timeout(score::HighResolutionTimer* self) E_SIGNAL(SCORE_LIB_BASE_EXPORT, timeout, self);
28 void started(score::HighResolutionTimer* self) E_SIGNAL(SCORE_LIB_BASE_EXPORT, started, self);
29 void stopped(score::HighResolutionTimer* self) E_SIGNAL(SCORE_LIB_BASE_EXPORT, stopped, self);
30
31 void setMaximumAccuracy(bool);
32private:
33 void timerEvent(QTimerEvent *k) override;
34 std::unique_ptr<HighResolutionTimerPrivate> d;
35};
36
37class SCORE_LIB_BASE_EXPORT Timers : public QObject
38{
39 W_OBJECT(Timers)
40
41public:
42 static Timers& instance();
43
44 explicit Timers(QObject* parent = nullptr);
45 ~Timers() override;
46
47 HighResolutionTimer* acquireTimer(QObject* user, double frequencyHz);
48 void releaseTimer(QObject* user, HighResolutionTimer* timerId);
49
50private:
51 struct TimerEntry
52 {
53 std::unique_ptr<HighResolutionTimer> timer;
54 ossia::small_flat_map<QObject*, int, 8> users;
55 };
56
57 mutable std::mutex m_mutex;
58 ossia::small_flat_map<double, TimerEntry, 16> m_timers; // frequency -> entry
59};
60}
61
62W_REGISTER_ARGTYPE(score::HighResolutionTimer*)
Definition Timers.hpp:14
Definition Timers.hpp:38
Base toolkit upon which the software is built.
Definition Application.cpp:99