]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/clock.cpp
Sync timetable on clock discontinuity
[r2c2.git] / source / libr2c2 / clock.cpp
1 #include "clock.h"
2
3 using namespace std;
4 using namespace Msp;
5
6 namespace R2C2 {
7
8 Clock::Clock():
9         rate(1),
10         stopped(false)
11 { }
12
13 void Clock::set_rate(float s)
14 {
15         rate = s;
16 }
17
18 void Clock::set_current_time(const Time::TimeDelta &t)
19 {
20         current_time = t;
21         signal_discontinuity.emit();
22 }
23
24 void Clock::stop(bool s)
25 {
26         stopped = s;
27 }
28
29 void Clock::tick(const Time::TimeDelta &dt)
30 {
31         if(stopped)
32                 return;
33
34         unsigned prev_minute = current_time/Time::min;
35         current_time += dt*rate;
36         if(current_time>=Time::day)
37                 current_time -= Time::day;
38         unsigned minute = current_time/Time::min;
39         if(minute!=prev_minute)
40                 signal_minute.emit();
41 }
42
43 void Clock::save(list<DataFile::Statement> &st) const
44 {
45         st.push_back((DataFile::Statement("rate"), rate));
46         st.push_back((DataFile::Statement("time"), current_time.raw()));
47 }
48
49
50 Clock::Loader::Loader(Clock &c):
51         DataFile::ObjectLoader<Clock>(c)
52 {
53         add("rate", &Clock::rate);
54         add("time", &Loader::time);
55 }
56
57 void Clock::Loader::time(Time::RawTime t)
58 {
59         obj.set_current_time(Time::TimeDelta(t));
60 }
61
62 } // namespace R2C2