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