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 set<unsigned> used_ids;
79 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
80 if((*i)->get_turnout_id())
81 used_ids.insert((*i)->get_turnout_id());
83 unsigned result = next_turnout_id;
84 while(used_ids.count(result) || (dbl && used_ids.count(result+1)))
86 next_turnout_id = result+1+dbl;
91 void Layout::add_block(Block &b)
96 Block &Layout::get_block(unsigned id) const
98 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
99 if((*i)->get_id()==id)
102 throw KeyError("Unknown block", lexical_cast(id));
105 void Layout::create_blocks()
107 set<Track *> used_tracks;
108 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
110 const set<Track *> &btracks = (*i)->get_tracks();
111 used_tracks.insert(btracks.begin(), btracks.end());
114 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
115 if(used_tracks.count(*i)==0)
117 Block *block = new Block(*this, **i);
118 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
121 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
122 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
124 (*i)->check_link(**j);
127 void Layout::create_blocks(Track &track)
129 /* Must collect the blocks in a set first while all tracks are still
130 guaranteed to have blocks and to avoid duplicate deletes */
131 set<Block *> del_blocks;
133 del_blocks.insert(&track.get_block());
135 const vector<Track *> &links = track.get_links();
136 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
138 del_blocks.insert(&(*i)->get_block());
140 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
146 void Layout::remove_block(Block &b)
151 void Layout::add_route(Route &r)
153 if(routes.insert(&r).second)
154 signal_route_added.emit(r);
157 Route &Layout::get_route(const string &name) const
159 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
160 if((*i)->get_name()==name)
162 throw KeyError("Unknown route", name);
165 void Layout::update_routes()
167 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
168 (*i)->update_turnouts();
171 void Layout::remove_route(Route &r)
174 signal_route_removed.emit(r);
177 void Layout::add_train(Train &t)
179 if(trains.count(t.get_address()))
180 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
182 trains[t.get_address()] = &t;
183 signal_train_added.emit(t);
186 Train &Layout::get_train(unsigned addr) const
188 map<unsigned, Train *>::const_iterator i = trains.find(addr);
190 throw KeyError("Unknown train", lexical_cast(addr));
194 void Layout::remove_train(Train &t)
196 if(trains.erase(t.get_address()))
197 signal_train_removed.emit(t);
200 void Layout::add_vehicle(Vehicle &v)
202 if(vehicles.insert(&v).second)
203 signal_vehicle_added.emit(v);
206 void Layout::remove_vehicle(Vehicle &v)
208 if(vehicles.erase(&v))
209 signal_vehicle_removed.emit(v);
217 Time::TimeStamp t = Time::now();
223 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
224 i->second->tick(t, dt);
227 void Layout::emergency(const string &msg)
231 IO::print("Emergency: %s\n", msg);
232 signal_emergency.emit(msg);
235 void Layout::save(const string &fn)
237 IO::BufferedFile out(fn, IO::M_WRITE);
238 DataFile::Writer writer(out);
241 writer.write((DataFile::Statement("base"), base));
243 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
245 DataFile::Statement st("track");
246 st.append((*i)->get_type().get_article_number());
251 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
253 if((*i)->is_temporary())
256 DataFile::Statement st("route");
262 void Layout::save_trains(const string &fn)
264 IO::BufferedFile out(fn, IO::M_WRITE);
265 DataFile::Writer writer(out);
267 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
269 DataFile::Statement st("train");
270 st.append(i->second->get_locomotive_type().get_article_number());
271 st.append(i->second->get_address());
272 i->second->save(st.sub);
277 void Layout::sensor_event(unsigned addr, bool state)
281 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
282 if((*i)->get_sensor_id()==addr)
284 if(!(*i)->get_train())
285 emergency(format("Unreserved sensor %d triggered", addr));
292 Layout::Loader::Loader(Layout &l):
293 DataFile::BasicLoader<Layout>(l),
296 add("base", &Layout::base);
297 add("route", static_cast<void (Loader::*)()>(&Loader::route));
298 add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
299 add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
300 add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
301 add("train", static_cast<void (Loader::*)(unsigned, unsigned)>(&Loader::train));
302 add("train", static_cast<void (Loader::*)(ArticleNumber, unsigned)>(&Loader::train));
305 void Layout::Loader::finish()
307 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
311 void Layout::Loader::route()
313 Route *rte = new Route(obj);
317 void Layout::Loader::route(const string &n)
319 Route *rte = new Route(obj);
324 void Layout::Loader::track(unsigned art_nr)
326 track(ArticleNumber(art_nr));
329 void Layout::Loader::track(ArticleNumber art_nr)
331 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
334 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
336 trk->snap_to(**i, true);
339 void Layout::Loader::train(unsigned art_nr, unsigned addr)
341 train(ArticleNumber(art_nr), addr);
344 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr)
346 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr);
350 } // namespace Marklin