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"
17 #include "trackcircuit.h"
18 #include "tracktype.h"
21 #include "vehicletype.h"
29 bool zone_order(const R2C2::Zone *z1, const R2C2::Zone *z2)
31 return z1->get_number()<z2->get_number();
39 Layout::Layout(Catalogue &c, Driver *d):
42 next_turnout_addr(0x800)
52 while(!trains.empty())
53 delete trains.begin()->second;
54 track_chains.del<Route>();
55 track_chains.del<Zone>();
56 objects.del<Signal>();
58 track_chains.del<Block>();
61 Driver &Layout::get_driver() const
64 throw logic_error("!driver");
68 void Layout::add(Object &o)
71 signal_object_added.emit(o);
74 void Layout::add(Track &t)
78 // Blocks must be recreated first
80 signal_object_added.emit(t);
84 void Layout::add(TrackChain &g)
86 if(track_chains.insert(g))
87 signal_track_chain_added.emit(g);
90 void Layout::add(Block &b)
92 if(track_chains.insert(b))
94 b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
95 signal_track_chain_added.emit(b);
99 void Layout::add(Sensor &s)
101 if(sensors.insert(s))
103 s.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::sensor_state_changed), sigc::ref(s)));
104 s.signal_state_changed.connect(sigc::bind<0>(signal_sensor_state_changed, sigc::ref(s)));
108 void Layout::add(BeamGate &g)
110 add(static_cast<Object &>(g));
111 add(static_cast<Sensor &>(g));
115 const set<Object *> &Layout::get_all<Object>() const
117 return objects.get();
121 const set<Track *> &Layout::get_all<Track>() const
123 return objects.get<Track>();
127 const set<Signal *> &Layout::get_all<Signal>() const
129 return objects.get<Signal>();
133 const set<Vehicle *> &Layout::get_all<Vehicle>() const
135 return objects.get<Vehicle>();
139 const set<TrackChain *> &Layout::get_all<TrackChain>() const
141 return track_chains.get();
145 const set<Block *> &Layout::get_all<Block>() const
147 return track_chains.get<Block>();
151 const set<Route *> &Layout::get_all<Route>() const
153 return track_chains.get<Route>();
157 const set<Zone *> &Layout::get_all<Zone>() const
159 return track_chains.get<Zone>();
163 const set<Sensor *> &Layout::get_all<Sensor>() const
165 return sensors.get<Sensor>();
169 const set<TrackCircuit *> &Layout::get_all<TrackCircuit>() const
171 return sensors.get<TrackCircuit>();
175 const set<BeamGate *> &Layout::get_all<BeamGate>() const
177 return sensors.get<BeamGate>();
181 T *Layout::pick(const Ray &ray)
183 const set<Object *> &objs = objects.get();
184 for(set<Object *>::const_iterator i=objs.begin(); i!=objs.end(); ++i)
185 if(T *t = dynamic_cast<T *>(*i))
186 if(t->collide_ray(ray))
192 template Object *Layout::pick<Object>(const Ray &);
193 template Track *Layout::pick<Track>(const Ray &);
194 template Signal *Layout::pick<Signal>(const Ray &);
195 template Vehicle *Layout::pick<Vehicle>(const Ray &);
197 void Layout::remove(Object &o)
200 signal_object_removed.emit(o);
203 void Layout::remove(Track &t)
207 // Blocks must be recreated first
209 signal_object_removed.emit(t);
213 void Layout::remove(TrackChain &g)
215 if(track_chains.erase(g))
216 signal_track_chain_removed.emit(g);
219 void Layout::remove(Sensor &s)
224 void Layout::remove(BeamGate &g)
226 remove(static_cast<Object &>(g));
227 remove(static_cast<Sensor &>(g));
230 unsigned Layout::allocate_turnout_address()
232 set<unsigned> used_addrs;
233 const set<Track *> &tracks = objects.get<Track>();
234 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
235 if((*i)->get_turnout_address())
236 used_addrs.insert((*i)->get_turnout_address());
238 unsigned result = next_turnout_addr;
239 while(used_addrs.count(result))
241 next_turnout_addr = result+1;
246 Block &Layout::get_block(unsigned id) const
248 const set<Block *> &blocks = track_chains.get<Block>();
249 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
250 if((*i)->get_id()==id)
256 void Layout::create_blocks()
258 set<Track *> loose_tracks = objects.get<Track>();
259 const set<Block *> *blocks = &track_chains.get<Block>();
260 for(set<Block *>::const_iterator i=blocks->begin(); i!=blocks->end(); ++i)
262 const set<Track *> &btracks = (*i)->get_tracks();
263 for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
264 loose_tracks.erase(*j);
267 list<Block *> created_blocks;
268 while(!loose_tracks.empty())
270 Block *block = new Block(*this, **loose_tracks.begin());
271 created_blocks.push_back(block);
273 const set<Track *> &btracks = block->get_tracks();
274 for(set<Track *>::const_iterator i=btracks.begin(); i!=btracks.end(); ++i)
275 loose_tracks.erase(*i);
278 // The previously obtained set has been invalidated by creating new blocks
279 blocks = &track_chains.get<Block>();
280 for(list<Block *>::iterator i=created_blocks.begin(); i!=created_blocks.end(); ++i)
281 for(set<Block *>::const_iterator j=blocks->begin(); j!=blocks->end(); ++j)
283 (*i)->check_link(**j);
286 void Layout::create_blocks(Track &track)
288 /* Must collect the blocks in a set first while all tracks are still
289 guaranteed to have blocks and to avoid duplicate deletes */
290 set<Block *> del_blocks;
292 del_blocks.insert(&track.get_block());
294 const vector<Track *> &links = track.get_links();
295 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
297 del_blocks.insert(&(*i)->get_block());
299 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
305 Route &Layout::get_route(const string &name) const
307 const set<Route *> &routes = track_chains.get<Route>();
308 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
309 if((*i)->get_name()==name)
311 throw key_error(name);
314 void Layout::update_routes()
316 const set<Route *> &routes = track_chains.get<Route>();
317 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
318 (*i)->update_turnouts();
321 Layout::ZoneArray Layout::get_zones(const string &group) const
324 const set<Zone *> &zones = track_chains.get<Zone>();
325 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
326 if((*i)->get_group()==group)
327 result.push_back(*i);
329 sort(result.begin(), result.end(), zone_order);
334 Zone &Layout::get_zone(const string &group, unsigned num) const
336 const set<Zone *> &zones = track_chains.get<Zone>();
337 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
338 if((*i)->get_group()==group && (*i)->get_number()==num)
341 throw key_error(format("%s %d", group, num));
344 void Layout::add_train(Train &t)
346 insert_unique(trains, t.get_address(), &t);
347 signal_train_added.emit(t);
350 Train &Layout::get_train(unsigned addr) const
352 return *get_item(trains, addr);
355 void Layout::remove_train(Train &t)
357 if(trains.erase(t.get_address()))
358 signal_train_removed.emit(t);
366 Time::TimeStamp t = Time::now();
374 for(set<Sensor *>::iterator i=sensors.get().begin(); i!=sensors.get().end(); ++i)
376 const set<Signal *> &signals = objects.get<Signal>();
377 for(set<Signal *>::iterator i=signals.begin(); i!=signals.end(); ++i)
379 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
383 void Layout::emergency(const string &msg)
387 IO::print("Emergency: %s\n", msg);
388 signal_emergency.emit(msg);
391 void Layout::save(const string &fn) const
393 IO::BufferedFile out(fn, IO::M_WRITE);
394 DataFile::Writer writer(out);
397 writer.write((DataFile::Statement("base"), base));
399 const set<Track *> &tracks = objects.get<Track>();
400 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
402 DataFile::Statement st("track");
403 st.append((*i)->get_type().get_article_number());
408 const set<Signal *> &signals = objects.get<Signal>();
409 for(set<Signal *>::const_iterator i=signals.begin(); i!=signals.end(); ++i)
411 DataFile::Statement st("signal");
412 st.append((*i)->get_type().get_article_number());
417 const set<BeamGate *> &gates = objects.get<BeamGate>();
418 for(set<BeamGate *>::const_iterator i=gates.begin(); i!=gates.end(); ++i)
420 DataFile::Statement st("beamgate");
425 const set<Route *> &routes = track_chains.get<Route>();
426 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
428 if((*i)->is_temporary())
431 DataFile::Statement st("route");
436 const set<Zone *> &zones = track_chains.get<Zone>();
437 for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
439 DataFile::Statement st("zone");
445 void Layout::save_dynamic(const string &fn) const
447 IO::BufferedFile out(fn, IO::M_WRITE);
448 DataFile::Writer writer(out);
450 const set<Track *> &tracks = objects.get<Track>();
451 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
452 if(unsigned taddr = (*i)->get_turnout_address())
453 writer.write((DataFile::Statement("turnout"), taddr, (*i)->get_active_path()));
455 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
457 DataFile::Statement st("train");
458 st.append(i->second->get_locomotive_type().get_article_number());
459 st.append(i->second->get_address());
460 st.append(i->second->get_protocol());
461 i->second->save(st.sub);
466 void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
468 if(state==Sensor::ACTIVE)
470 Block *block = sensor.get_block();
471 if(block && !block->get_train())
472 emergency(format("Unreserved sensor %d triggered", sensor.get_address()));
478 Layout::Storage<B>::~Storage()
481 delete *base.begin();
485 bool Layout::Storage<B>::insert(B &b)
487 bool result = base.insert(&b).second;
494 bool Layout::Storage<B>::erase(B &b)
496 bool result = base.erase(&b);
504 const set<T *> &Layout::Storage<B>::get() const
506 for(list<Variant>::const_iterator i=caches.begin(); i!=caches.end(); ++i)
507 if(i->check_type<set<T *> >())
508 return i->value<set<T *> >();
510 caches.push_back(set<T *>());
511 set<T *> &result = caches.back().value<set<T *> >();
512 for(typename set<B *>::const_iterator i=base.begin(); i!=base.end(); ++i)
513 if(T *t = dynamic_cast<T *>(*i))
521 void Layout::Storage<B>::del()
523 set<T *> ts = get<T>();
524 for(typename set<T *>::iterator i=ts.begin(); i!=ts.end(); ++i)
530 Layout::Loader::Loader(Layout &l):
531 DataFile::ObjectLoader<Layout>(l)
533 add("base", &Layout::base);
534 add("beamgate", &Loader::beamgate);
535 add("route", &Loader::route);
536 add("signal", &Loader::signal);
537 add("track", &Loader::track);
538 add("train", &Loader::train);
539 add("turnout", &Loader::turnout);
540 add("zone", &Loader::zone);
543 void Layout::Loader::beamgate()
545 BeamGate *gate = new BeamGate(obj);
549 void Layout::Loader::route()
551 Route *rte = new Route(obj);
555 void Layout::Loader::signal(ArticleNumber art_nr)
557 Signal *sig = new Signal(obj, obj.catalogue.get<SignalType>(art_nr));
561 void Layout::Loader::track(ArticleNumber art_nr)
563 Track *trk = new Track(obj, obj.catalogue.get<TrackType>(art_nr));
565 const set<Track *> &tracks = obj.objects.get<Track>();
566 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
571 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
573 Train *trn = new Train(obj, obj.catalogue.get<VehicleType>(art_nr), addr, proto);
577 void Layout::Loader::turnout(unsigned addr, unsigned path)
580 obj.driver->set_turnout(addr, path);
583 void Layout::Loader::zone()
585 Zone *zne = new Zone(obj);