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