From f083830b8de106ed51d39242008215e9ea2179b2 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Wed, 13 Apr 2016 14:45:11 +0300 Subject: CoreLib: use const (and const APIs) more For CoW types const methods will be called. Mark store_persistent_indexes() as const, because this method does not modify the object. Change-Id: Ic867913b4fb5aaebfbaaffe1d3be45cf7b646403 Reviewed-by: Thiago Macieira --- src/corelib/io/qsettings_mac.cpp | 4 ++-- src/corelib/io/qstandardpaths_mac.mm | 2 +- src/corelib/itemmodels/qabstractitemmodel.cpp | 12 ++++++------ src/corelib/itemmodels/qitemselectionmodel.cpp | 2 +- src/corelib/itemmodels/qsortfilterproxymodel.cpp | 6 +++--- src/corelib/kernel/qcoreapplication_win.cpp | 4 ++-- src/corelib/kernel/qeventdispatcher_glib.cpp | 2 +- src/corelib/kernel/qtimerinfo_unix.cpp | 2 +- src/corelib/mimetypes/qmimedatabase.cpp | 2 +- src/corelib/mimetypes/qmimeprovider.cpp | 6 +++--- src/corelib/statemachine/qhistorystate.cpp | 9 ++++++--- src/corelib/statemachine/qstatemachine.cpp | 2 +- src/corelib/thread/qthread_p.h | 2 +- src/corelib/thread/qthreadpool.cpp | 2 +- src/corelib/thread/qwaitcondition_win.cpp | 2 +- src/corelib/tools/qcommandlineparser.cpp | 7 ++++--- src/corelib/tools/qeasingcurve.cpp | 2 +- src/corelib/tools/qlocale_unix.cpp | 2 +- src/corelib/tools/qringbuffer.cpp | 12 ++++++------ src/corelib/tools/qtimezoneprivate_mac.mm | 2 +- 20 files changed, 44 insertions(+), 40 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/io/qsettings_mac.cpp b/src/corelib/io/qsettings_mac.cpp index ceee165459..d73cc4d298 100644 --- a/src/corelib/io/qsettings_mac.cpp +++ b/src/corelib/io/qsettings_mac.cpp @@ -149,7 +149,7 @@ static QCFType macValue(const QVariant &value) bool singleton = (values.count() == 1); if (singleton) { - switch (values.first().type()) { + switch (values.constFirst().type()) { // should be same as above (look for LIST) case QVariant::List: case QVariant::StringList: @@ -161,7 +161,7 @@ static QCFType macValue(const QVariant &value) } cfkeys[numUniqueKeys] = QCFString::toCFStringRef(key); - cfvalues[numUniqueKeys] = singleton ? macValue(values.first()) : macList(values); + cfvalues[numUniqueKeys] = singleton ? macValue(values.constFirst()) : macList(values); ++numUniqueKeys; } diff --git a/src/corelib/io/qstandardpaths_mac.mm b/src/corelib/io/qstandardpaths_mac.mm index 33c34d41af..f08a6dac53 100644 --- a/src/corelib/io/qstandardpaths_mac.mm +++ b/src/corelib/io/qstandardpaths_mac.mm @@ -273,7 +273,7 @@ QString QStandardPaths::displayName(StandardLocation type) return QCoreApplication::translate("QStandardPaths", "Applications"); if (QCFType url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, - standardLocations(type).first().toCFString(), + standardLocations(type).constFirst().toCFString(), kCFURLPOSIXPathStyle, true)) { QCFString name; CFURLCopyResourcePropertyForKey(url, kCFURLLocalizedNameKey, &name, NULL); diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 02b1e1c306..54afb8a974 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -58,8 +58,8 @@ QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex & QPersistentModelIndexData *d = 0; QAbstractItemModel *model = const_cast(index.model()); QHash &indexes = model->d_func()->persistent.indexes; - const QHash::iterator it = indexes.find(index); - if (it != indexes.end()) { + const auto it = indexes.constFind(index); + if (it != indexes.cend()) { d = (*it); } else { d = new QPersistentModelIndexData(index); @@ -603,13 +603,13 @@ void QAbstractItemModelPrivate::removePersistentIndexData(QPersistentModelIndexD } // make sure our optimization still works for (int i = persistent.moved.count() - 1; i >= 0; --i) { - int idx = persistent.moved[i].indexOf(data); + int idx = persistent.moved.at(i).indexOf(data); if (idx >= 0) persistent.moved[i].remove(idx); } // update the references to invalidated persistent indexes for (int i = persistent.invalidated.count() - 1; i >= 0; --i) { - int idx = persistent.invalidated[i].indexOf(data); + int idx = persistent.invalidated.at(i).indexOf(data); if (idx >= 0) persistent.invalidated[i].remove(idx); } @@ -2544,13 +2544,13 @@ bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &pare for (int i = 0; i < rows.count(); ++i) rowsToInsert[rows.at(i)] = 1; for (int i = 0; i < rowsToInsert.count(); ++i) { - if (rowsToInsert[i] == 1){ + if (rowsToInsert.at(i) == 1){ rowsToInsert[i] = dragRowCount; ++dragRowCount; } } for (int i = 0; i < rows.count(); ++i) - rows[i] = top + rowsToInsert[rows[i]]; + rows[i] = top + rowsToInsert.at(rows.at(i)); QBitArray isWrittenTo(dragRowCount * dragColumnCount); diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 56df8fd55a..6390d5f389 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -863,7 +863,7 @@ void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged(const QListrowCount(parent); tableColCount = model->columnCount(parent); diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 18cb49d483..98202b71ae 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -275,7 +275,7 @@ public: const QVector &source_to_proxy, const QVector &source_items, int &proxy_low, int &proxy_high) const; - QModelIndexPairList store_persistent_indexes(); + QModelIndexPairList store_persistent_indexes() const; void update_persistent_indexes(const QModelIndexPairList &source_indexes); void filter_about_to_be_changed(const QModelIndex &source_parent = QModelIndex()); @@ -1014,9 +1014,9 @@ void QSortFilterProxyModelPrivate::build_source_to_proxy_mapping( Maps the persistent proxy indexes to source indexes and returns the list of source indexes. */ -QModelIndexPairList QSortFilterProxyModelPrivate::store_persistent_indexes() +QModelIndexPairList QSortFilterProxyModelPrivate::store_persistent_indexes() const { - Q_Q(QSortFilterProxyModel); + Q_Q(const QSortFilterProxyModel); QModelIndexPairList source_indexes; source_indexes.reserve(persistent.indexes.count()); for (QPersistentModelIndexData *data : qAsConst(persistent.indexes)) { diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp index 9cd8420a32..601733b939 100644 --- a/src/corelib/kernel/qcoreapplication_win.cpp +++ b/src/corelib/kernel/qcoreapplication_win.cpp @@ -59,12 +59,12 @@ int appCmdShow = 0; Q_CORE_EXPORT QString qAppFileName() { - return QFileInfo(QCoreApplication::arguments().first()).filePath(); + return QFileInfo(QCoreApplication::arguments().constFirst()).filePath(); } QString QCoreApplicationPrivate::appName() const { - return QFileInfo(QCoreApplication::arguments().first()).baseName(); + return QFileInfo(QCoreApplication::arguments().constFirst()).baseName(); } #else diff --git a/src/corelib/kernel/qeventdispatcher_glib.cpp b/src/corelib/kernel/qeventdispatcher_glib.cpp index 8c2b47dccb..8ca2ac1c39 100644 --- a/src/corelib/kernel/qeventdispatcher_glib.cpp +++ b/src/corelib/kernel/qeventdispatcher_glib.cpp @@ -143,7 +143,7 @@ static gboolean timerSourceCheckHelper(GTimerSource *src) || (src->processEventsFlags & QEventLoop::X11ExcludeTimers)) return false; - if (src->timerList.updateCurrentTime() < src->timerList.first()->timeout) + if (src->timerList.updateCurrentTime() < src->timerList.constFirst()->timeout) return false; return true; diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp index 8f5e8c9523..56337bdb45 100644 --- a/src/corelib/kernel/qtimerinfo_unix.cpp +++ b/src/corelib/kernel/qtimerinfo_unix.cpp @@ -591,7 +591,7 @@ int QTimerInfoList::activateTimers() if (isEmpty()) break; - QTimerInfo *currentTimerInfo = first(); + QTimerInfo *currentTimerInfo = constFirst(); if (currentTime < currentTimerInfo->timeout) break; // no timer has expired diff --git a/src/corelib/mimetypes/qmimedatabase.cpp b/src/corelib/mimetypes/qmimedatabase.cpp index da84523dcb..a32031a788 100644 --- a/src/corelib/mimetypes/qmimedatabase.cpp +++ b/src/corelib/mimetypes/qmimedatabase.cpp @@ -406,7 +406,7 @@ QMimeType QMimeDatabase::mimeTypeForFile(const QString &fileName, MatchMode mode { if (mode == MatchExtension) { QMutexLocker locker(&d->mutex); - QStringList matches = d->mimeTypeForFileName(fileName); + const QStringList matches = d->mimeTypeForFileName(fileName); const int matchCount = matches.count(); if (matchCount == 0) { return d->mimeTypeForName(d->defaultMimeType()); diff --git a/src/corelib/mimetypes/qmimeprovider.cpp b/src/corelib/mimetypes/qmimeprovider.cpp index 677e87077f..dc6eb05d9a 100644 --- a/src/corelib/mimetypes/qmimeprovider.cpp +++ b/src/corelib/mimetypes/qmimeprovider.cpp @@ -205,7 +205,7 @@ bool QMimeBinaryProvider::isValid() return false; // We found exactly one file; is it the user-modified mimes, or a system file? - const QString foundFile = m_cacheFiles.first()->file.fileName(); + const QString foundFile = m_cacheFiles.constFirst()->file.fileName(); const QString localCacheFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/mime/mime.cache"); return foundFile != localCacheFile; @@ -629,7 +629,7 @@ void QMimeBinaryProvider::loadMimeTypePrivate(QMimeTypePrivate &data) // Let's assume that shared-mime-info is at least version 0.70 // Otherwise we would need 1) a version check, and 2) code for parsing patterns from the globs file. #if 1 - if (!mainPattern.isEmpty() && (data.globPatterns.isEmpty() || data.globPatterns.first() != mainPattern)) { + if (!mainPattern.isEmpty() && (data.globPatterns.isEmpty() || data.globPatterns.constFirst() != mainPattern)) { // ensure it's first in the list of patterns data.globPatterns.removeAll(mainPattern); data.globPatterns.prepend(mainPattern); @@ -637,7 +637,7 @@ void QMimeBinaryProvider::loadMimeTypePrivate(QMimeTypePrivate &data) #else const bool globsInXml = sharedMimeInfoVersion() >= QT_VERSION_CHECK(0, 70, 0); if (globsInXml) { - if (!mainPattern.isEmpty() && data.globPatterns.first() != mainPattern) { + if (!mainPattern.isEmpty() && data.globPatterns.constFirst() != mainPattern) { // ensure it's first in the list of patterns data.globPatterns.removeAll(mainPattern); data.globPatterns.prepend(mainPattern); diff --git a/src/corelib/statemachine/qhistorystate.cpp b/src/corelib/statemachine/qhistorystate.cpp index c3361ad17e..338c89c688 100644 --- a/src/corelib/statemachine/qhistorystate.cpp +++ b/src/corelib/statemachine/qhistorystate.cpp @@ -209,6 +209,11 @@ QAbstractState *QHistoryState::defaultState() const return d->defaultTransition ? d->defaultTransition->targetState() : Q_NULLPTR; } +static inline bool isSoleEntry(const QList &states, const QAbstractState * state) +{ + return states.size() == 1 && states.first() == state; +} + /*! Sets this history state's default state to be the given \a state. \a state must be a sibling of this history state. @@ -224,9 +229,7 @@ void QHistoryState::setDefaultState(QAbstractState *state) "to this history state's group (%p)", state, parentState()); return; } - if (!d->defaultTransition - || d->defaultTransition->targetStates().size() != 1 - || d->defaultTransition->targetStates().first() != state) { + if (!d->defaultTransition || !isSoleEntry(d->defaultTransition->targetStates(), state)) { if (!d->defaultTransition || !qobject_cast(d->defaultTransition)) { d->defaultTransition = new DefaultStateTransition(this, state); emit defaultTransitionChanged(QHistoryState::QPrivateSignal()); diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp index 62a4c03d26..d5b01f3c8a 100644 --- a/src/corelib/statemachine/qstatemachine.cpp +++ b/src/corelib/statemachine/qstatemachine.cpp @@ -692,7 +692,7 @@ void QStateMachinePrivate::microstep(QEvent *event, const QList= priority || + constLast().priority >= priority || insertionOffset >= size()) { // optimization: we can simply append if the last event in // the queue has higher or equal priority diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 9230854600..7ce757064f 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -228,7 +228,7 @@ int QThreadPoolPrivate::activeThreadCount() const void QThreadPoolPrivate::tryToStartMoreThreads() { // try to push tasks on the queue to any available threads - while (!queue.isEmpty() && tryStart(queue.first().first)) + while (!queue.isEmpty() && tryStart(queue.constFirst().first)) queue.removeFirst(); } diff --git a/src/corelib/thread/qwaitcondition_win.cpp b/src/corelib/thread/qwaitcondition_win.cpp index f3a645c504..a95ca0b8fd 100644 --- a/src/corelib/thread/qwaitcondition_win.cpp +++ b/src/corelib/thread/qwaitcondition_win.cpp @@ -141,7 +141,7 @@ void QWaitConditionPrivate::post(QWaitConditionEvent *wce, bool ret) // wakeups delivered after the timeout should be forwarded to the next waiter if (!ret && wce->wokenUp && !queue.isEmpty()) { - QWaitConditionEvent *other = queue.first(); + QWaitConditionEvent *other = queue.constFirst(); SetEvent(other->event); other->wokenUp = true; } diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index 6587d900d2..a7ab8b9e70 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -887,7 +887,8 @@ QStringList QCommandLineParser::values(const QString &optionName) const bool QCommandLineParser::isSet(const QCommandLineOption &option) const { // option.names() might be empty if the constructor failed - return !option.names().isEmpty() && isSet(option.names().first()); + const auto names = option.names(); + return !names.isEmpty() && isSet(names.first()); } /*! @@ -905,7 +906,7 @@ bool QCommandLineParser::isSet(const QCommandLineOption &option) const */ QString QCommandLineParser::value(const QCommandLineOption &option) const { - return value(option.names().first()); + return value(option.names().constFirst()); } /*! @@ -923,7 +924,7 @@ QString QCommandLineParser::value(const QCommandLineOption &option) const */ QStringList QCommandLineParser::values(const QCommandLineOption &option) const { - return values(option.names().first()); + return values(option.names().constFirst()); } /*! diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index 2851dc81d6..4b5f5e7830 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.cpp @@ -444,7 +444,7 @@ struct BezierEase : public QEasingCurveFunction void init() { - if (_bezierCurves.last() == QPointF(1.0, 1.0)) { + if (_bezierCurves.constLast() == QPointF(1.0, 1.0)) { _init = true; _curveCount = _bezierCurves.count() / 3; diff --git a/src/corelib/tools/qlocale_unix.cpp b/src/corelib/tools/qlocale_unix.cpp index 9b0d338e46..095001e0a3 100644 --- a/src/corelib/tools/qlocale_unix.cpp +++ b/src/corelib/tools/qlocale_unix.cpp @@ -122,7 +122,7 @@ QLocale QSystemLocale::fallbackUiLocale() const // the first part of LANGUAGE if LANGUAGE is set and has a first part: QByteArray language = qgetenv("LANGUAGE"); if (!language.isEmpty()) { - language = language.split(':').first(); + language = language.split(':').constFirst(); if (!language.isEmpty()) return QLocale(QString::fromLatin1(language)); } diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp index db2004dfd9..4a2dfdec2b 100644 --- a/src/corelib/tools/qringbuffer.cpp +++ b/src/corelib/tools/qringbuffer.cpp @@ -74,14 +74,14 @@ void QRingBuffer::free(qint64 bytes) Q_ASSERT(bytes <= bufferSize); while (bytes > 0) { - const qint64 blockSize = buffers.first().size() - head; + const qint64 blockSize = buffers.constFirst().size() - head; if (tailBuffer == 0 || blockSize > bytes) { // keep a single block around if it does not exceed // the basic block size, to avoid repeated allocations // between uses of the buffer if (bufferSize <= bytes) { - if (buffers.first().size() <= basicBlockSize) { + if (buffers.constFirst().size() <= basicBlockSize) { bufferSize = 0; head = tail = 0; } else { @@ -114,8 +114,8 @@ char *QRingBuffer::reserve(qint64 bytes) } else { const qint64 newSize = bytes + tail; // if need buffer reallocation - if (newSize > buffers.last().size()) { - if (newSize > buffers.last().capacity() && (tail >= basicBlockSize + if (newSize > buffers.constLast().size()) { + if (newSize > buffers.constLast().capacity() && (tail >= basicBlockSize || newSize >= MaxByteArraySize)) { // shrink this buffer to its current size buffers.last().resize(tail); @@ -180,7 +180,7 @@ void QRingBuffer::chop(qint64 bytes) // the basic block size, to avoid repeated allocations // between uses of the buffer if (bufferSize <= bytes) { - if (buffers.first().size() <= basicBlockSize) { + if (buffers.constFirst().size() <= basicBlockSize) { bufferSize = 0; head = tail = 0; } else { @@ -198,7 +198,7 @@ void QRingBuffer::chop(qint64 bytes) bytes -= tail; buffers.removeLast(); --tailBuffer; - tail = buffers.last().size(); + tail = buffers.constLast().size(); } } diff --git a/src/corelib/tools/qtimezoneprivate_mac.mm b/src/corelib/tools/qtimezoneprivate_mac.mm index 14b0523ca7..3a665c2b00 100644 --- a/src/corelib/tools/qtimezoneprivate_mac.mm +++ b/src/corelib/tools/qtimezoneprivate_mac.mm @@ -243,7 +243,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec } } if (secsList.size() >= 1) - return data(qint64(secsList.last()) * 1000); + return data(qint64(secsList.constLast()) * 1000); else return invalidData(); } -- cgit v1.2.3