]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/block.cpp
Handle sensors in a separate class
[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         layout(l),
18         id(0),
19         sensor_id(start.get_sensor_id()),
20         turnout_id(start.get_turnout_id()),
21         train(0)
22 {
23         tracks.insert(&start);
24         start.set_block(this);
25
26         list<Track *> queue;
27         queue.push_back(&start);
28
29         while(!queue.empty())
30         {
31                 Track *track = queue.front();
32                 queue.erase(queue.begin());
33
34                 const vector<Track *> &links = track->get_links();
35                 for(unsigned i=0; i<links.size(); ++i)
36                         if(links[i] && !tracks.count(links[i]))
37                         {
38                                 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
39                                 {
40                                         queue.push_back(links[i]);
41                                         tracks.insert(links[i]);
42                                         links[i]->set_block(this);
43                                 }
44                                 else
45                                         endpoints.push_back(Endpoint(track, i));
46                         }
47         }
48
49         determine_id();
50
51         for(unsigned i=0; i<endpoints.size(); ++i)
52         {
53                 unsigned path = 1<<i;
54                 endpoints[i].paths |= path;
55                 find_paths(endpoints[i].track_iter(), path);
56         }
57
58         sensor = new TrackCircuit(layout, *this);
59
60         layout.add_block(*this);
61 }
62
63 Block::~Block()
64 {
65         set<Track *> trks = tracks;
66         tracks.clear();
67         for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
68                 (*i)->set_block(0);
69
70         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
71                 if(Block *blk = i->link)
72                 {
73                         i->link = 0;
74                         blk->break_link(*this);
75                 }
76
77         layout.remove_block(*this);
78
79         delete sensor;
80 }
81
82 bool Block::has_track(Track &t) const
83 {
84         return tracks.count(&t);
85 }
86
87 const Block::Endpoint &Block::get_endpoint(unsigned i) const
88 {
89         if(i>=endpoints.size())
90                 throw out_of_range("Block::get_endpoint");
91
92         return endpoints[i];
93 }
94
95 int Block::get_endpoint_by_link(Block &other) const
96 {
97         for(unsigned i=0; i<endpoints.size(); ++i)
98                 if(endpoints[i].link==&other)
99                         return i;
100
101         return -1;
102 }
103
104 float Block::get_path_length(unsigned entry, const Route *route) const
105 {
106         if(entry>=endpoints.size())
107                 throw out_of_range("Block::get_path_length");
108
109         TrackIter t_iter = endpoints[entry].track_iter();
110
111         float result = 0;
112         while(t_iter && has_track(*t_iter))
113         {
114                 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
115                 result += t_iter->get_type().get_path_length(path);
116
117                 t_iter = t_iter.next(path);
118         }
119
120         return result;
121 }
122
123 void Block::check_link(Block &other)
124 {
125         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
126         {
127                 if(i->link)
128                         continue;
129
130                 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
131                         if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
132                         {
133                                 i->link = &other;
134                                 j->link = this;
135
136                                 determine_id();
137                                 other.determine_id();
138                         }
139         }
140 }
141
142 void Block::break_link(Block &other)
143 {
144         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
145                 if(i->link==&other)
146                 {
147                         i->link = 0;
148                         other.break_link(*this);
149                         determine_id();
150                 }
151 }
152
153 Block *Block::get_link(unsigned epi) const
154 {
155         if(epi>=endpoints.size())
156                 throw out_of_range("Block::get_link");
157         return endpoints[epi].link;
158 }
159
160 bool Block::reserve(Train *t)
161 {
162         if(!t || !train)
163         {
164                 train = t;
165                 signal_reserved.emit(train);
166                 return true;
167         }
168         else
169                 return false;
170 }
171
172 void Block::find_paths(const TrackIter &track, unsigned path)
173 {
174         unsigned mask = track.endpoint().paths;
175         for(unsigned i=0; mask>>i; ++i)
176                 if(mask&(1<<i))
177                 {
178                         TrackIter next = track.next(i);
179                         if(!next)
180                                 continue;
181                         else if(has_track(*next))
182                                 find_paths(track.next(i), path);
183                         else
184                         {
185                                 next = next.flip();
186                                 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
187                                         if(j->track==next.track() && j->track_ep==next.entry())
188                                                 j->paths |= path;
189                         }
190                 }
191 }
192
193 void Block::determine_id()
194 {
195         if(sensor_id)
196                 id = 0x1000|sensor_id;
197         else if(turnout_id)
198                 id = 0x2000|turnout_id;
199         else if(endpoints.size()==2)
200         {
201                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
202                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
203                 if(id2<id1)
204                         swap(id1, id2);
205                 id = (id1<<16)|id2;
206         }
207         else if(endpoints.size()==1)
208         {
209                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
210                 id = 0x10000 | id1;
211         }
212 }
213
214
215 Block::Endpoint::Endpoint(Track *t, unsigned e):
216         track(t),
217         track_ep(e),
218         link(0),
219         paths(0)
220 { }
221
222 TrackIter Block::Endpoint::track_iter() const
223 {
224         return TrackIter(track, track_ep);
225 }
226
227 } // namespace R2C2