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