]> git.tdb.fi Git - r2c2.git/blob - libr2c2/zone.cpp
Don't crash if a train has no router
[r2c2.git] / libr2c2 / zone.cpp
1 #include <msp/strings/format.h>
2 #include "block.h"
3 #include "layout.h"
4 #include "track.h"
5 #include "zone.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 namespace R2C2 {
11
12 Zone::Zone(Layout &l):
13         TrackChain(l),
14         number(0)
15 {
16         layout.add(*this);
17 }
18
19 Zone::~Zone()
20 {
21         layout.remove(*this);
22 }
23
24 void Zone::set_name(const string &g, const string &q, unsigned n)
25 {
26         group = g;
27         qualifier = q;
28         number = n;
29
30         signal_name_changed.emit(group, qualifier, number);
31 }
32
33 string Zone::get_name() const
34 {
35         string result = group;
36         if(!qualifier.empty())
37         {
38                 result += ' ';
39                 result += qualifier;
40         }
41         if(number)
42                 result += format(" %d", number);
43         return result;
44 }
45
46 void Zone::save(list<DataFile::Statement> &st) const
47 {
48         st.push_back((DataFile::Statement("group"), group));
49         if(!qualifier.empty())
50                 st.push_back((DataFile::Statement("qualifier"), qualifier));
51         if(number)
52                 st.push_back((DataFile::Statement("number"), number));
53
54         set<unsigned> block_ids;
55         for(TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
56                 block_ids.insert((*i)->get_block().get_id());
57
58         for(set<unsigned>::const_iterator i=block_ids.begin(); i!=block_ids.end(); ++i)
59                 st.push_back((DataFile::Statement("block"), *i));
60 }
61
62
63 Zone::Loader::Loader(Zone &z):
64         DataFile::ObjectLoader<Zone>(z)
65 {
66         add("block",     &Loader::block);
67         add("group",     &Zone::group);
68         add("number",    &Zone::number);
69         add("qualifier", &Zone::qualifier);
70 }
71
72 void Zone::Loader::block(unsigned b)
73 {
74         Block &blk = obj.layout.get_block(b);
75         const set<Track *> &btracks = blk.get_tracks();
76         obj.tracks.insert(btracks.begin(), btracks.end());
77 }
78
79 } // namespace R2C2