X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fblock.cpp;h=b9b65ad710dca9dd833177ee993b31562c3c4b5b;hb=e392d397f6b86a49a05e9738357ccbfc2a922f01;hp=d4a6878a68b1b6ba475b85fd17014fe490b6cceb;hpb=f42183985c65e1e12f19e9246dee90b8e7e44a34;p=r2c2.git diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index d4a6878..b9b65ad 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -1,31 +1,31 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ -#include "control.h" +#include #include "block.h" +#include "layout.h" +#include "route.h" +#include "trackiter.h" #include "tracktype.h" -#include "trafficmanager.h" -#include "turnout.h" using namespace std; using namespace Msp; namespace Marklin { -unsigned Block::next_id = 1; - -Block::Block(TrafficManager &tm, Track &start): - trfc_mgr(tm), - id(next_id++), +Block::Block(Layout &l, Track &start): + layout(l), + id(0), sensor_id(start.get_sensor_id()), turnout_id(start.get_turnout_id()), train(0) { tracks.insert(&start); + start.set_block(this); list queue; queue.push_back(&start); @@ -43,22 +43,48 @@ Block::Block(TrafficManager &tm, Track &start): { queue.push_back(links[i]); tracks.insert(links[i]); + links[i]->set_block(this); } else endpoints.push_back(Endpoint(track, i)); } } + determine_id(); + for(unsigned i=0; i visited; - find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited); + find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path); } + + layout.add_block(*this); } -int Block::get_endpoint_by_link(const Block &other) const +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) + { + i->link = 0; + blk->break_link(*this); + } + + layout.remove_block(*this); +} + +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; + 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)) { - unsigned cur_path = 0; - unsigned tid = track->get_turnout_id(); - if(tid) - { - Turnout &turnout = trfc_mgr.get_control().get_turnout(tid); - cur_path = turnout.get_path(); - } - - if(len) - *len += track->get_type().get_path_length(cur_path); + 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(); - unsigned other_ep = track->traverse(track_ep, cur_path); - for(unsigned i=0; i(other_ep)) - return i; + result += t_iter->get_type().get_path_length(path); - 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; + t_iter = t_iter.next(path); } + + return result; } void Block::check_link(Block &other) @@ -117,10 +129,24 @@ void Block::check_link(Block &other) { i->link = &other; j->link = this; + + determine_id(); + other.determine_id(); } } } +void Block::break_link(Block &other) +{ + for(vector::iterator i=endpoints.begin(); i!=endpoints.end(); ++i) + if(i->link==&other) + { + i->link = 0; + other.break_link(*this); + determine_id(); + } +} + Block *Block::get_link(unsigned epi) const { if(epi>=endpoints.size()) @@ -128,39 +154,57 @@ Block *Block::get_link(unsigned epi) const return endpoints[epi].link; } -bool Block::reserve(const Train *t) +bool Block::reserve(Train *t) { if(!t || !train) { train = t; - trfc_mgr.signal_block_reserved.emit(*this, train); + layout.signal_block_reserved.emit(*this, train); return true; } else return false; } -void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set &visited) +void Block::find_paths(TrackIter track, unsigned path) { - visited.insert(&track); - - const vector &eps = track.get_type().get_endpoints(); - for(unsigned i=0; iget_endpoint_by_link(track), path, visited); - else + unsigned mask = track->get_type().get_endpoints()[track.entry()].paths; + for(unsigned i=0; mask>>i; ++i) + if(mask&(1<::iterator j=endpoints.begin(); j!=endpoints.end(); ++j) - if(j->track==&track && j->track_ep==i) - j->paths |= path; + TrackIter next = track.next(i); + if(!next) + continue; + else if(has_track(*next)) + find_paths(track.next(i), path); + else + { + next = next.flip(); + for(vector::iterator j=endpoints.begin(); j!=endpoints.end(); ++j) + if(j->track==next.track() && j->track_ep==next.entry()) + j->paths |= path; + } } +} + +void Block::determine_id() +{ + if(sensor_id) + id = 0x1000|sensor_id; + else if(turnout_id) + id = 0x2000|turnout_id; + else if(endpoints.size()==2) + { + unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1; + unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1; + if(id2get_id() : 1; + id = 0x10000 | id1; } }