From 619f71492c7bdc9cca6be2de564d3e8e108e6638 Mon Sep 17 00:00:00 2001 From: Viktor Kopp Date: Tue, 24 Dec 2024 12:40:41 +0100 Subject: [PATCH] use std algos Signed-off-by: Viktor Kopp --- qdlt/qdltplugin.cpp | 2 +- qdlt/qdltplugin.h | 2 +- qdlt/qdltpluginmanager.cpp | 56 ++++++++++++++++++++------------------ qdlt/qdltpluginmanager.h | 14 ++-------- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/qdlt/qdltplugin.cpp b/qdlt/qdltplugin.cpp index 4a85d186..bda2574a 100644 --- a/qdlt/qdltplugin.cpp +++ b/qdlt/qdltplugin.cpp @@ -96,7 +96,7 @@ bool QDltPlugin::isCommand() } // generic plugin interfaces -QString QDltPlugin::name() +QString QDltPlugin::name() const { if(plugininterface) return plugininterface->name(); diff --git a/qdlt/qdltplugin.h b/qdlt/qdltplugin.h index 6980422f..5bb4688a 100644 --- a/qdlt/qdltplugin.h +++ b/qdlt/qdltplugin.h @@ -71,7 +71,7 @@ class QDLT_EXPORT QDltPlugin bool isCommand(); // generic plugin interfaces - QString name(); + QString name() const; QString pluginVersion(); QString pluginInterfaceVersion(); QString error(); diff --git a/qdlt/qdltpluginmanager.cpp b/qdlt/qdltpluginmanager.cpp index 19d96326..1c3966b8 100644 --- a/qdlt/qdltpluginmanager.cpp +++ b/qdlt/qdltpluginmanager.cpp @@ -119,7 +119,8 @@ QStringList QDltPluginManager::loadPluginsPath(QDir &dir) } void QDltPluginManager::loadConfig(QString pluginName, QString filename) { - forEachPlugin([&](auto* plugin) { + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) { if (plugin->name() == pluginName) plugin->setFilename(filename); }); @@ -136,26 +137,20 @@ void QDltPluginManager::decodeMsg(QDltMsg &msg, int triggeredByUser) } QDltPlugin* QDltPluginManager::findPlugin(const QString& name) const { - QDltPlugin* result = nullptr; + QMutexLocker mutexLocker(&pluginListMutex); - for (auto* plugin : plugins) { - if (plugin->name() == name) { - result = plugin; - break; - } - } + auto it = std::find_if(plugins.begin(), plugins.end(), [&](auto* plugin) { + return plugin->name() == name; + }); - return result; + return it != plugins.end() ? *it : nullptr; } void QDltPluginManager::initPluginPriority(const QStringList& desiredPrio) { - QStringList finalPrio; - if(plugins.size() > 1) { int prio = 0; - for (int i = 0; i < desiredPrio.count(); ++i) { - QString pluginName(desiredPrio[i]); + for (const auto& pluginName: desiredPrio) { if (setPluginPriority(pluginName, prio)) { ++prio; } @@ -194,8 +189,7 @@ bool QDltPluginManager::raisePluginPriority(const QString &name) QMutexLocker mutexLocker(&pluginListMutex); for(int num=1; num < plugins.size(); ++num) { - if( plugins[num]->name() == name) - { + if (plugins[num]->name() == name) { qDebug() << "raise prio of" << name << "from" << num << "to" << num-1; plugins.move(num, num-1); result = true; @@ -236,18 +230,22 @@ bool QDltPluginManager::setPluginPriority(const QString& name, int prio) QStringList QDltPluginManager::getPluginPriorities() const { QStringList finalPrio; + finalPrio.reserve(plugins.size()); + QMutexLocker mutexLocker(&pluginListMutex); - for (auto* plugin: plugins) { - finalPrio << plugin->name(); - } + std::transform(plugins.begin(), plugins.end(), std::back_inserter(finalPrio), [](const auto* plugin) { + return plugin->name(); + }); return finalPrio; } -QList QDltPluginManager::getDecoderPlugins() +QList QDltPluginManager::getDecoderPlugins() const { QList list; - forEachPlugin([&](auto* plugin) { + + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) { if (plugin->isDecoder() && plugin->getMode() >= QDltPlugin::ModeEnable) list.append(plugin); }); @@ -255,10 +253,12 @@ QList QDltPluginManager::getDecoderPlugins() return list; } -QList QDltPluginManager::getViewerPlugins() +QList QDltPluginManager::getViewerPlugins() const { QList list; - forEachPlugin([&](auto* plugin) { + + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) { if (plugin->isViewer() && plugin->getMode() >= QDltPlugin::ModeEnable) list.append(plugin); }); @@ -268,7 +268,8 @@ QList QDltPluginManager::getViewerPlugins() bool QDltPluginManager::stateChanged(int index, QDltConnection::QDltConnectionState connectionState,QString hostname) { - forEachPlugin([&](auto* plugin) { + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin) { if (plugin->isControl()) plugin->stateChanged(index, connectionState, hostname); }); @@ -278,7 +279,8 @@ bool QDltPluginManager::stateChanged(int index, QDltConnection::QDltConnectionSt bool QDltPluginManager::autoscrollStateChanged(bool enabled) { - forEachPlugin([&](auto* plugin){ + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin){ if(plugin->isControl() ) plugin->autoscrollStateChanged(enabled); }); @@ -288,7 +290,8 @@ bool QDltPluginManager::autoscrollStateChanged(bool enabled) bool QDltPluginManager::initControl(QDltControl *control) { - forEachPlugin([&](auto* plugin){ + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin){ if(plugin->isControl() ) plugin->initControl(control); }); @@ -298,7 +301,8 @@ bool QDltPluginManager::initControl(QDltControl *control) bool QDltPluginManager::initConnections(QStringList list) { - forEachPlugin([&](auto* plugin){ + QMutexLocker mutexLocker(&pluginListMutex); + std::for_each(plugins.begin(), plugins.end(), [&](auto* plugin){ if(plugin->isControl() ) plugin->initConnections(list); }); diff --git a/qdlt/qdltpluginmanager.h b/qdlt/qdltpluginmanager.h index d1459387..ce5c8949 100644 --- a/qdlt/qdltpluginmanager.h +++ b/qdlt/qdltpluginmanager.h @@ -52,13 +52,13 @@ class QDLT_EXPORT QDltPluginManager : public QDltMessageDecoder void decodeMsg(QDltMsg &msg,int triggeredByUser) override; //! Get the list of pointers to all loaded plugins - QList getPlugins() { return plugins; } + QList getPlugins() const { return plugins; } //! Get the list of pointers to all enabled decoder plugins - QList getDecoderPlugins(); + QList getDecoderPlugins() const; //! Get the list of pointers to all enabled viewer plugins - QList getViewerPlugins(); + QList getViewerPlugins() const; //! Find a plugin with the specific name /*! @@ -81,14 +81,6 @@ class QDLT_EXPORT QDltPluginManager : public QDltMessageDecoder QStringList getPluginPriorities() const; private: - template - void forEachPlugin(F&& f) { - QMutexLocker mutexLocker(&pluginListMutex); - for (QDltPlugin* plugin : plugins) { - f(plugin); - } - } - mutable QMutex pluginListMutex; //! The list of pointers to all loaded plugins