Andre Poenitz wrote:

> Okokok ;-)

What about this? It only adds 13 lines overall, buying us normal
const_iterator semantics for CursorSlices (Including automatic conversion
CursorSlice -> ConstCursorSlice). Something similar could be done for
DocIterator.

Another option would be to use templates like some stl container do, but I
don't know what would be the advantage in this case. 

A third option would be to eliminate const_iterator semantics completely...
What do people feel about this?

A/

Index: CursorSlice.h
===================================================================
--- CursorSlice.h	(revision 19719)
+++ CursorSlice.h	(working copy)
@@ -38,13 +38,8 @@
 // that of MathData and Text should vanish. They are conceptually the
 // same (now...)
 
-class CursorSlice {
+class ConstCursorSlice {
 public:
-	/// Those needs inset_ access.
-	///@{
-	friend class DocIterator;
-	friend class StableDocIterator;
-	///@}
 
 	/// type for cell number in inset
 	typedef size_t idx_type;
@@ -54,12 +49,12 @@
 	typedef size_t col_type;
 
 	///
-	CursorSlice();
+	ConstCursorSlice();
 	///
-	explicit CursorSlice(Inset &);
+	explicit ConstCursorSlice(Inset const &);
 
 	/// the current inset
-	Inset & inset() const { return *inset_; }
+	Inset const & inset() const { return *inset_; }
 	/// return the cell this cursor is in
 	idx_type idx() const { return idx_; }
 	/// return the cell this cursor is in
@@ -103,24 +98,20 @@
 	/// texted specific stuff
 	///
 	/// returns text corresponding to this position
-	Text * text() { return inset_->getText(idx_); }
-	/// returns text corresponding to this position
 	Text const * text() const { return inset_->getText(idx_); }
 	/// paragraph in this cell
-	Paragraph & paragraph();
-	/// paragraph in this cell
 	Paragraph const & paragraph() const;
 
 	///
 	/// mathed specific stuff
 	///
 	/// returns the owning inset if it is a InsetMath, else 0
-	InsetMath * asInsetMath() const { return inset_->asInsetMath(); }
+	InsetMath const * asInsetMath() const { return inset_->asInsetMath(); }
 	/// returns cell corresponding to this position
-	MathData & cell() const;
+	MathData const & cell() const;
 
 	/// write some debug information to \p os
-	friend std::ostream & operator<<(std::ostream &, CursorSlice const &);
+	friend std::ostream & operator<<(std::ostream &, ConstCursorSlice const &);
 	/// move to next position
 	void forwardPos();
 	/// move to previous position
@@ -130,11 +121,12 @@
 	/// are we at the start of this slice
 	bool at_begin() const;
 	
-private:
+protected:
 
 	/// pointer to 'owning' inset. This is some kind of cache.
 	Inset * inset_;
 
+private:
 	/*!
 	 * Cell index of a position in this inset.
 	 * This is the primary cell information also for grid like insets,
@@ -158,16 +150,43 @@
 	pos_type pos_;
 };
 
+
+
+class CursorSlice : public ConstCursorSlice 
+{
+
+public:
+	explicit CursorSlice(Inset & inset) : ConstCursorSlice(inset) {}
+
+	CursorSlice() : ConstCursorSlice() {}
+	/// Those needs inset_ access.
+	///@{
+	friend class DocIterator;
+	friend class StableDocIterator;
+	///@}
+	/// the current inset
+	Inset & inset() const { return const_cast<Inset &>(ConstCursorSlice::inset()); }
+	/// returns text corresponding to this position
+	Text * text() const { return const_cast<Text *>(ConstCursorSlice::text()); }
+	/// paragraph in this cell
+	Paragraph & paragraph() const { return const_cast <Paragraph &>(CursorSlice::paragraph()); }
+
+	/// returns the owning inset if it is a InsetMath, else 0
+	InsetMath * asInsetMath() const { return const_cast<InsetMath *>(CursorSlice::asInsetMath()); }
+	/// returns cell corresponding to this position
+	MathData & cell() const { return const_cast<MathData &>(CursorSlice::cell()); }
+};
+
 /// test for equality
-bool operator==(CursorSlice const &, CursorSlice const &);
+bool operator==(ConstCursorSlice const &, ConstCursorSlice const &);
 /// test for inequality
-bool operator!=(CursorSlice const &, CursorSlice const &);
+bool operator!=(ConstCursorSlice const &, ConstCursorSlice const &);
 /// test for order
-bool operator<(CursorSlice const &, CursorSlice const &);
+bool operator<(ConstCursorSlice const &, ConstCursorSlice const &);
 /// test for order
-bool operator>(CursorSlice const &, CursorSlice const &);
+bool operator>(ConstCursorSlice const &, ConstCursorSlice const &);
 /// test for order
-bool operator<=(CursorSlice const &, CursorSlice const &);
+bool operator<=(ConstCursorSlice const &, ConstCursorSlice const &);
 
 
 } // namespace lyx
Index: CursorSlice.cpp
===================================================================
--- CursorSlice.cpp	(revision 19719)
+++ CursorSlice.cpp	(working copy)
@@ -32,44 +32,38 @@
 namespace lyx {
 
 
-CursorSlice::CursorSlice()
+ConstCursorSlice::ConstCursorSlice()
 	: inset_(0), idx_(0), pit_(0), pos_(0)
 {}
 
 
-CursorSlice::CursorSlice(Inset & p)
-	: inset_(&p), idx_(0), pit_(0), pos_(0)
+ConstCursorSlice::ConstCursorSlice(Inset const & p)
+	: inset_(&const_cast<Inset &>(p)), idx_(0), pit_(0), pos_(0)
 {
 	BOOST_ASSERT(inset_);
 }
 
 
-MathData & CursorSlice::cell() const
+MathData const & ConstCursorSlice::cell() const
 {
 	return inset_->asInsetMath()->cell(idx_);
 }
 
 
-Paragraph & CursorSlice::paragraph()
+Paragraph const & ConstCursorSlice::paragraph() const
 {
 	return text()->getPar(pit_);
 }
 
 
-Paragraph const & CursorSlice::paragraph() const
+pos_type ConstCursorSlice::lastpos() const
 {
-	return text()->getPar(pit_);
-}
-
-
-pos_type CursorSlice::lastpos() const
-{
 	BOOST_ASSERT(inset_);
 	return inset_->asInsetMath() ? cell().size() : paragraph().size();
 }
 
 
-pit_type CursorSlice::lastpit() const
+pit_type ConstCursorSlice::lastpit() const
 {
 	if (inset().inMathed())
 		return 0;
@@ -77,21 +71,21 @@
 }
 
 
-CursorSlice::row_type CursorSlice::row() const
+ConstCursorSlice::row_type ConstCursorSlice::row() const
 {
 	BOOST_ASSERT(asInsetMath());
 	return asInsetMath()->row(idx_);
 }
 
 
-CursorSlice::col_type CursorSlice::col() const
+ConstCursorSlice::col_type ConstCursorSlice::col() const
 {
 	BOOST_ASSERT(asInsetMath());
 	return asInsetMath()->col(idx_);
 }
 
 
-void CursorSlice::forwardPos()
+void ConstCursorSlice::forwardPos()
 {
 	//  move on one position if possible
 	if (pos() < lastpos()) {
@@ -120,7 +114,7 @@
 }
 
 
-void CursorSlice::backwardPos()
+void ConstCursorSlice::backwardPos()
 {
 	if (pos() != 0) {
 		--pos();
@@ -144,19 +138,19 @@
 }
 
 
-bool CursorSlice::at_end() const 
+bool ConstCursorSlice::at_end() const 
 {
 	return idx() == lastidx() && pit() == lastpit() && pos() == lastpos();
 }
 
 
-bool CursorSlice::at_begin() const
+bool ConstCursorSlice::at_begin() const
 {
 	return idx() == 0 && pit() == 0 && pos() == 0;
 }
 
 
-bool operator==(CursorSlice const & p, CursorSlice const & q)
+bool operator==(ConstCursorSlice const & p, ConstCursorSlice const & q)
 {
 	return &p.inset() == &q.inset()
 	       && p.idx() == q.idx()
@@ -165,7 +159,7 @@
 }
 
 
-bool operator!=(CursorSlice const & p, CursorSlice const & q)
+bool operator!=(ConstCursorSlice const & p, ConstCursorSlice const & q)
 {
 	return &p.inset() != &q.inset()
 	       || p.idx() != q.idx()
@@ -174,7 +168,7 @@
 }
 
 
-bool operator<(CursorSlice const & p, CursorSlice const & q)
+bool operator<(ConstCursorSlice const & p, ConstCursorSlice const & q)
 {
 	if (&p.inset() != &q.inset()) {
 		lyxerr << "can't compare cursor and anchor in different insets\n"
@@ -189,19 +183,19 @@
 }
 
 
-bool operator>(CursorSlice const & p, CursorSlice const & q)
+bool operator>(ConstCursorSlice const & p, ConstCursorSlice const & q)
 {
 	return q < p;
 }
 
 
-bool operator<=(CursorSlice const & p, CursorSlice const & q)
+bool operator<=(ConstCursorSlice const & p, ConstCursorSlice const & q)
 {
 	return !(q < p);
 }
 
 
-std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
+std::ostream & operator<<(std::ostream & os, ConstCursorSlice const & item)
 {
 	return os
 	   << "inset: " << &item.inset()

Reply via email to