From: Mikko Rasa Date: Wed, 16 Dec 2009 21:20:34 +0000 (+0000) Subject: Reformat the block reservation loop X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e1e5bf92fc73bd117c509ceea1c7de7d95288a7d;hp=2fe7cbcb761487bc7409b93b372da6f8ab3c581e;p=r2c2.git Reformat the block reservation loop Don't crash if rsv_blocks is empty at the end of reserve_more --- diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 12c4c26..7af1028 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -323,33 +323,29 @@ unsigned Train::reserve_more() while(nsens<2) { int exit = last->block->traverse(last->entry); - if(exit>=0) + if(exit<0) + break; + + Block *link = last->block->get_link(exit); + if(!link || !link->reserve(this)) + break; + + if(route && link->get_turnout_id()) { - Block *link = last->block->get_link(exit); - if(link && link->reserve(this)) - { - if(route && link->get_turnout_id()) - { - int path = route->get_turnout(link->get_turnout_id()); - if(path>=0) - trfc_mgr.get_control().get_turnout(link->get_turnout_id()).set_path(path); - } - rsv_blocks.push_back(BlockRef(link, link->get_endpoint_by_link(*last->block))); - last = &rsv_blocks.back(); - if(last->block->get_sensor_id()) - { - ++nsens; - result = true; - } - } - else - break; + int path = route->get_turnout(link->get_turnout_id()); + if(path>=0) + trfc_mgr.get_control().get_turnout(link->get_turnout_id()).set_path(path); + } + rsv_blocks.push_back(BlockRef(link, link->get_endpoint_by_link(*last->block))); + last = &rsv_blocks.back(); + if(last->block->get_sensor_id()) + { + ++nsens; + result = true; } - else - break; } - while(last && !last->block->get_sensor_id()) + while(!rsv_blocks.empty() && !last->block->get_sensor_id()) { last->block->reserve(0); rsv_blocks.erase(--rsv_blocks.end());