mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
refs #937 Resolved clazy warnings: unnecessary detaching of containers.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include "blackmisc/connectionguard.h"
|
||||
#include "blackmisc/range.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -35,7 +36,7 @@ namespace BlackMisc
|
||||
{
|
||||
if (this->m_connections.isEmpty()) { return 0; }
|
||||
int c = 0;
|
||||
for (const QMetaObject::Connection &con : this->m_connections)
|
||||
for (const QMetaObject::Connection &con : as_const(this->m_connections))
|
||||
{
|
||||
if (QObject::disconnect(con)) { c++; }
|
||||
}
|
||||
|
||||
@@ -226,7 +226,7 @@ namespace BlackMisc
|
||||
qSwap(m_queue, queue);
|
||||
lock.unlock();
|
||||
|
||||
for (const auto &pair : queue)
|
||||
for (const auto &pair : BlackMisc::as_const(queue))
|
||||
{
|
||||
m_page->setValuesFromCache(pair.first, pair.second);
|
||||
}
|
||||
@@ -299,7 +299,7 @@ namespace BlackMisc
|
||||
auto msg = m_cache->loadFromFiles(persistentStore(), m_cache->m_revision.keysWithNewerTimestamps(), baseline.toVariantMap(), newValues, m_cache->m_revision.timestampsAsString());
|
||||
newValues.setTimestamps(m_cache->m_revision.newerTimestamps());
|
||||
|
||||
auto missingKeys = m_cache->m_revision.keysWithNewerTimestamps().subtract(newValues.keys());
|
||||
auto missingKeys = m_cache->m_revision.keysWithNewerTimestamps() - newValues.keys();
|
||||
if (! missingKeys.isEmpty()) { m_cache->m_revision.writeNewRevision({}, missingKeys); }
|
||||
|
||||
msg.setCategories(this);
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace BlackMisc
|
||||
{
|
||||
// further reduce by VATSIM flag
|
||||
QStringList furtherReduced;
|
||||
for (const QString &p : reduced)
|
||||
for (const QString &p : as_const(reduced))
|
||||
{
|
||||
if (CBuildConfig::isVatsimVersion())
|
||||
{
|
||||
@@ -203,7 +203,7 @@ namespace BlackMisc
|
||||
const QJsonObject platforms = json.value("platforms").toObject();
|
||||
const QStringList platformsKeys = platforms.keys();
|
||||
if (platformsKeys.isEmpty()) { return CDistribution(); } // no platforms, then the whole distribution is useless
|
||||
for (const QString platformKey : platformsKeys)
|
||||
for (const QString platformKey : as_const(platformsKeys))
|
||||
{
|
||||
QStringList platformFileNames;
|
||||
QJsonArray platformFiles = platforms.value(platformKey).toArray();
|
||||
|
||||
@@ -183,7 +183,7 @@ namespace BlackMisc
|
||||
m_connections.insert(connection.name(), connection);
|
||||
CLogMessage(this).debug() << "New Connection from:" << connection.name();
|
||||
bool success = true;
|
||||
for (auto i = m_objects.begin(); i != m_objects.end(); ++i)
|
||||
for (auto i = m_objects.cbegin(); i != m_objects.cend(); ++i)
|
||||
{
|
||||
CLogMessage(this).debug() << "Adding" << i.key() << getDBusInterfaceFromClassInfo(i.value()) << "to the new connection.";
|
||||
bool ok = connection.registerObject(i.key(), i.value(), registerOptions());
|
||||
@@ -228,7 +228,7 @@ namespace BlackMisc
|
||||
break;
|
||||
case SERVERMODE_P2P:
|
||||
{
|
||||
for (QDBusConnection connection : m_connections)
|
||||
for (QDBusConnection connection : as_const(m_connections))
|
||||
{
|
||||
if (connection.registerObject(path, object, registerOptions()))
|
||||
{
|
||||
@@ -277,7 +277,7 @@ namespace BlackMisc
|
||||
break;
|
||||
case SERVERMODE_P2P:
|
||||
{
|
||||
for (QDBusConnection connection : m_connections)
|
||||
for (QDBusConnection connection : as_const(m_connections))
|
||||
{
|
||||
connection.unregisterObject(path);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "blackmisc/directoryutils.h"
|
||||
#include "blackmisc/fileutils.h"
|
||||
#include "blackmisc/range.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QUrl>
|
||||
@@ -176,8 +177,8 @@ namespace BlackMisc
|
||||
comp.sameNameInTarget = CDirectoryUtils::filesToCanonicalNames(sameNames, sTargetCanonicalFiles);
|
||||
|
||||
Q_ASSERT_X(comp.sameNameInSource.size() == comp.sameNameInTarget.size(), Q_FUNC_INFO, "Same sets require same size");
|
||||
QSet<QString>::const_iterator targetIt = comp.sameNameInTarget.begin();
|
||||
for (const QString &sourceFile : comp.sameNameInSource)
|
||||
QSet<QString>::const_iterator targetIt = comp.sameNameInTarget.cbegin();
|
||||
for (const QString &sourceFile : as_const(comp.sameNameInSource))
|
||||
{
|
||||
const QFileInfo source(sourceFile);
|
||||
const QFileInfo target(*targetIt++);
|
||||
|
||||
@@ -130,7 +130,7 @@ namespace BlackMisc
|
||||
|
||||
void CLogHandler::logMessage(const CStatusMessage &statusMessage)
|
||||
{
|
||||
auto handlers = handlersForMessage(statusMessage);
|
||||
const auto handlers = handlersForMessage(statusMessage);
|
||||
|
||||
if (isFallThroughEnabled(handlers))
|
||||
{
|
||||
|
||||
@@ -289,24 +289,54 @@ namespace BlackMisc
|
||||
|
||||
/*!
|
||||
* Returns a CRange constructed from the begin and end iterators of the given container.
|
||||
*
|
||||
* The returned CRange may or may not be const, depending on the constness of the container.
|
||||
*/
|
||||
//! @{
|
||||
template <class T>
|
||||
auto makeRange(T &&container) -> CRange<decltype(container.begin())>
|
||||
auto makeRange(T &container) -> CRange<decltype(container.begin())>
|
||||
{
|
||||
return { container.begin(), container.end() };
|
||||
}
|
||||
template <class T>
|
||||
auto makeRange(const T &container) -> CRange<decltype(container.begin())>
|
||||
{
|
||||
return { container.begin(), container.end() };
|
||||
}
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Returns a const CRange constructed from the cbegin and cend iterators of the given container.
|
||||
*/
|
||||
template <class T>
|
||||
auto makeConstRange(T &&container) -> CRange<decltype(container.cbegin())>
|
||||
auto makeConstRange(const T &container) -> CRange<decltype(container.cbegin())>
|
||||
{
|
||||
return { container.cbegin(), container.cend() };
|
||||
}
|
||||
|
||||
/*!
|
||||
* Returns a const CRange for iterating over the keys of an associative container.
|
||||
*
|
||||
* This is more efficient than the keys() method of the container, as it doesn't allocate memory.
|
||||
*/
|
||||
template <class T>
|
||||
auto makeKeysRange(const T &container)
|
||||
{
|
||||
return makeRange(Iterators::makeKeyIterator(container.cbegin()), container.cend());
|
||||
}
|
||||
|
||||
/*!
|
||||
* Keys range for a non-const lvalue would be unsafe due to iterator invalidation on detach().
|
||||
*
|
||||
* \see http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem
|
||||
*/
|
||||
template <class T>
|
||||
void makeKeysRange(T &container) = delete;
|
||||
|
||||
/*!
|
||||
* Keys range for a temporary would be unsafe.
|
||||
*/
|
||||
template <class T>
|
||||
void makeKeysRange(const T &&container) = delete;
|
||||
|
||||
/*
|
||||
* Member functions of CRangeBase template defined out of line, because they depend on CRange etc.
|
||||
*/
|
||||
|
||||
@@ -38,11 +38,11 @@ namespace BlackMisc
|
||||
this->m_splitParts = m_cleanedLine.split(' ');
|
||||
if (!this->m_splitParts.isEmpty())
|
||||
{
|
||||
const QString first = this->m_splitParts.first();
|
||||
const QString &first = this->m_splitParts.constFirst();
|
||||
const QString formatted = formatCommand(first);
|
||||
if (isCommand(first))
|
||||
{
|
||||
this->m_commandPart = formatCommand(first);
|
||||
this->m_commandPart = formatted;
|
||||
this->m_knownCommand = this->m_knownCommands.contains(formatted);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ namespace BlackMisc
|
||||
QStringList remove(toUpper(modelsToBeRemoved));
|
||||
remove.sort();
|
||||
|
||||
QSet<QString> removeSet(knownModels.toSet().intersect(remove.toSet()));
|
||||
QSet<QString> removeSet(knownModels.toSet() & remove.toSet());
|
||||
int c = 0;
|
||||
for (const QString &model : removeSet)
|
||||
{
|
||||
|
||||
@@ -132,10 +132,11 @@ namespace BlackMisc
|
||||
|
||||
const QString &CVPilotRulesReader::standardMappingsDirectory()
|
||||
{
|
||||
//! \fixme not threadsafe
|
||||
static QString directory;
|
||||
if (directory.isEmpty())
|
||||
{
|
||||
directory = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first();
|
||||
directory = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).constFirst();
|
||||
if (!directory.endsWith('/')) { directory.append('/'); }
|
||||
directory.append("vPilot Files/Model Matching Rule Sets");
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ namespace BlackMisc
|
||||
file.close();
|
||||
parseFullPackage(content, package);
|
||||
|
||||
for (const auto &plane : package.planes)
|
||||
for (const auto &plane : as_const(package.planes))
|
||||
{
|
||||
if (installedModels.containsModelString(plane.getModelName()))
|
||||
{
|
||||
@@ -295,7 +295,7 @@ namespace BlackMisc
|
||||
|
||||
bool CAircraftModelLoaderXPlane::doPackageSub(QString &ioPath)
|
||||
{
|
||||
for (auto i = m_cslPackages.begin(); i != m_cslPackages.end(); ++i)
|
||||
for (auto i = m_cslPackages.cbegin(); i != m_cslPackages.cend(); ++i)
|
||||
{
|
||||
if (strncmp(qPrintable(i->name), qPrintable(ioPath), i->name.size()) == 0)
|
||||
{
|
||||
@@ -315,8 +315,8 @@ namespace BlackMisc
|
||||
return false;
|
||||
}
|
||||
|
||||
auto p = std::find_if(m_cslPackages.begin(), m_cslPackages.end(), [&tokens](CSLPackage p) { return p.name == tokens[1]; });
|
||||
if (p == m_cslPackages.end())
|
||||
auto p = std::find_if(m_cslPackages.cbegin(), m_cslPackages.cend(), [&tokens](const CSLPackage &p) { return p.name == tokens[1]; });
|
||||
if (p == m_cslPackages.cend())
|
||||
{
|
||||
package.path = path;
|
||||
package.name = tokens[1];
|
||||
@@ -338,7 +338,7 @@ namespace BlackMisc
|
||||
return false;
|
||||
}
|
||||
|
||||
if (std::count_if(m_cslPackages.begin(), m_cslPackages.end(), [&tokens](CSLPackage p) { return p.name == tokens[1]; }) == 0)
|
||||
if (std::count_if(m_cslPackages.cbegin(), m_cslPackages.cend(), [&tokens](const CSLPackage &p) { return p.name == tokens[1]; }) == 0)
|
||||
{
|
||||
CLogMessage(this).warning("WARNING: required package %1 not found. Aborting processing of this package.") << tokens[1];
|
||||
return false;
|
||||
|
||||
@@ -182,14 +182,14 @@ namespace BlackMisc
|
||||
CValueCache::Element &CValueCache::getElement(const QString &key)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
return getElement(key, m_elements.lowerBound(key));
|
||||
return getElement(key, as_const(m_elements).lowerBound(key));
|
||||
}
|
||||
|
||||
CValueCache::Element &CValueCache::getElement(const QString &key, QMap<QString, ElementPtr>::const_iterator pos)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
if (pos != m_elements.end() && pos.key() == key) { return **pos; }
|
||||
Q_ASSERT(pos == m_elements.lowerBound(key));
|
||||
if (pos != m_elements.cend() && pos.key() == key) { return **pos; }
|
||||
Q_ASSERT(pos == as_const(m_elements).lowerBound(key));
|
||||
return **m_elements.insert(pos, key, ElementPtr(new Element(key)));
|
||||
}
|
||||
|
||||
@@ -259,8 +259,9 @@ namespace BlackMisc
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
if (values.empty()) { return; }
|
||||
auto out = m_elements.lowerBound(values.cbegin().key());
|
||||
auto end = m_elements.upperBound((values.cend() - 1).key());
|
||||
m_elements.detach(); //! \fixme see http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem
|
||||
auto out = as_const(m_elements).lowerBound(values.cbegin().key());
|
||||
auto end = as_const(m_elements).upperBound((values.cend() - 1).key());
|
||||
for (auto in = values.cbegin(); in != values.cend(); ++in)
|
||||
{
|
||||
while (out != end && out.key() < in.key()) { ++out; }
|
||||
@@ -295,8 +296,9 @@ namespace BlackMisc
|
||||
}
|
||||
CValueCachePacket ratifiedChanges(values.isSaved());
|
||||
CValueCachePacket ackedChanges(values.isSaved());
|
||||
auto out = m_elements.lowerBound(values.cbegin().key());
|
||||
auto end = m_elements.upperBound((values.cend() - 1).key());
|
||||
m_elements.detach(); //! \fixme see http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem
|
||||
auto out = as_const(m_elements).lowerBound(values.cbegin().key());
|
||||
auto end = as_const(m_elements).upperBound((values.cend() - 1).key());
|
||||
for (auto in = values.cbegin(); in != values.cend(); ++in)
|
||||
{
|
||||
while (out != end && out.key() < in.key()) { ++out; }
|
||||
|
||||
Reference in New Issue
Block a user