Convert for loops to range-based iterators i Buffer.cpp.

No intended change of behavior. The ones that use DocIterators and
ParIterators cannot yet be changed, but they could be if we provided
the right sorts of methods, perhaps.
This commit is contained in:
Richard Heck 2017-11-04 22:15:01 -04:00
parent a0f6690114
commit 4ec80c1865

View File

@ -504,10 +504,8 @@ Buffer::~Buffer()
// ourselves as a child. // ourselves as a child.
d->clone_list_->erase(this); d->clone_list_->erase(this);
// loop over children // loop over children
Impl::BufferPositionMap::iterator it = d->children_positions.begin(); for (auto const & p : d->children_positions) {
Impl::BufferPositionMap::iterator end = d->children_positions.end(); Buffer * child = const_cast<Buffer *>(p.first);
for (; it != end; ++it) {
Buffer * child = const_cast<Buffer *>(it->first);
if (d->clone_list_->erase(child)) if (d->clone_list_->erase(child))
delete child; delete child;
} }
@ -533,10 +531,8 @@ Buffer::~Buffer()
d->position_to_children.clear(); d->position_to_children.clear();
} else { } else {
// loop over children // loop over children
Impl::BufferPositionMap::iterator it = d->children_positions.begin(); for (auto const & p : d->children_positions) {
Impl::BufferPositionMap::iterator end = d->children_positions.end(); Buffer * child = const_cast<Buffer *>(p.first);
for (; it != end; ++it) {
Buffer * child = const_cast<Buffer *>(it->first);
if (theBufferList().isLoaded(child)) { if (theBufferList().isLoaded(child)) {
if (theBufferList().isOthersChild(this, child)) if (theBufferList().isOthersChild(this, child))
child->setParent(0); child->setParent(0);
@ -614,12 +610,10 @@ void Buffer::cloneWithChildren(BufferMap & bufmap, CloneList * clones) const
// math macro caches need to be rethought and simplified. // math macro caches need to be rethought and simplified.
// I am not sure wether we should handle Buffer cloning here or in BufferList. // I am not sure wether we should handle Buffer cloning here or in BufferList.
// Right now BufferList knows nothing about buffer clones. // Right now BufferList knows nothing about buffer clones.
Impl::PositionScopeBufferMap::iterator it = d->position_to_children.begin(); for (auto const & p : d->position_to_children) {
Impl::PositionScopeBufferMap::iterator end = d->position_to_children.end(); DocIterator dit = p.first.clone(buffer_clone);
for (; it != end; ++it) {
DocIterator dit = it->first.clone(buffer_clone);
dit.setBuffer(buffer_clone); dit.setBuffer(buffer_clone);
Buffer * child = const_cast<Buffer *>(it->second.buffer); Buffer * child = const_cast<Buffer *>(p.second.buffer);
child->cloneWithChildren(bufmap, clones); child->cloneWithChildren(bufmap, clones);
BufferMap::iterator const bit = bufmap.find(child); BufferMap::iterator const bit = bufmap.find(child);
@ -712,10 +706,8 @@ BufferParams const & Buffer::masterParams() const
BufferParams & mparams = const_cast<Buffer *>(masterBuffer())->params(); BufferParams & mparams = const_cast<Buffer *>(masterBuffer())->params();
// Copy child authors to the params. We need those pointers. // Copy child authors to the params. We need those pointers.
AuthorList const & child_authors = params().authors(); for (Author const & a : params().authors())
AuthorList::Authors::const_iterator it = child_authors.begin(); mparams.authors().record(a);
for (; it != child_authors.end(); ++it)
mparams.authors().record(*it);
return mparams; return mparams;
} }
@ -1071,10 +1063,8 @@ bool Buffer::readDocument(Lexer & lex)
// inform parent buffer about local macros // inform parent buffer about local macros
if (parent()) { if (parent()) {
Buffer const * pbuf = parent(); Buffer const * pbuf = parent();
UserMacroSet::const_iterator cit = usermacros.begin(); for (auto const & m : usermacros)
UserMacroSet::const_iterator end = usermacros.end(); pbuf->usermacros.insert(m);
for (; cit != end; ++cit)
pbuf->usermacros.insert(*cit);
} }
usermacros.clear(); usermacros.clear();
updateMacros(); updateMacros();
@ -1631,10 +1621,8 @@ bool Buffer::write(ostream & ofs) const
/// For each author, set 'used' to true if there is a change /// For each author, set 'used' to true if there is a change
/// by this author in the document; otherwise set it to 'false'. /// by this author in the document; otherwise set it to 'false'.
AuthorList::Authors::const_iterator a_it = params().authors().begin(); for (Author const & a : params().authors())
AuthorList::Authors::const_iterator a_end = params().authors().end(); a.setUsed(false);
for (; a_it != a_end; ++a_it)
a_it->setUsed(false);
ParIterator const end = const_cast<Buffer *>(this)->par_iterator_end(); ParIterator const end = const_cast<Buffer *>(this)->par_iterator_end();
ParIterator it = const_cast<Buffer *>(this)->par_iterator_begin(); ParIterator it = const_cast<Buffer *>(this)->par_iterator_begin();
@ -1917,10 +1905,10 @@ void Buffer::writeLaTeXSource(otexstream & os,
vector<string> pll = features.getPolyglossiaExclusiveLanguages(); vector<string> pll = features.getPolyglossiaExclusiveLanguages();
if (!bll.empty()) { if (!bll.empty()) {
docstring langs; docstring langs;
for (vector<string>::const_iterator it = bll.begin(); it != bll.end(); ++it) { for (string const & sit : bll) {
if (!langs.empty()) if (!langs.empty())
langs += ", "; langs += ", ";
langs += _(*it); langs += _(sit);
} }
blangs = bll.size() > 1 ? blangs = bll.size() > 1 ?
bformat(_("The languages %1$s are only supported by Babel."), langs) bformat(_("The languages %1$s are only supported by Babel."), langs)
@ -1928,10 +1916,10 @@ void Buffer::writeLaTeXSource(otexstream & os,
} }
if (!pll.empty()) { if (!pll.empty()) {
docstring langs; docstring langs;
for (vector<string>::const_iterator it = pll.begin(); it != pll.end(); ++it) { for (string const & pit : pll) {
if (!langs.empty()) if (!langs.empty())
langs += ", "; langs += ", ";
langs += _(*it); langs += _(pit);
} }
plangs = pll.size() > 1 ? plangs = pll.size() > 1 ?
bformat(_("The languages %1$s are only supported by Polyglossia."), langs) bformat(_("The languages %1$s are only supported by Polyglossia."), langs)
@ -1971,10 +1959,8 @@ void Buffer::writeLaTeXSource(otexstream & os,
os.lastChar('\n'); os.lastChar('\n');
// output the parent macros // output the parent macros
MacroSet::iterator it = parentMacros.begin(); for (auto const & mac : parentMacros) {
MacroSet::iterator end = parentMacros.end(); int num_lines = mac->write(os.os(), true);
for (; it != end; ++it) {
int num_lines = (*it)->write(os.os(), true);
os.texrow().newlines(num_lines); os.texrow().newlines(num_lines);
} }
@ -2313,11 +2299,9 @@ void Buffer::getLabelList(vector<docstring> & list) const
list.clear(); list.clear();
shared_ptr<Toc> toc = d->toc_backend.toc("label"); shared_ptr<Toc> toc = d->toc_backend.toc("label");
Toc::const_iterator toc_it = toc->begin(); for (auto const & tocit : *toc) {
Toc::const_iterator end = toc->end(); if (tocit.depth() == 0)
for (; toc_it != end; ++toc_it) { list.push_back(tocit.str());
if (toc_it->depth() == 0)
list.push_back(toc_it->str());
} }
} }
@ -2784,9 +2768,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
vector<docstring> const branches = vector<docstring> const branches =
getVectorFromString(branch_name, branch_list.separator()); getVectorFromString(branch_name, branch_list.separator());
docstring msg; docstring msg;
for (vector<docstring>::const_iterator it = branches.begin(); for (docstring const & branch_name : branches) {
it != branches.end(); ++it) {
branch_name = *it;
Branch * branch = branch_list.find(branch_name); Branch * branch = branch_list.find(branch_name);
if (branch) { if (branch) {
LYXERR0("Branch " << branch_name << " already exists."); LYXERR0("Branch " << branch_name << " already exists.");
@ -2946,10 +2928,8 @@ void Buffer::getLanguages(std::set<Language const *> & languages) const
it->getLanguages(languages); it->getLanguages(languages);
// also children // also children
ListOfBuffers clist = getDescendents(); ListOfBuffers clist = getDescendents();
ListOfBuffers::const_iterator cit = clist.begin(); for (auto const & cit : clist)
ListOfBuffers::const_iterator const cen = clist.end(); cit->getLanguages(languages);
for (; cit != cen; ++cit)
(*cit)->getLanguages(languages);
} }
@ -3086,8 +3066,8 @@ void Buffer::markDirty()
DepClean::iterator it = d->dep_clean.begin(); DepClean::iterator it = d->dep_clean.begin();
DepClean::const_iterator const end = d->dep_clean.end(); DepClean::const_iterator const end = d->dep_clean.end();
for (; it != end; ++it) for (auto & depit : d->dep_clean)
it->second = false; depit.second = false;
} }
@ -3169,10 +3149,8 @@ vector<docstring> const Buffer::prepareBibFilePaths(OutputParams const & runpara
// check for spaces in paths // check for spaces in paths
bool found_space = false; bool found_space = false;
FileNamePairList::const_iterator it = bibfilelist.begin(); for (auto const & bit : bibfilelist) {
FileNamePairList::const_iterator en = bibfilelist.end(); string utf8input = to_utf8(bit.first);
for (; it != en; ++it) {
string utf8input = to_utf8(it->first);
string database = string database =
prepareFileNameForLaTeX(utf8input, ".bib", runparams.nice); prepareFileNameForLaTeX(utf8input, ".bib", runparams.nice);
FileName const try_in_file = FileName const try_in_file =
@ -3334,10 +3312,8 @@ bool Buffer::hasChildren() const
void Buffer::collectChildren(ListOfBuffers & clist, bool grand_children) const void Buffer::collectChildren(ListOfBuffers & clist, bool grand_children) const
{ {
// loop over children // loop over children
Impl::BufferPositionMap::iterator it = d->children_positions.begin(); for (auto const & p : d->children_positions) {
Impl::BufferPositionMap::iterator end = d->children_positions.end(); Buffer * child = const_cast<Buffer *>(p.first);
for (; it != end; ++it) {
Buffer * child = const_cast<Buffer *>(it->first);
// No duplicates // No duplicates
ListOfBuffers::const_iterator bit = find(clist.begin(), clist.end(), child); ListOfBuffers::const_iterator bit = find(clist.begin(), clist.end(), child);
if (bit != clist.end()) if (bit != clist.end())
@ -3534,17 +3510,15 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
while (it.pit() <= lastpit) { while (it.pit() <= lastpit) {
Paragraph & par = it.paragraph(); Paragraph & par = it.paragraph();
// FIXME Can this be done with the new-style iterators?
// iterate over the insets of the current paragraph // iterate over the insets of the current paragraph
InsetList const & insets = par.insetList(); for (auto const & insit : par.insetList()) {
InsetList::const_iterator iit = insets.begin(); it.pos() = insit.pos;
InsetList::const_iterator end = insets.end();
for (; iit != end; ++iit) {
it.pos() = iit->pos;
// is it a nested text inset? // is it a nested text inset?
if (iit->inset->asInsetText()) { if (insit.inset->asInsetText()) {
// Inset needs its own scope? // Inset needs its own scope?
InsetText const * itext = iit->inset->asInsetText(); InsetText const * itext = insit.inset->asInsetText();
bool newScope = itext->isMacroScope(); bool newScope = itext->isMacroScope();
// scope which ends just behind the inset // scope which ends just behind the inset
@ -3552,14 +3526,14 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
++insetScope.pos(); ++insetScope.pos();
// collect macros in inset // collect macros in inset
it.push_back(CursorSlice(*iit->inset)); it.push_back(CursorSlice(*insit.inset));
updateMacros(it, newScope ? insetScope : scope); updateMacros(it, newScope ? insetScope : scope);
it.pop_back(); it.pop_back();
continue; continue;
} }
if (iit->inset->asInsetTabular()) { if (insit.inset->asInsetTabular()) {
CursorSlice slice(*iit->inset); CursorSlice slice(*insit.inset);
size_t const numcells = slice.nargs(); size_t const numcells = slice.nargs();
for (; slice.idx() < numcells; slice.forwardIdx()) { for (; slice.idx() < numcells; slice.forwardIdx()) {
it.push_back(slice); it.push_back(slice);
@ -3570,10 +3544,10 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
} }
// is it an external file? // is it an external file?
if (iit->inset->lyxCode() == INCLUDE_CODE) { if (insit.inset->lyxCode() == INCLUDE_CODE) {
// get buffer of external file // get buffer of external file
InsetInclude const & inset = InsetInclude const & inset =
static_cast<InsetInclude const &>(*iit->inset); static_cast<InsetInclude const &>(*insit.inset);
macro_lock = true; macro_lock = true;
Buffer * child = inset.getChildBuffer(); Buffer * child = inset.getChildBuffer();
macro_lock = false; macro_lock = false;
@ -3591,19 +3565,19 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
continue; continue;
} }
InsetMath * im = iit->inset->asInsetMath(); InsetMath * im = insit.inset->asInsetMath();
if (doing_export && im) { if (doing_export && im) {
InsetMathHull * hull = im->asHullInset(); InsetMathHull * hull = im->asHullInset();
if (hull) if (hull)
hull->recordLocation(it); hull->recordLocation(it);
} }
if (iit->inset->lyxCode() != MATHMACRO_CODE) if (insit.inset->lyxCode() != MATHMACRO_CODE)
continue; continue;
// get macro data // get macro data
InsetMathMacroTemplate & macroTemplate = InsetMathMacroTemplate & macroTemplate =
*iit->inset->asInsetMath()->asMacroTemplate(); *insit.inset->asInsetMath()->asMacroTemplate();
MacroContext mc(owner_, it); MacroContext mc(owner_, it);
macroTemplate.updateToContext(mc); macroTemplate.updateToContext(mc);
@ -3713,16 +3687,12 @@ void Buffer::listMacroNames(MacroNameSet & macros) const
d->macro_lock = true; d->macro_lock = true;
// loop over macro names // loop over macro names
Impl::NamePositionScopeMacroMap::iterator nameIt = d->macros.begin(); for (auto const & nameit : d->macros)
Impl::NamePositionScopeMacroMap::iterator nameEnd = d->macros.end(); macros.insert(nameit.first);
for (; nameIt != nameEnd; ++nameIt)
macros.insert(nameIt->first);
// loop over children // loop over children
Impl::BufferPositionMap::iterator it = d->children_positions.begin(); for (auto const & p : d->children_positions) {
Impl::BufferPositionMap::iterator end = d->children_positions.end(); Buffer * child = const_cast<Buffer *>(p.first);
for (; it != end; ++it) {
Buffer * child = const_cast<Buffer *>(it->first);
// The buffer might have been closed (see #10766). // The buffer might have been closed (see #10766).
if (theBufferList().isLoaded(child)) if (theBufferList().isLoaded(child))
child->listMacroNames(macros); child->listMacroNames(macros);
@ -3747,12 +3717,9 @@ void Buffer::listParentMacros(MacroSet & macros, LaTeXFeatures & features) const
pbuf->listMacroNames(names); pbuf->listMacroNames(names);
// resolve macros // resolve macros
MacroNameSet::iterator it = names.begin(); for (auto const & mit : names) {
MacroNameSet::iterator end = names.end();
for (; it != end; ++it) {
// defined? // defined?
MacroData const * data = MacroData const * data = pbuf->getMacro(mit, *this, false);
pbuf->getMacro(*it, *this, false);
if (data) { if (data) {
macros.insert(data); macros.insert(data);
@ -3821,20 +3788,16 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to)
reloadBibInfoCache(); reloadBibInfoCache();
// Check if the label 'from' appears more than once // Check if the label 'from' appears more than once
BiblioInfo const & keys = masterBibInfo();
BiblioInfo::const_iterator bit = keys.begin();
BiblioInfo::const_iterator bend = keys.end();
vector<docstring> labels; vector<docstring> labels;
for (auto const & bibit : masterBibInfo())
for (; bit != bend; ++bit) labels.push_back(bibit.first);
// FIXME UNICODE
labels.push_back(bit->first);
if (count(labels.begin(), labels.end(), from) > 1) if (count(labels.begin(), labels.end(), from) > 1)
return; return;
string const paramName = "key"; string const paramName = "key";
for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { InsetIterator it = inset_iterator_begin(inset());
for (; it; ++it) {
if (it->lyxCode() != CITE_CODE) if (it->lyxCode() != CITE_CODE)
continue; continue;
InsetCommand * inset = it->asInsetCommand(); InsetCommand * inset = it->asInsetCommand();
@ -4185,10 +4148,8 @@ void Buffer::setExportStatus(bool e) const
{ {
d->doing_export = e; d->doing_export = e;
ListOfBuffers clist = getDescendents(); ListOfBuffers clist = getDescendents();
ListOfBuffers::const_iterator cit = clist.begin(); for (auto const & bit : clist)
ListOfBuffers::const_iterator const cen = clist.end(); bit->d->doing_export = e;
for (; cit != cen; ++cit)
(*cit)->d->doing_export = e;
} }
@ -4272,11 +4233,10 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
// Get shortest path to format // Get shortest path to format
converters.buildGraph(); converters.buildGraph();
Graph::EdgePath path; Graph::EdgePath path;
for (vector<string>::const_iterator it = backs.begin(); for (string const & sit : backs) {
it != backs.end(); ++it) { Graph::EdgePath p = converters.getPath(sit, format);
Graph::EdgePath p = converters.getPath(*it, format);
if (!p.empty() && (path.empty() || p.size() < path.size())) { if (!p.empty() && (path.empty() || p.size() < path.size())) {
backend_format = *it; backend_format = sit;
path = p; path = p;
} }
} }
@ -4291,10 +4251,8 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
return ExportNoPathToFormat; return ExportNoPathToFormat;
} }
runparams.flavor = converters.getFlavor(path, this); runparams.flavor = converters.getFlavor(path, this);
Graph::EdgePath::const_iterator it = path.begin(); for (auto const & edge : path)
Graph::EdgePath::const_iterator en = path.end(); if (theConverters().get(edge).nice()) {
for (; it != en; ++it)
if (theConverters().get(*it).nice()) {
need_nice_file = true; need_nice_file = true;
break; break;
} }
@ -4380,20 +4338,18 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
errors(error_type); errors(error_type);
// also to the children, in case of master-buffer-view // also to the children, in case of master-buffer-view
ListOfBuffers clist = getDescendents(); ListOfBuffers clist = getDescendents();
ListOfBuffers::const_iterator cit = clist.begin(); for (auto const & bit : clist) {
ListOfBuffers::const_iterator const cen = clist.end();
for (; cit != cen; ++cit) {
if (runparams.silent) if (runparams.silent)
(*cit)->d->errorLists[error_type].clear(); bit->d->errorLists[error_type].clear();
else if (d->cloned_buffer_) { else if (d->cloned_buffer_) {
// Enable reverse search by copying back the // Enable reverse search by copying back the
// texrow object to the cloned buffer. // texrow object to the cloned buffer.
// FIXME: this is not thread safe. // FIXME: this is not thread safe.
(*cit)->d->cloned_buffer_->d->texrow = (*cit)->d->texrow; bit->d->cloned_buffer_->d->texrow = bit->d->texrow;
(*cit)->d->cloned_buffer_->d->errorLists[error_type] = bit->d->cloned_buffer_->d->errorLists[error_type] =
(*cit)->d->errorLists[error_type]; bit->d->errorLists[error_type];
} else } else
(*cit)->errors(error_type, true); bit->errors(error_type, true);
} }
} }
@ -4427,11 +4383,13 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
: force_overwrite == ALL_FILES; : force_overwrite == ALL_FILES;
CopyStatus status = use_force ? FORCE : SUCCESS; CopyStatus status = use_force ? FORCE : SUCCESS;
vector<ExportedFile>::const_iterator it = files.begin(); for (ExportedFile const & exp : files) {
vector<ExportedFile>::const_iterator const en = files.end(); if (status == CANCEL) {
for (; it != en && status != CANCEL; ++it) { message(_("Document export cancelled."));
string const fmt = theFormats().getFormatFromFile(it->sourceName); return ExportCancel;
string fixedName = it->exportName; }
string const fmt = theFormats().getFormatFromFile(exp.sourceName);
string fixedName = exp.exportName;
if (!runparams.export_folder.empty()) { if (!runparams.export_folder.empty()) {
// Relative pathnames starting with ../ will be sanitized // Relative pathnames starting with ../ will be sanitized
// if exporting to a different folder // if exporting to a different folder
@ -4440,16 +4398,12 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
} }
FileName fixedFileName = makeAbsPath(fixedName, dest); FileName fixedFileName = makeAbsPath(fixedName, dest);
fixedFileName.onlyPath().createPath(); fixedFileName.onlyPath().createPath();
status = copyFile(fmt, it->sourceName, status = copyFile(fmt, exp.sourceName,
fixedFileName, fixedFileName,
it->exportName, status == FORCE, exp.exportName, status == FORCE,
runparams.export_folder.empty()); runparams.export_folder.empty());
} }
if (status == CANCEL) {
message(_("Document export cancelled."));
return ExportCancel;
}
if (tmp_result_file.exists()) { if (tmp_result_file.exists()) {
// Finally copy the main file // Finally copy the main file
@ -5032,11 +4986,9 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const
parit->addChangesToBuffer(*this); parit->addChangesToBuffer(*this);
// now the insets // now the insets
InsetList::const_iterator iit = parit->insetList().begin(); for (auto const & insit : parit->insetList()) {
InsetList::const_iterator end = parit->insetList().end(); parit.pos() = insit.pos;
for (; iit != end; ++iit) { insit.inset->updateBuffer(parit, utype);
parit.pos() = iit->pos;
iit->inset->updateBuffer(parit, utype);
} }
} }
} }
@ -5220,11 +5172,9 @@ bool Buffer::saveAs(FileName const & fn)
void Buffer::checkChildBuffers() void Buffer::checkChildBuffers()
{ {
Impl::BufferPositionMap::iterator it = d->children_positions.begin(); for (auto const & bit : d->children_positions) {
Impl::BufferPositionMap::iterator const en = d->children_positions.end(); DocIterator dit = bit.second;
for (; it != en; ++it) { Buffer * cbuf = const_cast<Buffer *>(bit.first);
DocIterator dit = it->second;
Buffer * cbuf = const_cast<Buffer *>(it->first);
if (!cbuf || !theBufferList().isLoaded(cbuf)) if (!cbuf || !theBufferList().isLoaded(cbuf))
continue; continue;
Inset * inset = dit.nextInset(); Inset * inset = dit.nextInset();