]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/timetable.h
Rename the project to R²C²
[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 Train;
21
22 class Timetable: public sigc::trackable
23 {
24 public:
25         class Loader: public Msp::DataFile::ObjectLoader<Timetable>
26         {
27         public:
28                 Loader(Timetable &);
29         private:
30                 void arrive();
31                 void go_to(const std::string &);
32                 void route(const std::string &);
33                 void reverse();
34                 void speed(unsigned);
35                 void travel(const std::string &);
36                 void wait(unsigned);
37                 void wait_train(unsigned, const std::string &);
38         };
39
40         enum RowType
41         {
42                 GOTO,
43                 TRAVEL,
44                 WAIT_TIME,
45                 WAIT_TRAIN,
46                 ARRIVE,
47                 SPEED,
48                 REVERSE,
49                 ROUTE
50         };
51
52         struct Row
53         {
54                 RowType type;
55                 std::vector<Msp::Variant> params;
56
57                 Row(RowType);
58
59                 template<typename T>
60                 Row(RowType, const T &);
61
62                 template<typename T>
63                 const T &get_param(unsigned) const;
64
65                 std::string str() const;
66
67                 Msp::DataFile::Statement save() const;
68
69                 static Row parse(const std::string &);
70         };
71
72 private:
73         Train &train;
74         bool enabled;
75         std::vector<Row> rows;
76         unsigned current_row;
77         bool executing;
78         Block *pending_block;
79         Train *pending_train;
80         Msp::Time::TimeStamp wait_timeout;
81
82 public:
83         Timetable(Train &);
84
85         void set_enabled(bool);
86         bool is_enabled() const { return enabled; }
87         void reset();
88
89         void clear();
90         void append(const Row &);
91         void insert(unsigned, const Row &);
92         unsigned get_n_rows() const { return rows.size(); }
93         const Row &get_row(unsigned) const;
94
95         void tick(const Msp::Time::TimeStamp &);
96         void save(std::list<Msp::DataFile::Statement> &) const;
97 private:
98         Block &parse_location(const std::string &);
99         void sensor_event(unsigned, bool);
100         void train_arrived();
101 };
102
103 } // namespace R2C2
104
105 #endif