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 const TrackType::Endpoint &TrackIter::endpoint() const
35 throw InvalidState("TrackIter is null");
37 return _track->get_type().get_endpoint(_entry);
40 int TrackIter::get_exit(unsigned path) const
42 const vector<TrackType::Endpoint> &eps = _track->get_type().get_endpoints();
44 // Find an endpoint that's connected to the entry and has the requested path
45 for(unsigned i=0; i<eps.size(); ++i)
46 if(i!=_entry && (eps[i].paths&(1<<path)) && (eps[i].paths&eps[_entry].paths))
52 TrackIter TrackIter::next() const
57 return next(_track->get_active_path());
60 TrackIter TrackIter::next(unsigned path) const
65 int exit = get_exit(path);
70 result._track = _track->get_link(exit);
71 result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
76 TrackIter TrackIter::reverse() const
81 return reverse(_track->get_active_path());
84 TrackIter TrackIter::reverse(unsigned path) const
89 int exit = get_exit(path);
93 return TrackIter(_track, exit);
96 TrackIter TrackIter::flip() const
102 result._track = _track->get_link(_entry);
103 result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
108 Track &TrackIter::operator*() const
111 throw InvalidState("TrackIter is null");
116 bool TrackIter::operator==(const TrackIter &other) const
118 return _track==other._track && _entry==other._entry;
122 TrackLoopIter::TrackLoopIter():
126 TrackLoopIter::TrackLoopIter(Track *t, unsigned e):
128 _visited(new TrackList()),
129 _last(_visited->insert(_visited->end(), track())),
133 TrackLoopIter::TrackLoopIter(const TrackIter &i):
135 _visited(new TrackList()),
136 _last(_visited->insert(_visited->end(), track())),
140 TrackLoopIter::TrackLoopIter(const TrackIter &i, RefPtr<TrackList> v, const TrackList::iterator &l):
148 _looped = (_visited && find(_visited->begin(), _last, track())!=_last);
151 if(_last!=_visited->end())
153 _visited = new TrackList(_visited->begin(), _last);
154 _last = _visited->end();
156 _visited->push_back(track());
161 TrackLoopIter TrackLoopIter::next() const
163 return TrackLoopIter(TrackIter::next(), _visited, _last);
166 TrackLoopIter TrackLoopIter::next(unsigned path) const
168 return TrackLoopIter(TrackIter::next(path), _visited, _last);
171 } // namespace Marklin