]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/block.cpp
Add TrackIter and BlockIter classes
[r2c2.git] / source / libmarklin / block.cpp
index ec07947d7e1cbffa500f05b29661ba7178b99fa8..952fe50d56adc538ad6f4e8cab526cb815485a33 100644 (file)
@@ -9,6 +9,7 @@ Distributed under the GPL
 #include "block.h"
 #include "layout.h"
 #include "route.h"
+#include "trackiter.h"
 #include "tracktype.h"
 
 using namespace std;
@@ -93,45 +94,28 @@ int Block::get_endpoint_by_link(Block &other) const
        return -1;
 }
 
-unsigned Block::traverse(unsigned epi, float *len) const
+float Block::get_path_length(unsigned entry, const Route *route) const
 {
-       return traverse(epi, 0, len);
-}
-
-unsigned Block::traverse(unsigned epi, const Route *route, float *len) const
-{
-       if(epi>=endpoints.size())
+       if(entry>=endpoints.size())
                throw InvalidParameterValue("Endpoint index out of range");
 
-       const Endpoint &ep = endpoints[epi];
-       Track *track = ep.track;
-       unsigned track_ep = ep.track_ep;
+       TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
 
-       if(len)
-               *len = 0;
-
-       while(1)
+       float result = 0;
+       while(t_iter && has_track(*t_iter))
        {
-               int cur_path = -1;
-               if(track->get_turnout_id() && route)
-                       cur_path = route->get_turnout(track->get_turnout_id());
-               if(cur_path==-1)
-                       cur_path = track->get_active_path();
-
-               if(len)
-                       *len += track->get_type().get_path_length(cur_path);
-
-               unsigned other_ep = track->traverse(track_ep, cur_path);
-               for(unsigned i=0; i<endpoints.size(); ++i)
-                       if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
-                               return i;
-
-               Track *next = track->get_link(other_ep);
-               if(!tracks.count(next))
-                       throw LogicError("Block traversal strayed out of the block");
-               track_ep = next->get_endpoint_by_link(*track);
-               track = next;
+               int path = -1;
+               if(t_iter->get_turnout_id() && route)
+                       path = route->get_turnout(t_iter->get_turnout_id());
+               if(path==-1)
+                       path = t_iter->get_active_path();
+
+               result += t_iter->get_type().get_path_length(path);
+
+               t_iter = t_iter.next(path);
        }
+
+       return result;
 }
 
 void Block::check_link(Block &other)