]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.h
Fix a segfault in removing vehicles when creating a new train
[r2c2.git] / source / libmarklin / train.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBMARKLIN_TRAIN_H_
9 #define LIBMARKLIN_TRAIN_H_
10
11 #include <sigc++/signal.h>
12 #include <sigc++/trackable.h>
13 #include <msp/time/timestamp.h>
14 #include "block.h"
15 #include "blockiter.h"
16 #include "controller.h"
17
18 namespace Marklin {
19
20 class ArticleNumber;
21 class Route;
22 class Timetable;
23 class Vehicle;
24 class VehicleType;
25
26 class Train: public sigc::trackable
27 {
28 public:
29         class Loader: public Msp::DataFile::BasicLoader<Train>
30         {
31         private:
32                 Block *prev_block;
33                 bool blocks_valid;
34
35         public:
36                 Loader(Train &);
37         private:
38                 virtual void finish();
39                 void block(unsigned);
40                 void block_hint(unsigned);
41                 void name(const std::string &);
42                 void real_speed(unsigned, float, float);
43                 void route(const std::string &);
44                 void timetable();
45                 void vehicle(ArticleNumber);
46         };
47
48         sigc::signal<void, const std::string &> signal_name_changed;
49         sigc::signal<void, const std::string &, float> signal_control_changed;
50         sigc::signal<void, unsigned, bool> signal_function_changed;
51         sigc::signal<void, const Route *> signal_route_changed;
52         sigc::signal<void> signal_arrived;
53         sigc::signal<void, const std::string &> signal_status_changed;
54
55 private:
56         struct RouteRef
57         {
58                 const Route *route;
59                 unsigned diversion;
60
61                 RouteRef(const Route *, unsigned = 0);
62         };
63
64         struct RealSpeed
65         {
66                 float speed;
67                 float weight;
68
69                 RealSpeed();
70                 void add(float, float);
71         };
72
73         typedef std::list<BlockIter> BlockList;
74
75         Layout &layout;
76         const VehicleType &loco_type;
77         unsigned address;
78         std::string protocol;
79         std::string name;
80         int priority;
81         const Train *yielding_to;
82         std::vector<Vehicle *> vehicles;
83         BlockList blocks;
84         BlockList::iterator cur_blocks_end;
85         BlockList::iterator clear_blocks_end;
86         Block *pending_block;
87         bool reserving;
88         bool advancing;
89         Controller *controller;
90         Timetable *timetable;
91         bool active;
92         unsigned current_speed_step;
93         bool speed_changing;
94         bool reverse;
95         Msp::Time::TimeStamp stop_timeout;
96         unsigned functions;
97         std::list<RouteRef> routes;
98         bool end_of_route;
99         std::string status;
100
101         Msp::Time::TimeStamp last_entry_time;
102         float travel_dist;
103         bool pure_speed;
104         std::vector<RealSpeed> real_speed;
105         bool accurate_position;
106         float overshoot_dist;
107
108 public:
109         Train(Layout &, const VehicleType &, unsigned, const std::string &);
110         ~Train();
111
112         Layout &get_layout() const { return layout; }
113         const VehicleType &get_locomotive_type() const { return loco_type; }
114         unsigned get_address() const { return address; }
115         const std::string &get_protocol() const { return protocol; }
116         void set_name(const std::string &);
117         const std::string &get_name() const { return name; }
118         void set_priority(int);
119         void yield_to(const Train &);
120         int get_priority() const { return priority; }
121         Controller &get_controller() const { return *controller; }
122
123         void add_vehicle(const VehicleType &);
124         void remove_vehicle(unsigned);
125         unsigned get_n_vehicles() const;
126         Vehicle &get_vehicle(unsigned);
127         const Vehicle &get_vehicle(unsigned) const;
128
129         void set_control(const std::string &, float);
130         void set_active(bool);
131         void set_function(unsigned, bool);
132         float get_control(const std::string &) const;
133         float get_speed() const;
134         bool is_active() const { return active; }
135         bool get_function(unsigned) const;
136         unsigned get_functions() const { return functions; }
137
138         void set_timetable(Timetable *);
139         Timetable *get_timetable() { return timetable; }
140
141         bool set_route(const Route *);
142         bool go_to(Track &);
143         bool divert(Track &);
144         const Route *get_route() const;
145         void place(Block &, unsigned);
146         void unplace();
147         bool is_placed() const { return !blocks.empty(); }
148         bool free_block(Block &);
149         void free_noncritical_blocks();
150         int get_entry_to_block(Block &) const;
151         float get_reserved_distance() const;
152
153         const std::string &get_status() const { return status; }
154
155         void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
156
157         void save(std::list<Msp::DataFile::Statement> &) const;
158 private:
159         void control_changed(const Controller::Control &);
160         void loco_speed_event(unsigned, unsigned, bool);
161         void loco_func_event(unsigned, unsigned, bool);
162         void sensor_event(unsigned, bool);
163         void turnout_path_changed(Track &);
164         void halt_event(bool);
165         void block_reserved(const Block &, const Train *);
166         void reserve_more();
167         void check_turnout_paths(bool);
168         float get_reserved_distance_until(const Block *, bool) const;
169         float get_real_speed(unsigned) const;
170         unsigned find_speed_step(float) const;
171         float get_travel_speed() const;
172         void set_status(const std::string &);
173         void release_blocks();
174         void release_blocks(BlockList::iterator, BlockList::iterator);
175         void reverse_blocks(BlockList &) const;
176         bool advance_route(std::list<RouteRef>::iterator &, Track &);
177         Route *create_lead_route(Route *, const Route *);
178         bool is_valid_diversion(const Route &, const TrackIter &);
179 };
180
181 } // namespace Marklin
182
183 #endif