-/* $Id$
-
-This file is part of R²C²
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
-#include <msp/core/except.h>
#include "block.h"
#include "blockiter.h"
#include "route.h"
_entry(b ? e : 0)
{
if(_block && _entry>_block->get_endpoints().size())
- throw InvalidParameterValue("Endpoint index not valid for block");
+ throw out_of_range("BlockIter::BlockIter");
}
TrackIter BlockIter::track_iter() const
if(!_block)
return TrackIter();
- const Block::Endpoint &ep = _block->get_endpoint(_entry);
- return TrackIter(ep.track, ep.track_ep);
+ return _block->get_endpoint(_entry).track_iter();
}
const Block::Endpoint &BlockIter::endpoint() const
{
if(!_block)
- throw InvalidState("BlockIter is null");
+ throw logic_error("null block");
return _block->get_endpoint(_entry);
}
int BlockIter::get_exit(const Route *route) const
{
const vector<Block::Endpoint> &eps = _block->get_endpoints();
- TrackIter t_iter = track_iter();
-
- while(t_iter)
+ if(_block->get_turnout_address())
{
- if(!_block->has_track(*t_iter))
- throw LogicError("Block traversal strayed out of the block");
-
+ /* The endpoints of a turnout block exactly correspond to the endpoints
+ of the track. */
+ TrackIter t_iter = track_iter();
unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
- TrackIter t_exit = t_iter.reverse(path);
-
- for(unsigned i=0; i<eps.size(); ++i)
- if(eps[i].track==t_exit.track() && eps[i].track_ep==t_exit.entry())
- return i;
-
- t_iter = t_exit.flip();
+ return t_iter.reverse(path).entry();
}
-
- return -1;
+ else if(eps.size()==2)
+ return 1-_entry;
+ else
+ return -1;
}
BlockIter BlockIter::next(const Route *route) const
Block &BlockIter::operator*() const
{
if(!_block)
- throw InvalidState("BlockIter is null");
+ throw logic_error("null block");
return *_block;
}