3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/core/refptr.h>
10 #include <msp/datafile/parser.h>
11 #include <msp/datafile/writer.h>
12 #include <msp/io/print.h>
13 #include <msp/time/utils.h>
15 #include "catalogue.h"
20 #include "tracktype.h"
22 #include "vehicletype.h"
29 Layout::Layout(Catalogue &c, Driver *d):
32 next_turnout_id(0x800)
35 driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
41 while(!trains.empty())
42 delete trains.begin()->second;
43 while(!routes.empty())
44 delete *routes.begin();
45 while(!tracks.empty())
46 delete *tracks.begin();
47 while(!blocks.empty())
48 delete *blocks.begin();
51 Driver &Layout::get_driver() const
54 throw InvalidState("No driver");
58 void Layout::add_track(Track &t)
60 if(tracks.insert(&t).second)
63 signal_track_added.emit(t);
67 void Layout::remove_track(Track &t)
72 signal_track_removed.emit(t);
76 unsigned Layout::allocate_turnout_id(bool dbl)
78 unsigned result = next_turnout_id++;
84 void Layout::add_block(Block &b)
89 Block &Layout::get_block(unsigned id) const
91 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
92 if((*i)->get_id()==id)
95 throw KeyError("Unknown block", lexical_cast(id));
98 void Layout::create_blocks()
100 set<Track *> used_tracks;
101 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
103 const set<Track *> &btracks = (*i)->get_tracks();
104 used_tracks.insert(btracks.begin(), btracks.end());
107 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
108 if(used_tracks.count(*i)==0)
110 Block *block = new Block(*this, **i);
111 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
114 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
115 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
117 (*i)->check_link(**j);
120 void Layout::create_blocks(Track &track)
122 /* Must collect the blocks in a set first while all tracks are still
123 guaranteed to have blocks and to avoid duplicate deletes */
124 set<Block *> del_blocks;
126 del_blocks.insert(&track.get_block());
128 const vector<Track *> &links = track.get_links();
129 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
131 del_blocks.insert(&(*i)->get_block());
133 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
139 void Layout::remove_block(Block &b)
144 void Layout::add_route(Route &r)
146 if(routes.insert(&r).second)
147 signal_route_added.emit(r);
150 Route &Layout::get_route(const string &name) const
152 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
153 if((*i)->get_name()==name)
155 throw KeyError("Unknown route", name);
158 void Layout::update_routes()
160 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
161 (*i)->update_turnouts();
164 void Layout::remove_route(Route &r)
167 signal_route_removed.emit(r);
170 void Layout::add_train(Train &t)
172 if(trains.count(t.get_address()))
173 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
175 trains[t.get_address()] = &t;
176 signal_train_added.emit(t);
179 Train &Layout::get_train(unsigned addr) const
181 map<unsigned, Train *>::const_iterator i = trains.find(addr);
183 throw KeyError("Unknown train", lexical_cast(addr));
187 void Layout::remove_train(Train &t)
189 if(trains.erase(t.get_address()))
190 signal_train_removed.emit(t);
193 void Layout::add_vehicle(Vehicle &v)
195 if(vehicles.insert(&v).second)
196 signal_vehicle_added.emit(v);
199 void Layout::remove_vehicle(Vehicle &v)
201 if(vehicles.erase(&v))
202 signal_vehicle_removed.emit(v);
210 Time::TimeStamp t = Time::now();
216 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
217 i->second->tick(t, dt);
220 void Layout::emergency(const string &msg)
224 IO::print("Emergency: %s\n", msg);
225 signal_emergency.emit(msg);
228 void Layout::save(const string &fn)
230 IO::BufferedFile out(fn, IO::M_WRITE);
231 DataFile::Writer writer(out);
234 writer.write((DataFile::Statement("base"), base));
236 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
238 DataFile::Statement st("track");
239 st.append((*i)->get_type().get_article_number());
244 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
246 if((*i)->is_temporary())
249 DataFile::Statement st("route");
255 void Layout::save_trains(const string &fn)
257 IO::BufferedFile out(fn, IO::M_WRITE);
258 DataFile::Writer writer(out);
260 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
262 DataFile::Statement st("train");
263 st.append(i->second->get_locomotive_type().get_article_number());
264 st.append(i->second->get_address());
265 i->second->save(st.sub);
270 void Layout::sensor_event(unsigned addr, bool state)
274 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
275 if((*i)->get_sensor_id()==addr)
277 if(!(*i)->get_train())
278 emergency(format("Unreserved sensor %d triggered", addr));
285 Layout::Loader::Loader(Layout &l):
286 DataFile::BasicLoader<Layout>(l),
289 add("base", &Layout::base);
290 add("route", static_cast<void (Loader::*)()>(&Loader::route));
291 add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
292 add("track", &Loader::track);
293 add("train", &Loader::train);
296 void Layout::Loader::finish()
298 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
302 void Layout::Loader::route()
304 Route *rte = new Route(obj);
308 void Layout::Loader::route(const string &n)
310 Route *rte = new Route(obj);
315 void Layout::Loader::track(unsigned art_nr)
317 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
320 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
322 trk->snap_to(**i, true);
325 void Layout::Loader::train(unsigned art_nr, unsigned addr)
327 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr);
331 } // namespace Marklin