]> git.lyx.org Git - lyx.git/blob - src/dociterator.h
dociterator dont inherit from std::vector and updates
[lyx.git] / src / dociterator.h
1 // -*- C++ -*-
2 /**
3  * \file dociterator.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DOCITERATOR_H
13 #define DOCITERATOR_H
14
15 #include "cursor_slice.h"
16
17 #include <vector>
18 #include <iosfwd>
19
20 class LyXText;
21 class MathAtom;
22 class Paragraph;
23 class Row;
24
25
26
27 // only needed for gcc 2.95, remove when support terminated
28 template <typename A, typename B>
29 bool ptr_cmp(A const * a, B const * b)
30 {
31         return a == b;
32 }
33
34
35 // The public inheritance should go in favour of a suitable data member
36 // (or maybe private inheritance) at some point of time.
37 class DocIterator // : public std::vector<CursorSlice>
38 {
39 public:
40         /// type for cell number in inset
41         typedef CursorSlice::idx_type idx_type;
42         /// type for paragraph numbers positions within a cell
43         typedef CursorSlice::pit_type pit_type;
44         /// type for cursor positions within a cell
45         typedef CursorSlice::pos_type pos_type;
46         /// type for row indices
47         typedef CursorSlice::row_type row_type;
48         /// type for col indices
49         typedef CursorSlice::col_type col_type;
50
51 public:
52         ///
53         DocIterator();
54         ///
55         explicit DocIterator(InsetBase & inset);
56
57         CursorSlice const & operator[](size_t i) const {
58                 return slices_[i];
59         }
60
61         CursorSlice & operator[](size_t i) {
62                 return slices_[i];
63         }
64
65         // What is the point of this function?
66         void resize(size_t i) { slices_.resize(i); }
67
68         /// is the iterator valid?
69         operator const void*() const { return empty() ? 0 : this; }
70         /// is this iterator invalid?
71         bool operator!() const { return empty(); }
72
73         bool empty() const { return slices_.empty(); }
74
75         //
76         // access to slice at tip
77         //
78         /// access to tip
79         CursorSlice & top() { return slices_.back(); }
80         /// access to tip
81         CursorSlice const & top() const { return slices_.back(); }
82         /// access to outermost slice
83         CursorSlice & bottom() { return slices_.front(); }
84         /// access to  outermost slicetip
85         CursorSlice const & bottom() const { return slices_.front(); }
86         /// how many nested insets do we have?
87         size_t depth() const { return slices_.size(); }
88         /// the containing inset
89         InsetBase & inset() const { return top().inset(); }
90         /// return the cell of the inset this cursor is in
91         idx_type idx() const { return top().idx(); }
92         /// return the cell of the inset this cursor is in
93         idx_type & idx() { return top().idx(); }
94         /// return the last possible cell in this inset
95         idx_type lastidx() const;
96         /// return the paragraph this cursor is in
97         pit_type pit() const { return top().pit(); }
98         /// return the paragraph this cursor is in
99         pit_type & pit() { return top().pit(); }
100         /// return the last possible paragraph in this inset
101         pit_type lastpit() const;
102         /// return the position within the paragraph
103         pos_type pos() const { return top().pos(); }
104         /// return the position within the paragraph
105         pos_type & pos() { return top().pos(); }
106         /// return the last position within the paragraph
107         pos_type lastpos() const;
108
109         /// return the number of embedded cells
110         size_t nargs() const;
111         /// return the number of embedded cells
112         size_t ncols() const;
113         /// return the number of embedded cells
114         size_t nrows() const;
115         /// return the grid row of the top cell
116         row_type row() const;
117         /// return the last row of the top grid
118         row_type lastrow() const { return nrows() - 1; }
119         /// return the grid column of the top cell
120         col_type col() const;
121         /// return the last column of the top grid
122         col_type lastcol() const { return ncols() - 1; }
123         /// the inset just behind the cursor
124         InsetBase * nextInset();
125         /// the inset just in front of the cursor
126         InsetBase * prevInset();
127         /// the inset just in front of the cursor
128         InsetBase const * prevInset() const;
129
130         /// are we in mathed?
131         bool inMathed() const;
132         /// are we in texted?
133         bool inTexted() const;
134
135         //
136         // math-specific part
137         //
138         /// return the mathed cell this cursor is in
139         MathArray const & cell() const;
140         /// return the mathed cell this cursor is in
141         MathArray & cell();
142         /// the mathatom left of the cursor
143         MathAtom const & prevAtom() const;
144         /// the mathatom left of the cursor
145         MathAtom & prevAtom();
146         /// the mathatom right of the cursor
147         MathAtom const & nextAtom() const;
148         /// the mathatom right of the cursor
149         MathAtom & nextAtom();
150
151         //
152         // text-specific part
153         //
154         /// see comment for boundary_ below
155         bool boundary() const { return top().boundary(); }
156         /// see comment for boundary_ below
157         bool & boundary() { return top().boundary(); }
158         /// the paragraph we're in
159         Paragraph & paragraph();
160         /// the paragraph we're in
161         Paragraph const & paragraph() const;
162         /// the row in the paragraph we're in
163         Row & textRow();
164         /// the row in the paragraph we're in
165         Row const & textRow() const;
166         ///
167         LyXText * text();
168         ///
169         LyXText const * text() const;
170         ///
171         InsetBase * innerInsetOfType(int code) const;
172         ///
173         LyXText * innerText();
174         ///
175         LyXText const * innerText() const;
176
177         //
178         // elementary moving
179         //
180         /// move on one logical position
181         void forwardPos();
182         /// move on one physical character or inset
183         void forwardChar();
184         /// move on one paragraph
185         void forwardPar();
186         /// move on one cell
187         void forwardIdx();
188         /// move on one inset
189         void forwardInset();
190         /// move backward one logical position
191         void backwardPos();
192         /// move backward one physical character or inset
193         void backwardChar();
194         /// move backward one paragraph
195         void backwardPar();
196         /// move backward one cell
197         void backwardIdx();
198         /// move backward one inset
199         void backwardInset();
200
201         /// are we some 'extension' (i.e. deeper nested) of the given iterator
202         bool hasPart(DocIterator const & it) const;
203
204         /// output
205         friend std::ostream &
206         operator<<(std::ostream & os, DocIterator const & cur);
207         friend bool operator==(DocIterator const &, DocIterator const &);
208         friend class StableDocIterator;
209 protected:
210         void clear() { slices_.clear(); }
211         void push_back(CursorSlice const & sl) {
212                 slices_.push_back(sl);
213         }
214         void pop_back() {
215                 slices_.pop_back();
216         }
217 private:
218         std::vector<CursorSlice> const & internalData() const {
219                 return slices_;
220         }
221         std::vector<CursorSlice> slices_;
222         InsetBase * inset_;
223 };
224
225
226 DocIterator doc_iterator_begin(InsetBase & inset);
227 DocIterator doc_iterator_end(InsetBase & inset);
228
229
230 inline
231 bool operator==(DocIterator const & di1, DocIterator const & di2)
232 {
233         return di1.slices_ == di2.slices_;
234 }
235
236
237 inline
238 bool operator!=(DocIterator const & di1, DocIterator const & di2)
239 {
240         return !(di1 == di2);
241 }
242
243
244 // The difference to a ('non stable') DocIterator is the removed
245 // (overwritte by 0...) part of the CursorSlice data items. So this thing
246 // is suitable for external storage, but not for iteration as such.
247
248 class StableDocIterator {
249 public:
250         ///
251         StableDocIterator() {}
252         /// non-explicit intended
253         StableDocIterator(const DocIterator & it);
254         ///
255         DocIterator asDocIterator(InsetBase * start) const;
256         ///
257         size_t size() const { return data_.size(); }
258         ///
259         friend std::ostream &
260         operator<<(std::ostream & os, StableDocIterator const & cur);
261         ///
262         friend std::istream &
263         operator>>(std::istream & is, StableDocIterator & cur);
264 private:
265         std::vector<CursorSlice> data_;
266 };
267
268 bool operator==(StableDocIterator const &, StableDocIterator const &);
269
270 #endif