]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/timetable.h
Handle sensors in a separate class
[r2c2.git] / source / libr2c2 / timetable.h
1 #ifndef LIBR2C2_TIMETABLE_H_
2 #define LIBR2C2_TIMETABLE_H_
3
4 #include <string>
5 #include <vector>
6 #include <sigc++/trackable.h>
7 #include <msp/datafile/objectloader.h>
8 #include "sensor.h"
9 #include "trainai.h"
10
11 namespace R2C2 {
12
13 class Block;
14 class Track;
15 class Train;
16 class Zone;
17
18 class Timetable: public TrainAI, public sigc::trackable
19 {
20 public:
21         class Loader: public Msp::DataFile::ObjectLoader<Timetable>
22         {
23         public:
24                 Loader(Timetable &);
25         private:
26                 void arrive();
27                 void goto_sensor(unsigned);
28                 void goto_sensor_str(const std::string &);
29                 void goto_zone(const std::string &);
30                 void route(const std::string &);
31                 void reverse();
32                 void speed(unsigned);
33                 void travel_to(unsigned);
34                 void travel_past(unsigned);
35                 void wait(unsigned);
36                 void wait_train(unsigned, unsigned);
37                 void wait_until(unsigned, unsigned);
38         };
39
40         enum RowType
41         {
42                 GOTO_SENSOR,
43                 GOTO_ZONE,
44                 TRAVEL_TO,
45                 TRAVEL_PAST,
46                 WAIT_TIME,
47                 WAIT_UNTIL,
48                 WAIT_TRAIN,
49                 ARRIVE,
50                 SPEED,
51                 REVERSE,
52                 ROUTE
53         };
54
55         struct Row
56         {
57                 RowType type;
58                 std::vector<Msp::Variant> params;
59
60                 Row(RowType);
61
62                 template<typename T>
63                 Row(RowType, const T &);
64
65                 template<typename T>
66                 const T &get_param(unsigned) const;
67
68                 std::string str() const;
69
70                 Msp::DataFile::Statement save() const;
71
72                 static Row parse(const std::string &);
73         };
74
75 private:
76         bool enabled;
77         std::vector<Row> rows;
78         unsigned current_row;
79         bool executing;
80         Block *pending_block;
81         Train *pending_train;
82         Msp::Time::TimeStamp wait_timeout;
83         bool arrived;
84
85 public:
86         Timetable(Train &);
87
88         void set_enabled(bool);
89         bool is_enabled() const { return enabled; }
90         void reset();
91
92         void clear();
93         void append(const Row &);
94         void insert(unsigned, const Row &);
95         unsigned get_n_rows() const { return rows.size(); }
96         const Row &get_row(unsigned) const;
97
98         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
99         void save(std::list<Msp::DataFile::Statement> &) const;
100 private:
101         Block &get_sensor(unsigned);
102         Track &get_turnout(unsigned);
103         Zone &get_zone(const std::string &);
104         void sensor_state_changed(Sensor &, Sensor::State);
105         void block_reserved(Block &, Train *);
106         void train_advanced(Block &);
107         void event(TrainAI &, const Message &);
108 };
109
110 } // namespace R2C2
111
112 #endif