2 #include <msp/core/maputils.h>
3 #include <msp/core/raii.h>
4 #include <msp/core/refptr.h>
5 #include <msp/datafile/parser.h>
6 #include <msp/datafile/writer.h>
7 #include <msp/io/print.h>
8 #include <msp/time/utils.h>
11 #include "catalogue.h"
16 #include "signaltype.h"
19 #include "trackcircuit.h"
20 #include "tracktype.h"
23 #include "vehicletype.h"
31 bool zone_order(const R2C2::Zone *z1, const R2C2::Zone *z2)
33 return z1->get_number()<z2->get_number();
41 Layout::Layout(Catalogue &c, Driver *d):
44 next_turnout_addr(0x800),
45 emitting_block_reserved(false)
55 while(!trains.empty())
56 delete trains.begin()->second;
57 track_chains.del<Route>();
58 track_chains.del<Zone>();
59 objects.del<Signal>();
61 track_chains.del<Block>();
64 Driver &Layout::get_driver() const
67 throw logic_error("!driver");
71 void Layout::add(Object &o)
77 signal_object_added.emit(o);
87 void Layout::add(Track &t)
93 // Blocks must be recreated first
95 signal_object_added.emit(t);
106 void Layout::add(TrackChain &g)
108 if(track_chains.insert(g))
112 signal_track_chain_added.emit(g);
116 track_chains.erase(g);
122 void Layout::add(Block &b)
124 if(track_chains.insert(b))
126 sigc::connection conn = b.signal_reserved.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::block_reserved), sigc::ref(b)));
129 signal_track_chain_added.emit(b);
133 track_chains.erase(b);
140 void Layout::add(Sensor &s)
142 if(sensors.insert(s))
144 s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
145 s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
149 void Layout::add(BeamGate &g)
151 add(static_cast<Object &>(g));
152 add(static_cast<Sensor &>(g));
156 const set<Object *> &Layout::get_all<Object>() const
158 return objects.get();
162 const set<Track *> &Layout::get_all<Track>() const
164 return objects.get<Track>();
168 const set<Signal *> &Layout::get_all<Signal>() const
170 return objects.get<Signal>();
174 const set<Vehicle *> &Layout::get_all<Vehicle>() const
176 return objects.get<Vehicle>();
180 const set<Terrain *> &Layout::get_all<Terrain>() const
182 return objects.get<Terrain>();
186 const set<TrackChain *> &Layout::get_all<TrackChain>() const
188 return track_chains.get();
192 const set<Block *> &Layout::get_all<Block>() const
194 return track_chains.get<Block>();
198 const set<Route *> &Layout::get_all<Route>() const
200 return track_chains.get<Route>();
204 const set<Zone *> &Layout::get_all<Zone>() const
206 return track_chains.get<Zone>();
210 const set<Sensor *> &Layout::get_all<Sensor>() const
212 return sensors.get<Sensor>();
216 const set<TrackCircuit *> &Layout::get_all<TrackCircuit>() const
218 return sensors.get<TrackCircuit>();
222 const set<BeamGate *> &Layout::get_all<BeamGate>() const
224 return sensors.get<BeamGate>();
228 T *Layout::pick(const Ray &ray)
230 const set<Object *> &objs = objects.get();
233 for(set<Object *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
234 if(T *t = dynamic_cast<T *>(*i))
237 if(t->collide_ray(ray, &d))
238 if(!closest || d<distance)
248 template Object *Layout::pick<Object>(const Ray &);
249 template Track *Layout::pick<Track>(const Ray &);
250 template Signal *Layout::pick<Signal>(const Ray &);
251 template Vehicle *Layout::pick<Vehicle>(const Ray &);
253 void Layout::remove(Object &o)
256 signal_object_removed.emit(o);
259 void Layout::remove(Track &t)
263 // Blocks must be recreated first
265 signal_object_removed.emit(t);
269 void Layout::remove(TrackChain &g)
271 if(track_chains.erase(g))
272 signal_track_chain_removed.emit(g);
275 void Layout::remove(Sensor &s)
280 void Layout::remove(BeamGate &g)
282 remove(static_cast<Object &>(g));
283 remove(static_cast<Sensor &>(g));
286 unsigned Layout::allocate_turnout_address()
288 set<unsigned> used_addrs;
289 const set<Track *> &tracks = objects.get<Track>();
290 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
291 if((*i)->get_type().is_turnout())
292 used_addrs.insert((*i)->get_turnout_address());
294 unsigned result = next_turnout_addr;
295 while(used_addrs.count(result))
297 next_turnout_addr = result+1;
302 Block &Layout::get_block(unsigned id) const
304 const set<Block *> &blocks = track_chains.get<Block>();
305 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
306 if((*i)->get_id()==id)
312 void Layout::create_blocks()
314 set<Track *> loose_tracks = objects.get<Track>();
315 const set<Block *> *blocks = &track_chains.get<Block>();
316 for(set<Block *>::const_iterator i=blocks->begin(); i!=blocks->end(); ++i)
318 const set<Track *> &btracks = (*i)->get_tracks();
319 for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
320 loose_tracks.erase(*j);
323 list<Block *> created_blocks;
324 while(!loose_tracks.empty())
326 Block *block = new Block(*this, **loose_tracks.begin());
327 created_blocks.push_back(block);
329 const set<Track *> &btracks = block->get_tracks();
330 for(set<Track *>::const_iterator i=btracks.begin(); i!=btracks.end(); ++i)
331 loose_tracks.erase(*i);
334 // The previously obtained set has been invalidated by creating new blocks
335 blocks = &track_chains.get<Block>();
336 for(list<Block *>::iterator i=created_blocks.begin(); i!=created_blocks.end(); ++i)
337 for(set<Block *>::const_iterator j=blocks->begin(); j!=blocks->end(); ++j)
339 (*i)->check_link(**j);
342 void Layout::create_blocks(Track &track)
344 /* Must collect the blocks in a set first while all tracks are still
345 guaranteed to have blocks and to avoid duplicate deletes */
346 set<Block *> del_blocks;
348 del_blocks.insert(&track.get_block());
350 const vector<Track *> &links = track.get_links();
351 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
353 del_blocks.insert(&(*i)->get_block());
355 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
361 Route &Layout::get_route(const string &name) const
363 const set<Route *> &routes = track_chains.get<Route>();
364 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
365 if((*i)->get_name()==name)
367 throw key_error(name);
370 void Layout::update_routes()
372 const set<Route *> &routes = track_chains.get<Route>();
373 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
374 (*i)->update_turnouts();
377 Layout::ZoneArray Layout::get_zones(const string &group) const
380 const set<Zone *> &zones = track_chains.get<Zone>();
381 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
382 if((*i)->get_group()==group)
383 result.push_back(*i);
385 sort(result.begin(), result.end(), zone_order);
390 Zone &Layout::get_zone(const string &group, unsigned num) const
392 const set<Zone *> &zones = track_chains.get<Zone>();
393 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
394 if((*i)->get_group()==group && (*i)->get_number()==num)
397 throw key_error(format("%s %d", group, num));
400 void Layout::add_train(Train &t)
402 insert_unique(trains, t.get_address(), &t);
403 signal_train_added.emit(t);
406 Train &Layout::get_train(unsigned addr) const
408 return *get_item(trains, addr);
411 void Layout::remove_train(Train &t)
413 if(trains.erase(t.get_address()))
414 signal_train_removed.emit(t);
422 Time::TimeStamp t = Time::now();
428 if(!driver || !driver->is_halted())
431 for(set<Sensor *>::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i)
433 const set<Signal *> &signals = objects.get<Signal>();
434 for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
436 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
440 void Layout::emergency(Block *block, const string &msg)
445 IO::print("Emergency at %s: %s\n", block->get_name(), msg);
447 IO::print("Emergency: %s\n", msg);
448 signal_emergency.emit(block, msg);
451 void Layout::save(const string &fn) const
453 IO::BufferedFile out(fn, IO::M_WRITE);
454 DataFile::Writer writer(out);
457 writer.write((DataFile::Statement("base"), base));
459 const set<Track *> &tracks = objects.get<Track>();
460 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
462 DataFile::Statement st("track");
463 st.append(catalogue.get_name(&(*i)->get_type()));
468 const set<Signal *> &signals = objects.get<Signal>();
469 for(set<Signal *>::const_iterator i=signals.begin(); i!=signals.end(); ++i)
471 DataFile::Statement st("signal");
472 st.append(catalogue.get_name(&(*i)->get_type()));
477 const set<Terrain *> &terrains = objects.get<Terrain>();
478 for(set<Terrain *>::const_iterator i=terrains.begin(); i!=terrains.end(); ++i)
480 DataFile::Statement st("terrain");
481 st.append(catalogue.get_name(&(*i)->get_type()));
486 const set<BeamGate *> &gates = objects.get<BeamGate>();
487 for(set<BeamGate *>::const_iterator i=gates.begin(); i!=gates.end(); ++i)
489 DataFile::Statement st("beamgate");
494 const set<Route *> &routes = track_chains.get<Route>();
495 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
497 if((*i)->is_temporary())
500 DataFile::Statement st("route");
505 const set<Zone *> &zones = track_chains.get<Zone>();
506 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
508 DataFile::Statement st("zone");
514 void Layout::save_dynamic(const string &fn) const
516 IO::BufferedFile out(fn, IO::M_WRITE);
517 DataFile::Writer writer(out);
520 DataFile::Statement st("clock");
525 const set<Track *> &tracks = objects.get<Track>();
526 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
527 if((*i)->get_type().is_turnout())
529 DataFile::Statement st("turnout");
530 st.append((*i)->get_turnout_address());
531 (*i)->save_dynamic(st.sub);
535 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
537 DataFile::Statement st("train");
538 st.append(catalogue.get_name(&i->second->get_locomotive_type()));
539 st.append(i->second->get_address());
540 st.append(i->second->get_protocol());
541 i->second->save(st.sub);
546 void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
548 if(state==Sensor::ACTIVE)
550 Block *block = sensor.get_block();
551 if(block && !block->get_train())
552 emergency(block, "Unreserved sensor triggered");
556 void Layout::block_reserved(Block &block, Train *train)
558 if(emitting_block_reserved)
559 block_reserve_queue.push_back(BlockReservation(block, train));
562 SetFlag setf(emitting_block_reserved);
563 signal_block_reserved.emit(block, train);
564 while(!block_reserve_queue.empty())
566 BlockReservation br = block_reserve_queue.front();
567 block_reserve_queue.pop_front();
568 signal_block_reserved.emit(br.block, br.train);
575 Layout::Storage<B>::~Storage()
578 delete *base.begin();
582 bool Layout::Storage<B>::insert(B &b)
584 bool result = base.insert(&b).second;
591 bool Layout::Storage<B>::erase(B &b)
593 bool result = base.erase(&b);
601 const set<T *> &Layout::Storage<B>::get() const
603 for(list<Variant>::const_iterator i=caches.begin(); i!=caches.end(); ++i)
604 if(i->check_type<set<T *> >())
605 return i->value<set<T *> >();
607 caches.push_back(set<T *>());
608 set<T *> &result = caches.back().value<set<T *> >();
609 for(typename set<B *>::const_iterator i=base.begin(); i!=base.end(); ++i)
610 if(T *t = dynamic_cast<T *>(*i))
618 void Layout::Storage<B>::del()
620 set<T *> ts = get<T>();
621 for(typename set<T *>::iterator i=ts.begin(); i!=ts.end(); ++i)
627 Layout::BlockReservation::BlockReservation(Block &b, Train *t):
633 Layout::Loader::Loader(Layout &l):
634 DataFile::ObjectLoader<Layout>(l)
636 add("base", &Layout::base);
637 add("beamgate", &Loader::beamgate);
638 add("clock", &Loader::clock);
639 add("route", &Loader::route);
640 add("signal", &Loader::signal);
641 add("terrain", &Loader::terrain);
642 add("track", &Loader::track);
643 add("train", &Loader::train);
644 add("turnout", &Loader::turnout);
645 add("zone", &Loader::zone);
648 add("turnout", &Loader::turnout2);
651 void Layout::Loader::beamgate()
653 BeamGate *gate = new BeamGate(obj);
657 void Layout::Loader::clock()
662 void Layout::Loader::route()
664 Route *rte = new Route(obj);
668 void Layout::Loader::signal(const string &n)
670 Signal *sig = new Signal(obj, obj.catalogue.get<SignalType>(n));
674 void Layout::Loader::terrain(const string &n)
676 Terrain *ter = new Terrain(obj, obj.catalogue.get<TerrainType>(n));
680 void Layout::Loader::track(const string &n)
682 Track *trk = new Track(obj, obj.catalogue.get<TrackType>(n));
684 const set<Track *> &tracks = obj.objects.get<Track>();
685 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
690 void Layout::Loader::train(const string &n, unsigned addr, const std::string &proto)
692 Train *trn = new Train(obj, obj.catalogue.get<VehicleType>(n), addr, proto);
696 void Layout::Loader::turnout(unsigned addr)
698 /* Kinda slow, but maintaining a map of turnouts seems more trouble than
699 it's worth at this point. */
700 const set<Track *> &tracks = obj.objects.get<Track>();
701 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
702 if((*i)->get_turnout_address()==addr)
708 throw key_error(addr);
711 void Layout::Loader::turnout2(unsigned addr, unsigned path)
713 // XXX Discrepancy between ids and addresses
715 obj.driver->set_turnout(addr, path);
718 void Layout::Loader::zone()
720 Zone *zne = new Zone(obj);