2 #include <msp/time/units.h>
7 #include "trackcircuit.h"
16 Block::Block(Layout &l, Track &start):
19 sensor_addr(start.get_sensor_address()),
20 turnout_addr(start.get_turnout_address()),
27 if(start.get_type().is_turnout())
29 unsigned nls = start.get_n_link_slots();
30 for(unsigned i=0; i<nls; ++i)
31 endpoints.push_back(Endpoint(&start, i));
35 unsigned nls = start.get_n_link_slots();
36 for(unsigned i=0; i<nls; ++i)
38 TrackIter iter = TrackIter(&start, i).flip();
39 for(; (iter && check_validity(*iter)==VALID); iter=iter.next())
41 if((iter = iter.flip()))
42 endpoints.push_back(Endpoint(iter.track(), iter.entry()));
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());
52 if(!conflict && sensor_addr)
53 sensor = new TrackCircuit(layout, *this);
60 set<Track *> trks = tracks;
62 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
65 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
66 if(Block *blk = i->link)
69 blk->break_link(*this);
77 void Block::on_track_added(Track &track)
79 track.set_block(this);
82 TrackChain::Validity Block::check_validity(Track &track) const
84 if(track.get_sensor_address()!=sensor_addr || track.get_turnout_address()!=turnout_addr)
87 return TrackChain::check_validity(track);
90 const Block::Endpoint &Block::get_endpoint(unsigned i) const
92 if(i>=endpoints.size())
93 throw out_of_range("Block::get_endpoint");
98 int Block::get_endpoint_by_link(Block &other) const
100 for(unsigned i=0; i<endpoints.size(); ++i)
101 if(endpoints[i].link==&other)
107 float Block::get_path_length(unsigned entry, const Route *route) const
109 if(entry>=endpoints.size())
110 throw out_of_range("Block::get_path_length");
112 TrackIter t_iter = endpoints[entry].track_iter();
115 while(t_iter && has_track(*t_iter))
117 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
118 result += t_iter->get_type().get_path_length(path);
120 t_iter = t_iter.next(path);
126 void Block::check_link(Block &other)
128 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
133 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
134 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
140 other.determine_id();
145 void Block::break_link(Block &other)
147 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
151 other.break_link(*this);
156 Block *Block::get_link(unsigned epi) const
158 if(epi>=endpoints.size())
159 throw out_of_range("Block::get_link");
160 return endpoints[epi].link;
163 bool Block::reserve(Train *t)
168 signal_reserved.emit(train);
175 void Block::determine_id()
178 id = 0x1000|sensor_addr;
179 else if(turnout_addr)
180 id = 0x2000|turnout_addr;
181 else if(endpoints.size()==2)
183 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
184 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
189 else if(endpoints.size()==1)
191 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
197 Block::Endpoint::Endpoint(Track *t, unsigned e):
203 TrackIter Block::Endpoint::track_iter() const
205 return TrackIter(track, track_ep);