]> git.tdb.fi Git - r2c2.git/commitdiff
Reformat the block reservation loop
authorMikko Rasa <tdb@tdb.fi>
Wed, 16 Dec 2009 21:20:34 +0000 (21:20 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 16 Dec 2009 21:20:34 +0000 (21:20 +0000)
Don't crash if rsv_blocks is empty at the end of reserve_more

source/libmarklin/train.cpp

index 12c4c26586a89df5ffa1422abf317b314b8e7ae2..7af10280228fa751970c4df739f1506400dc7271 100644 (file)
@@ -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());