]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/timetable.h
Handle the case of a train being late
[r2c2.git] / source / libr2c2 / timetable.h
1 #ifndef LIBR2C2_TIMETABLE_H_
2 #define LIBR2C2_TIMETABLE_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "trackchain.h"
6 #include "trainai.h"
7
8 namespace R2C2 {
9
10 class Layout;
11
12 class Timetable: public TrainAI
13 {
14 public:
15         class Loader: public Msp::DataFile::ObjectLoader<Timetable>
16         {
17         private:
18                 Layout &layout;
19
20         public:
21                 Loader(Timetable &, Layout &);
22
23         private:
24                 void row();
25         };
26
27         enum RowType
28         {
29                 ARRIVE = 1,
30                 DEPART,
31                 THROUGH
32         };
33
34         struct Row
35         {
36                 class Loader: public Msp::DataFile::ObjectLoader<Row>
37                 {
38                 private:
39                         Layout &layout;
40
41                 public:
42                         Loader(Row &, Layout &);
43
44                 private:
45                         void block(unsigned);
46                         void time(Msp::Time::RawTime);
47                         void zone(const std::string &);
48                         void zone_numbered(const std::string &, unsigned);
49                 };
50
51                 RowType type;
52                 TrackChain *target;
53                 TrackChain::Direction direction;
54                 Msp::Time::TimeDelta time;
55
56                 Row();
57
58                 void save(std::list<Msp::DataFile::Statement> &) const;
59         };
60
61         struct RowTypeMatch
62         {
63                 RowType type;
64
65                 RowTypeMatch(RowType t): type(t) { }
66
67                 bool operator()(const Row &r) const { return r.type==type; }
68         };
69
70         sigc::signal<void, unsigned, const Row &> signal_row_added;
71         sigc::signal<void, unsigned, const Row &> signal_row_modified;
72         sigc::signal<void, unsigned> signal_row_removed;
73
74 private:
75         std::list<Row> rows;
76         std::list<Row>::iterator current_row;
77         bool update_pending;
78         bool sync_to_clock;
79         bool late_arrival;
80         std::list<Row>::iterator next_depart;
81
82 public:
83         Timetable(Train &);
84
85         void append_row(const Row &);
86         void insert_row(unsigned, const Row &);
87         void modify_row(unsigned, const Row &);
88         void remove_row(unsigned);
89
90         unsigned get_length() const { return rows.size(); }
91         const Row &get_row(unsigned) const;
92
93         virtual void tick(const Msp::Time::TimeDelta &);
94 private:
95         bool passed_row(const Row &, const Msp::Time::TimeDelta &) const;
96
97 public:
98         void save(std::list<Msp::DataFile::Statement> &) const;
99
100 private:
101         void check_update(const std::list<Row>::const_iterator &);
102         std::list<Row>::iterator find_trip(const std::list<Row>::iterator &, std::list<Row>::iterator *);
103         void update_route();
104         void event(TrainAI &, const Message &);
105         void record_time();
106         void clock_discontinuity();
107 };
108
109 } // namespace R2C2
110
111 #endif