From ad7c5568cd0969b13218167127f82eb3b40a7f2b Mon Sep 17 00:00:00 2001 From: Yuriy Skalko Date: Fri, 27 Nov 2020 20:10:52 +0200 Subject: [PATCH] Make InsetIterator compatible with range-based loops --- src/Buffer.cpp | 28 +++++++++++++--------------- src/BufferEncodings.cpp | 7 ++----- src/CutAndPaste.cpp | 18 +++++++++--------- src/InsetIterator.cpp | 4 ++-- src/InsetIterator.h | 4 ++-- src/insets/InsetGraphics.cpp | 27 +++++++++------------------ src/insets/InsetTabular.cpp | 9 ++++----- 7 files changed, 41 insertions(+), 56 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index f537ac047d..058fb44fd5 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1275,9 +1275,9 @@ void Buffer::updatePreviews() const if (!ploader) return; - InsetIterator it = inset_iterator_begin(*d->inset); - InsetIterator const end = inset_iterator_end(*d->inset); - for (; it != end; ++it) + InsetIterator it = begin(*d->inset); + InsetIterator const itend = end(*d->inset); + for (; it != itend; ++it) it->addPreview(it, *ploader); ploader->startLoading(); @@ -2581,7 +2581,7 @@ void Buffer::collectBibKeys(FileNameList & checkedFiles) const if (!parent()) clearIncludeList(); - for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { + for (InsetIterator it = begin(inset()); it; ++it) { it->collectBibKeys(it, checkedFiles); if (it->lyxCode() == BIBITEM_CODE) { if (parent() != nullptr) @@ -2959,10 +2959,10 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) docstring const oldname = from_utf8(func.getArg(0)); docstring const newname = from_utf8(func.getArg(1)); - InsetIterator it = inset_iterator_begin(inset()); - InsetIterator const end = inset_iterator_end(inset()); + InsetIterator it = begin(inset()); + InsetIterator const itend = end(inset()); bool success = false; - for (; it != end; ++it) { + for (; it != itend; ++it) { if (it->lyxCode() == BRANCH_CODE) { InsetBranch & ins = static_cast(*it); if (ins.branch() == oldname) { @@ -3841,11 +3841,9 @@ void Buffer::updateMacros() const void Buffer::getUsedBranches(std::list & result, bool const from_master) const { - InsetIterator it = inset_iterator_begin(inset()); - InsetIterator const end = inset_iterator_end(inset()); - for (; it != end; ++it) { - if (it->lyxCode() == BRANCH_CODE) { - InsetBranch & br = static_cast(*it); + for (Inset const & it : inset()) { + if (it.lyxCode() == BRANCH_CODE) { + InsetBranch const & br = static_cast(it); docstring const name = br.branch(); if (!from_master && !params().branchlist().find(name)) result.push_back(name); @@ -3853,10 +3851,10 @@ void Buffer::getUsedBranches(std::list & result, bool const from_mast result.push_back(name); continue; } - if (it->lyxCode() == INCLUDE_CODE) { + if (it.lyxCode() == INCLUDE_CODE) { // get buffer of external file InsetInclude const & ins = - static_cast(*it); + static_cast(it); Buffer * child = ins.loadIfNeeded(); if (!child) continue; @@ -4027,7 +4025,7 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to) string const paramName = "key"; UndoGroupHelper ugh(this); - InsetIterator it = inset_iterator_begin(inset()); + InsetIterator it = begin(inset()); for (; it; ++it) { if (it->lyxCode() != CITE_CODE) continue; diff --git a/src/BufferEncodings.cpp b/src/BufferEncodings.cpp index d4f08fca34..dcf422d54b 100644 --- a/src/BufferEncodings.cpp +++ b/src/BufferEncodings.cpp @@ -35,11 +35,8 @@ void BufferEncodings::initUnicodeMath(Buffer const & buffer, bool for_master) } // Check this buffer - Inset & inset = buffer.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - for (; it != end; ++it) - it->initUnicodeMath(); + for (Inset const & it : buffer.inset()) + it.initUnicodeMath(); if (!for_master) return; diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index a1c4de5b35..b6aec8f5a5 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -292,8 +292,8 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, // Prepare the paragraphs and insets for insertion. insertion.swap(in.paragraphs()); - InsetIterator const i_end = inset_iterator_end(in); - for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) { + InsetIterator const i_end = end(in); + for (InsetIterator it = begin(in); it != i_end; ++it) { // Even though this will also be done later, it has to be done here // since some inset might try to access the buffer() member. it->setBuffer(const_cast(buffer)); @@ -315,7 +315,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, if (oldname == newname) continue; // adapt the references - for (InsetIterator itt = inset_iterator_begin(in); + for (InsetIterator itt = begin(in); itt != i_end; ++itt) { if (itt->lyxCode() == REF_CODE) { InsetCommand * ref = itt->asInsetCommand(); @@ -346,7 +346,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, if (oldname == newname) break; // adapt the references - for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) { + for (InsetIterator itt = begin(in); itt != i_end; ++itt) { if (itt->lyxCode() == REF_CODE) { InsetCommand & ref = static_cast(*itt); if (ref.getParam("reference") == oldname) @@ -393,7 +393,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, if (oldkey == newkey) break; // adapt the references - for (InsetIterator itt = inset_iterator_begin(in); + for (InsetIterator itt = begin(in); itt != i_end; ++itt) { if (itt->lyxCode() == CITE_CODE) { InsetCommand * ref = itt->asInsetCommand(); @@ -806,10 +806,10 @@ void switchBetweenClasses(DocumentClassConstPtr oldone, // layouts ParIterator it = par_iterator_begin(in); - ParIterator end = par_iterator_end(in); + ParIterator pend = par_iterator_end(in); // for remembering which layouts we've had to add set newlayouts; - for (; it != end; ++it) { + for (; it != pend; ++it) { docstring const name = it->layout().name(); // the pasted text will keep their own layout name. If this layout does @@ -832,8 +832,8 @@ void switchBetweenClasses(DocumentClassConstPtr oldone, } // character styles and hidden table cells - InsetIterator const i_end = inset_iterator_end(in); - for (InsetIterator iit = inset_iterator_begin(in); iit != i_end; ++iit) { + InsetIterator const i_end = end(in); + for (InsetIterator iit = begin(in); iit != i_end; ++iit) { InsetCode const code = iit->lyxCode(); if (code == FLEX_CODE) { // FIXME: Should we verify all InsetCollapsible? diff --git a/src/InsetIterator.cpp b/src/InsetIterator.cpp index d9ed1e3737..7ac238bd1b 100644 --- a/src/InsetIterator.cpp +++ b/src/InsetIterator.cpp @@ -25,7 +25,7 @@ InsetIterator::InsetIterator(Inset & inset) } -InsetIterator inset_iterator_begin(Inset & inset) +InsetIterator begin(Inset & inset) { InsetIterator it = InsetIterator(inset); it.forwardInset(); @@ -33,7 +33,7 @@ InsetIterator inset_iterator_begin(Inset & inset) } -InsetIterator inset_iterator_end(Inset & inset) +InsetIterator end(Inset & inset) { return InsetIterator(inset); } diff --git a/src/InsetIterator.h b/src/InsetIterator.h index 0029865a76..96d16cb386 100644 --- a/src/InsetIterator.h +++ b/src/InsetIterator.h @@ -33,9 +33,9 @@ public: Inset & operator*() { return *nextInset(); } }; -InsetIterator inset_iterator_begin(Inset & inset); +InsetIterator begin(Inset & inset); -InsetIterator inset_iterator_end(Inset & inset); +InsetIterator end(Inset & inset); diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp index 0631bb4932..cc8937b497 100644 --- a/src/insets/InsetGraphics.cpp +++ b/src/insets/InsetGraphics.cpp @@ -1189,11 +1189,8 @@ namespace graphics { void getGraphicsGroups(Buffer const & b, set & ids) { - Inset & inset = b.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - for (; it != end; ++it) { - InsetGraphics const * ins = it->asInsetGraphics(); + for (Inset const & it : b.inset()) { + InsetGraphics const * ins = it.asInsetGraphics(); if (!ins) continue; InsetGraphicsParams const & inspar = ins->getParams(); @@ -1208,11 +1205,8 @@ int countGroupMembers(Buffer const & b, string const & groupId) int n = 0; if (groupId.empty()) return n; - Inset & inset = b.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - for (; it != end; ++it) { - InsetGraphics const * ins = it->asInsetGraphics(); + for (Inset const & it : b.inset()) { + InsetGraphics const * ins = it.asInsetGraphics(); if (!ins) continue; if (ins->getParams().groupId == groupId) @@ -1226,11 +1220,8 @@ string getGroupParams(Buffer const & b, string const & groupId) { if (groupId.empty()) return string(); - Inset & inset = b.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - for (; it != end; ++it) { - InsetGraphics const * ins = it->asInsetGraphics(); + for (Inset const & it : b.inset()) { + InsetGraphics const * ins = it.asInsetGraphics(); if (!ins) continue; InsetGraphicsParams const & inspar = ins->getParams(); @@ -1252,9 +1243,9 @@ void unifyGraphicsGroups(Buffer & b, string const & argument) // This handles undo groups automagically UndoGroupHelper ugh(&b); Inset & inset = b.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - for (; it != end; ++it) { + InsetIterator it = begin(inset); + InsetIterator const itend = end(inset); + for (; it != itend; ++it) { InsetGraphics * ins = it->asInsetGraphics(); if (!ins) continue; diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index 627a27ace1..21947f46a5 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -3132,12 +3132,11 @@ void Tabular::TeXRow(otexstream & os, row_type row, } else if (ltCaption(row)) { // Inside longtable caption rows, we must only output the caption inset // with its content and omit anything outside of that (see #10791) - InsetIterator it = inset_iterator_begin(*const_cast(inset)); - InsetIterator i_end = inset_iterator_end(*const_cast(inset)); - for (; it != i_end; ++it) { - if (it->lyxCode() != CAPTION_CODE) + InsetTableCell & tc_inset = *const_cast(inset); + for (Inset const & it : tc_inset) { + if (it.lyxCode() != CAPTION_CODE) continue; - it->latex(os, runparams); + it.latex(os, runparams); break; } } else if (!isPartOfMultiRow(row, c)) {