3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/core/except.h>
10 #include "trackiter.h"
11 #include "tracktype.h"
18 TrackIter::TrackIter():
23 TrackIter::TrackIter(Track *t, unsigned e):
27 if(_track && _entry>_track->get_type().get_endpoints().size())
28 throw InvalidParameterValue("Endpoint index not valid for track");
31 int TrackIter::get_exit(unsigned path) const
33 const vector<TrackType::Endpoint> &eps = _track->get_type().get_endpoints();
35 // Find an endpoint that's connected to the entry and has the requested path
36 for(unsigned i=0; i<eps.size(); ++i)
37 if(i!=_entry && (eps[i].paths&(1<<path)) && (eps[i].paths&eps[_entry].paths))
43 TrackIter TrackIter::next() const
48 return next(_track->get_active_path());
51 TrackIter TrackIter::next(unsigned path) const
56 int exit = get_exit(path);
61 result._track = _track->get_link(exit);
62 result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
67 TrackIter TrackIter::reverse() const
72 return reverse(_track->get_active_path());
75 TrackIter TrackIter::reverse(unsigned path) const
80 int exit = get_exit(path);
84 return TrackIter(_track, exit);
87 TrackIter TrackIter::flip() const
93 result._track = _track->get_link(_entry);
94 result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
99 Track &TrackIter::operator*() const
102 throw InvalidState("TrackIter is null");
107 bool TrackIter::operator==(const TrackIter &other) const
109 return _track==other._track && _entry==other._entry;
112 } // namespace Marklin