Replace foreach with for

C++11 allows foreach-style iterations with the following syntax:

for (auto & element : container) {
 ...
}
This commit is contained in:
Guillaume Munch 2016-06-09 21:54:31 +01:00
parent 32ff5f0f30
commit 6144bbfbf8
9 changed files with 15 additions and 103 deletions

View File

@ -15,7 +15,6 @@
#include "qt_helpers.h"
#include "support/debug.h"
#include "support/foreach.h"
#include <QCheckBox>
#include <QPushButton>
@ -102,7 +101,7 @@ public:
bool checkWidgets() const
{
bool valid = true;
foreach (const CheckedLineEdit & w, checked_widgets_)
for (const CheckedLineEdit & w : checked_widgets_)
valid &= w.check();
return valid;
}
@ -252,10 +251,8 @@ void ButtonController::refreshReadOnly() const
{
if (d->read_only_.empty())
return;
bool const enable = !policy().isReadOnly();
foreach (QWidget * w, d->read_only_)
for(QWidget * w : d->read_only_)
setWidgetEnabled(w, enable);
}

View File

@ -64,7 +64,6 @@
#include "support/ExceptionMessage.h"
#include "support/FileName.h"
#include "support/filetools.h"
#include "support/foreach.h"
#include "support/ForkedCalls.h"
#include "support/gettext.h"
#include "support/lassert.h"
@ -1877,11 +1876,12 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
QList<GuiView *> allViews = d->views_.values();
// this foreach does not modify any buffer. It just collects info on local visibility of buffers
// and on which buffer is active in each view.
// this for does not modify any buffer. It just collects info on local
// visibility of buffers and on which buffer is active in each view.
Buffer * const last = theBufferList().last();
foreach (GuiView * view, allViews) {
// all of the buffers might be locally hidden. That is, there is no active buffer.
for(GuiView * view : allViews) {
// all of the buffers might be locally hidden. That is, there is no
// active buffer.
if (!view || !view->currentBufferView())
activeBuffers[view] = 0;
else
@ -1928,7 +1928,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
}
// put things back to how they were (if possible).
foreach (GuiView * view, allViews) {
for (GuiView * view : allViews) {
Buffer * originalBuf = activeBuffers[view];
// there might not have been an active buffer in this view or it might have been closed by the LFUN.
if (theBufferList().isLoaded(originalBuf))
@ -2811,7 +2811,7 @@ bool GuiApplication::closeAllViews()
theSession().lastOpened().clear();
QList<GuiView *> const views = d->views_.values();
foreach (GuiView * view, views) {
for (GuiView * view : views) {
if (!view->closeScheduled())
return false;
}
@ -2827,7 +2827,7 @@ bool GuiApplication::prepareAllViewsForLogout()
return true;
QList<GuiView *> const views = d->views_.values();
foreach (GuiView * view, views) {
for (GuiView * view : views) {
if (!view->prepareAllBuffersForLogout())
return false;
}
@ -2846,7 +2846,7 @@ GuiView & GuiApplication::view(int id) const
void GuiApplication::hideDialogs(string const & name, Inset * inset) const
{
QList<GuiView *> const views = d->views_.values();
foreach (GuiView * view, views)
for (GuiView * view : views)
view->hideDialog(name, inset);
}

View File

@ -26,7 +26,6 @@
#include "insets/InsetBox.h"
#include "support/gettext.h"
#include "support/foreach.h"
#include "support/lstrings.h"
#include <QComboBox>

View File

@ -41,7 +41,6 @@
#include "support/debug.h"
#include "support/FileName.h"
#include "support/filetools.h"
#include "support/foreach.h"
#include "support/gettext.h"
#include "support/lassert.h"
#include "support/lstrings.h"

View File

@ -16,7 +16,6 @@
#include "FuncRequest.h"
#include "support/filetools.h"
#include "support/foreach.h"
#include "support/FileName.h"
#include "qt_helpers.h"
@ -141,7 +140,7 @@ void GuiTexInfo::updateStyles(TexFileType type)
data.sort();
fileListLW->clear();
foreach (QString const & item, data)
for(QString const & item : data)
fileListLW->addItem(item);
activeStyle_ = type;

View File

@ -16,7 +16,6 @@
#include "qt_helpers.h"
#include "support/debug.h"
#include "support/foreach.h"
#include "support/lassert.h"
#include <QAbstractButton>
@ -244,11 +243,11 @@ void PanelStack::search()
// If the search string is empty we enable all the items
// otherwise we disable everything and then selectively
// re-enable matching items
foreach (QTreeWidgetItem * tree_item, panel_map_) {
for (QTreeWidgetItem * tree_item : panel_map_) {
setTreeItemStatus(tree_item, enable_all);
}
foreach (QTreeWidgetItem * tree_item, panel_map_) {
for (QTreeWidgetItem * tree_item : panel_map_) {
// Current widget
QWidget * pane_widget = widget_map_[tree_item];
@ -259,7 +258,7 @@ void PanelStack::search()
if (pane_widget) {
// Loops on the list of children widgets (recursive)
QWidgetList children = pane_widget->findChildren<QWidget *>();
foreach (QWidget * child_widget, children) {
for (QWidget * child_widget : children) {
bool widget_matches = false;
// Try to cast to the most common widgets and looks in it's content

View File

@ -28,7 +28,6 @@
#include "support/convert.h"
#include "support/debug.h"
#include "support/foreach.h"
#include "support/gettext.h"
#include "support/lstrings.h"
#include "support/lyxalgo.h"

View File

@ -1,79 +0,0 @@
/**
* \file foreach.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Matthias Ettrich
*
* Full author contact details are available in file CREDITS.
*
*/
#ifndef FOREACH_H
#define FOREACH_H
// Code stolen from Q_FOREACH, augmented to use a reference to the
// original container instead of a copy. Copies are cheap (if not
// mutated) for Qt's containers due to copy-on-write. The are less
// cheap for Standard containers, that's why the modification.
// Drawback is that we can't use temporary containers as they
// will be destroyed before the loop is finished. So always write
//
// Container const & container = functionReturningTemporaryOrReference()
// foreach (ContainerItem const & item, container) {
// ...
// }
//
// to extend the lifetime of the reference.
#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL)
/* make use of typeof-extension */
template <typename T>
class ForeachContainer {
public:
inline ForeachContainer(const T & t) : c(t), brk(0), i(c.begin()), e(c.end()) { }
const T & c;
int brk;
typename T::const_iterator i, e;
};
#define foreach(variable, container) \
for (ForeachContainer<__typeof__(container)> _container_(container); \
!_container_.brk && _container_.i != _container_.e; \
__extension__ ({ ++_container_.brk; ++_container_.i; })) \
for (variable = *_container_.i;; __extension__ ({--_container_.brk; break;}))
#else
struct ForeachContainerBase {};
template <typename T>
class ForeachContainer : public ForeachContainerBase {
public:
inline ForeachContainer(const T& t): c(t), brk(0), i(c.begin()), e(c.end()){}
const T & c;
mutable int brk;
mutable typename T::const_iterator i, e;
inline bool condition() const { return (!brk++ && i != e); }
};
template <typename T> inline T *foreachPointer(const T &) { return 0; }
template <typename T> inline ForeachContainer<T> foreachContainerNew(const T& t)
{ return ForeachContainer<T>(t); }
template <typename T>
inline const ForeachContainer<T> *foreachContainer(const ForeachContainerBase *base, const T *)
{ return static_cast<const ForeachContainer<T> *>(base); }
#define foreach(variable, container) \
for (const ForeachContainerBase &_container_ = foreachContainerNew(container); \
foreachContainer(&_container_, true ? 0 : foreachPointer(container))->condition(); \
++foreachContainer(&_container_, true ? 0 : foreachPointer(container))->i) \
for (variable = *foreachContainer(&_container_, true ? 0 : foreachPointer(container))->i; \
foreachContainer(&_container_, true ? 0 : foreachPointer(container))->brk; \
--foreachContainer(&_container_, true ? 0 : foreachPointer(container))->brk)
#endif
#endif // FOREACH_H

View File

@ -12,7 +12,6 @@
#include "Encoding.h"
#include "Parser.h"
#include "support/foreach.h"
#include "support/lstrings.h"
#include "support/textutils.h"