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>
10 #include "catalogue.h"
15 #include "signaltype.h"
18 #include "trackcircuit.h"
19 #include "tracktype.h"
22 #include "vehicletype.h"
30 bool zone_order(const R2C2::Zone *z1, const R2C2::Zone *z2)
32 return z1->get_number()<z2->get_number();
40 Layout::Layout(Catalogue &c, Driver *d):
43 next_turnout_addr(0x800)
53 while(!trains.empty())
54 delete trains.begin()->second;
55 track_chains.del<Route>();
56 track_chains.del<Zone>();
57 objects.del<Signal>();
59 track_chains.del<Block>();
62 Driver &Layout::get_driver() const
65 throw logic_error("!driver");
69 void Layout::add(Object &o)
72 signal_object_added.emit(o);
75 void Layout::add(Track &t)
79 // Blocks must be recreated first
81 signal_object_added.emit(t);
85 void Layout::add(TrackChain &g)
87 if(track_chains.insert(g))
88 signal_track_chain_added.emit(g);
91 void Layout::add(Block &b)
93 if(track_chains.insert(b))
95 b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
96 signal_track_chain_added.emit(b);
100 void Layout::add(Sensor &s)
102 if(sensors.insert(s))
104 s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
105 s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
109 void Layout::add(BeamGate &g)
111 add(static_cast<Object &>(g));
112 add(static_cast<Sensor &>(g));
116 const set<Object *> &Layout::get_all<Object>() const
118 return objects.get();
122 const set<Track *> &Layout::get_all<Track>() const
124 return objects.get<Track>();
128 const set<Signal *> &Layout::get_all<Signal>() const
130 return objects.get<Signal>();
134 const set<Vehicle *> &Layout::get_all<Vehicle>() const
136 return objects.get<Vehicle>();
140 const set<Terrain *> &Layout::get_all<Terrain>() const
142 return objects.get<Terrain>();
146 const set<TrackChain *> &Layout::get_all<TrackChain>() const
148 return track_chains.get();
152 const set<Block *> &Layout::get_all<Block>() const
154 return track_chains.get<Block>();
158 const set<Route *> &Layout::get_all<Route>() const
160 return track_chains.get<Route>();
164 const set<Zone *> &Layout::get_all<Zone>() const
166 return track_chains.get<Zone>();
170 const set<Sensor *> &Layout::get_all<Sensor>() const
172 return sensors.get<Sensor>();
176 const set<TrackCircuit *> &Layout::get_all<TrackCircuit>() const
178 return sensors.get<TrackCircuit>();
182 const set<BeamGate *> &Layout::get_all<BeamGate>() const
184 return sensors.get<BeamGate>();
188 T *Layout::pick(const Ray &ray)
190 const set<Object *> &objs = objects.get();
191 for(set<Object *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
192 if(T *t = dynamic_cast<T *>(*i))
193 if(t->collide_ray(ray))
199 template Object *Layout::pick<Object>(const Ray &);
200 template Track *Layout::pick<Track>(const Ray &);
201 template Signal *Layout::pick<Signal>(const Ray &);
202 template Vehicle *Layout::pick<Vehicle>(const Ray &);
204 void Layout::remove(Object &o)
207 signal_object_removed.emit(o);
210 void Layout::remove(Track &t)
214 // Blocks must be recreated first
216 signal_object_removed.emit(t);
220 void Layout::remove(TrackChain &g)
222 if(track_chains.erase(g))
223 signal_track_chain_removed.emit(g);
226 void Layout::remove(Sensor &s)
231 void Layout::remove(BeamGate &g)
233 remove(static_cast<Object &>(g));
234 remove(static_cast<Sensor &>(g));
237 unsigned Layout::allocate_turnout_address()
239 set<unsigned> used_addrs;
240 const set<Track *> &tracks = objects.get<Track>();
241 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
242 if((*i)->get_turnout_address())
243 used_addrs.insert((*i)->get_turnout_address());
245 unsigned result = next_turnout_addr;
246 while(used_addrs.count(result))
248 next_turnout_addr = result+1;
253 Block &Layout::get_block(unsigned id) const
255 const set<Block *> &blocks = track_chains.get<Block>();
256 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
257 if((*i)->get_id()==id)
263 void Layout::create_blocks()
265 set<Track *> loose_tracks = objects.get<Track>();
266 const set<Block *> *blocks = &track_chains.get<Block>();
267 for(set<Block *>::const_iterator i=blocks->begin(); i!=blocks->end(); ++i)
269 const set<Track *> &btracks = (*i)->get_tracks();
270 for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
271 loose_tracks.erase(*j);
274 list<Block *> created_blocks;
275 while(!loose_tracks.empty())
277 Block *block = new Block(*this, **loose_tracks.begin());
278 created_blocks.push_back(block);
280 const set<Track *> &btracks = block->get_tracks();
281 for(set<Track *>::const_iterator i=btracks.begin(); i!=btracks.end(); ++i)
282 loose_tracks.erase(*i);
285 // The previously obtained set has been invalidated by creating new blocks
286 blocks = &track_chains.get<Block>();
287 for(list<Block *>::iterator i=created_blocks.begin(); i!=created_blocks.end(); ++i)
288 for(set<Block *>::const_iterator j=blocks->begin(); j!=blocks->end(); ++j)
290 (*i)->check_link(**j);
293 void Layout::create_blocks(Track &track)
295 /* Must collect the blocks in a set first while all tracks are still
296 guaranteed to have blocks and to avoid duplicate deletes */
297 set<Block *> del_blocks;
299 del_blocks.insert(&track.get_block());
301 const vector<Track *> &links = track.get_links();
302 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
304 del_blocks.insert(&(*i)->get_block());
306 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
312 Route &Layout::get_route(const string &name) const
314 const set<Route *> &routes = track_chains.get<Route>();
315 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
316 if((*i)->get_name()==name)
318 throw key_error(name);
321 void Layout::update_routes()
323 const set<Route *> &routes = track_chains.get<Route>();
324 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
325 (*i)->update_turnouts();
328 Layout::ZoneArray Layout::get_zones(const string &group) const
331 const set<Zone *> &zones = track_chains.get<Zone>();
332 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
333 if((*i)->get_group()==group)
334 result.push_back(*i);
336 sort(result.begin(), result.end(), zone_order);
341 Zone &Layout::get_zone(const string &group, unsigned num) const
343 const set<Zone *> &zones = track_chains.get<Zone>();
344 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
345 if((*i)->get_group()==group && (*i)->get_number()==num)
348 throw key_error(format("%s %d", group, num));
351 void Layout::add_train(Train &t)
353 insert_unique(trains, t.get_address(), &t);
354 signal_train_added.emit(t);
357 Train &Layout::get_train(unsigned addr) const
359 return *get_item(trains, addr);
362 void Layout::remove_train(Train &t)
364 if(trains.erase(t.get_address()))
365 signal_train_removed.emit(t);
373 Time::TimeStamp t = Time::now();
381 for(set<Sensor *>::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i)
383 const set<Signal *> &signals = objects.get<Signal>();
384 for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
386 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
390 void Layout::emergency(const string &msg)
394 IO::print("Emergency: %s\n", msg);
395 signal_emergency.emit(msg);
398 void Layout::save(const string &fn) const
400 IO::BufferedFile out(fn, IO::M_WRITE);
401 DataFile::Writer writer(out);
404 writer.write((DataFile::Statement("base"), base));
406 const set<Track *> &tracks = objects.get<Track>();
407 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
409 DataFile::Statement st("track");
410 st.append((*i)->get_type().get_article_number());
415 const set<Signal *> &signals = objects.get<Signal>();
416 for(set<Signal *>::const_iterator i=signals.begin(); i!=signals.end(); ++i)
418 DataFile::Statement st("signal");
419 st.append((*i)->get_type().get_article_number());
424 const set<Terrain *> &terrains = objects.get<Terrain>();
425 for(set<Terrain *>::const_iterator i=terrains.begin(); i!=terrains.end(); ++i)
427 DataFile::Statement st("terrain");
428 st.append((*i)->get_type().get_article_number());
433 const set<BeamGate *> &gates = objects.get<BeamGate>();
434 for(set<BeamGate *>::const_iterator i=gates.begin(); i!=gates.end(); ++i)
436 DataFile::Statement st("beamgate");
441 const set<Route *> &routes = track_chains.get<Route>();
442 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
444 if((*i)->is_temporary())
447 DataFile::Statement st("route");
452 const set<Zone *> &zones = track_chains.get<Zone>();
453 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
455 DataFile::Statement st("zone");
461 void Layout::save_dynamic(const string &fn) const
463 IO::BufferedFile out(fn, IO::M_WRITE);
464 DataFile::Writer writer(out);
466 const set<Track *> &tracks = objects.get<Track>();
467 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
468 if(unsigned taddr = (*i)->get_turnout_address())
469 writer.write((DataFile::Statement("turnout"), taddr, (*i)->get_active_path()));
471 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
473 DataFile::Statement st("train");
474 st.append(i->second->get_locomotive_type().get_article_number());
475 st.append(i->second->get_address());
476 st.append(i->second->get_protocol());
477 i->second->save(st.sub);
482 void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
484 if(state==Sensor::ACTIVE)
486 Block *block = sensor.get_block();
487 if(block && !block->get_train())
488 emergency(format("Unreserved sensor %d triggered", sensor.get_address()));
494 Layout::Storage<B>::~Storage()
497 delete *base.begin();
501 bool Layout::Storage<B>::insert(B &b)
503 bool result = base.insert(&b).second;
510 bool Layout::Storage<B>::erase(B &b)
512 bool result = base.erase(&b);
520 const set<T *> &Layout::Storage<B>::get() const
522 for(list<Variant>::const_iterator i=caches.begin(); i!=caches.end(); ++i)
523 if(i->check_type<set<T *> >())
524 return i->value<set<T *> >();
526 caches.push_back(set<T *>());
527 set<T *> &result = caches.back().value<set<T *> >();
528 for(typename set<B *>::const_iterator i=base.begin(); i!=base.end(); ++i)
529 if(T *t = dynamic_cast<T *>(*i))
537 void Layout::Storage<B>::del()
539 set<T *> ts = get<T>();
540 for(typename set<T *>::iterator i=ts.begin(); i!=ts.end(); ++i)
546 Layout::Loader::Loader(Layout &l):
547 DataFile::ObjectLoader<Layout>(l)
549 add("base", &Layout::base);
550 add("beamgate", &Loader::beamgate);
551 add("route", &Loader::route);
552 add("signal", &Loader::signal);
553 add("terrain", &Loader::terrain);
554 add("track", &Loader::track);
555 add("train", &Loader::train);
556 add("turnout", &Loader::turnout);
557 add("zone", &Loader::zone);
560 void Layout::Loader::beamgate()
562 BeamGate *gate = new BeamGate(obj);
566 void Layout::Loader::route()
568 Route *rte = new Route(obj);
572 void Layout::Loader::signal(ArticleNumber art_nr)
574 Signal *sig = new Signal(obj, obj.catalogue.get<SignalType>(art_nr));
578 void Layout::Loader::terrain(ArticleNumber art_nr)
580 Terrain *ter = new Terrain(obj, obj.catalogue.get<TerrainType>(art_nr));
584 void Layout::Loader::track(ArticleNumber art_nr)
586 Track *trk = new Track(obj, obj.catalogue.get<TrackType>(art_nr));
588 const set<Track *> &tracks = obj.objects.get<Track>();
589 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
594 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
596 Train *trn = new Train(obj, obj.catalogue.get<VehicleType>(art_nr), addr, proto);
600 void Layout::Loader::turnout(unsigned addr, unsigned path)
603 obj.driver->set_turnout(addr, path);
606 void Layout::Loader::zone()
608 Zone *zne = new Zone(obj);