]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/trackiter.h
e2f1d85dfde9e682cc0d3a7b2f555bdd0d0313d9
[r2c2.git] / source / libmarklin / trackiter.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBMARKLIN_TRACKITER_H_
9 #define LIBMARKLIN_TRACKITER_H_
10
11 #include <set>
12 #include <msp/core/refptr.h>
13 #include "tracktype.h"
14
15 namespace Marklin {
16
17 class Track;
18
19 /**
20 An iterator for traversing tracks.
21 */
22 class TrackIter
23 {
24 private:
25         Track *_track;
26         unsigned _entry;
27
28 public:
29         TrackIter();
30         TrackIter(Track *, unsigned);
31
32         Track *track() const { return _track; }
33         unsigned entry() const { return _entry; }
34         const TrackType::Endpoint &endpoint() const;
35
36 private:
37         int get_exit(unsigned) const;
38 public:
39         TrackIter next() const;
40         TrackIter next(unsigned) const;
41         TrackIter reverse() const;
42         TrackIter reverse(unsigned) const;
43         TrackIter flip() const;
44
45         Track &operator*() const;
46         Track *operator->() const { return _track; }
47         bool operator==(const TrackIter &) const;
48         bool operator!=(const TrackIter &other) const { return !(*this==other); }
49         operator bool() const { return _track!=0; }
50 };
51
52
53 /**
54 A track iterator that detects looping.
55
56 A list of visited tracks is maintained internally to the iterator.  This list
57 is shared between iterators as long as next() is only called once per iterator.
58 Subsequent calls to next() cause the head of the list to be copied.
59 */
60 class TrackLoopIter: public TrackIter
61 {
62 private:
63         typedef std::list<Track *> TrackList;
64
65         Msp::RefPtr<TrackList> _visited;
66         TrackList::iterator _last;
67         bool _looped;
68
69 public:
70         TrackLoopIter();
71         TrackLoopIter(Track *, unsigned);
72         TrackLoopIter(const TrackIter &);
73 private:
74         TrackLoopIter(const TrackIter &, Msp::RefPtr<TrackList>, const TrackList::iterator &);
75
76 public:
77         bool looped() const { return _looped; }
78
79         TrackLoopIter next() const;
80         TrackLoopIter next(unsigned) const;
81 };
82
83 } // namespace Marklin
84
85 #endif