]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/block.cpp
Rename turnout/sensor_id fields in Track and Block to *_addr
[r2c2.git] / source / libr2c2 / block.cpp
1 #include <algorithm>
2 #include <msp/time/units.h>
3 #include "block.h"
4 #include "driver.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::on_track_added(Track &track)
78 {
79         track.set_block(this);
80 }
81
82 TrackChain::Validity Block::check_validity(Track &track) const
83 {
84         if(track.get_sensor_address()!=sensor_addr || track.get_turnout_address()!=turnout_addr)
85                 return INCOMPATIBLE;
86
87         return TrackChain::check_validity(track);
88 }
89
90 const Block::Endpoint &Block::get_endpoint(unsigned i) const
91 {
92         if(i>=endpoints.size())
93                 throw out_of_range("Block::get_endpoint");
94
95         return endpoints[i];
96 }
97
98 int Block::get_endpoint_by_link(Block &other) const
99 {
100         for(unsigned i=0; i<endpoints.size(); ++i)
101                 if(endpoints[i].link==&other)
102                         return i;
103
104         return -1;
105 }
106
107 float Block::get_path_length(unsigned entry, const Route *route) const
108 {
109         if(entry>=endpoints.size())
110                 throw out_of_range("Block::get_path_length");
111
112         TrackIter t_iter = endpoints[entry].track_iter();
113
114         float result = 0;
115         while(t_iter && has_track(*t_iter))
116         {
117                 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
118                 result += t_iter->get_type().get_path_length(path);
119
120                 t_iter = t_iter.next(path);
121         }
122
123         return result;
124 }
125
126 void Block::check_link(Block &other)
127 {
128         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
129         {
130                 if(i->link)
131                         continue;
132
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)
135                         {
136                                 i->link = &other;
137                                 j->link = this;
138
139                                 determine_id();
140                                 other.determine_id();
141                         }
142         }
143 }
144
145 void Block::break_link(Block &other)
146 {
147         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
148                 if(i->link==&other)
149                 {
150                         i->link = 0;
151                         other.break_link(*this);
152                         determine_id();
153                 }
154 }
155
156 Block *Block::get_link(unsigned epi) const
157 {
158         if(epi>=endpoints.size())
159                 throw out_of_range("Block::get_link");
160         return endpoints[epi].link;
161 }
162
163 bool Block::reserve(Train *t)
164 {
165         if(!t || !train)
166         {
167                 train = t;
168                 signal_reserved.emit(train);
169                 return true;
170         }
171         else
172                 return false;
173 }
174
175 void Block::determine_id()
176 {
177         if(sensor_addr)
178                 id = 0x1000|sensor_addr;
179         else if(turnout_addr)
180                 id = 0x2000|turnout_addr;
181         else if(endpoints.size()==2)
182         {
183                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
184                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
185                 if(id2<id1)
186                         swap(id1, id2);
187                 id = (id1<<16)|id2;
188         }
189         else if(endpoints.size()==1)
190         {
191                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
192                 id = 0x10000 | id1;
193         }
194 }
195
196
197 Block::Endpoint::Endpoint(Track *t, unsigned e):
198         track(t),
199         track_ep(e),
200         link(0)
201 { }
202
203 TrackIter Block::Endpoint::track_iter() const
204 {
205         return TrackIter(track, track_ep);
206 }
207
208 } // namespace R2C2