2 #include <msp/core/raii.h>
3 #include <msp/strings/format.h>
4 #include <msp/time/units.h>
8 #include "trackcircuit.h"
10 #include "tracktype.h"
17 Block::Block(Layout &l, Track &start):
20 sensor_addr(start.get_sensor_address()),
21 turnout_addr(start.get_turnout_address()),
28 if(start.get_type().is_turnout())
30 unsigned nls = start.get_n_link_slots();
31 for(unsigned i=0; i<nls; ++i)
32 endpoints.push_back(Endpoint(&start, i));
36 unsigned nls = start.get_n_link_slots();
37 for(unsigned i=0; i<nls; ++i)
39 TrackIter iter = TrackIter(&start, i).flip();
40 for(; (iter && check_validity(*iter)==VALID); iter=iter.next())
42 if((iter = iter.flip()))
43 endpoints.push_back(Endpoint(iter.track(), iter.entry()));
49 const set<Block *> &blocks = layout.get_all<Block>();
50 for(set<Block *>::const_iterator i=blocks.begin(); (!conflict && i!=blocks.end()); ++i)
51 conflict = (id==(*i)->get_id());
53 if(!conflict && sensor_addr)
54 sensor = new TrackCircuit(layout, *this);
61 set<Track *> trks = tracks;
63 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
66 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
67 if(Block *blk = i->link)
70 blk->break_link(*this);
78 void Block::set_name(const string &)
80 throw logic_error("Block names can't be set");
83 void Block::on_track_added(Track &track)
85 track.set_block(this);
88 TrackChain::Validity Block::check_validity(Track &track) const
90 if(track.get_sensor_address()!=sensor_addr || track.get_turnout_address()!=turnout_addr)
93 return TrackChain::check_validity(track);
96 const Block::Endpoint &Block::get_endpoint(unsigned i) const
98 if(i>=endpoints.size())
99 throw out_of_range("Block::get_endpoint");
104 int Block::get_endpoint_by_link(Block &other) const
106 for(unsigned i=0; i<endpoints.size(); ++i)
107 if(endpoints[i].link==&other)
113 float Block::get_path_length(unsigned entry, const Route *route) const
115 if(entry>=endpoints.size())
116 throw out_of_range("Block::get_path_length");
118 TrackIter t_iter = endpoints[entry].track_iter();
121 while(t_iter && has_track(*t_iter))
123 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
124 result += t_iter->get_type().get_path_length(path);
126 t_iter = t_iter.next(path);
132 void Block::check_link(Block &other)
134 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
139 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
140 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
146 other.determine_id();
151 void Block::break_link(Block &other)
153 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
157 other.break_link(*this);
162 Block *Block::get_link(unsigned epi) const
164 if(epi>=endpoints.size())
165 throw out_of_range("Block::get_link");
166 return endpoints[epi].link;
169 bool Block::reserve(Train *t)
174 signal_reserved.emit(t);
181 void Block::determine_id()
186 id = 0x1000|sensor_addr;
187 n = format("Sensor %d", sensor_addr);
189 else if(turnout_addr)
191 id = 0x2000|turnout_addr;
192 n = format("Turnout %d", turnout_addr);
194 else if(endpoints.size()==2)
196 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
197 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
202 else if(endpoints.size()==1)
204 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
209 n = format("Block %x", id);
212 signal_name_changed.emit(name);
215 DataFile::Statement Block::save_reference() const
217 return (DataFile::Statement("block"), id);
221 Block::Endpoint::Endpoint(Track *t, unsigned e):
227 TrackIter Block::Endpoint::track_iter() const
229 return TrackIter(track, track_ep);