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