mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 10:15:38 +08:00
Use std::as_const (C++17 feature)
This commit is contained in:
@@ -227,7 +227,7 @@ namespace BlackMisc
|
||||
if (sort) { icaos.sortByDesignatorAndRank(); }
|
||||
|
||||
// 3 steps to get a proper sort order of the string list
|
||||
for (const CAircraftIcaoCode &icao : as_const(icaos))
|
||||
for (const CAircraftIcaoCode &icao : std::as_const(icaos))
|
||||
{
|
||||
c.append(withCategory ? icao.getCombinedIcaoCategoryStringWithKey() : icao.getCombinedIcaoStringWithKey());
|
||||
}
|
||||
@@ -245,7 +245,7 @@ namespace BlackMisc
|
||||
{
|
||||
icaos = icaos.findWithIataCode(true);
|
||||
if (sort) { icaos.sortBy(&CAircraftIcaoCode::getIataCode, &CAircraftIcaoCode::getRank); }
|
||||
for (const CAircraftIcaoCode &icao : as_const(icaos))
|
||||
for (const CAircraftIcaoCode &icao : std::as_const(icaos))
|
||||
{
|
||||
c.append(icao.getCombinedIataStringWithKey());
|
||||
}
|
||||
@@ -441,7 +441,7 @@ namespace BlackMisc
|
||||
CAircraftIcaoCodeList grouped; // will contain the entries with the best rank
|
||||
QString designator;
|
||||
QString manufacturer;
|
||||
for (const CAircraftIcaoCode &code : as_const(copy))
|
||||
for (const CAircraftIcaoCode &code : std::as_const(copy))
|
||||
{
|
||||
if (code.getDesignator() != designator || code.getManufacturer() != manufacturer)
|
||||
{
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
*/
|
||||
|
||||
#include "blackmisc/connectionguard.h"
|
||||
#include "blackmisc/range.h" // for as_const
|
||||
|
||||
#include <utility> // for as_const
|
||||
|
||||
#include <QObject>
|
||||
|
||||
@@ -45,7 +46,7 @@ namespace BlackMisc
|
||||
{
|
||||
if (m_connections.isEmpty()) { return 0; }
|
||||
int c = 0;
|
||||
for (const QMetaObject::Connection &con : as_const(m_connections))
|
||||
for (const QMetaObject::Connection &con : std::as_const(m_connections))
|
||||
{
|
||||
if (QObject::disconnect(con)) { c++; }
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ namespace BlackMisc
|
||||
std::swap(m_queue, queue);
|
||||
lock.unlock();
|
||||
|
||||
for (const auto &pair : BlackMisc::as_const(queue))
|
||||
for (const auto &pair : std::as_const(queue))
|
||||
{
|
||||
m_page->setValuesFromCache(pair.first, pair.second);
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace BlackMisc
|
||||
CArtifactList copy(*this);
|
||||
copy.sortByVersion(Qt::DescendingOrder);
|
||||
QStringList names;
|
||||
for (const CArtifact &artifact : as_const(copy))
|
||||
for (const CArtifact &artifact : std::as_const(copy))
|
||||
{
|
||||
if (artifact.hasName())
|
||||
{
|
||||
@@ -180,7 +180,7 @@ namespace BlackMisc
|
||||
CArtifactList copy(*this);
|
||||
copy.sortByVersion(Qt::DescendingOrder);
|
||||
QStringList versions;
|
||||
for (const CArtifact &artifact : as_const(copy))
|
||||
for (const CArtifact &artifact : std::as_const(copy))
|
||||
{
|
||||
if (artifact.hasVersion())
|
||||
{
|
||||
|
||||
@@ -264,7 +264,7 @@ namespace BlackMisc
|
||||
break;
|
||||
case SERVERMODE_P2P:
|
||||
{
|
||||
for (QDBusConnection connection : as_const(m_connections))
|
||||
for (QDBusConnection connection : std::as_const(m_connections))
|
||||
{
|
||||
if (connection.registerObject(path, object, registerOptions()))
|
||||
{
|
||||
@@ -300,7 +300,7 @@ namespace BlackMisc
|
||||
|
||||
void CDBusServer::removeAllObjects()
|
||||
{
|
||||
for (const QString &path : makeKeysRange(as_const(m_objects)))
|
||||
for (const QString &path : makeKeysRange(std::as_const(m_objects)))
|
||||
{
|
||||
switch (m_serverMode)
|
||||
{
|
||||
@@ -311,7 +311,7 @@ namespace BlackMisc
|
||||
QDBusConnection::systemBus().unregisterObject(path);
|
||||
break;
|
||||
case SERVERMODE_P2P:
|
||||
for (QDBusConnection connection : as_const(m_connections))
|
||||
for (QDBusConnection connection : std::as_const(m_connections))
|
||||
{
|
||||
connection.unregisterObject(path);
|
||||
}
|
||||
|
||||
@@ -284,7 +284,7 @@ namespace BlackMisc
|
||||
|
||||
Q_ASSERT_X(comp.sameNameInSource.size() == comp.sameNameInTarget.size(), Q_FUNC_INFO, "Same sets require same size");
|
||||
QSet<QString>::const_iterator targetIt = comp.sameNameInTarget.cbegin();
|
||||
for (const QString &sourceFile : as_const(comp.sameNameInSource))
|
||||
for (const QString &sourceFile : std::as_const(comp.sameNameInSource))
|
||||
{
|
||||
const QFileInfo source(sourceFile);
|
||||
const QFileInfo target(*targetIt);
|
||||
|
||||
@@ -286,7 +286,7 @@ namespace BlackMisc
|
||||
|
||||
QString last;
|
||||
QStringList result;
|
||||
for (const QString &path : as_const(dirs))
|
||||
for (const QString &path : std::as_const(dirs))
|
||||
{
|
||||
if (path.isEmpty()) { continue; }
|
||||
if (last.isEmpty() || !path.startsWith(last, cs))
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace BlackMisc
|
||||
QString CJsonException::stackString()
|
||||
{
|
||||
QStringList list;
|
||||
for (const auto scope : BlackMisc::as_const(jsonStack()))
|
||||
for (const auto scope : std::as_const(jsonStack()))
|
||||
{
|
||||
list.push_back(scope->m_string ? *scope->m_string : scope->m_latin1); // clazy:exclude=reserve-candidates
|
||||
if (scope->m_index >= 0) { list.back() += u'[' % QString::number(scope->m_index) % u']'; }
|
||||
|
||||
@@ -27,18 +27,6 @@ namespace BlackMisc
|
||||
{
|
||||
template <class> class CRange;
|
||||
|
||||
/*!
|
||||
* Own implementation of C++17 std::as_const. Adds const to any lvalue.
|
||||
* Useful with non-mutating range-for loops to avoid unnecessary detachment of Qt implicitly shared containers.
|
||||
* Does not allow rvalues, as that could easily lead to undefined behaviour.
|
||||
*/
|
||||
//! @{
|
||||
template <class T>
|
||||
constexpr std::add_const_t<T> &as_const(T &v) noexcept { return v; }
|
||||
template <class T>
|
||||
void as_const(const T &&) = delete;
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Any container class with begin and end iterators can inherit from this CRTP class
|
||||
* to gain some useful algorithms as member functions.
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace BlackMisc
|
||||
|
||||
if (!m_duplex) { return; }
|
||||
|
||||
for (const auto &filter : as_const(getChannel(channel).peerSubscriptions))
|
||||
for (const auto &filter : std::as_const(getChannel(channel).peerSubscriptions))
|
||||
{
|
||||
if (filter.matches(param))
|
||||
{
|
||||
@@ -103,7 +103,7 @@ namespace BlackMisc
|
||||
|
||||
void CDataLinkDBus::handlePeerEvent(const QString &channel, const CVariant ¶m)
|
||||
{
|
||||
for (const auto &observerWeak : as_const(getChannel(channel).passiveObservers))
|
||||
for (const auto &observerWeak : std::as_const(getChannel(channel).passiveObservers))
|
||||
{
|
||||
auto observer = observerWeak.lock();
|
||||
if (observer && observer->eventSubscription().matches(param))
|
||||
@@ -124,7 +124,7 @@ namespace BlackMisc
|
||||
void CDataLinkDBus::announceLocalSubscriptions(const QString &channel)
|
||||
{
|
||||
CVariantList filters;
|
||||
for (const auto &observerWeak : as_const(getChannel(channel).passiveObservers))
|
||||
for (const auto &observerWeak : std::as_const(getChannel(channel).passiveObservers))
|
||||
{
|
||||
auto observer = observerWeak.lock();
|
||||
if (observer) { filters.push_back(observer->eventSubscription()); }
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace BlackMisc
|
||||
|
||||
void CDataLinkLocal::dispatchEvent(const CVariant ¶m, const QString &channel)
|
||||
{
|
||||
for (const auto &observerWeak : as_const(getChannel(channel).passiveObservers))
|
||||
for (const auto &observerWeak : std::as_const(getChannel(channel).passiveObservers))
|
||||
{
|
||||
auto observer = observerWeak.lock();
|
||||
if (observer && observer->eventSubscription().matches(param))
|
||||
|
||||
@@ -139,7 +139,7 @@ namespace BlackMisc
|
||||
|
||||
void CSimpleCommandParser::registerCommand(const CSimpleCommandParser::CommandHtmlHelp &command)
|
||||
{
|
||||
for (const CommandHtmlHelp &help : as_const(s_commands))
|
||||
for (const CommandHtmlHelp &help : std::as_const(s_commands))
|
||||
{
|
||||
// avoid duplicates
|
||||
if (help.command == command.command) { return; }
|
||||
|
||||
@@ -1572,7 +1572,7 @@ namespace BlackMisc
|
||||
)
|
||||
);
|
||||
|
||||
for (const CAircraftModel &model : as_const(sorted))
|
||||
for (const CAircraftModel &model : std::as_const(sorted))
|
||||
{
|
||||
if (wasStopped) { break; } // allow breaking from external
|
||||
bool ok = false;
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace BlackMisc
|
||||
CAircraftModelList sortedByAircraft(models);
|
||||
sortedByAircraft.sortBy(&CAircraftModel::getAircraftIcaoCodeDesignator);
|
||||
|
||||
for (const CAircraftModel &model : as_const(sortedByAircraft))
|
||||
for (const CAircraftModel &model : std::as_const(sortedByAircraft))
|
||||
{
|
||||
const QString aircraftIcao(model.hasAircraftDesignator() ? model.getAircraftIcaoCodeDesignator() : emptyDesignator);
|
||||
if (!modelsByDesignator.contains(aircraftIcao))
|
||||
@@ -81,7 +81,7 @@ namespace BlackMisc
|
||||
// header
|
||||
html += "<thead><tr>\n"
|
||||
"<th></th>";
|
||||
for (const QString &airline : as_const(airlineIcaos))
|
||||
for (const QString &airline : std::as_const(airlineIcaos))
|
||||
{
|
||||
html += "<th>";
|
||||
html += airline;
|
||||
@@ -91,7 +91,7 @@ namespace BlackMisc
|
||||
"<tbody>\n";
|
||||
|
||||
// fill data
|
||||
for (const QString &aircraftIcao : as_const(aircraftIcaos))
|
||||
for (const QString &aircraftIcao : std::as_const(aircraftIcaos))
|
||||
{
|
||||
html += "<tr>\n"
|
||||
" <th>";
|
||||
@@ -99,7 +99,7 @@ namespace BlackMisc
|
||||
html += "</th>\n";
|
||||
|
||||
const QMap<QString, CAircraftModelList> &airlineModels = modelsByDesignator[aircraftIcao];
|
||||
for (const QString &airline : as_const(airlineIcaos))
|
||||
for (const QString &airline : std::as_const(airlineIcaos))
|
||||
{
|
||||
if (airlineModels.contains(airline))
|
||||
{
|
||||
|
||||
@@ -389,7 +389,7 @@ namespace BlackMisc
|
||||
Q_ASSERT_X(fileTimestamp.isValid(), Q_FUNC_INFO, "Missing file timestamp");
|
||||
|
||||
CAircraftCfgEntriesList result;
|
||||
for (const CAircraftCfgEntries &e : as_const(tempEntries))
|
||||
for (const CAircraftCfgEntries &e : std::as_const(tempEntries))
|
||||
{
|
||||
if (e.getTitle().isEmpty())
|
||||
{
|
||||
|
||||
@@ -275,7 +275,7 @@ namespace BlackMisc
|
||||
file.close();
|
||||
parseFullPackage(content, package);
|
||||
|
||||
for (const auto &plane : as_const(package.planes))
|
||||
for (const auto &plane : std::as_const(package.planes))
|
||||
{
|
||||
if (installedModels.containsModelString(plane.getModelName()))
|
||||
{
|
||||
|
||||
@@ -178,14 +178,14 @@ namespace BlackMisc
|
||||
CValueCache::Element &CValueCache::getElement(const QString &key)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
return getElement(key, as_const(m_elements).lowerBound(key));
|
||||
return getElement(key, std::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.cend() && pos.key() == key) { return **pos; }
|
||||
Q_ASSERT(pos == as_const(m_elements).lowerBound(key));
|
||||
Q_ASSERT(pos == std::as_const(m_elements).lowerBound(key));
|
||||
return **m_elements.insert(pos, key, ElementPtr(new Element(key)));
|
||||
}
|
||||
|
||||
@@ -256,8 +256,8 @@ namespace BlackMisc
|
||||
QMutexLocker lock(&m_mutex);
|
||||
if (values.empty()) { return; }
|
||||
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());
|
||||
auto out = std::as_const(m_elements).lowerBound(values.cbegin().key());
|
||||
auto end = std::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; }
|
||||
@@ -293,8 +293,8 @@ namespace BlackMisc
|
||||
CValueCachePacket ratifiedChanges(values.isSaved());
|
||||
CValueCachePacket ackedChanges(values.isSaved());
|
||||
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());
|
||||
auto out = std::as_const(m_elements).lowerBound(values.cbegin().key());
|
||||
auto end = std::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; }
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace BlackMisc
|
||||
CTemperatureLayerList temperatureLayers = gridPoint.getTemperatureLayers();
|
||||
temperatureLayers.sort([](const CTemperatureLayer & a, const CTemperatureLayer & b) { return a.getLevel() < b.getLevel(); });
|
||||
qtout << " Temperature Layers: " << sep;
|
||||
for (const auto &temperatureLayer : as_const(temperatureLayers))
|
||||
for (const auto &temperatureLayer : std::as_const(temperatureLayers))
|
||||
{
|
||||
qtout << " Level: " << temperatureLayer.getLevel().toQString() << sep;
|
||||
qtout << " Temperature: " << temperatureLayer.getTemperature().toQString() << sep;
|
||||
@@ -89,7 +89,7 @@ namespace BlackMisc
|
||||
CWindLayerList windLayers = gridPoint.getWindLayers();
|
||||
windLayers.sort([](const CWindLayer & a, const CWindLayer & b) { return a.getLevel() < b.getLevel(); });
|
||||
qtout << " Wind Layers: " << sep;
|
||||
for (const auto &windLayer : as_const(windLayers))
|
||||
for (const auto &windLayer : std::as_const(windLayers))
|
||||
{
|
||||
qtout << " Level: " << windLayer.getLevel().toQString() << sep;
|
||||
qtout << " Wind: " << windLayer.getDirection().toQString() << " at " << windLayer.getSpeed().toQString() << sep;
|
||||
|
||||
Reference in New Issue
Block a user