3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/core/except.h>
11 #include "trackiter.h"
12 #include "tracktype.h"
19 TrackIter::TrackIter():
24 TrackIter::TrackIter(Track *t, unsigned e):
28 if(_track && _entry>_track->get_type().get_endpoints().size())
29 throw InvalidParameterValue("Endpoint index not valid for track");
32 int TrackIter::get_exit(unsigned path) const
34 const vector<TrackType::Endpoint> &eps = _track->get_type().get_endpoints();
36 // Find an endpoint that's connected to the entry and has the requested path
37 for(unsigned i=0; i<eps.size(); ++i)
38 if(i!=_entry && (eps[i].paths&(1<<path)) && (eps[i].paths&eps[_entry].paths))
44 TrackIter TrackIter::next() const
49 return next(_track->get_active_path());
52 TrackIter TrackIter::next(unsigned path) const
57 int exit = get_exit(path);
62 result._track = _track->get_link(exit);
63 result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
68 TrackIter TrackIter::reverse() const
73 return reverse(_track->get_active_path());
76 TrackIter TrackIter::reverse(unsigned path) const
81 int exit = get_exit(path);
85 return TrackIter(_track, exit);
88 TrackIter TrackIter::flip() const
94 result._track = _track->get_link(_entry);
95 result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
100 Track &TrackIter::operator*() const
103 throw InvalidState("TrackIter is null");
108 bool TrackIter::operator==(const TrackIter &other) const
110 return _track==other._track && _entry==other._entry;
114 TrackLoopIter::TrackLoopIter():
118 TrackLoopIter::TrackLoopIter(Track *t, unsigned e):
120 _visited(new TrackList()),
121 _last(_visited->insert(_visited->end(), track())),
125 TrackLoopIter::TrackLoopIter(const TrackIter &i):
127 _visited(new TrackList()),
128 _last(_visited->insert(_visited->end(), track())),
132 TrackLoopIter::TrackLoopIter(const TrackIter &i, RefPtr<TrackList> v, const TrackList::iterator &l):
140 _looped = (_visited && find(_visited->begin(), _last, track())!=_last);
143 if(_last!=_visited->end())
145 _visited = new TrackList(_visited->begin(), _last);
146 _last = _visited->end();
148 _visited->push_back(track());
153 TrackLoopIter TrackLoopIter::next() const
155 return TrackLoopIter(TrackIter::next(), _visited, _last);
158 TrackLoopIter TrackLoopIter::next(unsigned path) const
160 return TrackLoopIter(TrackIter::next(path), _visited, _last);
163 } // namespace Marklin