2 #include <msp/time/units.h>
7 #include "trackcircuit.h"
16 Block::Block(Layout &l, Track &start):
19 sensor_id(start.get_sensor_id()),
20 turnout_id(start.get_turnout_id()),
26 if(start.get_type().is_turnout())
28 unsigned nls = start.get_n_link_slots();
29 for(unsigned i=0; i<nls; ++i)
30 endpoints.push_back(Endpoint(&start, i));
34 unsigned nls = start.get_n_link_slots();
35 for(unsigned i=0; i<nls; ++i)
37 TrackIter iter = TrackIter(&start, i).flip();
38 for(; (iter && check_validity(*iter)==VALID); iter=iter.next())
40 if((iter = iter.flip()))
41 endpoints.push_back(Endpoint(iter.track(), iter.entry()));
48 sensor = new TrackCircuit(layout, *this);
55 set<Track *> trks = tracks;
57 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
60 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
61 if(Block *blk = i->link)
64 blk->break_link(*this);
72 void Block::on_track_added(Track &track)
74 track.set_block(this);
77 TrackChain::Validity Block::check_validity(Track &track) const
79 if(track.get_sensor_id()!=sensor_id || track.get_turnout_id()!=turnout_id)
82 return TrackChain::check_validity(track);
85 const Block::Endpoint &Block::get_endpoint(unsigned i) const
87 if(i>=endpoints.size())
88 throw out_of_range("Block::get_endpoint");
93 int Block::get_endpoint_by_link(Block &other) const
95 for(unsigned i=0; i<endpoints.size(); ++i)
96 if(endpoints[i].link==&other)
102 float Block::get_path_length(unsigned entry, const Route *route) const
104 if(entry>=endpoints.size())
105 throw out_of_range("Block::get_path_length");
107 TrackIter t_iter = endpoints[entry].track_iter();
110 while(t_iter && has_track(*t_iter))
112 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
113 result += t_iter->get_type().get_path_length(path);
115 t_iter = t_iter.next(path);
121 void Block::check_link(Block &other)
123 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
128 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
129 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
135 other.determine_id();
140 void Block::break_link(Block &other)
142 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
146 other.break_link(*this);
151 Block *Block::get_link(unsigned epi) const
153 if(epi>=endpoints.size())
154 throw out_of_range("Block::get_link");
155 return endpoints[epi].link;
158 bool Block::reserve(Train *t)
163 signal_reserved.emit(train);
170 void Block::determine_id()
173 id = 0x1000|sensor_id;
175 id = 0x2000|turnout_id;
176 else if(endpoints.size()==2)
178 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
179 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
184 else if(endpoints.size()==1)
186 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
192 Block::Endpoint::Endpoint(Track *t, unsigned e):
198 TrackIter Block::Endpoint::track_iter() const
200 return TrackIter(track, track_ep);