]> git.tdb.fi Git - r2c2.git/blob - timetable.h
86b4ae2944c8e21ebb207d49e27e54a946d62dbf
[r2c2.git] / 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(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(const std::string &);
40                 void wait(unsigned);
41                 void wait_train(unsigned, unsigned);
42         };
43
44         enum RowType
45         {
46                 GOTO_SENSOR,
47                 GOTO_ZONE,
48                 TRAVEL,
49                 WAIT_TIME,
50                 WAIT_TRAIN,
51                 ARRIVE,
52                 SPEED,
53                 REVERSE,
54                 ROUTE
55         };
56
57         struct Row
58         {
59                 RowType type;
60                 std::vector<Msp::Variant> params;
61
62                 Row(RowType);
63
64                 template<typename T>
65                 Row(RowType, const T &);
66
67                 template<typename T>
68                 const T &get_param(unsigned) const;
69
70                 std::string str() const;
71
72                 Msp::DataFile::Statement save() const;
73
74                 static Row parse(const std::string &);
75         };
76
77 private:
78         Train &train;
79         bool enabled;
80         std::vector<Row> rows;
81         unsigned current_row;
82         bool executing;
83         Block *pending_block;
84         Train *pending_train;
85         Msp::Time::TimeStamp wait_timeout;
86
87 public:
88         Timetable(Train &);
89
90         void set_enabled(bool);
91         bool is_enabled() const { return enabled; }
92         void reset();
93
94         void clear();
95         void append(const Row &);
96         void insert(unsigned, const Row &);
97         unsigned get_n_rows() const { return rows.size(); }
98         const Row &get_row(unsigned) const;
99
100         void tick(const Msp::Time::TimeStamp &);
101         void save(std::list<Msp::DataFile::Statement> &) const;
102 private:
103         Track &get_sensor(unsigned);
104         Zone &get_zone(const std::string &);
105         void sensor_event(unsigned, bool);
106         void train_arrived();
107 };
108
109 } // namespace R2C2
110
111 #endif