void Layout::add_route(Route &r)
{
- if(routes.insert(&r).second)
- signal_route_added.emit(r);
+ if(routes.count(r.get_name()))
+ throw KeyError("Duplicate route name");
+ routes[r.get_name()] = &r;
+ signal_route_added.emit(r);
+}
+
+Route &Layout::get_route(const string &name) const
+{
+ map<string, Route *>::const_iterator i = routes.find(name);
+ if(i==routes.end())
+ throw KeyError("Unknown route", name);
+ return *i->second;
}
void Layout::remove_route(Route &r)
{
- if(routes.erase(&r))
+ if(routes.erase(r.get_name()))
signal_route_removed.emit(r);
}
writer.write(st);
}
- for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+ for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
{
DataFile::Statement st("route");
- st.append((*i)->get_name());
- (*i)->save(st.sub);
+ st.append(i->first);
+ i->second->save(st.sub);
writer.write(st);
}
}
void Layout::check_routes()
{
- for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+ for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
{
// We must copy the turnout map, since adding tracks to the route will (temporarily) mess it up
- const map<unsigned, int> turnouts = (*i)->get_turnouts();
+ const map<unsigned, int> turnouts = i->second->get_turnouts();
Track *track = 0;
- unsigned trk_route = 0;
+ unsigned trk_path = 0;
for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
{
map<unsigned, int>::const_iterator k = turnouts.find((*j)->get_turnout_id());
if(k!=turnouts.end())
{
track = *j;
- trk_route = k->second;
+ trk_path = k->second;
break;
}
}
- (*i)->add_track(*track);
+ if(!track)
+ continue;
+
+ i->second->add_track(*track);
const vector<Endpoint> &eps = track->get_type().get_endpoints();
unsigned ep = 0;
for(unsigned j=0; j<eps.size(); ++i)
- if(eps[j].routes&(1<<trk_route))
+ if(eps[j].paths&(1<<trk_path))
{
ep = j;
break;
Track *start = track;
while(1)
{
- int out_ep = track->traverse(ep, trk_route);
- if(out_ep<0)
- break;
+ unsigned out_ep = track->traverse(ep, trk_path);
Track *next = track->get_links()[out_ep];
if(!next || next == start)
break;
ep = next->get_endpoint_by_link(*track);
- if(next->get_type().get_n_routes()>1)
+ if(next->get_type().get_n_paths()>1)
{
map<unsigned, int>::const_iterator j = turnouts.find(next->get_turnout_id());
if(j==turnouts.end())
break;
- trk_route = j->second;
+ trk_path = j->second;
}
else
- trk_route = 0;
- (*i)->add_track(*next);
+ trk_path = 0;
+ i->second->add_track(*next);
track = next;
}
}