]> git.tdb.fi Git - r2c2.git/blob - libr2c2/timetable.h
Don't crash if a train has no router
[r2c2.git] / libr2c2 / timetable.h
1 #ifndef LIBR2C2_TIMETABLE_H_
2 #define LIBR2C2_TIMETABLE_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "trainai.h"
6
7 namespace R2C2 {
8
9 class Layout;
10 class Zone;
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         };
32
33         struct Row
34         {
35                 class Loader: public Msp::DataFile::ObjectLoader<Row>
36                 {
37                 private:
38                         Layout &layout;
39
40                 public:
41                         Loader(Row &, Layout &);
42
43                 private:
44                         void zone(const std::string &, unsigned);
45                         void time(Msp::Time::RawTime);
46                 };
47
48                 RowType type;
49                 Zone *zone;
50                 Msp::Time::TimeDelta time;
51
52                 Row();
53
54                 void save(std::list<Msp::DataFile::Statement> &) const;
55         };
56
57         sigc::signal<void, unsigned, const Row &> signal_row_added;
58         sigc::signal<void, unsigned, const Row &> signal_row_modified;
59         sigc::signal<void, unsigned> signal_row_removed;
60
61 private:
62         std::list<Row> rows;
63         std::list<Row>::iterator current_row;
64         bool update_pending;
65
66 public:
67         Timetable(Train &);
68
69         void append_row(const Row &);
70         void insert_row(unsigned, const Row &);
71         void modify_row(unsigned, const Row &);
72         void remove_row(unsigned);
73
74         unsigned get_length() const { return rows.size(); }
75         const Row &get_row(unsigned) const;
76
77         virtual void tick(const Msp::Time::TimeDelta &);
78
79         void save(std::list<Msp::DataFile::Statement> &) const;
80
81 private:
82         void check_update(std::list<Row>::const_iterator);
83         void update_route();
84         void event(TrainAI &, const Message &);
85 };
86
87 } // namespace R2C2
88
89 #endif