]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/trackiter.h
Add TrackLoopIter
[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
14 namespace Marklin {
15
16 class Track;
17
18 /**
19 An iterator for traversing tracks.
20 */
21 class TrackIter
22 {
23 private:
24         Track *_track;
25         unsigned _entry;
26
27 public:
28         TrackIter();
29         TrackIter(Track *, unsigned);
30
31         Track *track() const { return _track; }
32         unsigned entry() const { return _entry; }
33
34 private:
35         int get_exit(unsigned) const;
36 public:
37         TrackIter next() const;
38         TrackIter next(unsigned) const;
39         TrackIter reverse() const;
40         TrackIter reverse(unsigned) const;
41         TrackIter flip() const;
42
43         Track &operator*() const;
44         Track *operator->() const { return _track; }
45         bool operator==(const TrackIter &) const;
46         bool operator!=(const TrackIter &other) const { return !(*this==other); }
47         operator bool() const { return _track!=0; }
48 };
49
50
51 /**
52 A track iterator that detects looping.
53
54 A list of visited tracks is maintained internally to the iterator.  This list
55 is shared between iterators as long as next() is only called once per iterator.
56 Subsequent calls to next() cause the head of the list to be copied.
57 */
58 class TrackLoopIter: public TrackIter
59 {
60 private:
61         typedef std::list<Track *> TrackList;
62
63         Msp::RefPtr<TrackList> _visited;
64         TrackList::iterator _last;
65         bool _looped;
66
67 public:
68         TrackLoopIter();
69         TrackLoopIter(Track *, unsigned);
70         TrackLoopIter(const TrackIter &);
71 private:
72         TrackLoopIter(const TrackIter &, Msp::RefPtr<TrackList>, const TrackList::iterator &);
73
74 public:
75         bool looped() const { return _looped; }
76
77         TrackLoopIter next() const;
78         TrackLoopIter next(unsigned) const;
79 };
80
81 } // namespace Marklin
82
83 #endif