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()),
25 if(start.get_type().is_turnout())
27 unsigned nls = start.get_n_link_slots();
28 for(unsigned i=0; i<nls; ++i)
29 endpoints.push_back(Endpoint(&start, i));
33 unsigned nls = start.get_n_link_slots();
34 for(unsigned i=0; i<nls; ++i)
36 TrackIter iter = TrackIter(&start, i).flip();
37 for(; (iter && check_validity(*iter)==VALID); iter=iter.next())
39 if((iter = iter.flip()))
40 endpoints.push_back(Endpoint(iter.track(), iter.entry()));
46 sensor = new TrackCircuit(layout, *this);
53 set<Track *> trks = tracks;
55 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
58 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
59 if(Block *blk = i->link)
62 blk->break_link(*this);
70 void Block::on_track_added(Track &track)
72 track.set_block(this);
75 TrackChain::Validity Block::check_validity(Track &track) const
77 if(track.get_sensor_id()!=sensor_id || track.get_turnout_id()!=turnout_id)
80 return TrackChain::check_validity(track);
83 const Block::Endpoint &Block::get_endpoint(unsigned i) const
85 if(i>=endpoints.size())
86 throw out_of_range("Block::get_endpoint");
91 int Block::get_endpoint_by_link(Block &other) const
93 for(unsigned i=0; i<endpoints.size(); ++i)
94 if(endpoints[i].link==&other)
100 float Block::get_path_length(unsigned entry, const Route *route) const
102 if(entry>=endpoints.size())
103 throw out_of_range("Block::get_path_length");
105 TrackIter t_iter = endpoints[entry].track_iter();
108 while(t_iter && has_track(*t_iter))
110 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
111 result += t_iter->get_type().get_path_length(path);
113 t_iter = t_iter.next(path);
119 void Block::check_link(Block &other)
121 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
126 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
127 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
133 other.determine_id();
138 void Block::break_link(Block &other)
140 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
144 other.break_link(*this);
149 Block *Block::get_link(unsigned epi) const
151 if(epi>=endpoints.size())
152 throw out_of_range("Block::get_link");
153 return endpoints[epi].link;
156 bool Block::reserve(Train *t)
161 signal_reserved.emit(train);
168 void Block::determine_id()
171 id = 0x1000|sensor_id;
173 id = 0x2000|turnout_id;
174 else if(endpoints.size()==2)
176 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
177 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
182 else if(endpoints.size()==1)
184 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
190 Block::Endpoint::Endpoint(Track *t, unsigned e):
196 TrackIter Block::Endpoint::track_iter() const
198 return TrackIter(track, track_ep);