]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/timetable.h
Rename ControlModel to Controller
[r2c2.git] / source / libmarklin / timetable.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBMARKLIN_TIMETABLE_H_
9 #define LIBMARKLIN_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 Marklin {
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 speed(int);
34                 void travel(const std::string &);
35                 void wait(unsigned);
36         };
37
38         enum RowType
39         {
40                 GOTO,
41                 TRAVEL,
42                 WAIT,
43                 ARRIVE,
44                 SPEED,
45                 ROUTE
46         };
47
48         struct Row
49         {
50                 RowType type;
51                 int intparam;
52                 std::string strparam;
53
54                 Row(RowType, int);
55                 Row(RowType, const std::string &);
56
57                 std::string str() const;
58
59                 static Row parse(const std::string &);
60         };
61
62 private:
63         Train &train;
64         bool enabled;
65         std::vector<Row> rows;
66         unsigned current_row;
67         bool executing;
68         Block *pending_block;
69         Msp::Time::TimeStamp wait_timeout;
70
71 public:
72         Timetable(Train &);
73
74         void set_enabled(bool);
75         bool is_enabled() const { return enabled; }
76         void reset();
77
78         void clear();
79         void append(const Row &);
80         void insert(unsigned, const Row &);
81         unsigned get_n_rows() const { return rows.size(); }
82         const Row &get_row(unsigned) const;
83
84         void tick(const Msp::Time::TimeStamp &);
85         void save(std::list<Msp::DataFile::Statement> &) const;
86 private:
87         Block &parse_location(const std::string &);
88         void sensor_event(unsigned, bool);
89         void train_arrived();
90 };
91
92 } // namespace Marklin
93
94 #endif