X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fblock.cpp;h=02b6da5c7203dffaf346e4ad3690a0c77b1d2bee;hb=bd649cbc44e04f5e456ca19b7a32ebe479c130e1;hp=db1942bb5b613fde7ff1b71272383455740bc875;hpb=98047057e4adae31aa449161ca845db1a6c2db77;p=r2c2.git diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index db1942b..02b6da5 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -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; @@ -24,6 +25,7 @@ Block::Block(Layout &l, Track &start): train(0) { tracks.insert(&start); + start.set_block(this); list queue; queue.push_back(&start); @@ -41,6 +43,7 @@ Block::Block(Layout &l, Track &start): { queue.push_back(links[i]); tracks.insert(links[i]); + links[i]->set_block(this); } else endpoints.push_back(Endpoint(track, i)); @@ -62,6 +65,11 @@ Block::Block(Layout &l, Track &start): Block::~Block() { + set trks = tracks; + tracks.clear(); + for(set::iterator i=trks.begin(); i!=trks.end(); ++i) + (*i)->set_block(0); + for(vector::iterator i=endpoints.begin(); i!=endpoints.end(); ++i) if(Block *blk = i->link) { @@ -72,7 +80,12 @@ Block::~Block() layout.remove_block(*this); } -int Block::get_endpoint_by_link(const Block &other) const +bool Block::has_track(Track &t) const +{ + return tracks.count(&t); +} + +int Block::get_endpoint_by_link(Block &other) const { for(unsigned i=0; i=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; - - if(len) - *len = 0; + TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep); - 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(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) @@ -175,7 +171,7 @@ void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set &eps = track.get_type().get_endpoints(); + const vector &eps = track.get_type().get_endpoints(); for(unsigned i=0; i