]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/layout.cpp
Store routes in a map by name rather than a set
[r2c2.git] / source / libmarklin / layout.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/parser.h>
10 #include <msp/datafile/writer.h>
11 #include "catalogue.h"
12 #include "layout.h"
13 #include "tracktype.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace Marklin {
19
20 Layout::Layout(const Catalogue &c):
21         catalogue(c)
22 { }
23
24 Layout::~Layout()
25 {
26         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
27                 delete *i;
28 }
29
30 void Layout::add_track(Track &t)
31 {
32         if(tracks.insert(&t).second)
33                 signal_track_added.emit(t);
34 }
35
36 void Layout::remove_track(Track &t)
37 {
38         if(tracks.erase(&t))
39                 signal_track_removed.emit(t);
40 }
41
42 void Layout::add_route(Route &r)
43 {
44         if(routes.count(r.get_name()))
45                 throw KeyError("Duplicate route name");
46         routes[r.get_name()] = &r;
47         signal_route_added.emit(r);
48 }
49
50 Route &Layout::get_route(const string &name) const
51 {
52         map<string, Route *>::const_iterator i = routes.find(name);
53         if(i==routes.end())
54                 throw KeyError("Unknown route", name);
55         return *i->second;
56 }
57
58 void Layout::remove_route(Route &r)
59 {
60         if(routes.erase(r.get_name()))
61                 signal_route_removed.emit(r);
62 }
63
64 void Layout::save(const string &fn)
65 {
66         IO::BufferedFile out(fn, IO::M_WRITE);
67         DataFile::Writer writer(out);
68
69         if(!base.empty())
70                 writer.write((DataFile::Statement("base"), base));
71
72         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
73         {
74                 DataFile::Statement st("track");
75                 st.append((*i)->get_type().get_article_number());
76                 (*i)->save(st.sub);
77                 writer.write(st);
78         }
79
80         for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
81         {
82                 DataFile::Statement st("route");
83                 st.append(i->first);
84                 i->second->save(st.sub);
85                 writer.write(st);
86         }
87 }
88
89 void Layout::check_links()
90 {
91         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
92                 (*i)->break_links();
93
94         list<Track *> flext;
95         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
96         {
97                 if((*i)->get_flex())
98                         flext.push_back(*i);
99                 else
100                 {
101                         for(set<Track *>::iterator j=i; j!=tracks.end(); ++j)
102                                 if(j!=i)
103                                         (*i)->snap_to(**j, true);
104                 }
105         }
106
107         for(list<Track *>::iterator i=flext.begin(); i!=flext.end(); ++i)
108                 for(set<Track *>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
109                         if(*j!=*i)
110                                 (*i)->snap_to(**j, true);
111 }
112
113 void Layout::check_routes()
114 {
115         for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
116         {
117                 // We must copy the turnout map, since adding tracks to the route will (temporarily) mess it up
118                 const map<unsigned, int> turnouts = i->second->get_turnouts();
119
120                 Track *track = 0;
121                 unsigned trk_path = 0;
122                 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
123                 {
124                         map<unsigned, int>::const_iterator k = turnouts.find((*j)->get_turnout_id());
125                         if(k!=turnouts.end())
126                         {
127                                 track = *j;
128                                 trk_path = k->second;
129                                 break;
130                         }
131                 }
132
133                 if(!track)
134                         continue;
135
136                 i->second->add_track(*track);
137
138                 const vector<Endpoint> &eps = track->get_type().get_endpoints();
139                 unsigned ep = 0;
140                 for(unsigned j=0; j<eps.size(); ++i)
141                         if(eps[j].paths&(1<<trk_path))
142                         {
143                                 ep = j;
144                                 break;
145                         }
146
147                 Track *start = track;
148                 while(1)
149                 {
150                         unsigned out_ep = track->traverse(ep, trk_path);
151                         Track *next = track->get_links()[out_ep];
152                         if(!next || next == start)
153                                 break;
154                         ep = next->get_endpoint_by_link(*track);
155                         if(next->get_type().get_n_paths()>1)
156                         {
157                                 map<unsigned, int>::const_iterator j = turnouts.find(next->get_turnout_id());
158                                 if(j==turnouts.end())
159                                         break;
160                                 trk_path = j->second;
161                         }
162                         else
163                                 trk_path = 0;
164                         i->second->add_track(*next);
165                         track = next;
166                 }
167         }
168 }
169
170
171 Layout::Loader::Loader(Layout &l):
172         DataFile::BasicLoader<Layout>(l)
173 {
174         add("base",  &Layout::base);
175         add("route", &Loader::route);
176         add("track", &Loader::track);
177 }
178
179 void Layout::Loader::finish()
180 {
181         obj.check_links();
182         obj.check_routes();
183
184         for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
185                 (*i)->check_slope();
186 }
187
188 void Layout::Loader::route(const string &n)
189 {
190         RefPtr<Route> rte = new Route(obj, n);
191         load_sub(*rte);
192         obj.add_route(*rte.release());
193 }
194
195 void Layout::Loader::track(unsigned art_nr)
196 {
197         TrackType &type = obj.catalogue.get_track(art_nr);
198
199         RefPtr<Track> trk = new Track(type);
200         load_sub(*trk);
201         obj.add_track(*trk.release());
202 }
203
204 } // namespace Marklin