]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/timetable.h
Add reverse and arrival row types to Timetable
[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 reverse();
34                 void speed(unsigned);
35                 void travel(const std::string &);
36                 void wait(unsigned);
37         };
38
39         enum RowType
40         {
41                 GOTO,
42                 TRAVEL,
43                 WAIT,
44                 ARRIVE,
45                 SPEED,
46                 REVERSE,
47                 ROUTE
48         };
49
50         struct Row
51         {
52                 RowType type;
53                 std::vector<Msp::Variant> params;
54
55                 Row(RowType);
56
57                 template<typename T>
58                 Row(RowType, const T &);
59
60                 template<typename T>
61                 const T &get_param(unsigned) const;
62
63                 std::string str() const;
64
65                 Msp::DataFile::Statement save() const;
66
67                 static Row parse(const std::string &);
68         };
69
70 private:
71         Train &train;
72         bool enabled;
73         std::vector<Row> rows;
74         unsigned current_row;
75         bool executing;
76         Block *pending_block;
77         Msp::Time::TimeStamp wait_timeout;
78
79 public:
80         Timetable(Train &);
81
82         void set_enabled(bool);
83         bool is_enabled() const { return enabled; }
84         void reset();
85
86         void clear();
87         void append(const Row &);
88         void insert(unsigned, const Row &);
89         unsigned get_n_rows() const { return rows.size(); }
90         const Row &get_row(unsigned) const;
91
92         void tick(const Msp::Time::TimeStamp &);
93         void save(std::list<Msp::DataFile::Statement> &) const;
94 private:
95         Block &parse_location(const std::string &);
96         void sensor_event(unsigned, bool);
97         void train_arrived();
98 };
99
100 } // namespace Marklin
101
102 #endif