From: Lars Gullik Bjønnes Date: Tue, 27 May 2003 22:41:04 +0000 (+0000) Subject: parlist-23-a,diff X-Git-Tag: 1.6.10~16736 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=850ec3731821edbf1903178126a4293e169d862a;p=features.git parlist-23-a,diff git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7056 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/ParagraphList.C b/src/ParagraphList.C index 0c70bee13d..3e19224f9c 100644 --- a/src/ParagraphList.C +++ b/src/ParagraphList.C @@ -80,6 +80,80 @@ bool operator!=(ParagraphList::iterator const & i1, return !(i1 == i2); } +////////// The ParagraphList::const_iterator + +ParagraphList::const_iterator::const_iterator() + : ptr(0) +{} + + +ParagraphList::const_iterator::const_iterator(Paragraph * p) + : ptr(p) +{} + + +ParagraphList::const_iterator::const_reference +ParagraphList::const_iterator::operator*() +{ + return *ptr; +} + + +ParagraphList::const_iterator::const_pointer +ParagraphList::const_iterator::operator->() +{ + return ptr; +} + + +ParagraphList::const_iterator & +ParagraphList::const_iterator::operator++() +{ + ptr = ptr->next_par_; + return *this; +} + + +ParagraphList::const_iterator +ParagraphList::const_iterator::operator++(int) +{ + const_iterator tmp = *this; + ++*this; + return tmp; +} + + +ParagraphList::const_iterator & +ParagraphList::const_iterator::operator--() +{ + ptr = ptr->prev_par_; + return *this; +} + + +ParagraphList::const_iterator +ParagraphList::const_iterator::operator--(int) +{ + const_iterator tmp = *this; + --*this; + return tmp; +} + + +bool operator==(ParagraphList::const_iterator const & i1, + ParagraphList::const_iterator const & i2) +{ + return &(*const_cast(i1)) + == &(*const_cast(i2)); +} + + +bool operator!=(ParagraphList::const_iterator const & i1, + ParagraphList::const_iterator const & i2) +{ + return !(i1 == i2); +} + ////////// ////////// The ParagraphList proper ////////// @@ -93,8 +167,8 @@ ParagraphList::ParagraphList(ParagraphList const & pl) : parlist(0) { // Deep copy. - ParagraphList::iterator it = pl.begin(); - ParagraphList::iterator end = pl.end(); + ParagraphList::const_iterator it = pl.begin(); + ParagraphList::const_iterator end = pl.end(); for (; it != end; ++it) { push_back(*it); } @@ -231,9 +305,9 @@ ParagraphList::iterator ParagraphList::begin() } -ParagraphList::iterator ParagraphList::begin() const +ParagraphList::const_iterator ParagraphList::begin() const { - return iterator(parlist); + return const_iterator(parlist); } @@ -243,9 +317,9 @@ ParagraphList::iterator ParagraphList::end() } -ParagraphList::iterator ParagraphList::end() const +ParagraphList::const_iterator ParagraphList::end() const { - return iterator(); + return const_iterator(); } diff --git a/src/ParagraphList.h b/src/ParagraphList.h index 649106e757..55ee259fd8 100644 --- a/src/ParagraphList.h +++ b/src/ParagraphList.h @@ -58,6 +58,40 @@ public: Paragraph * ptr; }; /// + class const_iterator { + public: + friend class ParagraphList; + /// + typedef std::bidirectional_iterator_tag iterator_category; + /// + typedef Paragraph * value_type; + /// + typedef ptrdiff_t difference_type; + /// + typedef Paragraph const * const_pointer; + /// + typedef Paragraph const & const_reference; + /// + const_iterator(); + /// + const_reference operator*(); + /// + const_pointer operator->(); + /// + const_iterator & operator++(); + /// + const_iterator operator++(int); + /// + const_iterator & operator--(); + /// + const_iterator operator--(int); + private: + /// + const_iterator(value_type); + /// + Paragraph * ptr; + }; + /// ParagraphList(); /// ParagraphList(ParagraphList const &); @@ -80,11 +114,11 @@ public: /// iterator begin(); /// - iterator begin() const; + const_iterator begin() const; /// iterator end(); /// - iterator end() const; + const_iterator end() const; /// void push_back(Paragraph const &); /// @@ -117,6 +151,13 @@ bool operator==(ParagraphList::iterator const & i1, bool operator!=(ParagraphList::iterator const & i1, ParagraphList::iterator const & i2); +/// +bool operator==(ParagraphList::const_iterator const & i1, + ParagraphList::const_iterator const & i2); +/// +bool operator!=(ParagraphList::const_iterator const & i1, + ParagraphList::const_iterator const & i2); + #endif #endif diff --git a/src/buffer.C b/src/buffer.C index d1291f503b..0e0a47d04d 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -681,8 +681,8 @@ bool Buffer::writeFile(string const & fname) const // this will write out all the paragraphs // using recursive descent. - ParagraphList::iterator pit = paragraphs.begin(); - ParagraphList::iterator pend = paragraphs.end(); + ParagraphList::const_iterator pit = paragraphs.begin(); + ParagraphList::const_iterator pend = paragraphs.end(); for (; pit != pend; ++pit) pit->write(this, ofs, params, depth); @@ -2254,13 +2254,13 @@ ParIterator Buffer::par_iterator_end() ParConstIterator Buffer::par_iterator_begin() const { - return ParConstIterator(paragraphs.begin(), paragraphs); + return ParConstIterator(const_cast(paragraphs).begin(), paragraphs); } ParConstIterator Buffer::par_iterator_end() const { - return ParConstIterator(paragraphs.end(), paragraphs); + return ParConstIterator(const_cast(paragraphs).end(), paragraphs); } diff --git a/src/buffer.h b/src/buffer.h index 468178b0f8..455225f4c9 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -399,7 +399,7 @@ public: /// inset_iterator inset_const_iterator_begin() const { - return inset_iterator(paragraphs.begin(), paragraphs.end()); + return inset_iterator(const_cast(paragraphs).begin(), const_cast(paragraphs).end()); } /// diff --git a/src/insets/insetbibitem.C b/src/insets/insetbibitem.C index eb22763941..cc26ab979b 100644 --- a/src/insets/insetbibitem.C +++ b/src/insets/insetbibitem.C @@ -169,11 +169,11 @@ string const bibitemWidest(Buffer const * buffer) int w = 0; // Does look like a hack? It is! (but will change at 0.13) - InsetBibitem * bitem = 0; + InsetBibitem const * bitem = 0; LyXFont font; - ParagraphList::iterator it = buffer->paragraphs.begin(); - ParagraphList::iterator end = buffer->paragraphs.end(); + ParagraphList::const_iterator it = buffer->paragraphs.begin(); + ParagraphList::const_iterator end = buffer->paragraphs.end(); for (; it != end; ++it) { if (it->bibitem()) { int const wx = diff --git a/src/insets/insettext.C b/src/insets/insettext.C index 703563b036..0c50c6c0c1 100644 --- a/src/insets/insettext.C +++ b/src/insets/insettext.C @@ -79,8 +79,8 @@ using lyx::textclass_type; void InsetText::saveLyXTextState(LyXText * t) const { // check if my paragraphs are still valid - ParagraphList::iterator it = paragraphs.begin(); - ParagraphList::iterator end = paragraphs.end(); + ParagraphList::iterator it = const_cast(paragraphs).begin(); + ParagraphList::iterator end = const_cast(paragraphs).end(); for (; it != end; ++it) { if (it == t->cursor.par()) break; @@ -99,14 +99,14 @@ void InsetText::saveLyXTextState(LyXText * t) const sstate.selection = t->selection.set(); sstate.mark_set = t->selection.mark(); } else { - sstate.lpar = paragraphs.end(); + sstate.lpar = const_cast(paragraphs).end(); } } void InsetText::restoreLyXTextState(LyXText * t) const { - if (sstate.lpar == paragraphs.end()) + if (sstate.lpar == const_cast(paragraphs).end()) return; t->selection.set(true); @@ -286,8 +286,8 @@ void InsetText::write(Buffer const * buf, ostream & os) const void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const { - ParagraphList::iterator it = paragraphs.begin(); - ParagraphList::iterator end = paragraphs.end(); + ParagraphList::const_iterator it = paragraphs.begin(); + ParagraphList::const_iterator end = paragraphs.end(); Paragraph::depth_type dth = 0; for (; it != end; ++it) { it->write(buf, os, buf->params, dth); @@ -1504,9 +1504,9 @@ int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const { unsigned int lines = 0; - ParagraphList::iterator beg = paragraphs.begin(); - ParagraphList::iterator end = paragraphs.end(); - ParagraphList::iterator it = beg; + ParagraphList::const_iterator beg = paragraphs.begin(); + ParagraphList::const_iterator end = paragraphs.end(); + ParagraphList::const_iterator it = beg; for (; it != end; ++it) { string const tmp = buf->asciiParagraph(*it, linelen, it == beg); lines += lyx::count(tmp.begin(), tmp.end(), '\n'); @@ -1528,8 +1528,8 @@ int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const Paragraph::depth_type depth = 0; // paragraph depth - ParagraphList::iterator pit = paragraphs.begin(); - ParagraphList::iterator pend = paragraphs.end(); + ParagraphList::iterator pit = const_cast(paragraphs).begin(); + ParagraphList::iterator pend = const_cast(paragraphs).end(); for (; pit != pend; ++pit) { string sgmlparam; @@ -1865,8 +1865,8 @@ vector const InsetText::getLabelList() const { vector label_list; - ParagraphList::iterator pit = paragraphs.begin(); - ParagraphList::iterator pend = paragraphs.end(); + ParagraphList::const_iterator pit = paragraphs.begin(); + ParagraphList::const_iterator pend = paragraphs.end(); for (; pit != pend; ++pit) { InsetList::iterator beg = pit->insetlist.begin(); InsetList::iterator end = pit->insetlist.end(); @@ -1995,8 +1995,8 @@ void InsetText::setParagraphData(ParagraphList const & plist) // See if this can be simplified when std::list is in effect. paragraphs.clear(); - ParagraphList::iterator it = plist.begin(); - ParagraphList::iterator end = plist.end(); + ParagraphList::const_iterator it = plist.begin(); + ParagraphList::const_iterator end = plist.end(); for (; it != end; ++it) { paragraphs.push_back(*it); Paragraph & tmp = paragraphs.back(); @@ -2178,7 +2178,7 @@ LyXText * InsetText::getLyXText(BufferView const * lbv, if (recursive && the_locking_inset) return the_locking_inset->getLyXText(lbv, true); LyXText * lt = cached_text.get(); - lyx::Assert(lt && lt->rows().begin()->par() == paragraphs.begin()); + lyx::Assert(lt && lt->rows().begin()->par() == const_cast(paragraphs).begin()); return lt; } // Super UGLY! (Lgb) @@ -2204,7 +2204,7 @@ LyXText * InsetText::getLyXText(BufferView const * lbv, if (locked) { saveLyXTextState(it->second.text.get()); } else { - sstate.lpar = paragraphs.end(); + sstate.lpar = const_cast(paragraphs).end(); } } // @@ -2251,7 +2251,8 @@ void InsetText::deleteLyXText(BufferView * bv, bool recursive) const it->second.remove = true; if (recursive) { /// then remove all LyXText in text-insets - for_each(paragraphs.begin(), paragraphs.end(), + for_each(const_cast(paragraphs).begin(), + const_cast(paragraphs).end(), boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv)); } } @@ -2288,7 +2289,8 @@ void InsetText::resizeLyXText(BufferView * bv, bool force) const LyXText * t = it->second.text.get(); saveLyXTextState(t); - for_each(paragraphs.begin(), paragraphs.end(), + for_each(const_cast(paragraphs).begin(), + const_cast(paragraphs).end(), boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv)); t->init(bv, true); @@ -2327,7 +2329,8 @@ void InsetText::reinitLyXText() const saveLyXTextState(t); - for_each(paragraphs.begin(), paragraphs.end(), + for_each(const_cast(paragraphs).begin(), + const_cast(paragraphs).end(), boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv)); t->init(bv, true); @@ -2440,8 +2443,8 @@ Inset * InsetText::getInsetFromID(int id_arg) const if (id_arg == id()) return const_cast(this); - ParagraphList::iterator pit = paragraphs.begin(); - ParagraphList::iterator pend = paragraphs.end(); + ParagraphList::const_iterator pit = paragraphs.begin(); + ParagraphList::const_iterator pend = paragraphs.end(); for (; pit != pend; ++pit) { InsetList::iterator it = pit->insetlist.begin(); InsetList::iterator end = pit->insetlist.end(); @@ -2734,8 +2737,8 @@ void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist) void InsetText::addPreview(grfx::PreviewLoader & loader) const { - ParagraphList::iterator pit = paragraphs.begin(); - ParagraphList::iterator pend = paragraphs.end(); + ParagraphList::const_iterator pit = paragraphs.begin(); + ParagraphList::const_iterator pend = paragraphs.end(); for (; pit != pend; ++pit) { InsetList::iterator it = pit->insetlist.begin(); diff --git a/src/iterators.C b/src/iterators.C index 011e44062b..72d8627044 100644 --- a/src/iterators.C +++ b/src/iterators.C @@ -45,7 +45,7 @@ public: ParPosition::ParPosition(ParagraphList::iterator p, ParagraphList const & pl) : pit(p), plist(&pl) { - if (p != pl.end()) { + if (p != const_cast(pl).end()) { it.reset(p->insetlist.begin()); } } @@ -127,7 +127,7 @@ ParIterator & ParIterator::operator++() } // Try to go to the next paragarph - if (next(p.pit) != p.plist->end() + if (next(p.pit) != const_cast(p.plist)->end() || pimpl_->positions.size() == 1) { ++p.pit; p.index.reset(); @@ -239,7 +239,7 @@ ParConstIterator & ParConstIterator::operator++() } // Try to go to the next paragarph - if (next(p.pit) != p.plist->end() + if (next(p.pit) != const_cast(p.plist)->end() || pimpl_->positions.size() == 1) { ++p.pit; p.index.reset(); diff --git a/src/paragraph.C b/src/paragraph.C index 20ced3064f..a8ef2aa214 100644 --- a/src/paragraph.C +++ b/src/paragraph.C @@ -727,7 +727,7 @@ int Paragraph::getPositionOfInset(Inset const * inset) const } -InsetBibitem * Paragraph::bibitem() +InsetBibitem * Paragraph::bibitem() const { InsetList::iterator it = insetlist.begin(); if (it != insetlist.end() && it.getInset()->lyxCode() == Inset::BIBTEX_CODE) diff --git a/src/paragraph.h b/src/paragraph.h index 5abcaf4d78..feb3ba5fe7 100644 --- a/src/paragraph.h +++ b/src/paragraph.h @@ -45,6 +45,7 @@ public: // Remove this whan ParagraphList transition is over. (Lgb) friend class ParagraphList; friend class ParagraphList::iterator; + friend class ParagraphList::const_iterator; #endif /// enum META_KIND { @@ -145,7 +146,7 @@ public: char itemdepth; /// - InsetBibitem * bibitem(); // ale970302 + InsetBibitem * bibitem() const; // ale970302 /// initialise tracking for this par void trackChanges(Change::Type = Change::UNCHANGED); diff --git a/src/paragraph_funcs.C b/src/paragraph_funcs.C index 694c3a9a2b..3b0201e0c3 100644 --- a/src/paragraph_funcs.C +++ b/src/paragraph_funcs.C @@ -203,7 +203,7 @@ ParagraphList::iterator depthHook(ParagraphList::iterator pit, Paragraph::depth_type depth) { ParagraphList::iterator newpit = pit; - ParagraphList::iterator beg = plist.begin(); + ParagraphList::iterator beg = const_cast(plist).begin(); if (newpit != beg) --newpit; @@ -223,7 +223,7 @@ ParagraphList::iterator outerHook(ParagraphList::iterator pit, ParagraphList const & plist) { if (!pit->getDepth()) - return plist.end(); + return const_cast(plist).end(); return depthHook(pit, plist, Paragraph::depth_type(pit->getDepth() - 1)); } @@ -243,12 +243,12 @@ int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist) { ParagraphList::iterator pit = p; Paragraph::depth_type par_depth = p->getDepth(); - while (pit != plist.end()) { + while (pit != const_cast(plist).end()) { LyXLayout_ptr const & layout = pit->layout(); int const endlabeltype = layout->endlabeltype; if (endlabeltype != END_LABEL_NO_LABEL) { - if (boost::next(p) == plist.end()) + if (boost::next(p) == const_cast(plist).end()) return endlabeltype; Paragraph::depth_type const next_depth = boost::next(p)->getDepth(); @@ -261,7 +261,7 @@ int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist) if (par_depth == 0) break; pit = outerHook(pit, plist); - if (pit != plist.end()) + if (pit != const_cast(plist).end()) par_depth = pit->getDepth(); } return END_LABEL_NO_LABEL; @@ -296,7 +296,7 @@ TeXDeeper(Buffer const * buf, lyxerr[Debug::LATEX] << "TeXDeeper... " << &*pit << endl; ParagraphList::iterator par = pit; - while (par != paragraphs.end() && + while (par != const_cast(paragraphs).end() && par->params().depth() == pit->params().depth()) { if (par->layout()->isEnvironment()) { par = TeXEnvironment(buf, paragraphs, par, @@ -328,7 +328,7 @@ TeXEnvironment(Buffer const * buf, Language const * language = pit->getParLanguage(bparams); Language const * doc_language = bparams.language; Language const * previous_language = - (pit != paragraphs.begin()) + (pit != const_cast(paragraphs).begin()) ? boost::prior(pit)->getParLanguage(bparams) : doc_language; if (language->babel() != previous_language->babel()) { @@ -379,7 +379,7 @@ TeXEnvironment(Buffer const * buf, do { par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams); - if (par != paragraphs.end()&& par->params().depth() > pit->params().depth()) { + if (par != const_cast(paragraphs).end()&& par->params().depth() > pit->params().depth()) { if (par->layout()->isParagraph()) { // Thinko! @@ -402,7 +402,7 @@ TeXEnvironment(Buffer const * buf, par = TeXDeeper(buf, paragraphs, par, os, texrow, runparams); } - } while (par != paragraphs.end() + } while (par != const_cast(paragraphs).end() && par->layout() == pit->layout() && par->params().depth() == pit->params().depth() && par->params().leftIndent() == pit->params().leftIndent()); @@ -466,7 +466,7 @@ TeXOnePar(Buffer const * buf, } if (!pit->params().spacing().isDefault() - && (pit == paragraphs.begin() || !boost::prior(pit)->hasSameLayout(*pit))) { + && (pit == const_cast(paragraphs).begin() || !boost::prior(pit)->hasSameLayout(*pit))) { os << pit->params().spacing().writeEnvirBegin() << '\n'; texrow.newline(); } @@ -504,14 +504,14 @@ TeXOnePar(Buffer const * buf, Language const * language = pit->getParLanguage(bparams); Language const * doc_language = bparams.language; Language const * previous_language = - (pit != paragraphs.begin()) + (pit != const_cast(paragraphs).begin()) ? boost::prior(pit)->getParLanguage(bparams) : doc_language; if (language->babel() != previous_language->babel() // check if we already put language command in TeXEnvironment() && !(style->isEnvironment() - && (pit == paragraphs.begin() || + && (pit == const_cast(paragraphs).begin() || (boost::prior(pit)->layout() != pit->layout() && boost::prior(pit)->getDepth() <= pit->getDepth()) || boost::prior(pit)->getDepth() < pit->getDepth()))) @@ -591,7 +591,7 @@ TeXOnePar(Buffer const * buf, bool is_command = style->isCommand(); if (style->resfont.size() != font.size() - && boost::next(pit) != paragraphs.end() + && boost::next(pit) != const_cast(paragraphs).end() && !is_command) { if (!need_par) os << '{'; @@ -604,7 +604,7 @@ TeXOnePar(Buffer const * buf, switch (style->latextype) { case LATEX_ITEM_ENVIRONMENT: case LATEX_LIST_ENVIRONMENT: - if (boost::next(pit) != paragraphs.end() + if (boost::next(pit) != const_cast(paragraphs).end() && (pit->params().depth() < boost::next(pit)->params().depth())) { os << '\n'; texrow.newline(); @@ -613,14 +613,14 @@ TeXOnePar(Buffer const * buf, case LATEX_ENVIRONMENT: // if its the last paragraph of the current environment // skip it otherwise fall through - if (boost::next(pit) != paragraphs.end() + if (boost::next(pit) != const_cast(paragraphs).end() && (boost::next(pit)->layout() != pit->layout() || boost::next(pit)->params().depth() != pit->params().depth())) break; // fall through possible default: // we don't need it for the last paragraph!!! - if (boost::next(pit) != paragraphs.end()) { + if (boost::next(pit) != const_cast(paragraphs).end()) { os << '\n'; texrow.newline(); } @@ -649,14 +649,14 @@ TeXOnePar(Buffer const * buf, } if (!pit->params().spacing().isDefault() - && (boost::next(pit) == paragraphs.end()|| !boost::next(pit)->hasSameLayout(*pit))) { + && (boost::next(pit) == const_cast(paragraphs).end()|| !boost::next(pit)->hasSameLayout(*pit))) { os << pit->params().spacing().writeEnvirEnd() << '\n'; texrow.newline(); } } // we don't need it for the last paragraph!!! - if (boost::next(pit) != paragraphs.end()) { + if (boost::next(pit) != const_cast(paragraphs).end()) { os << '\n'; texrow.newline(); } else { @@ -699,8 +699,8 @@ void latexParagraphs(Buffer const * buf, bool was_title = false; bool already_title = false; LyXTextClass const & tclass = buf->params.getLyXTextClass(); - ParagraphList::iterator par = paragraphs.begin(); - ParagraphList::iterator endpar = paragraphs.end(); + ParagraphList::iterator par = const_cast(paragraphs).begin(); + ParagraphList::iterator endpar = const_cast(paragraphs).end(); // if only_body while (par != endpar) { @@ -1057,10 +1057,10 @@ LyXFont const outerFont(ParagraphList::iterator pit, LyXFont tmpfont(LyXFont::ALL_INHERIT); // Resolve against environment font information - while (pit != plist.end() && + while (pit != const_cast(plist).end() && par_depth && !tmpfont.resolved()) { pit = outerHook(pit, plist); - if (pit != plist.end()) { + if (pit != const_cast(plist).end()) { tmpfont.realize(pit->layout()->font); par_depth = pit->getDepth(); } diff --git a/src/tabular.C b/src/tabular.C index 295aaa3417..a5e498a2f6 100644 --- a/src/tabular.C +++ b/src/tabular.C @@ -2590,8 +2590,8 @@ vector const LyXTabular::getLabelList() const LyXTabular::BoxType LyXTabular::UseParbox(int cell) const { ParagraphList const & parlist = GetCellInset(cell)->paragraphs; - ParagraphList::iterator cit = parlist.begin(); - ParagraphList::iterator end = parlist.end(); + ParagraphList::const_iterator cit = parlist.begin(); + ParagraphList::const_iterator end = parlist.end(); for (; cit != end; ++cit) { for (int i = 0; i < cit->size(); ++i) {