]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/block.cpp
Get rid of some obsolete #includes
[r2c2.git] / source / libr2c2 / block.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include <msp/time/units.h>
4 #include "block.h"
5 #include "layout.h"
6 #include "route.h"
7 #include "trackcircuit.h"
8 #include "trackiter.h"
9 #include "tracktype.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Block::Block(Layout &l, Track &start):
17         TrackChain(l),
18         id(0),
19         sensor_addr(start.get_sensor_address()),
20         turnout_addr(start.get_turnout_address()),
21         conflict(false),
22         sensor(0),
23         train(0)
24 {
25         add_track(start);
26
27         if(start.get_type().is_turnout())
28         {
29                 unsigned nls = start.get_n_link_slots();
30                 for(unsigned i=0; i<nls; ++i)
31                         endpoints.push_back(Endpoint(&start, i));
32         }
33         else
34         {
35                 unsigned nls = start.get_n_link_slots();
36                 for(unsigned i=0; i<nls; ++i)
37                 {
38                         TrackIter iter = TrackIter(&start, i).flip();
39                         for(; (iter && check_validity(*iter)==VALID); iter=iter.next())
40                                 add_track(*iter);
41                         if((iter = iter.flip()))
42                                 endpoints.push_back(Endpoint(iter.track(), iter.entry()));
43                 }
44         }
45
46         determine_id();
47
48         const set<Block *> &blocks = layout.get_all<Block>();
49         for(set<Block *>::const_iterator i=blocks.begin(); (!conflict && i!=blocks.end()); ++i)
50                 conflict = (id==(*i)->get_id());
51
52         if(!conflict && sensor_addr)
53                 sensor = new TrackCircuit(layout, *this);
54
55         layout.add(*this);
56 }
57
58 Block::~Block()
59 {
60         set<Track *> trks = tracks;
61         tracks.clear();
62         for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
63                 (*i)->set_block(0);
64
65         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
66                 if(Block *blk = i->link)
67                 {
68                         i->link = 0;
69                         blk->break_link(*this);
70                 }
71
72         layout.remove(*this);
73
74         delete sensor;
75 }
76
77 void Block::set_name(const string &)
78 {
79         throw logic_error("Block names can't be set");
80 }
81
82 void Block::on_track_added(Track &track)
83 {
84         track.set_block(this);
85 }
86
87 TrackChain::Validity Block::check_validity(Track &track) const
88 {
89         if(track.get_sensor_address()!=sensor_addr || track.get_turnout_address()!=turnout_addr)
90                 return INCOMPATIBLE;
91
92         return TrackChain::check_validity(track);
93 }
94
95 const Block::Endpoint &Block::get_endpoint(unsigned i) const
96 {
97         if(i>=endpoints.size())
98                 throw out_of_range("Block::get_endpoint");
99
100         return endpoints[i];
101 }
102
103 int Block::get_endpoint_by_link(Block &other) const
104 {
105         for(unsigned i=0; i<endpoints.size(); ++i)
106                 if(endpoints[i].link==&other)
107                         return i;
108
109         return -1;
110 }
111
112 float Block::get_path_length(unsigned entry, const Route *route) const
113 {
114         if(entry>=endpoints.size())
115                 throw out_of_range("Block::get_path_length");
116
117         TrackIter t_iter = endpoints[entry].track_iter();
118
119         float result = 0;
120         while(t_iter && has_track(*t_iter))
121         {
122                 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
123                 result += t_iter->get_type().get_path_length(path);
124
125                 t_iter = t_iter.next(path);
126         }
127
128         return result;
129 }
130
131 void Block::check_link(Block &other)
132 {
133         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
134         {
135                 if(i->link)
136                         continue;
137
138                 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
139                         if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
140                         {
141                                 i->link = &other;
142                                 j->link = this;
143
144                                 determine_id();
145                                 other.determine_id();
146                         }
147         }
148 }
149
150 void Block::break_link(Block &other)
151 {
152         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
153                 if(i->link==&other)
154                 {
155                         i->link = 0;
156                         other.break_link(*this);
157                         determine_id();
158                 }
159 }
160
161 Block *Block::get_link(unsigned epi) const
162 {
163         if(epi>=endpoints.size())
164                 throw out_of_range("Block::get_link");
165         return endpoints[epi].link;
166 }
167
168 bool Block::reserve(Train *t)
169 {
170         if(!t || !train)
171         {
172                 train = t;
173                 signal_reserved.emit(train);
174                 return true;
175         }
176         else
177                 return false;
178 }
179
180 void Block::determine_id()
181 {
182         string n;
183         if(sensor_addr)
184         {
185                 id = 0x1000|sensor_addr;
186                 n = format("Sensor %d", sensor_addr);
187         }
188         else if(turnout_addr)
189         {
190                 id = 0x2000|turnout_addr;
191                 n = format("Turnout %d", turnout_addr);
192         }
193         else if(endpoints.size()==2)
194         {
195                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
196                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
197                 if(id2<id1)
198                         swap(id1, id2);
199                 id = (id1<<16)|id2;
200         }
201         else if(endpoints.size()==1)
202         {
203                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
204                 id = 0x10000 | id1;
205         }
206
207         if(n.empty())
208                 n = format("Block %x", id);
209
210         name = n;
211         signal_name_changed.emit(name);
212 }
213
214 DataFile::Statement Block::save_reference() const
215 {
216         return (DataFile::Statement("block"), id);
217 }
218
219
220 Block::Endpoint::Endpoint(Track *t, unsigned e):
221         track(t),
222         track_ep(e),
223         link(0)
224 { }
225
226 TrackIter Block::Endpoint::track_iter() const
227 {
228         return TrackIter(track, track_ep);
229 }
230
231 } // namespace R2C2