]> 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 clock();
38                 void route();
39                 void signal(ArticleNumber);
40                 void terrain(ArticleNumber);
41                 void track(ArticleNumber);
42                 void train(ArticleNumber, unsigned, const std::string &);
43                 void turnout(unsigned, unsigned);
44                 void zone();
45         };
46
47 private:
48         template<typename B>
49         class Storage
50         {
51         private:
52                 std::set<B *> base;
53                 mutable std::list<Msp::Variant> caches;
54
55         public:
56                 ~Storage();
57
58                 bool insert(B &);
59                 bool erase(B &);
60
61                 const std::set<B *> &get() const { return base; }
62
63                 template<typename T>
64                 const std::set<T *> &get() const;
65
66                 template<typename T>
67                 void del();
68         };
69
70 public:
71         typedef std::vector<Zone *> ZoneArray;
72
73         sigc::signal<void, Object &> signal_object_added;
74         sigc::signal<void, Object &> signal_object_removed;
75         sigc::signal<void, TrackChain &> signal_track_chain_added;
76         sigc::signal<void, TrackChain &> signal_track_chain_removed;
77         sigc::signal<void, Train &> signal_train_added;
78         sigc::signal<void, Train &> signal_train_removed;
79         sigc::signal<void, Block &, Train *> signal_block_reserved;
80         sigc::signal<void, Sensor &, Sensor::State> signal_sensor_state_changed;
81         sigc::signal<void, const std::string &> signal_emergency;
82
83 private:
84         Catalogue &catalogue;
85         Driver *driver;
86         Clock clock;
87         std::string base;
88         Storage<Object> objects;
89         Storage<TrackChain> track_chains;
90         Storage<Sensor> sensors;
91         std::map<unsigned, Train *> trains;
92         Msp::Time::TimeStamp last_tick;
93         unsigned next_turnout_addr;
94
95 public:
96         Layout(Catalogue &, Driver * = 0);
97         ~Layout();
98
99         Catalogue &get_catalogue() const { return catalogue; }
100         bool has_driver() const { return driver; }
101         Driver &get_driver() const;
102         Clock &get_clock() { return clock; }
103         const std::string &get_base() const { return base; }
104
105         void add(Object &);
106         void add(Track &);
107         void add(TrackChain &);
108         void add(Block &);
109         void add(Sensor &);
110         void add(BeamGate &);
111
112         template<typename T>
113         const std::set<T *> &get_all() const;
114
115         template<typename T>
116         T *pick(const Ray &);
117
118         void remove(Object &);
119         void remove(Track &);
120         void remove(TrackChain &);
121         void remove(Sensor &);
122         void remove(BeamGate &);
123
124         unsigned allocate_turnout_address();
125
126         Block &get_block(unsigned) const;
127         void create_blocks();
128         void create_blocks(Track &);
129
130         Route &get_route(const std::string &) const;
131         void update_routes();
132
133         ZoneArray get_zones(const std::string &) const;
134         Zone &get_zone(const std::string &, unsigned) const;
135
136         void add_train(Train &);
137         Train &get_train(unsigned) const;
138         const std::map<unsigned, Train *> &get_trains() const { return trains; }
139         void remove_train(Train &);
140
141         void tick();
142         void emergency(const std::string &);
143
144         void save(const std::string &) const;
145         void save_dynamic(const std::string &) const;
146 private:
147         void sensor_state_changed(Sensor &, Sensor::State);
148 };
149
150 } // namespace R2C2
151
152 #endif
153