2 #include <msp/core/maputils.h>
3 #include <msp/core/refptr.h>
4 #include <msp/datafile/parser.h>
5 #include <msp/datafile/writer.h>
6 #include <msp/io/print.h>
7 #include <msp/time/utils.h>
14 #include "signaltype.h"
16 #include "trackcircuit.h"
17 #include "tracktype.h"
20 #include "vehicletype.h"
28 bool zone_order(const R2C2::Zone *z1, const R2C2::Zone *z2)
30 return z1->get_number()<z2->get_number();
38 Layout::Layout(Catalogue &c, Driver *d):
41 next_turnout_id(0x800)
51 while(!trains.empty())
52 delete trains.begin()->second;
53 track_chains.del<Route>();
54 track_chains.del<Zone>();
55 objects.del<Signal>();
57 track_chains.del<Block>();
60 Driver &Layout::get_driver() const
63 throw logic_error("!driver");
67 void Layout::add(Object &o)
70 signal_object_added.emit(o);
73 void Layout::add(Track &t)
77 // Blocks must be recreated first
79 signal_object_added.emit(t);
83 void Layout::add(TrackChain &g)
85 if(track_chains.insert(g))
86 signal_track_chain_added.emit(g);
89 void Layout::add(Block &b)
91 if(track_chains.insert(b))
93 b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
94 signal_track_chain_added.emit(b);
98 void Layout::add(Sensor &s)
100 if(sensors.insert(s))
102 s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
103 s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
108 const set<Object *> &Layout::get_all<Object>() const
110 return objects.get();
114 const set<Track *> &Layout::get_all<Track>() const
116 return objects.get<Track>();
120 const set<Signal *> &Layout::get_all<Signal>() const
122 return objects.get<Signal>();
126 const set<Vehicle *> &Layout::get_all<Vehicle>() const
128 return objects.get<Vehicle>();
132 const set<TrackChain *> &Layout::get_all<TrackChain>() const
134 return track_chains.get();
138 const set<Block *> &Layout::get_all<Block>() const
140 return track_chains.get<Block>();
144 const set<Route *> &Layout::get_all<Route>() const
146 return track_chains.get<Route>();
150 const set<Zone *> &Layout::get_all<Zone>() const
152 return track_chains.get<Zone>();
156 const set<Sensor *> &Layout::get_all<Sensor>() const
158 return sensors.get<Sensor>();
162 const set<TrackCircuit *> &Layout::get_all<TrackCircuit>() const
164 return sensors.get<TrackCircuit>();
168 T *Layout::pick(const Ray &ray)
170 const set<Object *> &objs = objects.get();
171 for(set<Object *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
172 if(T *t = dynamic_cast<T *>(*i))
173 if(t->collide_ray(ray))
179 template Object *Layout::pick<Object>(const Ray &);
180 template Track *Layout::pick<Track>(const Ray &);
181 template Signal *Layout::pick<Signal>(const Ray &);
182 template Vehicle *Layout::pick<Vehicle>(const Ray &);
184 void Layout::remove(Object &o)
187 signal_object_removed.emit(o);
190 void Layout::remove(Track &t)
194 // Blocks must be recreated first
196 signal_object_removed.emit(t);
200 void Layout::remove(TrackChain &g)
202 if(track_chains.erase(g))
203 signal_track_chain_removed.emit(g);
206 void Layout::remove(Sensor &s)
211 unsigned Layout::allocate_turnout_id()
213 set<unsigned> used_ids;
214 const set<Track *> &tracks = objects.get<Track>();
215 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
216 if((*i)->get_turnout_id())
217 used_ids.insert((*i)->get_turnout_id());
219 unsigned result = next_turnout_id;
220 while(used_ids.count(result))
222 next_turnout_id = result+1;
227 Block &Layout::get_block(unsigned id) const
229 const set<Block *> &blocks = track_chains.get<Block>();
230 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
231 if((*i)->get_id()==id)
237 void Layout::create_blocks()
239 set<Track *> used_tracks;
240 const set<Block *> *blocks = &track_chains.get<Block>();
241 for(set<Block *>::const_iterator i=blocks->begin(); i!=blocks->end(); ++i)
243 const set<Track *> &btracks = (*i)->get_tracks();
244 used_tracks.insert(btracks.begin(), btracks.end());
247 const set<Track *> &tracks = objects.get<Track>();
248 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
249 if(used_tracks.count(*i)==0)
251 Block *block = new Block(*this, **i);
252 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
255 blocks = &track_chains.get<Block>();
256 for(set<Block *>::iterator i=blocks->begin(); i!=blocks->end(); ++i)
257 for(set<Block *>::iterator j=i; j!=blocks->end(); ++j)
259 (*i)->check_link(**j);
262 void Layout::create_blocks(Track &track)
264 /* Must collect the blocks in a set first while all tracks are still
265 guaranteed to have blocks and to avoid duplicate deletes */
266 set<Block *> del_blocks;
268 del_blocks.insert(&track.get_block());
270 const vector<Track *> &links = track.get_links();
271 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
273 del_blocks.insert(&(*i)->get_block());
275 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
281 Route &Layout::get_route(const string &name) const
283 const set<Route *> &routes = track_chains.get<Route>();
284 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
285 if((*i)->get_name()==name)
287 throw key_error(name);
290 void Layout::update_routes()
292 const set<Route *> &routes = track_chains.get<Route>();
293 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
294 (*i)->update_turnouts();
297 Layout::ZoneArray Layout::get_zones(const string &group) const
300 const set<Zone *> &zones = track_chains.get<Zone>();
301 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
302 if((*i)->get_group()==group)
303 result.push_back(*i);
305 sort(result.begin(), result.end(), zone_order);
310 Zone &Layout::get_zone(const string &group, unsigned num) const
312 const set<Zone *> &zones = track_chains.get<Zone>();
313 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
314 if((*i)->get_group()==group && (*i)->get_number()==num)
317 throw key_error(format("%s %d", group, num));
320 void Layout::add_train(Train &t)
322 insert_unique(trains, t.get_address(), &t);
323 signal_train_added.emit(t);
326 Train &Layout::get_train(unsigned addr) const
328 return *get_item(trains, addr);
331 void Layout::remove_train(Train &t)
333 if(trains.erase(t.get_address()))
334 signal_train_removed.emit(t);
342 Time::TimeStamp t = Time::now();
350 for(set<Sensor *>::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i)
352 const set<Signal *> &signals = objects.get<Signal>();
353 for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
355 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
359 void Layout::emergency(const string &msg)
363 IO::print("Emergency: %s\n", msg);
364 signal_emergency.emit(msg);
367 void Layout::save(const string &fn) const
369 IO::BufferedFile out(fn, IO::M_WRITE);
370 DataFile::Writer writer(out);
373 writer.write((DataFile::Statement("base"), base));
375 const set<Track *> &tracks = objects.get<Track>();
376 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
378 DataFile::Statement st("track");
379 st.append((*i)->get_type().get_article_number());
384 const set<Signal *> &signals = objects.get<Signal>();
385 for(set<Signal *>::const_iterator i=signals.begin(); i!=signals.end(); ++i)
387 DataFile::Statement st("signal");
388 st.append((*i)->get_type().get_article_number());
393 const set<Route *> &routes = track_chains.get<Route>();
394 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
396 if((*i)->is_temporary())
399 DataFile::Statement st("route");
404 const set<Zone *> &zones = track_chains.get<Zone>();
405 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
407 DataFile::Statement st("zone");
413 void Layout::save_dynamic(const string &fn) const
415 IO::BufferedFile out(fn, IO::M_WRITE);
416 DataFile::Writer writer(out);
418 const set<Track *> &tracks = objects.get<Track>();
419 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
420 if(unsigned tid = (*i)->get_turnout_id())
421 writer.write((DataFile::Statement("turnout"), tid, (*i)->get_active_path()));
423 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
425 DataFile::Statement st("train");
426 st.append(i->second->get_locomotive_type().get_article_number());
427 st.append(i->second->get_address());
428 st.append(i->second->get_protocol());
429 i->second->save(st.sub);
434 void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
436 if(state==Sensor::ACTIVE)
438 Block *block = sensor.get_block();
439 if(block && !block->get_train())
440 emergency(format("Unreserved sensor %d triggered", sensor.get_address()));
446 Layout::Storage<B>::~Storage()
449 delete *base.begin();
453 bool Layout::Storage<B>::insert(B &b)
455 bool result = base.insert(&b).second;
462 bool Layout::Storage<B>::erase(B &b)
464 bool result = base.erase(&b);
472 const set<T *> &Layout::Storage<B>::get() const
474 for(list<Variant>::const_iterator i=caches.begin(); i!=caches.end(); ++i)
475 if(i->check_type<set<T *> >())
476 return i->value<set<T *> >();
478 caches.push_back(set<T *>());
479 set<T *> &result = caches.back().value<set<T *> >();
480 for(typename set<B *>::const_iterator i=base.begin(); i!=base.end(); ++i)
481 if(T *t = dynamic_cast<T *>(*i))
489 void Layout::Storage<B>::del()
491 set<T *> ts = get<T>();
492 for(typename set<T *>::iterator i=ts.begin(); i!=ts.end(); ++i)
498 Layout::Loader::Loader(Layout &l):
499 DataFile::ObjectLoader<Layout>(l),
502 add("base", &Layout::base);
503 add("route", &Loader::route);
504 add("signal", &Loader::signal);
505 add("track", &Loader::track);
506 add("train", &Loader::train);
507 add("turnout", &Loader::turnout);
508 add("zone", &Loader::zone);
511 void Layout::Loader::route()
513 Route *rte = new Route(obj);
517 void Layout::Loader::signal(ArticleNumber art_nr)
519 Signal *sig = new Signal(obj, obj.catalogue.get_signal(art_nr));
523 void Layout::Loader::track(ArticleNumber art_nr)
525 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
528 const set<Track *> &tracks = obj.objects.get<Track>();
529 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
534 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
536 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr, proto);
540 void Layout::Loader::turnout(unsigned addr, unsigned path)
543 obj.driver->set_turnout(addr, path);
546 void Layout::Loader::zone()
548 Zone *zne = new Zone(obj);