]> git.tdb.fi Git - r2c2.git/blob - libr2c2/layout.h
Don't crash if a train has no router
[r2c2.git] / libr2c2 / layout.h
1 #ifndef LIBR2C2_LAYOUT_H_
2 #define LIBR2C2_LAYOUT_H_
3
4 #include <set>
5 #include <sigc++/sigc++.h>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/time/timestamp.h>
8 #include "geometry.h"
9 #include "sensor.h"
10 #include "clock.h"
11
12 namespace R2C2 {
13
14 class ArticleNumber;
15 class BeamGate;
16 class Block;
17 class Catalogue;
18 class Driver;
19 class Object;
20 class Route;
21 class Signal;
22 class Track;
23 class TrackChain;
24 class Train;
25 class Vehicle;
26 class Zone;
27
28 class Layout
29 {
30 public:
31         class Loader: public Msp::DataFile::ObjectLoader<Layout>
32         {
33         public:
34                 Loader(Layout &);
35         private:
36                 void beamgate();
37                 void route();
38                 void signal(ArticleNumber);
39                 void terrain(ArticleNumber);
40                 void track(ArticleNumber);
41                 void train(ArticleNumber, unsigned, const std::string &);
42                 void turnout(unsigned, unsigned);
43                 void zone();
44         };
45
46 private:
47         template<typename B>
48         class Storage
49         {
50         private:
51                 std::set<B *> base;
52                 mutable std::list<Msp::Variant> caches;
53
54         public:
55                 ~Storage();
56
57                 bool insert(B &);
58                 bool erase(B &);
59
60                 const std::set<B *> &get() const { return base; }
61
62                 template<typename T>
63                 const std::set<T *> &get() const;
64
65                 template<typename T>
66                 void del();
67         };
68
69 public:
70         typedef std::vector<Zone *> ZoneArray;
71
72         sigc::signal<void, Object &> signal_object_added;
73         sigc::signal<void, Object &> signal_object_removed;
74         sigc::signal<void, TrackChain &> signal_track_chain_added;
75         sigc::signal<void, TrackChain &> signal_track_chain_removed;
76         sigc::signal<void, Train &> signal_train_added;
77         sigc::signal<void, Train &> signal_train_removed;
78         sigc::signal<void, Block &, Train *> signal_block_reserved;
79         sigc::signal<void, Sensor &, Sensor::State> signal_sensor_state_changed;
80         sigc::signal<void, const std::string &> signal_emergency;
81
82 private:
83         Catalogue &catalogue;
84         Driver *driver;
85         Clock clock;
86         std::string base;
87         Storage<Object> objects;
88         Storage<TrackChain> track_chains;
89         Storage<Sensor> sensors;
90         std::map<unsigned, Train *> trains;
91         Msp::Time::TimeStamp last_tick;
92         unsigned next_turnout_addr;
93
94 public:
95         Layout(Catalogue &, Driver * = 0);
96         ~Layout();
97
98         Catalogue &get_catalogue() const { return catalogue; }
99         bool has_driver() const { return driver; }
100         Driver &get_driver() const;
101         const Clock &get_clock() const { return clock; }
102         const std::string &get_base() const { return base; }
103
104         void add(Object &);
105         void add(Track &);
106         void add(TrackChain &);
107         void add(Block &);
108         void add(Sensor &);
109         void add(BeamGate &);
110
111         template<typename T>
112         const std::set<T *> &get_all() const;
113
114         template<typename T>
115         T *pick(const Ray &);
116
117         void remove(Object &);
118         void remove(Track &);
119         void remove(TrackChain &);
120         void remove(Sensor &);
121         void remove(BeamGate &);
122
123         unsigned allocate_turnout_address();
124
125         Block &get_block(unsigned) const;
126         void create_blocks();
127         void create_blocks(Track &);
128
129         Route &get_route(const std::string &) const;
130         void update_routes();
131
132         ZoneArray get_zones(const std::string &) const;
133         Zone &get_zone(const std::string &, unsigned) const;
134
135         void add_train(Train &);
136         Train &get_train(unsigned) const;
137         const std::map<unsigned, Train *> &get_trains() const { return trains; }
138         void remove_train(Train &);
139
140         void tick();
141         void emergency(const std::string &);
142
143         void save(const std::string &) const;
144         void save_dynamic(const std::string &) const;
145 private:
146         void sensor_state_changed(Sensor &, Sensor::State);
147 };
148
149 } // namespace R2C2
150
151 #endif
152