X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=26886344b0a1312a9bb8023c065ed6220fdcb70c;hb=971d9b523807d5d1c626a00bfb22b9d13f5edfc0;hp=8ecb9b9bac00833eaa70190982fcb1933b43e9ae;hpb=64d23de66c33d0f77454c3db2e40cccc18f7851b;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 8ecb9b9..2688634 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -33,19 +33,26 @@ Track::Track(Layout &l, const TrackType &t): block(0), slope(0), flex(false), - turnout_id(0), - sensor_id(0), + turnout_addr(0), + sensor_addr(0), links(type.get_endpoints().size()), active_path(0), path_changing(false) { if(type.is_turnout()) - turnout_id = layout.allocate_turnout_id(); + { + turnout_addr = layout.allocate_turnout_address(); + + if(layout.has_driver()) + { + Driver &driver = layout.get_driver(); + turnout_id = driver.add_turnout(turnout_addr, type); + driver.signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event)); + } + } layout.add(*this); - if(layout.has_driver()) - layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event)); for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ; } @@ -53,6 +60,8 @@ Track::Track(Layout &l, const TrackType &t): Track::~Track() { break_links(); + if(layout.has_driver() && turnout_id) + layout.get_driver().remove_turnout(turnout_id); layout.remove(*this); } @@ -148,32 +157,38 @@ void Track::check_slope() signal_moved.emit(); } -void Track::set_turnout_id(unsigned i) +void Track::set_turnout_address(unsigned a) { if(!type.is_turnout()) throw logic_error("not a turnout"); - if(!i) + if(!a) throw invalid_argument("Track::set_turnout_id"); - turnout_id = i; + Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0); + + if(driver && turnout_id) + driver->remove_turnout(turnout_id); + turnout_addr = a; layout.create_blocks(*this); layout.update_routes(); - if(layout.has_driver() && turnout_id) - layout.get_driver().add_turnout(turnout_id, type); + if(driver && turnout_addr) + turnout_id = driver->add_turnout(turnout_addr, type); + else + turnout_id = 0; } -void Track::set_sensor_id(unsigned i) +void Track::set_sensor_address(unsigned a) { if(type.is_turnout()) throw logic_error("is a turnout"); - sensor_id = i; + sensor_addr = a; layout.create_blocks(*this); } void Track::set_active_path(unsigned p) { - if(!turnout_id) + if(!type.is_turnout()) throw logic_error("not a turnout"); if(!(type.get_paths()&(1<
break_link(j);
links[i] = otrack;
otrack->links[j] = this;
check_slope();
@@ -337,9 +360,14 @@ bool Track::break_link(unsigned i)
return false;
links[i] = 0;
- other->break_link(*this);
- // XXX Creates the blocks twice, because the other track calls this too
- layout.create_blocks(*this);
+ if(!other->break_link(*this))
+ {
+ /* If the call doesn't succeed, it means that the other track already
+ broke the link and is calling us right now. Recreate blocks in the inner
+ call so it occurs before any signals are emitted. */
+ layout.create_blocks(*this);
+ }
+
signal_link_changed.emit(i, 0);
return true;
@@ -372,20 +400,17 @@ void Track::save(list