|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 namespace system_time_periods {
|
|
|
4 static constexpr t_filetimestamp second = filetimestamp_1second_increment;
|
|
|
5 static constexpr t_filetimestamp minute = second * 60;
|
|
|
6 static constexpr t_filetimestamp hour = minute * 60;
|
|
|
7 static constexpr t_filetimestamp day = hour * 24;
|
|
|
8 static constexpr t_filetimestamp week = day * 7;
|
|
|
9 };
|
|
|
10 class system_time_callback {
|
|
|
11 public:
|
|
|
12 virtual void on_time_changed(t_filetimestamp newVal) = 0;
|
|
|
13 };
|
|
|
14 //! \since 0.9.6
|
|
|
15 class system_time_keeper : public service_base {
|
|
|
16 public:
|
|
|
17 //! The callback object receives an on_changed() call with the current time inside the register_callback() call.
|
|
|
18 virtual void register_callback(system_time_callback * callback, t_filetimestamp resolution) = 0;
|
|
|
19
|
|
|
20 virtual void unregister_callback(system_time_callback * callback) = 0;
|
|
|
21
|
|
|
22 FB2K_MAKE_SERVICE_COREAPI(system_time_keeper)
|
|
|
23 };
|
|
|
24
|
|
|
25 class system_time_callback_impl : public system_time_callback {
|
|
|
26 public:
|
|
|
27 system_time_callback_impl() {}
|
|
|
28 ~system_time_callback_impl() {stop_timer();}
|
|
|
29
|
|
|
30 void stop_timer() {
|
|
|
31 if (m_registered) {
|
|
|
32 system_time_keeper::get()->unregister_callback(this);
|
|
|
33 m_registered = false;
|
|
|
34 }
|
|
|
35 }
|
|
|
36 //! You get a on_changed() call inside the initialize_timer() call.
|
|
|
37 void initialize_timer(t_filetimestamp period) {
|
|
|
38 stop_timer();
|
|
|
39 system_time_keeper::get()->register_callback(this, period);
|
|
|
40 m_registered = true;
|
|
|
41 }
|
|
|
42
|
|
|
43 //! Override me
|
|
|
44 void on_time_changed(t_filetimestamp) override {}
|
|
|
45
|
|
|
46 PFC_CLASS_NOT_COPYABLE_EX(system_time_callback_impl)
|
|
|
47 private:
|
|
|
48 bool m_registered = false;
|
|
|
49 };
|
|
|
50
|