]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/cursor.hh
2e205ba33063c85b2a0cfd7b224594a1ce83b764
[lilypond.git] / flower / include / cursor.hh
1 // cursor.hh
2
3 #ifndef __CURSOR_HH
4 #define __CURSOR_HH
5
6 #include "link.hh"
7 template<class T> class List;
8
9 /**  iterator to List.  
10   add and insert extend the list
11   items are always stored as copies in List, but:
12   List<String> :  copies of String stored 
13   List<String*> : copies of String* stored!
14
15     the operations add and insert actually delegate the work to List class.
16  */
17 template<class T>
18 class Cursor 
19 {
20  public:
21     /** create cursor, set at top. The const part isn't true, actually, #list#
22       surely isn't const, but I get tired of the warning messages.  */
23     Cursor (const List<T>& list, Link<T>* pointer = 0);
24     /**
25       Create an invalid cursor (pointing to nothing, associated with   no list.)
26      */
27     Cursor();
28     Cursor (const Cursor<T>& cursor);
29
30     T& thing();
31
32     /// return current T
33     T& operator *() { return thing(); }
34     operator T() { return thing(); }
35     Cursor<T> operator =( const Cursor<T>& c);
36
37     /// make cursor with #no# items back
38     Cursor<T> operator -( int no) const;
39
40     /// make cursor with #no# items further
41     Cursor<T> operator +( int no) const;
42     int operator -(Cursor<T> op) const;
43     Cursor<T> operator -=(int);
44     Cursor<T> operator +=(int);
45     /// move one down
46     void next();
47     /// move one up.
48     void previous();
49     /// return current and move one down
50     Cursor<T> operator ++( int);
51     
52     /// return current and move one up
53     Cursor<T> operator --( int); 
54
55     /// point to link?
56     bool ok()const;
57
58     /// ++ items left?
59     bool forward()const;                
60
61     /// -- items left?
62     bool backward()const;
63
64     /**  put (copy) after me in List. 
65       analogously to editor. ok() interpreted as at end
66       of line.
67
68       PRE: !ok, POST: added to bottom()
69
70       PRE: ok, POST: added after me
71
72       cursor points to same object, cursor.next() is newly added
73       object.
74       */
75     void add (T const & thing);
76
77     /**  put (copy) before me in List. 
78       analogously to editor. ok() interpreted as at begin of
79       line.
80       
81       PRE: !ok, POST: add to top()
82
83       PRE: ok, POST: add before me
84
85       cursor points to same object, cursor.previous()
86       is newly inserted object.
87       */
88     
89     void insert (T const & thing);
90     ///
91     void backspace();
92
93     /// 
94     void del();
95     
96     /// access the list this came from
97     List<T>& list() const ;
98     Link<T>* pointer();
99     static   int compare (Cursor<T> a,Cursor<T>b) { return a-b; }
100 private:
101     List<T>& list_;
102     Link<T>* pointer_;
103 };
104
105
106 /*
107   comparisons.
108   */
109 #include "compare.hh"
110
111
112 TEMPLATE_INSTANTIATE_COMPARE(Cursor<T>, Cursor<T>::compare, template<class T>);
113
114 #include "pcursor.hh"
115 #include "list.hh"
116 #include "cursor.icc"
117 #include "iterate.hh"
118
119 #endif // CURSOR_HH